Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 9e819e8

Browse files
lsmith77ymc9
andauthored
add test for pg computed values across multiple schema (#615)
* add test for pg computed values across multiple schema * Add an any cast to type check test file --------- Co-authored-by: Yiming Cao <yiming@whimslab.io>
1 parent 94c5c8a commit 9e819e8

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { createTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Postgres multi-schema with computed fields', () => {
5+
it('supports computed fields on models in custom schemas', async () => {
6+
const db = await createTestClient(
7+
`
8+
datasource db {
9+
provider = "postgresql"
10+
url = '$DB_URL'
11+
schemas = ["public", "mySchema1", "mySchema2"]
12+
}
13+
14+
model Author {
15+
id Int @id @default(autoincrement())
16+
name String
17+
books Book[]
18+
@@schema("mySchema1")
19+
}
20+
21+
model Book {
22+
id Int @id @default(autoincrement())
23+
title String
24+
authorId Int
25+
author Author @relation(fields: [authorId], references: [id])
26+
authorName String @computed()
27+
@@schema("mySchema2")
28+
}
29+
`,
30+
{
31+
provider: 'postgresql',
32+
usePrismaPush: true,
33+
computedFields: {
34+
Book: {
35+
authorName: (eb: any) => eb
36+
.selectFrom('mySchema1.Author')
37+
.select('Author.name')
38+
.whereRef('Author.id', '=', 'authorId')
39+
.limit(1)
40+
},
41+
},
42+
} as any
43+
);
44+
45+
// Create author and book
46+
const author = await db.author.create({ data: { name: 'Jane Doe' } });
47+
const book = await db.book.create({ data: { title: 'ZenStack Guide', authorId: author.id } });
48+
49+
// Fetch book and check computed field
50+
const fetched = await db.book.findUnique({ where: { id: book.id } });
51+
expect(fetched?.authorName).toBe('Jane Doe');
52+
});
53+
});

0 commit comments

Comments
 (0)