-
-
Notifications
You must be signed in to change notification settings - Fork 134
fix(orm): fixed 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
Open
eredzik
wants to merge
4
commits into
zenstackhq:dev
Choose a base branch
from
eredzik:fix/order-by-in-nested-queries
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−8
Open
Changes from all commits
Commits
Show all changes
4 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
145 changes: 145 additions & 0 deletions
145
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,145 @@ | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| 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('Relation orderBy with nested includes', () => { | ||
| let db: any; | ||
|
|
||
| afterEach(async () => { | ||
| await db?.$disconnect(); | ||
| }); | ||
|
|
||
| it('keeps stable order for to-many include with nested includes', async () => { | ||
| const count = 2000; | ||
|
|
||
| db = await createTestClient(schema); | ||
|
|
||
| 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 = 2000; | ||
|
|
||
| db = await createTestClient(schema); | ||
|
|
||
| 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.