Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
david-plugge committed Jan 12, 2024
1 parent 22cdab4 commit b240db1
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 479 deletions.
145 changes: 87 additions & 58 deletions src/new/index.ts → src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import PocketBase, {
SendOptions
} from 'pocketbase';
import {
BaseRecord,
GenericCollection,
GenericSchema,
MaybeArray,
RecordWithExpandToDotPath
} from '../types.js';
} from './types.js';
import {
ResolveSelectWithExpand,
SelectWithExpand,
resolveSelect
} from '../queryParams.js';
import { Sort } from '../sort.js';
import { Filter, FilterParam } from '../filter.js';
} from './select.js';
import { Sort } from './sort.js';
import { Filter } from './filter.js';

export interface ViewCollectionService<Collection extends GenericCollection> {
export interface ViewCollectionService<
Collection extends GenericCollection,
ExpandedRecord extends BaseRecord = RecordWithExpandToDotPath<Collection>
> {
collectionName: Collection['collectionName'];
client: PocketBase;

Expand All @@ -29,24 +33,24 @@ export interface ViewCollectionService<Collection extends GenericCollection> {
select?: TSelect;
page?: number;
perPage?: number;
sort?: MaybeArray<Sort<Collection>>;
filter?: FilterParam<RecordWithExpandToDotPath<Collection>>;
sort?: MaybeArray<Sort<ExpandedRecord>>;
filter?: Filter<ExpandedRecord>;
} & SendOptions
): Promise<ResolveSelectWithExpand<Collection, TSelect>[]>;
getList<TSelect extends SelectWithExpand<Collection>>(
page?: number,
perPage?: number,
options?: {
select?: TSelect;
sort?: MaybeArray<Sort<Collection>>;
filter?: FilterParam<RecordWithExpandToDotPath<Collection>>;
sort?: MaybeArray<Sort<ExpandedRecord>>;
filter?: Filter<ExpandedRecord>;
} & SendOptions
): Promise<ListResult<ResolveSelectWithExpand<Collection, TSelect>>>;
getFirstListItem<TSelect extends SelectWithExpand<Collection>>(
filter: FilterParam<RecordWithExpandToDotPath<Collection>>,
filter: Filter<ExpandedRecord>,
options?: {
select?: TSelect;
sort?: MaybeArray<Sort<Collection>>;
sort?: MaybeArray<Sort<ExpandedRecord>>;
} & SendOptions
): Promise<ResolveSelectWithExpand<Collection, TSelect>>;
getOne<TSelect extends SelectWithExpand<Collection>>(
Expand All @@ -56,9 +60,9 @@ export interface ViewCollectionService<Collection extends GenericCollection> {
} & SendOptions
): Promise<ResolveSelectWithExpand<Collection, TSelect>>;

createFilter(filter: Filter<Collection>): Filter<Collection>;
createFilter(filter: Filter<ExpandedRecord>): Filter<ExpandedRecord>;

createSort(...sort: Sort<Collection>[]): Sort<Collection>[];
createSort(...sort: Sort<ExpandedRecord>[]): Sort<ExpandedRecord>;

createSelect<T extends SelectWithExpand<Collection>>(select: T): T;
}
Expand Down Expand Up @@ -90,7 +94,9 @@ export interface AuthCollectionService<Collection extends GenericCollection>
options?: {
select?: TSelect;
} & SendOptions
): Promise<ResolveSelectWithExpand<Collection, TSelect>>;
): Promise<
RecordAuthResponse<ResolveSelectWithExpand<Collection, TSelect>>
>;
authWithOAuth2Code<TSelect extends SelectWithExpand<Collection>>(
provider: string,
code: string,
Expand All @@ -102,7 +108,9 @@ export interface AuthCollectionService<Collection extends GenericCollection>
options?: {
select?: TSelect;
} & SendOptions
): Promise<ResolveSelectWithExpand<Collection, TSelect>>;
): Promise<
RecordAuthResponse<ResolveSelectWithExpand<Collection, TSelect>>
>;
authWithOAuth2(
options: Omit<OAuth2AuthConfig, 'createData'> & {
createData?: Collection['create'];
Expand All @@ -112,7 +120,9 @@ export interface AuthCollectionService<Collection extends GenericCollection>
options?: {
select?: TSelect;
} & SendOptions
): Promise<ResolveSelectWithExpand<Collection, TSelect>>;
): Promise<
RecordAuthResponse<ResolveSelectWithExpand<Collection, TSelect>>
>;
}

const FORWARD_METHODS = [
Expand All @@ -127,8 +137,10 @@ const FORWARD_METHODS = [
'unlinkExternalAuth'
] as const;

export class TypedRecordService {
constructor(readonly service: RecordService) {
export class TypedRecordService
implements BaseCollectionService<GenericCollection>
{
constructor(readonly service: RecordService<any>) {
for (const name of FORWARD_METHODS) {
// @ts-ignore
this[name] = this.service[name].bind(this.service);
Expand Down Expand Up @@ -160,71 +172,90 @@ export class TypedRecordService {
return opts;
}

filter(filter: string) {
return filter;
createFilter(filter: string) {
return filter ? filter : '';
}
sort(sort: string | string[]) {
return sort;

createSort(...sorters: any[]): any {
return sorters.filter((x) => typeof x === 'string').join(',');
}
select(select: any) {

createSelect(select: any) {
return select;
}

getFullList(options: {
select?: any;
page?: number;
perPage?: number;
sort?: string | string[];
filter?: string;
}) {
return this.service.getFullList(this.prepareOptions(options));
getFullList(options?: SendOptions) {
return this.service.getFullList<any>(this.prepareOptions(options));
}

getList(
page?: number,
perPage?: number,
options?: {
select?: any;
sort?: string | string[];
filter?: string;
}
) {
getList(page?: number, perPage?: number, options?: SendOptions) {
return this.service.getList(
page,
perPage,
this.prepareOptions(options)
);
}

getFirstListItem(id: string) {
return this.service.getFirstListItem(id);
getFirstListItem(filter: string, options?: SendOptions) {
return this.service.getFirstListItem(
filter,
this.prepareOptions(options)
);
}

getOne(id: string): Promise<any> {
return this.service.getOne(id);
getOne(
id: string,
options?: {
select?: any;
} & SendOptions
): Promise<any> {
return this.service.getOne(id, this.prepareOptions(options));
}

create() {
this.service.create({});
create(
bodyParams?:
| {
[key: string]: any;
}
| FormData,
options?: {
select?: any;
} & SendOptions
) {
return this.service.create(bodyParams, this.prepareOptions(options));
}

update(id: string) {
this.service.update(id, {});
update(
id: string,
bodyParams?:
| FormData
| {
[key: string]: any;
},
options?: {
select?: any;
} & SendOptions
) {
return this.service.update(
id,
bodyParams,
this.prepareOptions(options)
);
}

delete(id: string) {
this.service.delete(id);
delete(id: string, options?: SendOptions) {
return this.service.delete(id, this.prepareOptions(options));
}

authWithPassword(
usernameOrEmail: string,
password: string,
options?: RecordOptions | undefined
): Promise<RecordAuthResponse> {
) {
return this.service.authWithPassword(
usernameOrEmail,
password,
options
this.prepareOptions(options)
);
}

Expand All @@ -235,25 +266,23 @@ export class TypedRecordService {
redirectUrl: string,
createData?: { [key: string]: any } | undefined,
options?: RecordOptions | undefined
): Promise<RecordAuthResponse> {
) {
return this.service.authWithOAuth2Code(
provider,
code,
codeVerifier,
redirectUrl,
createData,
options
this.prepareOptions(options)
);
}

authWithOAuth2(options: OAuth2AuthConfig): Promise<RecordAuthResponse> {
return this.service.authWithOAuth2(options);
}

authRefresh(
options?: RecordOptions | undefined
): Promise<RecordAuthResponse> {
return this.authRefresh(options);
authRefresh(options?: RecordOptions | undefined) {
return this.service.authRefresh(this.prepareOptions(options));
}
}

Expand Down
53 changes: 34 additions & 19 deletions src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,39 @@ ${tables
// ===== ${t.name} =====
export interface ${t.typeName}Response extends ${baseRecord} {
collectionName: '${t.name}';${!t.columns.response.length ? '' : `
${t.columns.response.join('\n' + indent)}`}
collectionName: '${t.name}';${
!t.columns.response.length
? ''
: `
${t.columns.response.join('\n' + indent)}`
}
}
${
// view collections are readonly
t.type === 'view'
? ''
: `
export interface ${t.typeName}Create extends ${t.type === "base" ? "BaseCollectionRecordCreate" : "AuthCollectionRecordCreate"} ${!t.columns.create.length ? '{}' : `{
export interface ${t.typeName}Create extends ${
t.type === 'base'
? 'BaseCollectionRecordCreate'
: 'AuthCollectionRecordCreate'
} ${
!t.columns.create.length
? '{}'
: `{
${t.columns.create.join('\n' + indent)}
}`}
}`
}
export interface ${t.typeName}Update${t.type === "base" ? "" : " extends AuthCollectionRecordUpdate"} ${!t.columns.update.length ? '{}' : `{
export interface ${t.typeName}Update${
t.type === 'base' ? '' : ' extends AuthCollectionRecordUpdate'
} ${
!t.columns.update.length
? '{}'
: `{
${t.columns.update.join('\n' + indent)}
}`}
}`
}
`
}
export interface ${t.typeName}Collection {
Expand Down Expand Up @@ -264,12 +282,11 @@ function getFieldType(field: Field, { response, create, update }: Columns) {
}
case 'select': {
const single = field.options.maxSelect === 1;
const values = !field.required && single
? ["", ...field.options.values]
: field.options.values;
const singleType = values
.map((v) => `'${v}'`)
.join(' | ');
const values =
!field.required && single
? ['', ...field.options.values]
: field.options.values;
const singleType = values.map((v) => `'${v}'`).join(' | ');
const type = single ? `${singleType}` : `MaybeArray<${singleType}>`;

addResponse(single ? singleType : `Array<${singleType}>`);
Expand All @@ -285,7 +302,7 @@ function getFieldType(field: Field, { response, create, update }: Columns) {
case 'relation': {
const singleType = 'string';
const single = field.options.maxSelect === 1;
const type = single ? `${singleType}` : `MaybeArray<${singleType}>`;
const type = single ? singleType : `MaybeArray<${singleType}>`;

addResponse(single ? singleType : `Array<${singleType}>`);
addCreate(type);
Expand All @@ -297,15 +314,13 @@ function getFieldType(field: Field, { response, create, update }: Columns) {
break;
}
case 'file': {
const singleType = 'string';
const single = field.options.maxSelect === 1;
const type = single ? `${singleType}` : `MaybeArray<${singleType}>`;

addResponse(single ? singleType : `Array<${singleType}>`);
addCreate(type);
addUpdate(type);
addResponse(single ? 'string' : `Array<string>`);
addCreate(single ? `File` : `MaybeArray<File>`);
addUpdate(single ? `File` : `MaybeArray<File>`);
if (!single) {
addUpdate(type, `'${field.name}-'`);
addUpdate('string', `'${field.name}-'`);
}
break;
}
Expand Down
Loading

0 comments on commit b240db1

Please sign in to comment.