diff --git a/packages/runtime/test/typing/schema.ts b/packages/runtime/test/typing/schema.ts index 6cc4110c..63ba6c92 100644 --- a/packages/runtime/test/typing/schema.ts +++ b/packages/runtime/test/typing/schema.ts @@ -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"; @@ -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 { + throw new Error("This is a stub for computed field"); + } } }, Post: { diff --git a/packages/runtime/test/typing/typing-test.zmodel b/packages/runtime/test/typing/typing-test.zmodel index 2f3a4bb4..022bc1a9 100644 --- a/packages/runtime/test/typing/typing-test.zmodel +++ b/packages/runtime/test/typing/typing-test.zmodel @@ -11,6 +11,7 @@ model User { email String @unique posts Post[] profile Profile? + postCount Int @computed } model Post { diff --git a/packages/runtime/test/typing/verify-typing.ts b/packages/runtime/test/typing/verify-typing.ts index cf4207d5..7fa91666 100644 --- a/packages/runtime/test/typing/verify-typing.ts +++ b/packages/runtime/test/typing/verify-typing.ts @@ -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().as('postCount')), + }, + }, +}); async function main() { await find(); @@ -11,6 +21,7 @@ async function main() { await count(); await aggregate(); await groupBy(); + await queryBuilder(); } async function find() { @@ -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({ @@ -44,7 +58,7 @@ async function find() { }); const user2 = await client.user.findUniqueOrThrow({ - where: { email: 'alex@zenstack.dev' }, + where: { email: 'alex@zenstack.dev', postCount: { gt: 0 } }, select: { email: true, profile: true }, }); console.log(user2.email); @@ -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();