-
-
Notifications
You must be signed in to change notification settings - Fork 139
feat: customId function
#2377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
feat: customId function
#2377
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1cabdf9
feat: `customId` function
7338c11
chore: add tests and validations
77ffc74
chore: fix tests
sanny-io 2a5ad8f
chore: address comments
sanny-io f09a9e7
Add `client` and `field` to context.
sanny-io c70b544
chore: add client test coverage
sanny-io d1d7d3c
Add typing to `model`.
sanny-io 0105750
Remove field support.
sanny-io 14ecbef
Use config errors.
sanny-io File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| const schema = ` | ||
| model User { | ||
| uid String @id @default(customId()) | ||
| posts Post[] | ||
| } | ||
|
|
||
| model Post { | ||
| pid String @id @default(customId()) | ||
| userId String? | ||
| user User? @relation(fields: [userId], references: [uid]) | ||
| comments Comment[] | ||
| } | ||
|
|
||
| model Comment { | ||
| cid String @id @default(customId()) | ||
| postId String? | ||
| post Post? @relation(fields: [postId], references: [pid]) | ||
| } | ||
| `; | ||
|
|
||
| describe('customId', () => { | ||
| it('works with no arguments', async () => { | ||
| let client = await createTestClient(schema, { | ||
| customId: ({ model, length, client }) => `${model}.${length ?? 16}.${client.$auth!['uid']}`, | ||
| }); | ||
|
|
||
| client = client.$setAuth({ | ||
| uid: '1', | ||
| }); | ||
|
|
||
| await expect(client.user.create({ data: {} })).resolves.toMatchObject({ | ||
| uid: 'User.16.1', | ||
| }); | ||
|
|
||
| await expect(client.post.create({ data: {} })).resolves.toMatchObject({ | ||
| pid: 'Post.16.1', | ||
| }); | ||
|
|
||
| await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ | ||
| cid: 'Comment.16.1', | ||
| }); | ||
| }); | ||
|
|
||
| it('works with arguments', async () => { | ||
| const schema = ` | ||
| model User { | ||
| uid String @id @default(customId(8)) | ||
| posts Post[] | ||
| } | ||
|
|
||
| model Post { | ||
| pid String @id @default(customId(8)) | ||
| userId String? | ||
| user User? @relation(fields: [userId], references: [uid]) | ||
| comments Comment[] | ||
| } | ||
|
|
||
| model Comment { | ||
| cid String @id @default(customId(8)) | ||
| postId String? | ||
| post Post? @relation(fields: [postId], references: [pid]) | ||
| } | ||
| `; | ||
|
|
||
| let client = await createTestClient(schema, { | ||
| customId: ({ model, length, client }) => `${model}.${length}.${client.$auth!['uid']}`, | ||
| }); | ||
|
|
||
| client = client.$setAuth({ | ||
| uid: '1', | ||
| }); | ||
|
|
||
| await expect(client.user.create({ data: {} })).resolves.toMatchObject({ | ||
| uid: 'User.8.1', | ||
| }); | ||
|
|
||
| await expect(client.post.create({ data: {} })).resolves.toMatchObject({ | ||
| pid: 'Post.8.1', | ||
| }); | ||
|
|
||
| await expect(client.comment.create({ data: {} })).resolves.toMatchObject({ | ||
| cid: 'Comment.8.1', | ||
| }); | ||
| }); | ||
|
|
||
| it('works with nested', async () => { | ||
| let client = await createTestClient(schema, { | ||
| customId: ({ model, length, client }) => `${model}.${length ?? 16}.${client.$auth!['uid']}`, | ||
| }); | ||
|
|
||
| client = client.$setAuth({ | ||
| uid: '1', | ||
| }); | ||
|
|
||
| await expect(client.user.create({ | ||
| data: { | ||
| posts: { | ||
| create: {}, | ||
| }, | ||
| }, | ||
| })).resolves.toMatchObject({ | ||
| uid: 'User.16.1', | ||
| }); | ||
|
|
||
| await expect(client.post.findUnique({ | ||
| where: { | ||
| pid: 'Post.16.1', | ||
| } | ||
| })).resolves.toBeTruthy(); | ||
| }); | ||
|
|
||
| it('works with deeply nested', async () => { | ||
| let client = await createTestClient(schema, { | ||
| customId: ({ model, length, client }) => `${model}.${length ?? 16}.${client.$auth!['uid']}`, | ||
| }); | ||
|
|
||
| client = client.$setAuth({ | ||
| uid: '1', | ||
| }); | ||
|
|
||
| await expect(client.user.create({ | ||
| data: { | ||
| posts: { | ||
| create: { | ||
| comments: { | ||
| create: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| })).resolves.toMatchObject({ | ||
| uid: 'User.16.1', | ||
| }); | ||
|
|
||
| await expect(client.post.findUnique({ | ||
| where: { | ||
| pid: 'Post.16.1', | ||
| } | ||
| })).resolves.toBeTruthy(); | ||
|
|
||
| await expect(client.comment.findUnique({ | ||
| where: { | ||
| cid: 'Comment.16.1', | ||
| } | ||
| })).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 (undefined)', async () => { | ||
| // @ts-expect-error | ||
| const client = await createTestClient(schema, { | ||
| customId: () => undefined, | ||
| }); | ||
| // @ts-expect-error | ||
| await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); | ||
| }); | ||
|
|
||
| it('rejects without a valid implementation (empty string)', async () => { | ||
| const client = await createTestClient(schema, { | ||
| customId: () => '', | ||
| }); | ||
| await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); | ||
| }); | ||
|
|
||
| it('rejects without a valid implementation (non-string)', async () => { | ||
| // @ts-expect-error | ||
| const client = await createTestClient(schema, { | ||
| customId: () => 1, | ||
| }); | ||
| // @ts-expect-error | ||
| await expect(client.user.create({ data: {} })).rejects.toThrowError('non-empty string'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.