-
-
Notifications
You must be signed in to change notification settings - Fork 144
fix(orm): fixed postgres orderBy in nested queries (includes/selects) #2518
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
+224
−8
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0a7b4c7
feat(orm): enhance lateral join dialect with orderBy support for arra…
eredzik 1507f18
fix: fixed ai review comments
eredzik c339fe7
fix: fixed mysql query
eredzik 16c196a
chore: clean up test file
ymc9 184b6e4
fix: use correct sort for negative take
ymc9 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
156 changes: 156 additions & 0 deletions
156
tests/e2e/orm/client-api/relation/order-by-nested-includes.test.ts
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,156 @@ | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
|
|
||
| const TEST_DB = 'client-api-relation-test-order-by-nested-includes'; | ||
|
|
||
| const schema = ` | ||
| model User { | ||
| id String @id | ||
| email String @unique | ||
| posts Post[] | ||
| comments Comment[] | ||
| } | ||
|
|
||
| model Post { | ||
| id String @id | ||
| sequence Int | ||
| title String | ||
| author User @relation(fields: [authorId], references: [id]) | ||
| authorId String | ||
| comments Comment[] | ||
| } | ||
|
|
||
| model Comment { | ||
| id String @id | ||
| content String | ||
| post Post @relation(fields: [postId], references: [id]) | ||
| postId String | ||
| author User? @relation(fields: [authorId], references: [id]) | ||
| authorId String? | ||
| } | ||
| `; | ||
|
|
||
| function makePostsData(count: number) { | ||
| return Array.from({ length: count }, (_, i) => { | ||
| const sequence = count - i; // insert descending | ||
| return { | ||
| id: `p${sequence}`, | ||
| sequence, | ||
| title: `P${sequence}`, | ||
| // Keep outer relation (User -> posts) required. | ||
| authorId: 'u1', | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| function makeCommentsData(count: number) { | ||
| return Array.from({ length: count }, (_, i) => { | ||
| const sequence = count - i; | ||
| return { | ||
| id: `c${sequence}`, | ||
| postId: `p${sequence}`, | ||
| content: `C${sequence}`, | ||
| // Make nested to-one include nullable to vary lateral join execution. | ||
| authorId: sequence % 11 === 0 ? null : 'u1', | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| describe.each([{ provider: 'sqlite' as const }, { provider: 'postgresql' as const }])( | ||
| 'Relation orderBy with nested includes ($provider)', | ||
| ({ provider }) => { | ||
| let db: any; | ||
|
|
||
| afterEach(async () => { | ||
| await db?.$disconnect(); | ||
| }); | ||
|
|
||
| it('keeps stable order for to-many include with nested includes', async () => { | ||
| const count = provider === 'postgresql' ? 2000 : 10; | ||
|
|
||
| db = await createTestClient(schema, { | ||
| provider, | ||
| dbName: `${TEST_DB}-${provider}-count-${count}`, | ||
| }); | ||
|
|
||
| await db.user.create({ data: { id: 'u1', email: 'u1@example.com' } }); | ||
| await db.post.createMany({ data: makePostsData(count) }); | ||
| await db.comment.createMany({ data: makeCommentsData(count) }); | ||
|
|
||
| const user = await db.user.findFirst({ | ||
| where: { id: 'u1' }, | ||
| include: { | ||
| posts: { | ||
| orderBy: { sequence: 'asc' }, | ||
| include: { author: true, comments: { include: { author: true } } }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const ascSequences = user.posts.map((p: any) => p.sequence); | ||
| expect(ascSequences).toEqual(Array.from({ length: count }, (_, i) => i + 1)); | ||
|
|
||
| const userDesc = await db.user.findFirst({ | ||
| where: { id: 'u1' }, | ||
| include: { | ||
| posts: { | ||
| orderBy: { sequence: 'desc' }, | ||
| include: { author: true, comments: { include: { author: true } } }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const descSequences = userDesc.posts.map((p: any) => p.sequence) | ||
| expect(descSequences).toEqual(Array.from({ length: count }, (_, i) => count - i)); | ||
| }); | ||
|
|
||
| it('keeps stable order for to-many select with nested selects', async () => { | ||
| const count = provider === 'postgresql' ? 2000 : 10; | ||
|
|
||
| db = await createTestClient(schema, { | ||
| provider, | ||
| dbName: `${TEST_DB}-${provider}-select-count-${count}`, | ||
| }); | ||
|
|
||
| await db.user.create({ data: { id: 'u1', email: 'u1@example.com' } }); | ||
| await db.post.createMany({ data: makePostsData(count) }); | ||
| await db.comment.createMany({ data: makeCommentsData(count) }); | ||
|
|
||
| const user = await db.user.findFirst({ | ||
| where: { id: 'u1' }, | ||
| select: { | ||
| id: true, | ||
| posts: { | ||
| orderBy: { sequence: 'asc' }, | ||
| select: { | ||
| sequence: true, | ||
| author: { select: { id: true } }, | ||
| comments: { select: { author: { select: { id: true } } } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const ascSequences = user.posts.map((p: any) => p.sequence); | ||
| expect(ascSequences).toEqual(Array.from({ length: count }, (_, i) => i + 1)); | ||
|
|
||
| const userDesc = await db.user.findFirst({ | ||
| where: { id: 'u1' }, | ||
| select: { | ||
| id: true, | ||
| posts: { | ||
| orderBy: { sequence: 'desc' }, | ||
| select: { | ||
| sequence: true, | ||
| author: { select: { id: true } }, | ||
| comments: { select: { author: { select: { id: true } } } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const descSequences = userDesc.posts.map((p: any) => p.sequence); | ||
| expect(descSequences).toEqual(Array.from({ length: count }, (_, i) => count - i)); | ||
| }); | ||
| }, | ||
| ); |
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.