From 1cabdf9148d11bccea6759e6c35ba29ba71e8f5a Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 12 Feb 2026 02:30:00 +0000 Subject: [PATCH 1/9] feat: `customId` function --- packages/language/res/stdlib.zmodel | 7 +++++++ .../orm/src/client/crud/operations/base.ts | 14 +++++++++++-- packages/orm/src/client/options.ts | 20 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/language/res/stdlib.zmodel b/packages/language/res/stdlib.zmodel index cb604c74a..e7f9124e6 100644 --- a/packages/language/res/stdlib.zmodel +++ b/packages/language/res/stdlib.zmodel @@ -104,6 +104,13 @@ function nanoid(length: Int?, format: String?): String { function ulid(format: String?): String { } @@@expressionContext([DefaultValue]) +/** + * Generates a custom identifier. The ORM client must be initialized with an + * implementation of this function. + */ +function customId(length: Int?): String { +} @@@expressionContext([DefaultValue]) + /** * Creates a sequence of integers in the underlying database and assign the incremented * values to the ID values of the created records based on the sequence. diff --git a/packages/orm/src/client/crud/operations/base.ts b/packages/orm/src/client/crud/operations/base.ts index fc75cac9d..7b5f7b0f9 100644 --- a/packages/orm/src/client/crud/operations/base.ts +++ b/packages/orm/src/client/crud/operations/base.ts @@ -1044,7 +1044,7 @@ export abstract class BaseOperationHandler { } if (!(field in data)) { if (typeof fieldDef?.default === 'object' && 'kind' in fieldDef.default) { - const generated = this.evalGenerator(fieldDef.default); + const generated = this.evalGenerator(fieldDef.default, modelDef.name); if (generated !== undefined) { values[field] = this.dialect.transformInput( generated, @@ -1072,7 +1072,7 @@ export abstract class BaseOperationHandler { return values; } - private evalGenerator(defaultValue: Expression) { + private evalGenerator(defaultValue: Expression, model: string) { if (ExpressionUtils.isCall(defaultValue)) { const firstArgVal = defaultValue.args?.[0] && ExpressionUtils.isLiteral(defaultValue.args[0]) @@ -1095,6 +1095,16 @@ export abstract class BaseOperationHandler { return this.formatGeneratedValue(generated, defaultValue.args?.[1]); }) .with('ulid', () => this.formatGeneratedValue(ulid(), defaultValue.args?.[0])) + .with('customId', () => { + invariant(this.client.$options.customId, '"customId" implementation not provided'); + const length = typeof firstArgVal === 'number' ? firstArgVal : undefined; + const generated = this.client.$options.customId({ + model, + length, + }); + invariant(generated && typeof generated === 'string', '"customId" must return a non-empty string'); + return generated; + }) .otherwise(() => undefined); } else if ( ExpressionUtils.isMember(defaultValue) && diff --git a/packages/orm/src/client/options.ts b/packages/orm/src/client/options.ts index 6439e3996..8f22d5e39 100644 --- a/packages/orm/src/client/options.ts +++ b/packages/orm/src/client/options.ts @@ -40,6 +40,20 @@ export type ZModelFunction = ( context: ZModelFunctionContext, ) => Expression; +export type CustomIdFunctionContext = { + /** + * The model for which the ID should be generated for. + */ + model: string; + + /** + * The requested length of the ID. + */ + length?: number; +}; + +export type CustomIdFunction = (ctx: CustomIdFunctionContext) => string; + /** * ZenStack client options. */ @@ -82,6 +96,12 @@ export type ClientOptions = { */ validateInput?: boolean; + /** + * Implementation of a custom ID generation function, which is called from ZModel as + * `@default(customId())`. + */ + customId?: CustomIdFunction; + /** * Options for omitting fields in ORM query results. */ From 7338c118b182e3c90de3cced1e12df208e5b9eee Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 12 Feb 2026 02:30:23 +0000 Subject: [PATCH 2/9] chore: add tests and validations --- .../function-invocation-validator.ts | 14 ++ .../language/test/function-invocation.test.ts | 18 ++ tests/e2e/orm/client-api/custom-id.test.ts | 161 ++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 tests/e2e/orm/client-api/custom-id.test.ts diff --git a/packages/language/src/validators/function-invocation-validator.ts b/packages/language/src/validators/function-invocation-validator.ts index 8ce1035e3..25106f1a6 100644 --- a/packages/language/src/validators/function-invocation-validator.ts +++ b/packages/language/src/validators/function-invocation-validator.ts @@ -237,6 +237,20 @@ export default class FunctionInvocationValidator implements AstValidator(lengthArg); + if (length !== undefined && length <= 0) { + accept('error', 'first argument must be a positive number', { + node: expr.args[0]!, + }); + } + } + } + @func('auth') private _checkAuth(expr: InvocationExpr, accept: ValidationAcceptor) { if (!expr.$resolvedType) { diff --git a/packages/language/test/function-invocation.test.ts b/packages/language/test/function-invocation.test.ts index ff6bb45ef..6f5a22e93 100644 --- a/packages/language/test/function-invocation.test.ts +++ b/packages/language/test/function-invocation.test.ts @@ -414,4 +414,22 @@ describe('Function Invocation Tests', () => { ); }); }); + + describe('customId() length validation', () => { + it('should reject non-positive lengths', async () => { + await loadSchemaWithError( + ` + datasource db { + provider = 'sqlite' + url = 'file:./dev.db' + } + + model User { + id String @id @default(customId(-1)) + } + `, + 'first argument must be a positive number', + ); + }); + }); }); diff --git a/tests/e2e/orm/client-api/custom-id.test.ts b/tests/e2e/orm/client-api/custom-id.test.ts new file mode 100644 index 000000000..58bf5e4f4 --- /dev/null +++ b/tests/e2e/orm/client-api/custom-id.test.ts @@ -0,0 +1,161 @@ +import { createTestClient } from '@zenstackhq/testtools'; +import { describe, expect, it } from 'vitest'; + +const schema = ` + model User { + id String @id @default(customId()) + posts Post[] + } + + model Post { + id String @id @default(customId()) + userId String? + user User? @relation(fields: [userId], references: [id]) + comments Comment[] + } + + model Comment { + id String @id @default(customId()) + postId String? + post Post? @relation(fields: [postId], references: [id]) + } +`; + +describe('customId', async () => { + it('works with no arguments', async () => { + const client = await createTestClient(schema, { + customId: ({ model, length }) => `${model}.${length ?? 16}`, + }); + + await expect(client.user.create({ data: {} })).resolves.toMatchObject({ + id: 'User.16', + }); + + await expect(client.post.create({ data: {} })).resolves.toMatchObject({ + id: 'Post.16', + }); + + await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ + id: 'Comment.16', + }); + }); + + it('works with arguments', async () => { + const schema = ` + model User { + id String @id @default(customId(8)) + posts Post[] + } + + model Post { + id String @id @default(customId(8)) + userId String? + user User? @relation(fields: [userId], references: [id]) + comments Comment[] + } + + model Comment { + id String @id @default(customId(8)) + postId String? + post Post? @relation(fields: [postId], references: [id]) + } + `; + + const client = await createTestClient(schema, { + customId: ({ model, length }) => `${model}.${length}`, + }); + + await expect(client.user.create({ data: {} })).resolves.toMatchObject({ + id: 'User.8', + }); + + await expect(client.post.create({ data: {} })).resolves.toMatchObject({ + id: 'Post.8', + }); + + await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ + id: 'Comment.8', + }); + }); + + it('works with nested', async () => { + const client = await createTestClient(schema, { + customId: ({ model, length }) => `${model}.${length ?? 16}`, + }); + + await expect(client.user.create({ + data: { + posts: { + create: {}, + }, + }, + })).resolves.toMatchObject({ + id: 'User.16', + }); + + await expect(client.post.findUnique({ + where: { + id: 'Post.16', + } + })).resolves.toBeTruthy(); + }); + + it('works with deeply nested', async () => { + const client = await createTestClient(schema, { + customId: ({ model, length }) => `${model}.${length ?? 16}`, + }); + + await expect(client.user.create({ + data: { + posts: { + create: { + comments: { + create: {}, + }, + }, + }, + }, + })).resolves.toMatchObject({ + id: 'User.16', + }); + + await expect(client.post.findUnique({ + where: { + id: 'Post.16', + } + })).resolves.toBeTruthy(); + + await expect(client.comment.findUnique({ + where: { + id: 'Comment.16', + } + })).resolves.toBeTruthy(); + }); + + it('rejects without an implementation', async () => { + const client = await createTestClient(schema); + await expect(client.user.create({ data: {} })).rejects.toThrowError('implementation not provided'); + }); + + it('rejects without a valid implementation', async () => { + // @ts-expect-error + let client = await createTestClient(schema, { + customId: () => undefined, + }); + // @ts-expect-error + await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); + + client = await createTestClient(schema, { + customId: () => '', + }); + // @ts-expect-error + await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); + + // @ts-expect-error + client = await createTestClient(schema, { + customId: () => 1, + }); + // @ts-expect-error + await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); + }); +}); From 77ffc7485f47c8952bab31d2475d5bfe1ef61727 Mon Sep 17 00:00:00 2001 From: sanny-io Date: Thu, 12 Feb 2026 02:50:12 +0000 Subject: [PATCH 3/9] chore: fix tests --- tests/e2e/orm/client-api/custom-id.test.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/e2e/orm/client-api/custom-id.test.ts b/tests/e2e/orm/client-api/custom-id.test.ts index 58bf5e4f4..c3ffd0910 100644 --- a/tests/e2e/orm/client-api/custom-id.test.ts +++ b/tests/e2e/orm/client-api/custom-id.test.ts @@ -137,22 +137,25 @@ describe('customId', async () => { await expect(client.user.create({ data: {} })).rejects.toThrowError('implementation not provided'); }); - it('rejects without a valid implementation', async () => { + it('rejects without a valid implementation (undefined)', async () => { // @ts-expect-error - let client = await createTestClient(schema, { + const client = await createTestClient(schema, { customId: () => undefined, }); // @ts-expect-error await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); + }); - client = await createTestClient(schema, { + it('rejects without a valid implementation (empty string)', async () => { + const client = await createTestClient(schema, { customId: () => '', }); - // @ts-expect-error await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); + }); + it('rejects without a valid implementation (non-string)', async () => { // @ts-expect-error - client = await createTestClient(schema, { + const client = await createTestClient(schema, { customId: () => 1, }); // @ts-expect-error From 2a5ad8ff5dd9e50d0fb764ef7eb84397d51e52d7 Mon Sep 17 00:00:00 2001 From: sanny-io Date: Thu, 12 Feb 2026 03:04:04 +0000 Subject: [PATCH 4/9] chore: address comments --- packages/language/test/function-invocation.test.ts | 14 ++++++++++++++ tests/e2e/orm/client-api/custom-id.test.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/language/test/function-invocation.test.ts b/packages/language/test/function-invocation.test.ts index 6f5a22e93..0fbd469a7 100644 --- a/packages/language/test/function-invocation.test.ts +++ b/packages/language/test/function-invocation.test.ts @@ -417,6 +417,20 @@ describe('Function Invocation Tests', () => { describe('customId() length validation', () => { it('should reject non-positive lengths', async () => { + await loadSchemaWithError( + ` + datasource db { + provider = 'sqlite' + url = 'file:./dev.db' + } + + model User { + id String @id @default(customId(0)) + } + `, + 'first argument must be a positive number', + ); + await loadSchemaWithError( ` datasource db { diff --git a/tests/e2e/orm/client-api/custom-id.test.ts b/tests/e2e/orm/client-api/custom-id.test.ts index c3ffd0910..3c24958e3 100644 --- a/tests/e2e/orm/client-api/custom-id.test.ts +++ b/tests/e2e/orm/client-api/custom-id.test.ts @@ -21,7 +21,7 @@ const schema = ` } `; -describe('customId', async () => { +describe('customId', () => { it('works with no arguments', async () => { const client = await createTestClient(schema, { customId: ({ model, length }) => `${model}.${length ?? 16}`, From f09a9e7c7bed56972bca56360f35edb60335a13b Mon Sep 17 00:00:00 2001 From: sanny-io Date: Thu, 12 Feb 2026 04:42:56 +0000 Subject: [PATCH 5/9] Add `client` and `field` to context. --- .../orm/src/client/crud/operations/base.ts | 6 ++- packages/orm/src/client/options.ts | 18 +++++-- tests/e2e/orm/client-api/custom-id.test.ts | 50 +++++++++---------- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/packages/orm/src/client/crud/operations/base.ts b/packages/orm/src/client/crud/operations/base.ts index 7b5f7b0f9..970a2f9ed 100644 --- a/packages/orm/src/client/crud/operations/base.ts +++ b/packages/orm/src/client/crud/operations/base.ts @@ -1044,7 +1044,7 @@ export abstract class BaseOperationHandler { } if (!(field in data)) { if (typeof fieldDef?.default === 'object' && 'kind' in fieldDef.default) { - const generated = this.evalGenerator(fieldDef.default, modelDef.name); + const generated = this.evalGenerator(fieldDef.default, modelDef.name, field); if (generated !== undefined) { values[field] = this.dialect.transformInput( generated, @@ -1072,7 +1072,7 @@ export abstract class BaseOperationHandler { return values; } - private evalGenerator(defaultValue: Expression, model: string) { + private evalGenerator(defaultValue: Expression, model: string, field: string) { if (ExpressionUtils.isCall(defaultValue)) { const firstArgVal = defaultValue.args?.[0] && ExpressionUtils.isLiteral(defaultValue.args[0]) @@ -1099,7 +1099,9 @@ export abstract class BaseOperationHandler { invariant(this.client.$options.customId, '"customId" implementation not provided'); const length = typeof firstArgVal === 'number' ? firstArgVal : undefined; const generated = this.client.$options.customId({ + client: this.client, model, + field, length, }); invariant(generated && typeof generated === 'string', '"customId" must return a non-empty string'); diff --git a/packages/orm/src/client/options.ts b/packages/orm/src/client/options.ts index 8f22d5e39..5b7ec8c1c 100644 --- a/packages/orm/src/client/options.ts +++ b/packages/orm/src/client/options.ts @@ -40,19 +40,29 @@ export type ZModelFunction = ( context: ZModelFunctionContext, ) => Expression; -export type CustomIdFunctionContext = { +export type CustomIdFunctionContext = { + /** + * ZenStack client instance. + */ + client: ClientContract; + /** * The model for which the ID should be generated for. */ model: string; /** - * The requested length of the ID. + * The field for which the ID should be generated for. + */ + field: string; + + /** + * The length of the ID as requested by the schema. */ length?: number; }; -export type CustomIdFunction = (ctx: CustomIdFunctionContext) => string; +export type CustomIdFunction = (ctx: CustomIdFunctionContext) => string; /** * ZenStack client options. @@ -100,7 +110,7 @@ export type ClientOptions = { * Implementation of a custom ID generation function, which is called from ZModel as * `@default(customId())`. */ - customId?: CustomIdFunction; + customId?: CustomIdFunction; /** * Options for omitting fields in ORM query results. diff --git a/tests/e2e/orm/client-api/custom-id.test.ts b/tests/e2e/orm/client-api/custom-id.test.ts index 3c24958e3..6ffca2b3e 100644 --- a/tests/e2e/orm/client-api/custom-id.test.ts +++ b/tests/e2e/orm/client-api/custom-id.test.ts @@ -3,84 +3,84 @@ import { describe, expect, it } from 'vitest'; const schema = ` model User { - id String @id @default(customId()) + uid String @id @default(customId()) posts Post[] } model Post { - id String @id @default(customId()) + pid String @id @default(customId()) userId String? - user User? @relation(fields: [userId], references: [id]) + user User? @relation(fields: [userId], references: [uid]) comments Comment[] } model Comment { - id String @id @default(customId()) + cid String @id @default(customId()) postId String? - post Post? @relation(fields: [postId], references: [id]) + post Post? @relation(fields: [postId], references: [pid]) } `; describe('customId', () => { it('works with no arguments', async () => { const client = await createTestClient(schema, { - customId: ({ model, length }) => `${model}.${length ?? 16}`, + customId: ({ model, field, length }) => `${model}.${field}.${length ?? 16}`, }); await expect(client.user.create({ data: {} })).resolves.toMatchObject({ - id: 'User.16', + uid: 'User.uid.16', }); await expect(client.post.create({ data: {} })).resolves.toMatchObject({ - id: 'Post.16', + pid: 'Post.pid.16', }); await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ - id: 'Comment.16', + cid: 'Comment.cid.16', }); }); it('works with arguments', async () => { const schema = ` model User { - id String @id @default(customId(8)) + uid String @id @default(customId(8)) posts Post[] } model Post { - id String @id @default(customId(8)) + pid String @id @default(customId(8)) userId String? - user User? @relation(fields: [userId], references: [id]) + user User? @relation(fields: [userId], references: [uid]) comments Comment[] } model Comment { - id String @id @default(customId(8)) + cid String @id @default(customId(8)) postId String? - post Post? @relation(fields: [postId], references: [id]) + post Post? @relation(fields: [postId], references: [pid]) } `; const client = await createTestClient(schema, { - customId: ({ model, length }) => `${model}.${length}`, + customId: ({ model, field, length }) => `${model}.${field}.${length}`, }); await expect(client.user.create({ data: {} })).resolves.toMatchObject({ - id: 'User.8', + uid: 'User.uid.8', }); await expect(client.post.create({ data: {} })).resolves.toMatchObject({ - id: 'Post.8', + pid: 'Post.pid.8', }); await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ - id: 'Comment.8', + cid: 'Comment.cid.8', }); }); it('works with nested', async () => { const client = await createTestClient(schema, { - customId: ({ model, length }) => `${model}.${length ?? 16}`, + customId: ({ model, field, length }) => `${model}.${field}.${length ?? 16}`, }); await expect(client.user.create({ @@ -90,19 +90,19 @@ describe('customId', () => { }, }, })).resolves.toMatchObject({ - id: 'User.16', + uid: 'User.uid.16', }); await expect(client.post.findUnique({ where: { - id: 'Post.16', + pid: 'Post.pid.16', } })).resolves.toBeTruthy(); }); it('works with deeply nested', async () => { const client = await createTestClient(schema, { - customId: ({ model, length }) => `${model}.${length ?? 16}`, + customId: ({ model, field, length }) => `${model}.${field}.${length ?? 16}`, }); await expect(client.user.create({ @@ -116,18 +116,18 @@ describe('customId', () => { }, }, })).resolves.toMatchObject({ - id: 'User.16', + uid: 'User.uid.16', }); await expect(client.post.findUnique({ where: { - id: 'Post.16', + pid: 'Post.pid.16', } })).resolves.toBeTruthy(); await expect(client.comment.findUnique({ where: { - id: 'Comment.16', + cid: 'Comment.cid.16', } })).resolves.toBeTruthy(); }); From c70b5440792547afc5c0d3a8bc8edd29ad6f4d4a Mon Sep 17 00:00:00 2001 From: sanny-io Date: Thu, 12 Feb 2026 04:51:26 +0000 Subject: [PATCH 6/9] chore: add client test coverage --- tests/e2e/orm/client-api/custom-id.test.ts | 54 ++++++++++++++-------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/tests/e2e/orm/client-api/custom-id.test.ts b/tests/e2e/orm/client-api/custom-id.test.ts index 6ffca2b3e..9e85ad246 100644 --- a/tests/e2e/orm/client-api/custom-id.test.ts +++ b/tests/e2e/orm/client-api/custom-id.test.ts @@ -23,20 +23,24 @@ const schema = ` describe('customId', () => { it('works with no arguments', async () => { - const client = await createTestClient(schema, { - customId: ({ model, field, length }) => `${model}.${field}.${length ?? 16}`, + let client = await createTestClient(schema, { + customId: ({ model, field, length, client }) => `${model}.${field}.${length ?? 16}.${client.$auth!['uid']}`, + }); + + client = client.$setAuth({ + uid: '1', }); await expect(client.user.create({ data: {} })).resolves.toMatchObject({ - uid: 'User.uid.16', + uid: 'User.uid.16.1', }); await expect(client.post.create({ data: {} })).resolves.toMatchObject({ - pid: 'Post.pid.16', + pid: 'Post.pid.16.1', }); await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ - cid: 'Comment.cid.16', + cid: 'Comment.cid.16.1', }); }); @@ -61,26 +65,34 @@ describe('customId', () => { } `; - const client = await createTestClient(schema, { - customId: ({ model, field, length }) => `${model}.${field}.${length}`, + let client = await createTestClient(schema, { + customId: ({ model, field, length }) => `${model}.${field}.${length}.${client.$auth!['uid']}`, + }); + + client = client.$setAuth({ + uid: '1', }); await expect(client.user.create({ data: {} })).resolves.toMatchObject({ - uid: 'User.uid.8', + uid: 'User.uid.8.1', }); await expect(client.post.create({ data: {} })).resolves.toMatchObject({ - pid: 'Post.pid.8', + pid: 'Post.pid.8.1', }); await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ - cid: 'Comment.cid.8', + cid: 'Comment.cid.8.1', }); }); it('works with nested', async () => { - const client = await createTestClient(schema, { - customId: ({ model, field, length }) => `${model}.${field}.${length ?? 16}`, + let client = await createTestClient(schema, { + customId: ({ model, field, length, client }) => `${model}.${field}.${length ?? 16}.${client.$auth!['uid']}`, + }); + + client = client.$setAuth({ + uid: '1', }); await expect(client.user.create({ @@ -90,19 +102,23 @@ describe('customId', () => { }, }, })).resolves.toMatchObject({ - uid: 'User.uid.16', + uid: 'User.uid.16.1', }); await expect(client.post.findUnique({ where: { - pid: 'Post.pid.16', + pid: 'Post.pid.16.1', } })).resolves.toBeTruthy(); }); it('works with deeply nested', async () => { - const client = await createTestClient(schema, { - customId: ({ model, field, length }) => `${model}.${field}.${length ?? 16}`, + let client = await createTestClient(schema, { + customId: ({ model, field, length, client }) => `${model}.${field}.${length ?? 16}.${client.$auth!['uid']}`, + }); + + client = client.$setAuth({ + uid: '1', }); await expect(client.user.create({ @@ -116,18 +132,18 @@ describe('customId', () => { }, }, })).resolves.toMatchObject({ - uid: 'User.uid.16', + uid: 'User.uid.16.1', }); await expect(client.post.findUnique({ where: { - pid: 'Post.pid.16', + pid: 'Post.pid.16.1', } })).resolves.toBeTruthy(); await expect(client.comment.findUnique({ where: { - cid: 'Comment.cid.16', + cid: 'Comment.cid.16.1', } })).resolves.toBeTruthy(); }); From d1d7d3cb390ca313a518714d5ad1a1964915106e Mon Sep 17 00:00:00 2001 From: sanny-io Date: Thu, 12 Feb 2026 04:57:27 +0000 Subject: [PATCH 7/9] Add typing to `model`. --- packages/orm/src/client/options.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/orm/src/client/options.ts b/packages/orm/src/client/options.ts index 5b7ec8c1c..422e05b06 100644 --- a/packages/orm/src/client/options.ts +++ b/packages/orm/src/client/options.ts @@ -47,12 +47,12 @@ export type CustomIdFunctionContext = { client: ClientContract; /** - * The model for which the ID should be generated for. + * The model for which the ID should be generated. */ - model: string; + model: GetModels; /** - * The field for which the ID should be generated for. + * The field for which the ID should be generated. */ field: string; From 0105750684cc3a8faf9b4fd28c11f6bbe002cb1a Mon Sep 17 00:00:00 2001 From: sanny-io Date: Thu, 12 Feb 2026 05:02:09 +0000 Subject: [PATCH 8/9] Remove field support. --- .../orm/src/client/crud/operations/base.ts | 7 ++--- packages/orm/src/client/options.ts | 5 ---- tests/e2e/orm/client-api/custom-id.test.ts | 30 +++++++++---------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/packages/orm/src/client/crud/operations/base.ts b/packages/orm/src/client/crud/operations/base.ts index 970a2f9ed..92e21e536 100644 --- a/packages/orm/src/client/crud/operations/base.ts +++ b/packages/orm/src/client/crud/operations/base.ts @@ -1044,7 +1044,7 @@ export abstract class BaseOperationHandler { } if (!(field in data)) { if (typeof fieldDef?.default === 'object' && 'kind' in fieldDef.default) { - const generated = this.evalGenerator(fieldDef.default, modelDef.name, field); + const generated = this.evalGenerator(fieldDef.default, modelDef.name); if (generated !== undefined) { values[field] = this.dialect.transformInput( generated, @@ -1072,7 +1072,7 @@ export abstract class BaseOperationHandler { return values; } - private evalGenerator(defaultValue: Expression, model: string, field: string) { + private evalGenerator(defaultValue: Expression, model: string) { if (ExpressionUtils.isCall(defaultValue)) { const firstArgVal = defaultValue.args?.[0] && ExpressionUtils.isLiteral(defaultValue.args[0]) @@ -1100,8 +1100,7 @@ export abstract class BaseOperationHandler { const length = typeof firstArgVal === 'number' ? firstArgVal : undefined; const generated = this.client.$options.customId({ client: this.client, - model, - field, + model: model as GetModels, length, }); invariant(generated && typeof generated === 'string', '"customId" must return a non-empty string'); diff --git a/packages/orm/src/client/options.ts b/packages/orm/src/client/options.ts index 422e05b06..7d21a3d3d 100644 --- a/packages/orm/src/client/options.ts +++ b/packages/orm/src/client/options.ts @@ -51,11 +51,6 @@ export type CustomIdFunctionContext = { */ model: GetModels; - /** - * The field for which the ID should be generated. - */ - field: string; - /** * The length of the ID as requested by the schema. */ diff --git a/tests/e2e/orm/client-api/custom-id.test.ts b/tests/e2e/orm/client-api/custom-id.test.ts index 9e85ad246..15a82b663 100644 --- a/tests/e2e/orm/client-api/custom-id.test.ts +++ b/tests/e2e/orm/client-api/custom-id.test.ts @@ -24,7 +24,7 @@ const schema = ` describe('customId', () => { it('works with no arguments', async () => { let client = await createTestClient(schema, { - customId: ({ model, field, length, client }) => `${model}.${field}.${length ?? 16}.${client.$auth!['uid']}`, + customId: ({ model, length, client }) => `${model}.${length ?? 16}.${client.$auth!['uid']}`, }); client = client.$setAuth({ @@ -32,15 +32,15 @@ describe('customId', () => { }); await expect(client.user.create({ data: {} })).resolves.toMatchObject({ - uid: 'User.uid.16.1', + uid: 'User.16.1', }); await expect(client.post.create({ data: {} })).resolves.toMatchObject({ - pid: 'Post.pid.16.1', + pid: 'Post.16.1', }); await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ - cid: 'Comment.cid.16.1', + cid: 'Comment.16.1', }); }); @@ -66,7 +66,7 @@ describe('customId', () => { `; let client = await createTestClient(schema, { - customId: ({ model, field, length }) => `${model}.${field}.${length}.${client.$auth!['uid']}`, + customId: ({ model, length, client }) => `${model}.${length}.${client.$auth!['uid']}`, }); client = client.$setAuth({ @@ -74,21 +74,21 @@ describe('customId', () => { }); await expect(client.user.create({ data: {} })).resolves.toMatchObject({ - uid: 'User.uid.8.1', + uid: 'User.8.1', }); await expect(client.post.create({ data: {} })).resolves.toMatchObject({ - pid: 'Post.pid.8.1', + pid: 'Post.8.1', }); await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ - cid: 'Comment.cid.8.1', + cid: 'Comment.8.1', }); }); it('works with nested', async () => { let client = await createTestClient(schema, { - customId: ({ model, field, length, client }) => `${model}.${field}.${length ?? 16}.${client.$auth!['uid']}`, + customId: ({ model, length, client }) => `${model}.${length ?? 16}.${client.$auth!['uid']}`, }); client = client.$setAuth({ @@ -102,19 +102,19 @@ describe('customId', () => { }, }, })).resolves.toMatchObject({ - uid: 'User.uid.16.1', + uid: 'User.16.1', }); await expect(client.post.findUnique({ where: { - pid: 'Post.pid.16.1', + pid: 'Post.16.1', } })).resolves.toBeTruthy(); }); it('works with deeply nested', async () => { let client = await createTestClient(schema, { - customId: ({ model, field, length, client }) => `${model}.${field}.${length ?? 16}.${client.$auth!['uid']}`, + customId: ({ model, length, client }) => `${model}.${length ?? 16}.${client.$auth!['uid']}`, }); client = client.$setAuth({ @@ -132,18 +132,18 @@ describe('customId', () => { }, }, })).resolves.toMatchObject({ - uid: 'User.uid.16.1', + uid: 'User.16.1', }); await expect(client.post.findUnique({ where: { - pid: 'Post.pid.16.1', + pid: 'Post.16.1', } })).resolves.toBeTruthy(); await expect(client.comment.findUnique({ where: { - cid: 'Comment.cid.16.1', + cid: 'Comment.16.1', } })).resolves.toBeTruthy(); }); From 14ecbeff96193c35160fd55526b526cdf0f0de5b Mon Sep 17 00:00:00 2001 From: sanny-io Date: Thu, 12 Feb 2026 05:07:46 +0000 Subject: [PATCH 9/9] Use config errors. --- packages/orm/src/client/crud/operations/base.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/orm/src/client/crud/operations/base.ts b/packages/orm/src/client/crud/operations/base.ts index 92e21e536..33c3b62aa 100644 --- a/packages/orm/src/client/crud/operations/base.ts +++ b/packages/orm/src/client/crud/operations/base.ts @@ -25,6 +25,7 @@ import { NUMERIC_FIELD_TYPES } from '../../constants'; import { TransactionIsolationLevel, type ClientContract, type CRUD } from '../../contract'; import type { FindArgs, SelectIncludeOmit, WhereInput } from '../../crud-types'; import { + createConfigError, createDBQueryError, createInternalError, createInvalidInputError, @@ -1096,14 +1097,18 @@ export abstract class BaseOperationHandler { }) .with('ulid', () => this.formatGeneratedValue(ulid(), defaultValue.args?.[0])) .with('customId', () => { - invariant(this.client.$options.customId, '"customId" implementation not provided'); + if (!this.client.$options.customId) { + throw createConfigError('"customId" implementation not provided'); + } const length = typeof firstArgVal === 'number' ? firstArgVal : undefined; const generated = this.client.$options.customId({ client: this.client, model: model as GetModels, length, }); - invariant(generated && typeof generated === 'string', '"customId" must return a non-empty string'); + if (!generated || typeof generated !== 'string') { + throw createConfigError('"customId" must return a non-empty string'); + } return generated; }) .otherwise(() => undefined);