Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/payload/src/bin/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export async function generateTypes(

const i18n = await initI18n({ config: config.i18n, context: 'api', language })

const jsonSchema = configToJSONSchema(config, config.db.defaultIDType, i18n)
const jsonSchema = configToJSONSchema(
config,
config.typescript?.defaultIDType ?? config.db.defaultIDType,
i18n,
)

const declare = `declare module 'payload' {\n export interface GeneratedTypes extends Config {}\n}`
const declareWithTSIgnoreError = `declare module 'payload' {\n // @ts-ignore \n export interface GeneratedTypes extends Config {}\n}`
Expand Down
8 changes: 7 additions & 1 deletion packages/payload/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,6 @@ export type Config = {
* @default true
*/
autoGenerate?: boolean

/** Disable declare block in generated types file */
declare?:
| {
Expand All @@ -1307,6 +1306,13 @@ export type Config = {
}
| false

/**
* Override the default ID field type set by the database adapter for generated types.
* This is useful when switching between database adapters in order to ensure the generated types remain consistent,
* for example in the Payload monorepo.
*/
defaultIDType?: 'number' | 'text'

/** Filename to write the generated types to */
outputFile?: string

Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/utilities/configToJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ function generateAuthEntitySchemas(entities: SanitizedCollectionConfig[]): JSONS
* @example { db: idType: string }
*/
function generateDbEntitySchema(config: SanitizedConfig): JSONSchema4 {
const defaultIDTypeConfig = config.typescript?.defaultIDType || config.db?.defaultIDType
const defaultIDType: JSONSchema4 =
config.db?.defaultIDType === 'number' ? { type: 'number' } : { type: 'string' }
defaultIDTypeConfig === 'number' ? { type: 'number' } : { type: 'string' }

return {
type: 'object',
Expand Down
2 changes: 1 addition & 1 deletion test/_community/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export interface Post {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down
24 changes: 13 additions & 11 deletions test/access-control/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export interface HiddenField {
}[]
| null;
hidden?: boolean | null;
hiddenWithDefault?: string | null;
updatedAt: string;
createdAt: string;
}
Expand Down Expand Up @@ -490,7 +491,7 @@ export interface RichText {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down Expand Up @@ -520,7 +521,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -538,7 +539,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -555,7 +556,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -576,7 +577,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -593,7 +594,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -615,7 +616,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -635,7 +636,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -655,7 +656,7 @@ export interface Regression1 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down Expand Up @@ -685,7 +686,7 @@ export interface Regression2 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -704,7 +705,7 @@ export interface Regression2 {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down Expand Up @@ -1116,6 +1117,7 @@ export interface HiddenFieldsSelect<T extends boolean = true> {
id?: T;
};
hidden?: T;
hiddenWithDefault?: T;
updatedAt?: T;
createdAt?: T;
}
Expand Down
14 changes: 14 additions & 0 deletions test/admin-bar/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ export interface User {
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
sessions?:
| {
id: string;
createdAt?: string | null;
expiresAt: string;
}[]
| null;
password?: string | null;
}
/**
Expand Down Expand Up @@ -323,6 +330,13 @@ export interface UsersSelect<T extends boolean = true> {
hash?: T;
loginAttempts?: T;
lockUntil?: T;
sessions?:
| T
| {
id?: T;
createdAt?: T;
expiresAt?: T;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
Expand Down
14 changes: 14 additions & 0 deletions test/admin-root/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ export interface User {
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
sessions?:
| {
id: string;
createdAt?: string | null;
expiresAt: string;
}[]
| null;
password?: string | null;
}
/**
Expand Down Expand Up @@ -226,6 +233,13 @@ export interface UsersSelect<T extends boolean = true> {
hash?: T;
loginAttempts?: T;
lockUntil?: T;
sessions?:
| T
| {
id?: T;
createdAt?: T;
expiresAt?: T;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
Expand Down
14 changes: 14 additions & 0 deletions test/array-update/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ export interface User {
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
sessions?:
| {
id: string;
createdAt?: string | null;
expiresAt: string;
}[]
| null;
password?: string | null;
}
/**
Expand Down Expand Up @@ -246,6 +253,13 @@ export interface UsersSelect<T extends boolean = true> {
hash?: T;
loginAttempts?: T;
lockUntil?: T;
sessions?:
| T
| {
id?: T;
createdAt?: T;
expiresAt?: T;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
Expand Down
14 changes: 14 additions & 0 deletions test/auth-basic/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ export interface User {
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
sessions?:
| {
id: string;
createdAt?: string | null;
expiresAt: string;
}[]
| null;
password?: string | null;
}
/**
Expand Down Expand Up @@ -194,6 +201,13 @@ export interface UsersSelect<T extends boolean = true> {
hash?: T;
loginAttempts?: T;
lockUntil?: T;
sessions?:
| T
| {
id?: T;
createdAt?: T;
expiresAt?: T;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
Expand Down
6 changes: 3 additions & 3 deletions test/auth/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export interface User {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down Expand Up @@ -275,6 +275,7 @@ export interface User {
unnamedTabSaveToJWTString?: string | null;
unnamedTabSaveToJWTFalse?: string | null;
custom?: string | null;
shouldNotShowInClientConfigUnlessAuthenticated?: string | null;
updatedAt: string;
createdAt: string;
enableAPIKey?: boolean | null;
Expand Down Expand Up @@ -348,7 +349,6 @@ export interface ApiKey {
*/
export interface PublicUser {
id: string;
shouldNotShowInClientConfigUnlessAuthenticated?: string | null;
updatedAt: string;
createdAt: string;
email: string;
Expand Down Expand Up @@ -545,6 +545,7 @@ export interface UsersSelect<T extends boolean = true> {
unnamedTabSaveToJWTString?: T;
unnamedTabSaveToJWTFalse?: T;
custom?: T;
shouldNotShowInClientConfigUnlessAuthenticated?: T;
updatedAt?: T;
createdAt?: T;
enableAPIKey?: T;
Expand Down Expand Up @@ -612,7 +613,6 @@ export interface ApiKeysSelect<T extends boolean = true> {
* via the `definition` "public-users_select".
*/
export interface PublicUsersSelect<T extends boolean = true> {
shouldNotShowInClientConfigUnlessAuthenticated?: T;
updatedAt?: T;
createdAt?: T;
email?: T;
Expand Down
16 changes: 15 additions & 1 deletion test/benchmark-blocks/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8529,7 +8529,7 @@ export interface Post {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down Expand Up @@ -35012,6 +35012,13 @@ export interface User {
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
sessions?:
| {
id: string;
createdAt?: string | null;
expiresAt: string;
}[]
| null;
password?: string | null;
}
/**
Expand Down Expand Up @@ -35763,6 +35770,13 @@ export interface UsersSelect<T extends boolean = true> {
hash?: T;
loginAttempts?: T;
lockUntil?: T;
sessions?:
| T
| {
id?: T;
createdAt?: T;
expiresAt?: T;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
Expand Down
2 changes: 2 additions & 0 deletions test/buildConfigWithDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export async function buildConfigWithDefaults(
...(testConfig?.i18n || {}),
},
typescript: {
// Set defaultIDType to 'text' to ensure consistent generated types when switching db adapters
defaultIDType: 'text',
declare: {
ignoreTSError: true,
},
Expand Down
14 changes: 14 additions & 0 deletions test/collections-graphql/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ export interface User {
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
sessions?:
| {
id: string;
createdAt?: string | null;
expiresAt: string;
}[]
| null;
password?: string | null;
}
/**
Expand Down Expand Up @@ -435,6 +442,13 @@ export interface UsersSelect<T extends boolean = true> {
hash?: T;
loginAttempts?: T;
lockUntil?: T;
sessions?:
| T
| {
id?: T;
createdAt?: T;
expiresAt?: T;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
Expand Down
Loading
Loading