Skip to content

Commit d2393b2

Browse files
authored
chore: update typing test (#24)
1 parent c519480 commit d2393b2

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

packages/runtime/test/typing/schema.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
44
//////////////////////////////////////////////////////////////////////////////////////////////
55

6-
import { type SchemaDef, ExpressionUtils } from "../../dist/schema";
6+
import { type SchemaDef, type OperandExpression, ExpressionUtils } from "../../dist/schema";
77
import path from "node:path";
88
import url from "node:url";
99
import { toDialectConfig } from "../../dist/utils/sqlite-utils";
@@ -50,12 +50,22 @@ export const schema = {
5050
type: "Profile",
5151
optional: true,
5252
relation: { opposite: "user" }
53+
},
54+
postCount: {
55+
type: "Int",
56+
attributes: [{ name: "@computed" }],
57+
computed: true
5358
}
5459
},
5560
idFields: ["id"],
5661
uniqueFields: {
5762
id: { type: "Int" },
5863
email: { type: "String" }
64+
},
65+
computedFields: {
66+
postCount(): OperandExpression<number> {
67+
throw new Error("This is a stub for computed field");
68+
}
5969
}
6070
},
6171
Post: {

packages/runtime/test/typing/typing-test.zmodel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ model User {
1111
email String @unique
1212
posts Post[]
1313
profile Profile?
14+
postCount Int @computed
1415
}
1516

1617
model Post {

packages/runtime/test/typing/verify-typing.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { ZenStackClient } from '../../dist';
22
import { schema } from './schema';
33

4-
const client = new ZenStackClient(schema);
4+
const client = new ZenStackClient(schema, {
5+
computedFields: {
6+
User: {
7+
postCount: (eb) =>
8+
eb
9+
.selectFrom('Post')
10+
.whereRef('Post.authorId', '=', 'User.id')
11+
.select(({ fn }) => fn.countAll<number>().as('postCount')),
12+
},
13+
},
14+
});
515

616
async function main() {
717
await find();
@@ -11,6 +21,7 @@ async function main() {
1121
await count();
1222
await aggregate();
1323
await groupBy();
24+
await queryBuilder();
1425
}
1526

1627
async function find() {
@@ -20,16 +31,19 @@ async function find() {
2031
},
2132
});
2233
console.log(user1?.name);
34+
console.log(user1?.postCount);
2335

2436
const users = await client.user.findMany({
2537
include: { posts: true },
26-
omit: { email: true },
38+
omit: { email: true, postCount: true },
2739
});
2840
console.log(users.length);
2941
console.log(users[0]?.name);
3042
console.log(users[0]?.posts.length);
3143
// @ts-expect-error
3244
console.log(users[0]?.email);
45+
// @ts-expect-error
46+
console.log(users[0]?.postCount);
3347

3448
// @ts-expect-error select/omit are not allowed together
3549
await client.user.findMany({
@@ -44,7 +58,7 @@ async function find() {
4458
});
4559

4660
const user2 = await client.user.findUniqueOrThrow({
47-
where: { email: '[email protected]' },
61+
where: { email: '[email protected]', postCount: { gt: 0 } },
4862
select: { email: true, profile: true },
4963
});
5064
console.log(user2.email);
@@ -532,4 +546,16 @@ async function groupBy() {
532546
console.log(r[0]?.age);
533547
}
534548

549+
async function queryBuilder() {
550+
const r = await client.$qb
551+
.selectFrom('User')
552+
.where('name', '=', 'Alex')
553+
.select(['id', 'email'])
554+
.executeTakeFirstOrThrow();
555+
console.log(r.id);
556+
console.log(r.email);
557+
// @ts-expect-error
558+
console.log(r.name);
559+
}
560+
535561
main();

0 commit comments

Comments
 (0)