Skip to content

Commit

Permalink
Improve type generation for autodate fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpajon committed Dec 27, 2024
1 parent ec26ee2 commit 6798291
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
5 changes: 2 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export type ${RECORD_ID_STRING_NAME} = string
export type ${HTML_STRING_NAME} = string
export type Nullable<T> = T | null | ''`;
var NOT_COMMON_COLLECTIONS = ["_authOrigins", "_externalAuths", "_mfas", "_otps"];
var EXTRA_SYSTEM_FIELDS = ["created", "updated"];
var BASE_SYSTEM_FIELDS_DEFINITION = `// System fields
export type BaseSystemFields<T = never> = {
id: ${RECORD_ID_STRING_NAME}
Expand Down Expand Up @@ -381,7 +380,7 @@ function createCreateType(collectionSchemaEntry) {
includeExpand: false
});
const systemFields = getSystemCreateFields(type);
const collectionFields = fields.filter((fieldSchema) => !fieldSchema.system && !EXTRA_SYSTEM_FIELDS.includes(fieldSchema.name)).map((fieldSchema) => createTypeCreateField(name, fieldSchema)).sort().join("\n");
const collectionFields = fields.filter((fieldSchema) => !fieldSchema.system).map((fieldSchema) => createTypeCreateField(name, fieldSchema)).sort().join("\n");
return `export type ${typeName}Create${genericArgs} = ${collectionFields ? `{
${collectionFields}
} & ${systemFields}` : systemFields}`;
Expand All @@ -393,7 +392,7 @@ function createUpdateType(collectionSchemaEntry) {
includeExpand: false
});
const systemFields = getSystemUpdateFields(type);
const collectionFields = fields.filter((fieldSchema) => !fieldSchema.system && !EXTRA_SYSTEM_FIELDS.includes(fieldSchema.name)).map((fieldSchema) => createTypeUpdateField(name, fieldSchema)).sort().join("\n");
const collectionFields = fields.filter((fieldSchema) => !fieldSchema.system).map((fieldSchema) => createTypeUpdateField(name, fieldSchema)).sort().join("\n");
return `export type ${typeName}Update${genericArgs} = ${collectionFields ? `{
${collectionFields}
} & ${systemFields}` : systemFields}`;
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type ${RECORD_ID_STRING_NAME} = string
export type ${HTML_STRING_NAME} = string
export type Nullable<T> = T | null | ''`
export const NOT_COMMON_COLLECTIONS = ['_authOrigins', '_externalAuths', '_mfas', '_otps']
export const EXTRA_SYSTEM_FIELDS = ['created', 'updated']

export const BASE_SYSTEM_FIELDS_DEFINITION = `// System fields
export type BaseSystemFields<T = never> = {
Expand Down
7 changes: 3 additions & 4 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import {
CREATE_TYPE_COMMENT,
EXPAND_GENERIC_NAME,
EXPORT_COMMENT,
EXTRA_SYSTEM_FIELDS,
IMPORTS,
NOT_COMMON_COLLECTIONS,
RECORD_TYPE_COMMENT,
RESPONSE_TYPE_COMMENT,
TYPED_POCKETBASE_COMMENT,
UPDATE_TYPE_COMMENT,
UPDATE_TYPE_COMMENT
} from "./constants"
import { createSelectOptions, createTypeCreateField, createTypeField, createTypeUpdateField } from "./fields"
import {
Expand Down Expand Up @@ -126,7 +125,7 @@ export function createCreateType(
})
const systemFields = getSystemCreateFields(type)
const collectionFields = fields
.filter((fieldSchema: FieldSchema) => !fieldSchema.system && !EXTRA_SYSTEM_FIELDS.includes(fieldSchema.name))
.filter((fieldSchema: FieldSchema) => !fieldSchema.system)
.map((fieldSchema: FieldSchema) => createTypeCreateField(name, fieldSchema))
.sort()
.join("\n")
Expand All @@ -150,7 +149,7 @@ export function createUpdateType(
})
const systemFields = getSystemUpdateFields(type)
const collectionFields = fields
.filter((fieldSchema: FieldSchema) => !fieldSchema.system && !EXTRA_SYSTEM_FIELDS.includes(fieldSchema.name))
.filter((fieldSchema: FieldSchema) => !fieldSchema.system)
.map((fieldSchema: FieldSchema) => createTypeUpdateField(name, fieldSchema))
.sort()
.join("\n")
Expand Down
20 changes: 20 additions & 0 deletions test/__snapshots__/fromJSON.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,21 @@ export type UsersRecord = {
export type SuperusersCreate = AuthSystemCreateFields
export type BaseCreate = {
created?: IsoDateString
field?: string
updated?: IsoDateString
} & BaseSystemCreateFields
export type CustomAuthCreate = {
created?: IsoDateString
custom_field?: string
updated?: IsoDateString
} & AuthSystemCreateFields
export type EverythingCreate<Tanother_json_field = unknown, Tjson_field = unknown> = {
another_json_field?: null | Tanother_json_field
bool_field?: boolean
created?: IsoDateString
custom_relation_field?: RecordIdString[]
date_field?: IsoDateString
email_field?: string
Expand All @@ -223,6 +228,7 @@ export type EverythingCreate<Tanother_json_field = unknown, Tjson_field = unknow
select_field_no_values?: string
text_field?: string
three_files_field?: File[]
updated?: IsoDateString
url_field?: string
user_relation_field?: RecordIdString
} & BaseSystemCreateFields
Expand All @@ -234,31 +240,40 @@ export type MyViewCreate<Tjson_field = unknown> = {
} & BaseSystemCreateFields
export type PostsCreate = {
created?: IsoDateString
field1?: number
nonempty_bool: boolean
nonempty_field: string
updated?: IsoDateString
} & BaseSystemCreateFields
export type UsersCreate = {
avatar?: File
created?: IsoDateString
name?: string
updated?: IsoDateString
} & AuthSystemCreateFields
// Update types for each collection
export type SuperusersUpdate = AuthSystemUpdateFields
export type BaseUpdate = {
created?: IsoDateString
field?: string
updated?: IsoDateString
} & BaseSystemUpdateFields
export type CustomAuthUpdate = {
created?: IsoDateString
custom_field?: string
updated?: IsoDateString
} & AuthSystemUpdateFields
export type EverythingUpdate<Tanother_json_field = unknown, Tjson_field = unknown> = {
another_json_field?: null | Tanother_json_field
bool_field?: boolean
created?: IsoDateString
custom_relation_field?: RecordIdString[]
date_field?: IsoDateString
email_field?: string
Expand All @@ -271,6 +286,7 @@ export type EverythingUpdate<Tanother_json_field = unknown, Tjson_field = unknow
select_field_no_values?: string
text_field?: string
three_files_field?: Nullable<File[]>
updated?: IsoDateString
url_field?: string
user_relation_field?: RecordIdString
} & BaseSystemUpdateFields
Expand All @@ -282,14 +298,18 @@ export type MyViewUpdate<Tjson_field = unknown> = {
} & BaseSystemUpdateFields
export type PostsUpdate = {
created?: IsoDateString
field1?: number
nonempty_bool?: boolean
nonempty_field?: string
updated?: IsoDateString
} & BaseSystemUpdateFields
export type UsersUpdate = {
avatar?: Nullable<File>
created?: IsoDateString
name?: string
updated?: IsoDateString
} & AuthSystemUpdateFields
// Response types include system fields and match responses from the PocketBase API
Expand Down
20 changes: 20 additions & 0 deletions test/pocketbase-types-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,21 @@ export type UsersRecord = {
export type SuperusersCreate = AuthSystemCreateFields

export type BaseCreate = {
created?: IsoDateString
field?: string
updated?: IsoDateString
} & BaseSystemCreateFields

export type CustomAuthCreate = {
created?: IsoDateString
custom_field?: string
updated?: IsoDateString
} & AuthSystemCreateFields

export type EverythingCreate<Tanother_json_field = unknown, Tjson_field = unknown> = {
another_json_field?: null | Tanother_json_field
bool_field?: boolean
created?: IsoDateString
custom_relation_field?: RecordIdString[]
date_field?: IsoDateString
email_field?: string
Expand All @@ -220,6 +225,7 @@ export type EverythingCreate<Tanother_json_field = unknown, Tjson_field = unknow
select_field_no_values?: string
text_field?: string
three_files_field?: File[]
updated?: IsoDateString
url_field?: string
user_relation_field?: RecordIdString
} & BaseSystemCreateFields
Expand All @@ -231,31 +237,40 @@ export type MyViewCreate<Tjson_field = unknown> = {
} & BaseSystemCreateFields

export type PostsCreate = {
created?: IsoDateString
field1?: number
nonempty_bool: boolean
nonempty_field: string
updated?: IsoDateString
} & BaseSystemCreateFields

export type UsersCreate = {
avatar?: File
created?: IsoDateString
name?: string
updated?: IsoDateString
} & AuthSystemCreateFields

// Update types for each collection

export type SuperusersUpdate = AuthSystemUpdateFields

export type BaseUpdate = {
created?: IsoDateString
field?: string
updated?: IsoDateString
} & BaseSystemUpdateFields

export type CustomAuthUpdate = {
created?: IsoDateString
custom_field?: string
updated?: IsoDateString
} & AuthSystemUpdateFields

export type EverythingUpdate<Tanother_json_field = unknown, Tjson_field = unknown> = {
another_json_field?: null | Tanother_json_field
bool_field?: boolean
created?: IsoDateString
custom_relation_field?: RecordIdString[]
date_field?: IsoDateString
email_field?: string
Expand All @@ -268,6 +283,7 @@ export type EverythingUpdate<Tanother_json_field = unknown, Tjson_field = unknow
select_field_no_values?: string
text_field?: string
three_files_field?: Nullable<File[]>
updated?: IsoDateString
url_field?: string
user_relation_field?: RecordIdString
} & BaseSystemUpdateFields
Expand All @@ -279,14 +295,18 @@ export type MyViewUpdate<Tjson_field = unknown> = {
} & BaseSystemUpdateFields

export type PostsUpdate = {
created?: IsoDateString
field1?: number
nonempty_bool?: boolean
nonempty_field?: string
updated?: IsoDateString
} & BaseSystemUpdateFields

export type UsersUpdate = {
avatar?: Nullable<File>
created?: IsoDateString
name?: string
updated?: IsoDateString
} & AuthSystemUpdateFields

// Response types include system fields and match responses from the PocketBase API
Expand Down

0 comments on commit 6798291

Please # to comment.