Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/support-pocketbase-v0.20' into s…
Browse files Browse the repository at this point in the history
…upport-pocketbase-v0.20
  • Loading branch information
david-plugge committed Jan 11, 2024
2 parents a25fbdc + 1fe4143 commit 43eee83
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
48 changes: 38 additions & 10 deletions src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ interface BaseCollectionRecord {
collectionName: string;
}
// https://pocketbase.io/docs/api-records/#create-record
interface BaseCollectionRecordCreate {
id?: string;
}
// https://pocketbase.io/docs/collections/#auth-collection
interface AuthCollectionRecord extends BaseCollectionRecord {
username: string;
Expand All @@ -115,6 +120,26 @@ interface AuthCollectionRecord extends BaseCollectionRecord {
verified: boolean;
}
// https://pocketbase.io/docs/api-records/#create-record
interface AuthCollectionRecordCreate extends BaseCollectionRecordCreate {
username?: string;
email?: string;
emailVisibility?: boolean;
verified?: boolean;
password: string;
passwordConfirm: string;
}
// https://pocketbase.io/docs/api-records/#update-record
interface AuthCollectionRecordUpdate {
username?: string;
email?: string;
emailVisibility?: boolean;
verified?: boolean;
password?: string;
passwordConfirm?: string;
}
// https://pocketbase.io/docs/collections/#view-collection
interface ViewCollectionRecord {
id: string;
Expand All @@ -137,21 +162,21 @@ ${tables
// ===== ${t.name} =====
export interface ${t.typeName}Response extends ${baseRecord} {
collectionName: '${t.name}';
${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 {
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 {
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 All @@ -171,7 +196,7 @@ export interface ${t.typeName}Collection {
: `{
${t.relations
.map((col) => `${col.name}: ${col.target};`)
.join('\n' + ' '.repeat(8))}
.join('\n' + indent.repeat(2))}
}`
};
}
Expand All @@ -196,7 +221,7 @@ function getFieldType(field: Field, { response, create, update }: Columns) {
const req = field.required ? '' : '?';

const addResponse = (type: string, name = field.name) =>
response.push(`${name}${req}: ${type};`);
response.push(`${name}: ${type};`);
const addCreate = (type: string, name = field.name) =>
create.push(`${name}${req}: ${type};`);
const addUpdate = (type: string, name = field.name) =>
Expand Down Expand Up @@ -238,10 +263,13 @@ function getFieldType(field: Field, { response, create, update }: Columns) {
break;
}
case 'select': {
const singleType = field.options.values
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 single = field.options.maxSelect === 1;
const type = single ? `${singleType}` : `MaybeArray<${singleType}>`;

addResponse(single ? singleType : `Array<${singleType}>`);
Expand Down
7 changes: 7 additions & 0 deletions src/queryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ export const createOptions: CreateOptions = (options) => {

export interface TypedBaseQueryParams<TSelect> {
select?: TSelect;
requestKey?: string | null;
/**
* @deprecated use `requestKey:null` instead
*/
$autoCancel?: boolean;
/**
* @deprecated use `requestKey:string` instead
*/
$cancelKey?: string;
}

Expand Down
16 changes: 3 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RecordModel as PocketBaseRecord } from 'pocketbase';

export type Prettify<T> = T extends infer o ? { [K in keyof o]: o[K] } : never;
export type MaybeArray<T> = T | T[];
export type MaybeMakeArray<T, Out> = T extends any[] ? Out[] : Out;
Expand Down Expand Up @@ -47,17 +45,9 @@ export type GenericSchema = {
export type TypedRecord<
Data extends BaseRecord,
Expand extends GenericExpand = {}
> = Pick<PocketBaseRecord, 'load' | '$load' | '$isNew'> &
Data & {
load(data: Data): void;
$load(data: Data): void;
clone(): TypedRecord<Data, Expand>;
$clone(): TypedRecord<Data, Expand>;
export(): Data;
$export(): Data;

expand: Expand;
};
> = Data & {
expand: Expand;
};

export interface SystemFields {
id: string;
Expand Down

0 comments on commit 43eee83

Please sign in to comment.