Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/runtime/test/typing/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
//////////////////////////////////////////////////////////////////////////////////////////////

import { type SchemaDef, ExpressionUtils } from "../../dist/schema";
import { type SchemaDef, type OperandExpression, ExpressionUtils } from "../../dist/schema";
import path from "node:path";
import url from "node:url";
import { toDialectConfig } from "../../dist/utils/sqlite-utils";
Expand Down Expand Up @@ -50,12 +50,22 @@ export const schema = {
type: "Profile",
optional: true,
relation: { opposite: "user" }
},
postCount: {
type: "Int",
attributes: [{ name: "@computed" }],
computed: true
}
},
idFields: ["id"],
uniqueFields: {
id: { type: "Int" },
email: { type: "String" }
},
computedFields: {
postCount(): OperandExpression<number> {
throw new Error("This is a stub for computed field");
}
}
},
Post: {
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/test/typing/typing-test.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ model User {
email String @unique
posts Post[]
profile Profile?
postCount Int @computed
}

model Post {
Expand Down
32 changes: 29 additions & 3 deletions packages/runtime/test/typing/verify-typing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { ZenStackClient } from '../../dist';
import { schema } from './schema';

const client = new ZenStackClient(schema);
const client = new ZenStackClient(schema, {
computedFields: {
User: {
postCount: (eb) =>
eb
.selectFrom('Post')
.whereRef('Post.authorId', '=', 'User.id')
.select(({ fn }) => fn.countAll<number>().as('postCount')),
},
},
});

async function main() {
await find();
Expand All @@ -11,6 +21,7 @@ async function main() {
await count();
await aggregate();
await groupBy();
await queryBuilder();
}

async function find() {
Expand All @@ -20,16 +31,19 @@ async function find() {
},
});
console.log(user1?.name);
console.log(user1?.postCount);

const users = await client.user.findMany({
include: { posts: true },
omit: { email: true },
omit: { email: true, postCount: true },
});
console.log(users.length);
console.log(users[0]?.name);
console.log(users[0]?.posts.length);
// @ts-expect-error
console.log(users[0]?.email);
// @ts-expect-error
console.log(users[0]?.postCount);

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

const user2 = await client.user.findUniqueOrThrow({
where: { email: '[email protected]' },
where: { email: '[email protected]', postCount: { gt: 0 } },
select: { email: true, profile: true },
});
console.log(user2.email);
Expand Down Expand Up @@ -532,4 +546,16 @@ async function groupBy() {
console.log(r[0]?.age);
}

async function queryBuilder() {
const r = await client.$qb
.selectFrom('User')
.where('name', '=', 'Alex')
.select(['id', 'email'])
.executeTakeFirstOrThrow();
console.log(r.id);
console.log(r.email);
// @ts-expect-error
console.log(r.name);
}

main();