|
| 1 | +import { createTestClient } from '@zenstackhq/testtools'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +// https://github.com/zenstackhq/zenstack/issues/2588 |
| 5 | +describe('Regression for issue 2588', () => { |
| 6 | + const schema = ` |
| 7 | +model Asset { |
| 8 | + id String @id @default(uuid()) |
| 9 | + createdAt DateTime @default(now()) |
| 10 | + assetType String |
| 11 | + @@delegate(assetType) |
| 12 | +} |
| 13 | +
|
| 14 | +model Notification extends Asset { |
| 15 | + title String |
| 16 | +} |
| 17 | +`; |
| 18 | + |
| 19 | + async function setup() { |
| 20 | + const db = await createTestClient(schema); |
| 21 | + const a = await db.notification.create({ |
| 22 | + data: { title: 'A', createdAt: new Date('2025-01-01T00:00:00Z') }, |
| 23 | + }); |
| 24 | + const b = await db.notification.create({ |
| 25 | + data: { title: 'B', createdAt: new Date('2025-01-02T00:00:00Z') }, |
| 26 | + }); |
| 27 | + const c = await db.notification.create({ |
| 28 | + data: { title: 'C', createdAt: new Date('2025-01-03T00:00:00Z') }, |
| 29 | + }); |
| 30 | + return { db, a, b, c }; |
| 31 | + } |
| 32 | + |
| 33 | + it('cursor + orderBy on delegate parent field does not error', async () => { |
| 34 | + const { db, b } = await setup(); |
| 35 | + |
| 36 | + const result = await db.notification.findMany({ |
| 37 | + cursor: { id: b.id }, |
| 38 | + orderBy: { createdAt: 'asc' }, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(result.map((n: any) => n.title)).toEqual(['B', 'C']); |
| 42 | + }); |
| 43 | + |
| 44 | + it('cursor + skip + orderBy on delegate parent field works', async () => { |
| 45 | + const { db, a } = await setup(); |
| 46 | + |
| 47 | + const result = await db.notification.findMany({ |
| 48 | + cursor: { id: a.id }, |
| 49 | + skip: 1, |
| 50 | + orderBy: { createdAt: 'asc' }, |
| 51 | + take: 25, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(result.map((n: any) => n.title)).toEqual(['B', 'C']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('cursor + multiple orderBy mixing child and delegate fields', async () => { |
| 58 | + const { db, a } = await setup(); |
| 59 | + |
| 60 | + const result = await db.notification.findMany({ |
| 61 | + cursor: { id: a.id }, |
| 62 | + orderBy: [{ createdAt: 'asc' }, { id: 'asc' }], |
| 63 | + }); |
| 64 | + |
| 65 | + expect(result.map((n: any) => n.title)).toEqual(['A', 'B', 'C']); |
| 66 | + }); |
| 67 | + |
| 68 | + it('cursor + orderBy on child field still works', async () => { |
| 69 | + const { db, b } = await setup(); |
| 70 | + |
| 71 | + const result = await db.notification.findMany({ |
| 72 | + cursor: { id: b.id }, |
| 73 | + orderBy: { title: 'asc' }, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(result.map((n: any) => n.title)).toEqual(['B', 'C']); |
| 77 | + }); |
| 78 | +}); |
0 commit comments