Files
pocketbase-typegen/test/pocketbase-types-example.ts
2025-06-02 14:14:46 +00:00

295 lines
8.0 KiB
TypeScript

/**
* This file was @generated using pocketbase-typegen
*/
import type PocketBase from 'pocketbase'
import type { RecordOptions, RecordService } from 'pocketbase'
export enum Collections {
Authorigins = "_authOrigins",
Externalauths = "_externalAuths",
Mfas = "_mfas",
Otps = "_otps",
Superusers = "_superusers",
Base = "base",
CustomAuth = "custom_auth",
Everything = "everything",
MyView = "my_view",
Posts = "posts",
Users = "users",
}
// Alias types for improved usability
export type IsoDateString = string
export type IsoAutoDateString = string & { readonly autodate: unique symbol }
export type RecordIdString = string
export type FileNameString = string & { readonly filename: unique symbol }
export type HTMLString = string
export type GeoPoint = {
lon: number
lat: number
}
type ExpandType<T> = unknown extends T
? T extends unknown
? { expand?: unknown }
: { expand: T }
: { expand: T }
// System fields
export type BaseSystemFields<T = unknown> = {
id: RecordIdString
collectionId: string
collectionName: Collections
} & ExpandType<T>
export type AuthSystemFields<T = unknown> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
} & BaseSystemFields<T>
// Record types for each collection
export type AuthoriginsRecord = {
collectionRef: string
created: IsoAutoDateString
fingerprint: string
id: string
recordRef: string
updated: IsoAutoDateString
}
export type ExternalauthsRecord = {
collectionRef: string
created: IsoAutoDateString
id: string
provider: string
providerId: string
recordRef: string
updated: IsoAutoDateString
}
export type MfasRecord = {
collectionRef: string
created: IsoAutoDateString
id: string
method: string
recordRef: string
updated: IsoAutoDateString
}
export type OtpsRecord = {
collectionRef: string
created: IsoAutoDateString
id: string
password: string
recordRef: string
sentTo?: string
updated: IsoAutoDateString
}
export type SuperusersRecord = {
created: IsoAutoDateString
email: string
emailVisibility?: boolean
id: string
password: string
tokenKey: string
updated: IsoAutoDateString
verified?: boolean
}
export type BaseRecord = {
created: IsoAutoDateString
field?: string
id: string
updated: IsoAutoDateString
}
export type CustomAuthRecord = {
created: IsoAutoDateString
custom_field?: string
email: string
emailVisibility?: boolean
id: string
password: string
tokenKey: string
updated: IsoAutoDateString
verified?: boolean
}
export enum EverythingSelectFieldOptions {
"optionA" = "optionA",
"OptionA" = "OptionA",
"optionB" = "optionB",
"optionC" = "optionC",
"option with space" = "option with space",
"sy?mb@!$" = "sy?mb@!$",
}
export type EverythingRecord<Tanother_json_field = unknown, Tjson_field = unknown> = {
another_json_field?: null | Tanother_json_field
bool_field?: boolean
created: IsoAutoDateString
custom_relation_field?: RecordIdString[]
date_field?: IsoDateString
email_field?: string
file_field?: FileNameString
geopoint_field?: GeoPoint
id: string
json_field?: null | Tjson_field
number_field?: number
post_relation_field?: RecordIdString
rich_editor_field?: HTMLString
select_field?: EverythingSelectFieldOptions
select_field_no_values?: string
text_field?: string
three_files_field?: FileNameString[]
updated?: IsoAutoDateString
url_field?: string
user_relation_field?: RecordIdString
}
export type MyViewRecord<Tjson_field = unknown> = {
id: string
json_field?: null | Tjson_field
post_relation_field?: RecordIdString
text_field?: string
}
export type PostsRecord = {
created: IsoAutoDateString
field1?: number
id: string
nonempty_bool: boolean
nonempty_field: string
updated?: IsoAutoDateString
}
export type UsersRecord = {
avatar?: FileNameString
created: IsoAutoDateString
email: string
emailVisibility?: boolean
id: string
name?: string
password: string
tokenKey: string
updated: IsoAutoDateString
verified?: boolean
}
// Response types include system fields and match responses from the PocketBase API
export type AuthoriginsResponse<Texpand = unknown> = Required<AuthoriginsRecord> & BaseSystemFields<Texpand>
export type ExternalauthsResponse<Texpand = unknown> = Required<ExternalauthsRecord> & BaseSystemFields<Texpand>
export type MfasResponse<Texpand = unknown> = Required<MfasRecord> & BaseSystemFields<Texpand>
export type OtpsResponse<Texpand = unknown> = Required<OtpsRecord> & BaseSystemFields<Texpand>
export type SuperusersResponse<Texpand = unknown> = Required<SuperusersRecord> & AuthSystemFields<Texpand>
export type BaseResponse<Texpand = unknown> = Required<BaseRecord> & BaseSystemFields<Texpand>
export type CustomAuthResponse<Texpand = unknown> = Required<CustomAuthRecord> & AuthSystemFields<Texpand>
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = Required<EverythingRecord<Tanother_json_field, Tjson_field>> & BaseSystemFields<Texpand>
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = Required<MyViewRecord<Tjson_field>> & BaseSystemFields<Texpand>
export type PostsResponse<Texpand = unknown> = Required<PostsRecord> & BaseSystemFields<Texpand>
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand>
// Types containing all Records and Responses, useful for creating typing helper functions
export type CollectionRecords = {
_authOrigins: AuthoriginsRecord
_externalAuths: ExternalauthsRecord
_mfas: MfasRecord
_otps: OtpsRecord
_superusers: SuperusersRecord
base: BaseRecord
custom_auth: CustomAuthRecord
everything: EverythingRecord
my_view: MyViewRecord
posts: PostsRecord
users: UsersRecord
}
export type CollectionResponses = {
_authOrigins: AuthoriginsResponse
_externalAuths: ExternalauthsResponse
_mfas: MfasResponse
_otps: OtpsResponse
_superusers: SuperusersResponse
base: BaseResponse
custom_auth: CustomAuthResponse
everything: EverythingResponse
my_view: MyViewResponse
posts: PostsResponse
users: UsersResponse
}
// Utility types for create/update operations
type ProcessCreateAndUpdateFields<T> = Omit<{
// Omit AutoDate fields
[K in keyof T as Extract<T[K], IsoAutoDateString> extends never ? K : never]:
// Convert FileNameString to File
T[K] extends infer U ?
U extends (FileNameString | FileNameString[]) ?
U extends any[] ? File[] : File
: U
: never
}, 'id'>
// Create type for Auth collections
export type CreateAuth<T> = {
id?: RecordIdString
email: string
emailVisibility?: boolean
password: string
passwordConfirm: string
verified?: boolean
} & ProcessCreateAndUpdateFields<T>
// Create type for Base collections
export type CreateBase<T> = {
id?: RecordIdString
} & ProcessCreateAndUpdateFields<T>
// Update type for Auth collections
export type UpdateAuth<T> = Partial<
Omit<ProcessCreateAndUpdateFields<T>, keyof AuthSystemFields>
> & {
email?: string
emailVisibility?: boolean
oldPassword?: string
password?: string
passwordConfirm?: string
verified?: boolean
}
// Update type for Base collections
export type UpdateBase<T> = Partial<
Omit<ProcessCreateAndUpdateFields<T>, keyof BaseSystemFields>
>
// Get the correct create type for any collection
export type Create<T extends keyof CollectionResponses> =
CollectionResponses[T] extends AuthSystemFields
? CreateAuth<CollectionRecords[T]>
: CreateBase<CollectionRecords[T]>
// Get the correct update type for any collection
export type Update<T extends keyof CollectionResponses> =
CollectionResponses[T] extends AuthSystemFields
? UpdateAuth<CollectionRecords[T]>
: UpdateBase<CollectionRecords[T]>
// Type for usage with type asserted PocketBase instance
// https://github.com/pocketbase/js-sdk#specify-typescript-definitions
export type TypedPocketBase = {
collection<T extends keyof CollectionResponses>(
idOrName: T
): Omit<RecordService<CollectionResponses[T]>, 'create' | 'update'> & {
create(bodyParams?: Create<T> | FormData, options?: RecordOptions): Promise<CollectionResponses[T]>;
update(id: string, bodyParams?: Update<T> | FormData, options?: RecordOptions): Promise<CollectionResponses[T]>;
}
} & PocketBase