-
-
Notifications
You must be signed in to change notification settings - Fork 17
fix(orm): entity is not created when upserting with an empty update payload #477
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ describe('Client upsert tests', () => { | |
| let client: ClientContract<typeof schema>; | ||
|
|
||
| beforeEach(async () => { | ||
| client = (await createTestClient(schema)) as any; | ||
| client = await createTestClient(schema); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
|
|
@@ -69,4 +69,113 @@ describe('Client upsert tests', () => { | |
| email: '[email protected]', | ||
| }); | ||
| }); | ||
|
|
||
| it('works with upsert with empty update payload', async () => { | ||
| // Test 1: Upsert with empty update should create new record when it doesn't exist | ||
| const created = await client.user.upsert({ | ||
| where: { id: '1' }, | ||
| create: { | ||
| id: '1', | ||
| email: '[email protected]', | ||
| name: 'John', | ||
| }, | ||
| update: {}, | ||
| select: { id: true, email: true, name: true }, | ||
| }); | ||
|
|
||
| expect(created).toMatchObject({ | ||
| id: '1', | ||
| email: '[email protected]', | ||
| name: 'John', | ||
| }); | ||
|
|
||
| // Verify the record was created | ||
| const fetchedAfterCreate = await client.user.findUnique({ | ||
| where: { id: '1' }, | ||
| select: { id: true, email: true, name: true }, | ||
| }); | ||
|
|
||
| expect(fetchedAfterCreate).toMatchObject({ | ||
| id: '1', | ||
| email: '[email protected]', | ||
| name: 'John', | ||
| }); | ||
|
|
||
| // Test 2: Upsert with empty update should return existing record unchanged | ||
| const result = await client.user.upsert({ | ||
| where: { id: '1' }, | ||
| create: { | ||
| id: '1', | ||
| email: '[email protected]', | ||
| name: 'Jane', | ||
| }, | ||
| update: {}, | ||
| select: { id: true, email: true, name: true }, | ||
| }); | ||
|
|
||
| expect(result).toMatchObject({ | ||
| id: '1', | ||
| email: '[email protected]', | ||
| name: 'John', // Should remain unchanged | ||
| }); | ||
|
|
||
| // Verify the record was not modified | ||
| const fetched = await client.user.findUnique({ | ||
| where: { id: '1' }, | ||
| select: { id: true, email: true, name: true }, | ||
| }); | ||
|
|
||
| expect(fetched).toMatchObject({ | ||
| id: '1', | ||
| email: '[email protected]', | ||
| name: 'John', | ||
| }); | ||
| }); | ||
|
|
||
| it('works with upsert with empty create payload', async () => { | ||
| const db = await createTestClient( | ||
| ` | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| name String? | ||
| } | ||
| `, | ||
| { dbName: 'orm_upsert_empty_create' }, | ||
| ); | ||
|
|
||
| // Test 1: First upsert should create the entity with empty data | ||
| const created = await db.user.upsert({ | ||
| where: { id: '1' }, | ||
| create: {}, | ||
| update: { name: 'Updated' }, | ||
| }); | ||
|
|
||
| expect(created).toBeTruthy(); | ||
|
|
||
| // Verify the record was created | ||
| await expect(db.user.findFirst()).resolves.toMatchObject(created); | ||
|
|
||
| // Test 2: Second upsert should update the existing entity | ||
| const updated = await db.user.upsert({ | ||
| where: { id: created.id }, | ||
| create: {}, | ||
| update: { name: 'Updated' }, | ||
| }); | ||
|
|
||
| expect(updated).toMatchObject({ | ||
| id: created.id, | ||
| name: 'Updated', | ||
| }); | ||
|
|
||
| // Verify the record was updated | ||
| await expect( | ||
| db.user.findUnique({ | ||
| where: { id: created.id }, | ||
| }), | ||
| ).resolves.toMatchObject({ | ||
| name: 'Updated', | ||
| }); | ||
|
|
||
| await db.$disconnect(); | ||
| }); | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
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
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.