From c6d62d318d6fb21333676242879e2a98555e1ae9 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Mon, 28 Jul 2025 17:11:48 +0800 Subject: [PATCH] chore: reorg test schemas --- packages/runtime/package.json | 4 +- .../runtime/test/client-api/aggregate.test.ts | 2 +- .../runtime/test/client-api/client-specs.ts | 2 +- .../runtime/test/client-api/count.test.ts | 2 +- .../client-api/create-many-and-return.test.ts | 2 +- .../test/client-api/create-many.test.ts | 2 +- .../runtime/test/client-api/create.test.ts | 2 +- .../test/client-api/delete-many.test.ts | 2 +- .../runtime/test/client-api/delete.test.ts | 2 +- .../runtime/test/client-api/filter.test.ts | 2 +- packages/runtime/test/client-api/find.test.ts | 2 +- .../runtime/test/client-api/group-by.test.ts | 2 +- .../runtime/test/client-api/raw-query.test.ts | 2 +- .../test/client-api/transaction.test.ts | 2 +- .../test/client-api/undefined-values.test.ts | 2 +- .../test/client-api/update-many.test.ts | 2 +- .../runtime/test/client-api/update.test.ts | 2 +- .../runtime/test/client-api/upsert.test.ts | 2 +- packages/runtime/test/client-api/utils.ts | 2 +- .../test/plugin/kysely-on-query.test.ts | 2 +- .../test/plugin/mutation-hooks.test.ts | 2 +- .../test/plugin/query-lifecycle.test.ts | 2 +- packages/runtime/test/policy/read.test.ts | 2 +- .../runtime/test/policy/todo-sample.test.ts | 17 +- packages/runtime/test/policy/utils.ts | 14 +- .../test/query-builder/query-builder.test.ts | 2 +- .../{test-schema => schemas/basic}/helper.ts | 0 .../{test-schema => schemas/basic}/index.ts | 0 .../{test-schema => schemas/basic}/input.ts | 0 .../{test-schema => schemas/basic}/models.ts | 0 .../{test-schema => schemas/basic}/schema.ts | 2 +- .../basic}/schema.zmodel | 0 packages/runtime/test/schemas/todo/input.ts | 110 +++++ packages/runtime/test/schemas/todo/models.ts | 14 + packages/runtime/test/schemas/todo/schema.ts | 395 ++++++++++++++++++ .../test/schemas/{ => todo}/todo.zmodel | 0 .../test/{ => schemas}/typing/input.ts | 0 .../test/{ => schemas}/typing/models.ts | 0 .../test/{ => schemas}/typing/schema.ts | 2 +- .../typing/schema.zmodel} | 0 .../{ => schemas}/typing/verify-typing.ts | 2 +- packages/runtime/test/scripts/generate.ts | 14 +- packages/runtime/test/utils.ts | 4 +- packages/sdk/src/ts-schema-generator.ts | 23 +- 44 files changed, 588 insertions(+), 59 deletions(-) rename packages/runtime/test/{test-schema => schemas/basic}/helper.ts (100%) rename packages/runtime/test/{test-schema => schemas/basic}/index.ts (100%) rename packages/runtime/test/{test-schema => schemas/basic}/input.ts (100%) rename packages/runtime/test/{test-schema => schemas/basic}/models.ts (100%) rename packages/runtime/test/{test-schema => schemas/basic}/schema.ts (99%) rename packages/runtime/test/{test-schema => schemas/basic}/schema.zmodel (100%) create mode 100644 packages/runtime/test/schemas/todo/input.ts create mode 100644 packages/runtime/test/schemas/todo/models.ts create mode 100644 packages/runtime/test/schemas/todo/schema.ts rename packages/runtime/test/schemas/{ => todo}/todo.zmodel (100%) rename packages/runtime/test/{ => schemas}/typing/input.ts (100%) rename packages/runtime/test/{ => schemas}/typing/models.ts (100%) rename packages/runtime/test/{ => schemas}/typing/schema.ts (99%) rename packages/runtime/test/{typing/typing-test.zmodel => schemas/typing/schema.zmodel} (100%) rename packages/runtime/test/{ => schemas}/typing/verify-typing.ts (99%) diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 1b23dc63..eaaafa35 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -4,10 +4,10 @@ "description": "ZenStack Runtime", "type": "module", "scripts": { - "build": "tsup-node", + "build": "tsup-node && pnpm test:generate", "watch": "tsup-node --watch", "lint": "eslint src --ext ts", - "test": "vitest run && pnpm test:generate && pnpm test:typecheck", + "test": "vitest run && pnpm test:typecheck", "test:generate": "tsx test/scripts/generate.ts", "test:typecheck": "tsc --project tsconfig.test.json", "pack": "pnpm pack" diff --git a/packages/runtime/test/client-api/aggregate.test.ts b/packages/runtime/test/client-api/aggregate.test.ts index baf5b8e7..0c8ffd27 100644 --- a/packages/runtime/test/client-api/aggregate.test.ts +++ b/packages/runtime/test/client-api/aggregate.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; import { createUser } from './utils'; diff --git a/packages/runtime/test/client-api/client-specs.ts b/packages/runtime/test/client-api/client-specs.ts index 6a14ab43..fef380b9 100644 --- a/packages/runtime/test/client-api/client-specs.ts +++ b/packages/runtime/test/client-api/client-specs.ts @@ -1,5 +1,5 @@ import type { LogEvent } from 'kysely'; -import { getSchema, schema } from '../test-schema'; +import { getSchema, schema } from '../schemas/basic'; import { makePostgresClient, makeSqliteClient } from '../utils'; import type { ClientContract } from '../../src'; diff --git a/packages/runtime/test/client-api/count.test.ts b/packages/runtime/test/client-api/count.test.ts index 86e22264..743b4169 100644 --- a/packages/runtime/test/client-api/count.test.ts +++ b/packages/runtime/test/client-api/count.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-count-tests'; diff --git a/packages/runtime/test/client-api/create-many-and-return.test.ts b/packages/runtime/test/client-api/create-many-and-return.test.ts index 258194b2..29d5887e 100644 --- a/packages/runtime/test/client-api/create-many-and-return.test.ts +++ b/packages/runtime/test/client-api/create-many-and-return.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-create-many-and-return-tests'; diff --git a/packages/runtime/test/client-api/create-many.test.ts b/packages/runtime/test/client-api/create-many.test.ts index 1c2e119e..d25d8587 100644 --- a/packages/runtime/test/client-api/create-many.test.ts +++ b/packages/runtime/test/client-api/create-many.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-create-many-tests'; diff --git a/packages/runtime/test/client-api/create.test.ts b/packages/runtime/test/client-api/create.test.ts index 6ac87637..8cd692fd 100644 --- a/packages/runtime/test/client-api/create.test.ts +++ b/packages/runtime/test/client-api/create.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-create-tests'; diff --git a/packages/runtime/test/client-api/delete-many.test.ts b/packages/runtime/test/client-api/delete-many.test.ts index 86bc8a59..df8f3ac4 100644 --- a/packages/runtime/test/client-api/delete-many.test.ts +++ b/packages/runtime/test/client-api/delete-many.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-delete-many-tests'; diff --git a/packages/runtime/test/client-api/delete.test.ts b/packages/runtime/test/client-api/delete.test.ts index 57d70c60..b67216ae 100644 --- a/packages/runtime/test/client-api/delete.test.ts +++ b/packages/runtime/test/client-api/delete.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-delete-tests'; diff --git a/packages/runtime/test/client-api/filter.test.ts b/packages/runtime/test/client-api/filter.test.ts index cda36583..e9f49ce1 100644 --- a/packages/runtime/test/client-api/filter.test.ts +++ b/packages/runtime/test/client-api/filter.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-filter-tests'; diff --git a/packages/runtime/test/client-api/find.test.ts b/packages/runtime/test/client-api/find.test.ts index d239c76d..e1a05be1 100644 --- a/packages/runtime/test/client-api/find.test.ts +++ b/packages/runtime/test/client-api/find.test.ts @@ -1,7 +1,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; import { NotFoundError } from '../../src/client/errors'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; import { createPosts, createUser } from './utils'; diff --git a/packages/runtime/test/client-api/group-by.test.ts b/packages/runtime/test/client-api/group-by.test.ts index 0e0256e2..98c155fc 100644 --- a/packages/runtime/test/client-api/group-by.test.ts +++ b/packages/runtime/test/client-api/group-by.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; import { createUser } from './utils'; diff --git a/packages/runtime/test/client-api/raw-query.test.ts b/packages/runtime/test/client-api/raw-query.test.ts index 05049ba7..b1754b67 100644 --- a/packages/runtime/test/client-api/raw-query.test.ts +++ b/packages/runtime/test/client-api/raw-query.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-raw-query-tests'; diff --git a/packages/runtime/test/client-api/transaction.test.ts b/packages/runtime/test/client-api/transaction.test.ts index 60d58178..1daac7c5 100644 --- a/packages/runtime/test/client-api/transaction.test.ts +++ b/packages/runtime/test/client-api/transaction.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-transaction-tests'; diff --git a/packages/runtime/test/client-api/undefined-values.test.ts b/packages/runtime/test/client-api/undefined-values.test.ts index e9657b9e..07037be7 100644 --- a/packages/runtime/test/client-api/undefined-values.test.ts +++ b/packages/runtime/test/client-api/undefined-values.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; import { createUser } from './utils'; diff --git a/packages/runtime/test/client-api/update-many.test.ts b/packages/runtime/test/client-api/update-many.test.ts index e8d08cd9..eaef7e63 100644 --- a/packages/runtime/test/client-api/update-many.test.ts +++ b/packages/runtime/test/client-api/update-many.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-update-many-tests'; diff --git a/packages/runtime/test/client-api/update.test.ts b/packages/runtime/test/client-api/update.test.ts index 8c6ec359..2fc75fb8 100644 --- a/packages/runtime/test/client-api/update.test.ts +++ b/packages/runtime/test/client-api/update.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; import { createUser } from './utils'; diff --git a/packages/runtime/test/client-api/upsert.test.ts b/packages/runtime/test/client-api/upsert.test.ts index 34e402f9..02ede41c 100644 --- a/packages/runtime/test/client-api/upsert.test.ts +++ b/packages/runtime/test/client-api/upsert.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; import { createClientSpecs } from './client-specs'; const PG_DB_NAME = 'client-api-upsert-tests'; diff --git a/packages/runtime/test/client-api/utils.ts b/packages/runtime/test/client-api/utils.ts index 78646e36..2f1b85d0 100644 --- a/packages/runtime/test/client-api/utils.ts +++ b/packages/runtime/test/client-api/utils.ts @@ -1,5 +1,5 @@ import type { ClientContract } from '../../src/client'; -import type { schema } from '../test-schema'; +import type { schema } from '../schemas/basic'; type ClientType = ClientContract; diff --git a/packages/runtime/test/plugin/kysely-on-query.test.ts b/packages/runtime/test/plugin/kysely-on-query.test.ts index b098c8a4..90c47bb8 100644 --- a/packages/runtime/test/plugin/kysely-on-query.test.ts +++ b/packages/runtime/test/plugin/kysely-on-query.test.ts @@ -2,7 +2,7 @@ import SQLite from 'better-sqlite3'; import { InsertQueryNode, Kysely, PrimitiveValueListNode, ValuesNode, type QueryResult } from 'kysely'; import { beforeEach, describe, expect, it } from 'vitest'; import { ZenStackClient, type ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; describe('Kysely onQuery tests', () => { let _client: ClientContract; diff --git a/packages/runtime/test/plugin/mutation-hooks.test.ts b/packages/runtime/test/plugin/mutation-hooks.test.ts index c6bd08c7..08023e28 100644 --- a/packages/runtime/test/plugin/mutation-hooks.test.ts +++ b/packages/runtime/test/plugin/mutation-hooks.test.ts @@ -2,7 +2,7 @@ import SQLite from 'better-sqlite3'; import { DeleteQueryNode, InsertQueryNode, UpdateQueryNode } from 'kysely'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { ZenStackClient, type ClientContract } from '../../src'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; describe('Entity lifecycle tests', () => { let _client: ClientContract; diff --git a/packages/runtime/test/plugin/query-lifecycle.test.ts b/packages/runtime/test/plugin/query-lifecycle.test.ts index 15bc85a7..70658c55 100644 --- a/packages/runtime/test/plugin/query-lifecycle.test.ts +++ b/packages/runtime/test/plugin/query-lifecycle.test.ts @@ -1,7 +1,7 @@ import SQLite from 'better-sqlite3'; import { beforeEach, describe, expect, it } from 'vitest'; import { definePlugin, ZenStackClient, type ClientContract } from '../../src/client'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; describe('Query interception tests', () => { let _client: ClientContract; diff --git a/packages/runtime/test/policy/read.test.ts b/packages/runtime/test/policy/read.test.ts index b6cad7f4..eb1ccb41 100644 --- a/packages/runtime/test/policy/read.test.ts +++ b/packages/runtime/test/policy/read.test.ts @@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { type ClientContract } from '../../src/client'; import { PolicyPlugin } from '../../src/plugins/policy/plugin'; import { createClientSpecs } from '../client-api/client-specs'; -import { schema } from '../test-schema'; +import { schema } from '../schemas/basic'; const PG_DB_NAME = 'policy-read-tests'; diff --git a/packages/runtime/test/policy/todo-sample.test.ts b/packages/runtime/test/policy/todo-sample.test.ts index 4246969f..83c812b5 100644 --- a/packages/runtime/test/policy/todo-sample.test.ts +++ b/packages/runtime/test/policy/todo-sample.test.ts @@ -1,17 +1,8 @@ -import { generateTsSchemaFromFile } from '@zenstackhq/testtools'; -import path from 'node:path'; -import { beforeAll, describe, expect, it } from 'vitest'; -import type { SchemaDef } from '../../src/schema'; +import { describe, expect, it } from 'vitest'; +import { schema } from '../schemas/todo/schema'; import { createPolicyTestClient } from './utils'; describe('todo sample tests', () => { - let schema: SchemaDef; - - beforeAll(async () => { - const r = await generateTsSchemaFromFile(path.join(__dirname, '../schemas/todo.zmodel')); - schema = r.schema; - }); - it('works with user CRUD', async () => { const user1 = { id: 'user1', @@ -383,13 +374,13 @@ describe('todo sample tests', () => { where: { id: 'space1' }, include: { lists: true }, }); - expect(r.lists).toHaveLength(2); + expect(r?.lists).toHaveLength(2); const r1 = await user2Db.space.findFirst({ where: { id: 'space1' }, include: { lists: true }, }); - expect(r1.lists).toHaveLength(1); + expect(r1?.lists).toHaveLength(1); }); // TODO: `future()` support diff --git a/packages/runtime/test/policy/utils.ts b/packages/runtime/test/policy/utils.ts index dc599ce2..2fecc72b 100644 --- a/packages/runtime/test/policy/utils.ts +++ b/packages/runtime/test/policy/utils.ts @@ -1,8 +1,20 @@ +import type { ClientContract } from '../../src'; import { PolicyPlugin } from '../../src/plugins/policy'; import type { SchemaDef } from '../../src/schema'; import { createTestClient, type CreateTestClientOptions } from '../utils'; -export function createPolicyTestClient(schema: string | SchemaDef, options?: CreateTestClientOptions) { +export async function createPolicyTestClient( + schema: Schema, + options?: CreateTestClientOptions, +): Promise>; +export async function createPolicyTestClient( + schema: string, + options?: CreateTestClientOptions, +): Promise; +export async function createPolicyTestClient( + schema: Schema | string, + options?: CreateTestClientOptions, +): Promise { return createTestClient( schema as any, { diff --git a/packages/runtime/test/query-builder/query-builder.test.ts b/packages/runtime/test/query-builder/query-builder.test.ts index 5e7754e9..2eb3de5d 100644 --- a/packages/runtime/test/query-builder/query-builder.test.ts +++ b/packages/runtime/test/query-builder/query-builder.test.ts @@ -2,7 +2,7 @@ import { createId } from '@paralleldrive/cuid2'; import SQLite from 'better-sqlite3'; import { describe, expect, it } from 'vitest'; import { ZenStackClient } from '../../src'; -import { getSchema } from '../test-schema'; +import { getSchema } from '../schemas/basic'; describe('Client API tests', () => { const schema = getSchema('sqlite'); diff --git a/packages/runtime/test/test-schema/helper.ts b/packages/runtime/test/schemas/basic/helper.ts similarity index 100% rename from packages/runtime/test/test-schema/helper.ts rename to packages/runtime/test/schemas/basic/helper.ts diff --git a/packages/runtime/test/test-schema/index.ts b/packages/runtime/test/schemas/basic/index.ts similarity index 100% rename from packages/runtime/test/test-schema/index.ts rename to packages/runtime/test/schemas/basic/index.ts diff --git a/packages/runtime/test/test-schema/input.ts b/packages/runtime/test/schemas/basic/input.ts similarity index 100% rename from packages/runtime/test/test-schema/input.ts rename to packages/runtime/test/schemas/basic/input.ts diff --git a/packages/runtime/test/test-schema/models.ts b/packages/runtime/test/schemas/basic/models.ts similarity index 100% rename from packages/runtime/test/test-schema/models.ts rename to packages/runtime/test/schemas/basic/models.ts diff --git a/packages/runtime/test/test-schema/schema.ts b/packages/runtime/test/schemas/basic/schema.ts similarity index 99% rename from packages/runtime/test/test-schema/schema.ts rename to packages/runtime/test/schemas/basic/schema.ts index e2c8fb52..d594432e 100644 --- a/packages/runtime/test/test-schema/schema.ts +++ b/packages/runtime/test/schemas/basic/schema.ts @@ -5,7 +5,7 @@ /* eslint-disable */ -import { type SchemaDef, ExpressionUtils } from "../../dist/schema"; +import { type SchemaDef, ExpressionUtils } from "../../../dist/schema"; export const schema = { provider: { type: "sqlite" diff --git a/packages/runtime/test/test-schema/schema.zmodel b/packages/runtime/test/schemas/basic/schema.zmodel similarity index 100% rename from packages/runtime/test/test-schema/schema.zmodel rename to packages/runtime/test/schemas/basic/schema.zmodel diff --git a/packages/runtime/test/schemas/todo/input.ts b/packages/runtime/test/schemas/todo/input.ts new file mode 100644 index 00000000..27c268fc --- /dev/null +++ b/packages/runtime/test/schemas/todo/input.ts @@ -0,0 +1,110 @@ +////////////////////////////////////////////////////////////////////////////////////////////// +// DO NOT MODIFY THIS FILE // +// This file is automatically generated by ZenStack CLI and should not be manually updated. // +////////////////////////////////////////////////////////////////////////////////////////////// + +/* eslint-disable */ + +import { type SchemaType as $Schema } from "./schema"; +import type { FindManyArgs as $FindManyArgs, FindUniqueArgs as $FindUniqueArgs, FindFirstArgs as $FindFirstArgs, CreateArgs as $CreateArgs, CreateManyArgs as $CreateManyArgs, CreateManyAndReturnArgs as $CreateManyAndReturnArgs, UpdateArgs as $UpdateArgs, UpdateManyArgs as $UpdateManyArgs, UpdateManyAndReturnArgs as $UpdateManyAndReturnArgs, UpsertArgs as $UpsertArgs, DeleteArgs as $DeleteArgs, DeleteManyArgs as $DeleteManyArgs, CountArgs as $CountArgs, AggregateArgs as $AggregateArgs, GroupByArgs as $GroupByArgs, WhereInput as $WhereInput, SelectInput as $SelectInput, IncludeInput as $IncludeInput, OmitInput as $OmitInput } from "@zenstackhq/runtime"; +import type { SimplifiedModelResult as $SimplifiedModelResult, SelectIncludeOmit as $SelectIncludeOmit } from "@zenstackhq/runtime"; +export type SpaceFindManyArgs = $FindManyArgs<$Schema, "Space">; +export type SpaceFindUniqueArgs = $FindUniqueArgs<$Schema, "Space">; +export type SpaceFindFirstArgs = $FindFirstArgs<$Schema, "Space">; +export type SpaceCreateArgs = $CreateArgs<$Schema, "Space">; +export type SpaceCreateManyArgs = $CreateManyArgs<$Schema, "Space">; +export type SpaceCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "Space">; +export type SpaceUpdateArgs = $UpdateArgs<$Schema, "Space">; +export type SpaceUpdateManyArgs = $UpdateManyArgs<$Schema, "Space">; +export type SpaceUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "Space">; +export type SpaceUpsertArgs = $UpsertArgs<$Schema, "Space">; +export type SpaceDeleteArgs = $DeleteArgs<$Schema, "Space">; +export type SpaceDeleteManyArgs = $DeleteManyArgs<$Schema, "Space">; +export type SpaceCountArgs = $CountArgs<$Schema, "Space">; +export type SpaceAggregateArgs = $AggregateArgs<$Schema, "Space">; +export type SpaceGroupByArgs = $GroupByArgs<$Schema, "Space">; +export type SpaceWhereInput = $WhereInput<$Schema, "Space">; +export type SpaceSelect = $SelectInput<$Schema, "Space">; +export type SpaceInclude = $IncludeInput<$Schema, "Space">; +export type SpaceOmit = $OmitInput<$Schema, "Space">; +export type SpaceGetPayload> = $SimplifiedModelResult<$Schema, "Space", Args>; +export type SpaceUserFindManyArgs = $FindManyArgs<$Schema, "SpaceUser">; +export type SpaceUserFindUniqueArgs = $FindUniqueArgs<$Schema, "SpaceUser">; +export type SpaceUserFindFirstArgs = $FindFirstArgs<$Schema, "SpaceUser">; +export type SpaceUserCreateArgs = $CreateArgs<$Schema, "SpaceUser">; +export type SpaceUserCreateManyArgs = $CreateManyArgs<$Schema, "SpaceUser">; +export type SpaceUserCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "SpaceUser">; +export type SpaceUserUpdateArgs = $UpdateArgs<$Schema, "SpaceUser">; +export type SpaceUserUpdateManyArgs = $UpdateManyArgs<$Schema, "SpaceUser">; +export type SpaceUserUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "SpaceUser">; +export type SpaceUserUpsertArgs = $UpsertArgs<$Schema, "SpaceUser">; +export type SpaceUserDeleteArgs = $DeleteArgs<$Schema, "SpaceUser">; +export type SpaceUserDeleteManyArgs = $DeleteManyArgs<$Schema, "SpaceUser">; +export type SpaceUserCountArgs = $CountArgs<$Schema, "SpaceUser">; +export type SpaceUserAggregateArgs = $AggregateArgs<$Schema, "SpaceUser">; +export type SpaceUserGroupByArgs = $GroupByArgs<$Schema, "SpaceUser">; +export type SpaceUserWhereInput = $WhereInput<$Schema, "SpaceUser">; +export type SpaceUserSelect = $SelectInput<$Schema, "SpaceUser">; +export type SpaceUserInclude = $IncludeInput<$Schema, "SpaceUser">; +export type SpaceUserOmit = $OmitInput<$Schema, "SpaceUser">; +export type SpaceUserGetPayload> = $SimplifiedModelResult<$Schema, "SpaceUser", Args>; +export type UserFindManyArgs = $FindManyArgs<$Schema, "User">; +export type UserFindUniqueArgs = $FindUniqueArgs<$Schema, "User">; +export type UserFindFirstArgs = $FindFirstArgs<$Schema, "User">; +export type UserCreateArgs = $CreateArgs<$Schema, "User">; +export type UserCreateManyArgs = $CreateManyArgs<$Schema, "User">; +export type UserCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "User">; +export type UserUpdateArgs = $UpdateArgs<$Schema, "User">; +export type UserUpdateManyArgs = $UpdateManyArgs<$Schema, "User">; +export type UserUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "User">; +export type UserUpsertArgs = $UpsertArgs<$Schema, "User">; +export type UserDeleteArgs = $DeleteArgs<$Schema, "User">; +export type UserDeleteManyArgs = $DeleteManyArgs<$Schema, "User">; +export type UserCountArgs = $CountArgs<$Schema, "User">; +export type UserAggregateArgs = $AggregateArgs<$Schema, "User">; +export type UserGroupByArgs = $GroupByArgs<$Schema, "User">; +export type UserWhereInput = $WhereInput<$Schema, "User">; +export type UserSelect = $SelectInput<$Schema, "User">; +export type UserInclude = $IncludeInput<$Schema, "User">; +export type UserOmit = $OmitInput<$Schema, "User">; +export type UserGetPayload> = $SimplifiedModelResult<$Schema, "User", Args>; +export type ListFindManyArgs = $FindManyArgs<$Schema, "List">; +export type ListFindUniqueArgs = $FindUniqueArgs<$Schema, "List">; +export type ListFindFirstArgs = $FindFirstArgs<$Schema, "List">; +export type ListCreateArgs = $CreateArgs<$Schema, "List">; +export type ListCreateManyArgs = $CreateManyArgs<$Schema, "List">; +export type ListCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "List">; +export type ListUpdateArgs = $UpdateArgs<$Schema, "List">; +export type ListUpdateManyArgs = $UpdateManyArgs<$Schema, "List">; +export type ListUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "List">; +export type ListUpsertArgs = $UpsertArgs<$Schema, "List">; +export type ListDeleteArgs = $DeleteArgs<$Schema, "List">; +export type ListDeleteManyArgs = $DeleteManyArgs<$Schema, "List">; +export type ListCountArgs = $CountArgs<$Schema, "List">; +export type ListAggregateArgs = $AggregateArgs<$Schema, "List">; +export type ListGroupByArgs = $GroupByArgs<$Schema, "List">; +export type ListWhereInput = $WhereInput<$Schema, "List">; +export type ListSelect = $SelectInput<$Schema, "List">; +export type ListInclude = $IncludeInput<$Schema, "List">; +export type ListOmit = $OmitInput<$Schema, "List">; +export type ListGetPayload> = $SimplifiedModelResult<$Schema, "List", Args>; +export type TodoFindManyArgs = $FindManyArgs<$Schema, "Todo">; +export type TodoFindUniqueArgs = $FindUniqueArgs<$Schema, "Todo">; +export type TodoFindFirstArgs = $FindFirstArgs<$Schema, "Todo">; +export type TodoCreateArgs = $CreateArgs<$Schema, "Todo">; +export type TodoCreateManyArgs = $CreateManyArgs<$Schema, "Todo">; +export type TodoCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "Todo">; +export type TodoUpdateArgs = $UpdateArgs<$Schema, "Todo">; +export type TodoUpdateManyArgs = $UpdateManyArgs<$Schema, "Todo">; +export type TodoUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "Todo">; +export type TodoUpsertArgs = $UpsertArgs<$Schema, "Todo">; +export type TodoDeleteArgs = $DeleteArgs<$Schema, "Todo">; +export type TodoDeleteManyArgs = $DeleteManyArgs<$Schema, "Todo">; +export type TodoCountArgs = $CountArgs<$Schema, "Todo">; +export type TodoAggregateArgs = $AggregateArgs<$Schema, "Todo">; +export type TodoGroupByArgs = $GroupByArgs<$Schema, "Todo">; +export type TodoWhereInput = $WhereInput<$Schema, "Todo">; +export type TodoSelect = $SelectInput<$Schema, "Todo">; +export type TodoInclude = $IncludeInput<$Schema, "Todo">; +export type TodoOmit = $OmitInput<$Schema, "Todo">; +export type TodoGetPayload> = $SimplifiedModelResult<$Schema, "Todo", Args>; diff --git a/packages/runtime/test/schemas/todo/models.ts b/packages/runtime/test/schemas/todo/models.ts new file mode 100644 index 00000000..131c09d2 --- /dev/null +++ b/packages/runtime/test/schemas/todo/models.ts @@ -0,0 +1,14 @@ +////////////////////////////////////////////////////////////////////////////////////////////// +// DO NOT MODIFY THIS FILE // +// This file is automatically generated by ZenStack CLI and should not be manually updated. // +////////////////////////////////////////////////////////////////////////////////////////////// + +/* eslint-disable */ + +import { type SchemaType as $Schema } from "./schema"; +import { type ModelResult as $ModelResult } from "@zenstackhq/runtime"; +export type Space = $ModelResult<$Schema, "Space">; +export type SpaceUser = $ModelResult<$Schema, "SpaceUser">; +export type User = $ModelResult<$Schema, "User">; +export type List = $ModelResult<$Schema, "List">; +export type Todo = $ModelResult<$Schema, "Todo">; diff --git a/packages/runtime/test/schemas/todo/schema.ts b/packages/runtime/test/schemas/todo/schema.ts new file mode 100644 index 00000000..25a8290c --- /dev/null +++ b/packages/runtime/test/schemas/todo/schema.ts @@ -0,0 +1,395 @@ +////////////////////////////////////////////////////////////////////////////////////////////// +// DO NOT MODIFY THIS FILE // +// This file is automatically generated by ZenStack CLI and should not be manually updated. // +////////////////////////////////////////////////////////////////////////////////////////////// + +/* eslint-disable */ + +import { type SchemaDef, ExpressionUtils } from "../../../dist/schema"; +export const schema = { + provider: { + type: "sqlite" + }, + models: { + Space: { + name: "Space", + fields: { + id: { + name: "id", + type: "String", + id: true, + attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("uuid") }] }], + default: ExpressionUtils.call("uuid") + }, + createdAt: { + name: "createdAt", + type: "DateTime", + attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }], + default: ExpressionUtils.call("now") + }, + updatedAt: { + name: "updatedAt", + type: "DateTime", + updatedAt: true, + attributes: [{ name: "@updatedAt" }] + }, + name: { + name: "name", + type: "String", + attributes: [{ name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(4) }, { name: "max", value: ExpressionUtils.literal(50) }] }] + }, + slug: { + name: "slug", + type: "String", + unique: true, + attributes: [{ name: "@unique" }, { name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(4) }, { name: "max", value: ExpressionUtils.literal(16) }] }] + }, + owner: { + name: "owner", + type: "User", + optional: true, + attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("ownerId")]) }, { name: "references", value: ExpressionUtils.array([ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }], + relation: { opposite: "ownedSpaces", fields: ["ownerId"], references: ["id"], onDelete: "Cascade" } + }, + ownerId: { + name: "ownerId", + type: "String", + optional: true, + foreignKeyFor: [ + "owner" + ] + }, + members: { + name: "members", + type: "SpaceUser", + array: true, + relation: { opposite: "space" } + }, + lists: { + name: "lists", + type: "List", + array: true, + relation: { opposite: "space" } + } + }, + attributes: [ + { name: "@@deny", args: [{ name: "operation", value: ExpressionUtils.literal("all") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.call("auth"), "==", ExpressionUtils._null()) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("create") }, { name: "condition", value: ExpressionUtils.literal(true) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("read") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.field("members"), "?", ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"]))) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("update,delete") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.field("members"), "?", ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])), "&&", ExpressionUtils.binary(ExpressionUtils.field("role"), "==", ExpressionUtils.literal("ADMIN")))) }] } + ], + idFields: ["id"], + uniqueFields: { + id: { type: "String" }, + slug: { type: "String" } + } + }, + SpaceUser: { + name: "SpaceUser", + fields: { + id: { + name: "id", + type: "String", + id: true, + attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("uuid") }] }], + default: ExpressionUtils.call("uuid") + }, + createdAt: { + name: "createdAt", + type: "DateTime", + attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }], + default: ExpressionUtils.call("now") + }, + updatedAt: { + name: "updatedAt", + type: "DateTime", + updatedAt: true, + attributes: [{ name: "@updatedAt" }] + }, + space: { + name: "space", + type: "Space", + attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("spaceId")]) }, { name: "references", value: ExpressionUtils.array([ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }], + relation: { opposite: "members", fields: ["spaceId"], references: ["id"], onDelete: "Cascade" } + }, + spaceId: { + name: "spaceId", + type: "String", + foreignKeyFor: [ + "space" + ] + }, + user: { + name: "user", + type: "User", + attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("userId")]) }, { name: "references", value: ExpressionUtils.array([ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }], + relation: { opposite: "spaces", fields: ["userId"], references: ["id"], onDelete: "Cascade" } + }, + userId: { + name: "userId", + type: "String", + foreignKeyFor: [ + "user" + ] + }, + role: { + name: "role", + type: "String" + } + }, + attributes: [ + { name: "@@unique", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("userId"), ExpressionUtils.field("spaceId")]) }] }, + { name: "@@deny", args: [{ name: "operation", value: ExpressionUtils.literal("all") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.call("auth"), "==", ExpressionUtils._null()) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("create,update,delete") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("space"), ["ownerId"]), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])), "||", ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("space"), ["members"]), "?", ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])), "&&", ExpressionUtils.binary(ExpressionUtils.field("role"), "==", ExpressionUtils.literal("ADMIN"))))) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("read") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("space"), ["members"]), "?", ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"]))) }] } + ], + idFields: ["id"], + uniqueFields: { + id: { type: "String" }, + userId_spaceId: { userId: { type: "String" }, spaceId: { type: "String" } } + } + }, + User: { + name: "User", + fields: { + id: { + name: "id", + type: "String", + id: true, + attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("uuid") }] }], + default: ExpressionUtils.call("uuid") + }, + createdAt: { + name: "createdAt", + type: "DateTime", + attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }], + default: ExpressionUtils.call("now") + }, + updatedAt: { + name: "updatedAt", + type: "DateTime", + updatedAt: true, + attributes: [{ name: "@updatedAt" }] + }, + email: { + name: "email", + type: "String", + unique: true, + attributes: [{ name: "@unique" }, { name: "@email" }] + }, + password: { + name: "password", + type: "String", + optional: true, + attributes: [{ name: "@password" }, { name: "@omit" }] + }, + emailVerified: { + name: "emailVerified", + type: "DateTime", + optional: true + }, + name: { + name: "name", + type: "String", + optional: true + }, + ownedSpaces: { + name: "ownedSpaces", + type: "Space", + array: true, + relation: { opposite: "owner" } + }, + spaces: { + name: "spaces", + type: "SpaceUser", + array: true, + relation: { opposite: "user" } + }, + image: { + name: "image", + type: "String", + optional: true, + attributes: [{ name: "@url" }] + }, + lists: { + name: "lists", + type: "List", + array: true, + relation: { opposite: "owner" } + }, + todos: { + name: "todos", + type: "Todo", + array: true, + relation: { opposite: "owner" } + } + }, + attributes: [ + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("create") }, { name: "condition", value: ExpressionUtils.literal(true) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("read") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.field("spaces"), "?", ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("space"), ["members"]), "?", ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])))) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("all") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"]), "==", ExpressionUtils.field("id")) }] } + ], + idFields: ["id"], + uniqueFields: { + id: { type: "String" }, + email: { type: "String" } + } + }, + List: { + name: "List", + fields: { + id: { + name: "id", + type: "String", + id: true, + attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("uuid") }] }], + default: ExpressionUtils.call("uuid") + }, + createdAt: { + name: "createdAt", + type: "DateTime", + attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }], + default: ExpressionUtils.call("now") + }, + updatedAt: { + name: "updatedAt", + type: "DateTime", + updatedAt: true, + attributes: [{ name: "@updatedAt" }] + }, + space: { + name: "space", + type: "Space", + attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("spaceId")]) }, { name: "references", value: ExpressionUtils.array([ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }], + relation: { opposite: "lists", fields: ["spaceId"], references: ["id"], onDelete: "Cascade" } + }, + spaceId: { + name: "spaceId", + type: "String", + foreignKeyFor: [ + "space" + ] + }, + owner: { + name: "owner", + type: "User", + attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("ownerId")]) }, { name: "references", value: ExpressionUtils.array([ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }], + relation: { opposite: "lists", fields: ["ownerId"], references: ["id"], onDelete: "Cascade" } + }, + ownerId: { + name: "ownerId", + type: "String", + foreignKeyFor: [ + "owner" + ] + }, + title: { + name: "title", + type: "String", + attributes: [{ name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(1) }, { name: "max", value: ExpressionUtils.literal(100) }] }] + }, + private: { + name: "private", + type: "Boolean", + attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.literal(false) }] }], + default: false + }, + todos: { + name: "todos", + type: "Todo", + array: true, + relation: { opposite: "list" } + }, + revision: { + name: "revision", + type: "Int", + attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.literal(0) }] }], + default: 0 + } + }, + attributes: [ + { name: "@@deny", args: [{ name: "operation", value: ExpressionUtils.literal("all") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.call("auth"), "==", ExpressionUtils._null()) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("read") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.field("ownerId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])), "||", ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("space"), ["members"]), "?", ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"]))), "&&", ExpressionUtils.unary("!", ExpressionUtils.field("private")))) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("create") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.field("ownerId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])), "&&", ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("space"), ["members"]), "?", ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])))) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("update") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.field("ownerId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])), "&&", ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("space"), ["members"]), "?", ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])))) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("delete") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.field("ownerId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])) }] } + ], + idFields: ["id"], + uniqueFields: { + id: { type: "String" } + } + }, + Todo: { + name: "Todo", + fields: { + id: { + name: "id", + type: "String", + id: true, + attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("uuid") }] }], + default: ExpressionUtils.call("uuid") + }, + createdAt: { + name: "createdAt", + type: "DateTime", + attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }], + default: ExpressionUtils.call("now") + }, + updatedAt: { + name: "updatedAt", + type: "DateTime", + updatedAt: true, + attributes: [{ name: "@updatedAt" }] + }, + owner: { + name: "owner", + type: "User", + attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("ownerId")]) }, { name: "references", value: ExpressionUtils.array([ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }], + relation: { opposite: "todos", fields: ["ownerId"], references: ["id"], onDelete: "Cascade" } + }, + ownerId: { + name: "ownerId", + type: "String", + foreignKeyFor: [ + "owner" + ] + }, + list: { + name: "list", + type: "List", + attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array([ExpressionUtils.field("listId")]) }, { name: "references", value: ExpressionUtils.array([ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }], + relation: { opposite: "todos", fields: ["listId"], references: ["id"], onDelete: "Cascade" } + }, + listId: { + name: "listId", + type: "String", + foreignKeyFor: [ + "list" + ] + }, + title: { + name: "title", + type: "String", + attributes: [{ name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(1) }, { name: "max", value: ExpressionUtils.literal(100) }] }] + }, + completedAt: { + name: "completedAt", + type: "DateTime", + optional: true + } + }, + attributes: [ + { name: "@@deny", args: [{ name: "operation", value: ExpressionUtils.literal("all") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.call("auth"), "==", ExpressionUtils._null()) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("all") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("list"), ["ownerId"]), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"])) }] }, + { name: "@@allow", args: [{ name: "operation", value: ExpressionUtils.literal("all") }, { name: "condition", value: ExpressionUtils.binary(ExpressionUtils.binary(ExpressionUtils.member(ExpressionUtils.field("list"), ["space", "members"]), "?", ExpressionUtils.binary(ExpressionUtils.field("userId"), "==", ExpressionUtils.member(ExpressionUtils.call("auth"), ["id"]))), "&&", ExpressionUtils.unary("!", ExpressionUtils.member(ExpressionUtils.field("list"), ["private"]))) }] } + ], + idFields: ["id"], + uniqueFields: { + id: { type: "String" } + } + } + }, + authType: "User", + plugins: {} +} as const satisfies SchemaDef; +export type SchemaType = typeof schema; diff --git a/packages/runtime/test/schemas/todo.zmodel b/packages/runtime/test/schemas/todo/todo.zmodel similarity index 100% rename from packages/runtime/test/schemas/todo.zmodel rename to packages/runtime/test/schemas/todo/todo.zmodel diff --git a/packages/runtime/test/typing/input.ts b/packages/runtime/test/schemas/typing/input.ts similarity index 100% rename from packages/runtime/test/typing/input.ts rename to packages/runtime/test/schemas/typing/input.ts diff --git a/packages/runtime/test/typing/models.ts b/packages/runtime/test/schemas/typing/models.ts similarity index 100% rename from packages/runtime/test/typing/models.ts rename to packages/runtime/test/schemas/typing/models.ts diff --git a/packages/runtime/test/typing/schema.ts b/packages/runtime/test/schemas/typing/schema.ts similarity index 99% rename from packages/runtime/test/typing/schema.ts rename to packages/runtime/test/schemas/typing/schema.ts index de56a9dc..a86dda22 100644 --- a/packages/runtime/test/typing/schema.ts +++ b/packages/runtime/test/schemas/typing/schema.ts @@ -5,7 +5,7 @@ /* eslint-disable */ -import { type SchemaDef, type OperandExpression, ExpressionUtils } from "../../dist/schema"; +import { type SchemaDef, type OperandExpression, ExpressionUtils } from "../../../dist/schema"; export const schema = { provider: { type: "sqlite" diff --git a/packages/runtime/test/typing/typing-test.zmodel b/packages/runtime/test/schemas/typing/schema.zmodel similarity index 100% rename from packages/runtime/test/typing/typing-test.zmodel rename to packages/runtime/test/schemas/typing/schema.zmodel diff --git a/packages/runtime/test/typing/verify-typing.ts b/packages/runtime/test/schemas/typing/verify-typing.ts similarity index 99% rename from packages/runtime/test/typing/verify-typing.ts rename to packages/runtime/test/schemas/typing/verify-typing.ts index e815758f..912486db 100644 --- a/packages/runtime/test/typing/verify-typing.ts +++ b/packages/runtime/test/schemas/typing/verify-typing.ts @@ -1,5 +1,5 @@ import SQLite from 'better-sqlite3'; -import { ZenStackClient } from '../../dist'; +import { ZenStackClient } from '../../../dist'; import { Role, type Identity, type IdentityProvider } from './models'; import { schema } from './schema'; diff --git a/packages/runtime/test/scripts/generate.ts b/packages/runtime/test/scripts/generate.ts index da402546..a5cf10e9 100644 --- a/packages/runtime/test/scripts/generate.ts +++ b/packages/runtime/test/scripts/generate.ts @@ -1,14 +1,18 @@ -import { glob } from 'glob'; import { TsSchemaGenerator } from '@zenstackhq/sdk'; -import path from 'node:path'; +import { glob } from 'glob'; import fs from 'node:fs'; +import path from 'node:path'; import { fileURLToPath } from 'node:url'; const dir = path.dirname(fileURLToPath(import.meta.url)); async function main() { - await generate(path.resolve(dir, '../typing/typing-test.zmodel')); - await generate(path.resolve(dir, '../test-schema/schema.zmodel')); + // glob all zmodel files in "e2e" directory + const zmodelFiles = glob.sync(path.resolve(dir, '../schemas/**/*.zmodel')); + for (const file of zmodelFiles) { + console.log(`Generating TS schema for: ${file}`); + await generate(file); + } } async function generate(schemaPath: string) { @@ -18,7 +22,7 @@ async function generate(schemaPath: string) { const pluginModelFiles = glob.sync(path.resolve(dir, '../../dist/**/plugin.zmodel')); await generator.generate(schemaPath, pluginModelFiles, outputDir); const content = fs.readFileSync(tsPath, 'utf-8'); - fs.writeFileSync(tsPath, content.replace(/@zenstackhq\/runtime/g, '../../dist')); + fs.writeFileSync(tsPath, content.replace(/@zenstackhq\/runtime/g, '../../../dist')); console.log('TS schema generated at:', outputDir); } diff --git a/packages/runtime/test/utils.ts b/packages/runtime/test/utils.ts index 82b05e82..7c2e02d4 100644 --- a/packages/runtime/test/utils.ts +++ b/packages/runtime/test/utils.ts @@ -7,7 +7,7 @@ import { execSync } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; import { Client as PGClient, Pool } from 'pg'; -import type { ClientOptions } from '../src/client'; +import type { ClientContract, ClientOptions } from '../src/client'; import { ZenStackClient } from '../src/client'; import type { SchemaDef } from '../src/schema'; @@ -67,7 +67,7 @@ export type CreateTestClientOptions = Omit( schema: Schema, options?: CreateTestClientOptions, -): Promise; +): Promise>; export async function createTestClient( schema: string, options?: CreateTestClientOptions, diff --git a/packages/sdk/src/ts-schema-generator.ts b/packages/sdk/src/ts-schema-generator.ts index e5202b32..ba4f31a5 100644 --- a/packages/sdk/src/ts-schema-generator.ts +++ b/packages/sdk/src/ts-schema-generator.ts @@ -1073,7 +1073,7 @@ export class TsSchemaGenerator { const statements: ts.Statement[] = []; // generate: import { schema as $schema, type SchemaType as $Schema } from './schema'; - statements.push(this.generateSchemaTypeImport(true, true)); + statements.push(this.generateSchemaImport(model, true, true)); // generate: import type { ModelResult as $ModelResult } from '@zenstackhq/runtime'; statements.push( @@ -1196,17 +1196,20 @@ export class TsSchemaGenerator { fs.writeFileSync(outputFile, result); } - private generateSchemaTypeImport(schemaObject: boolean, schemaType: boolean) { + private generateSchemaImport(model: Model, schemaObject: boolean, schemaType: boolean) { const importSpecifiers = []; if (schemaObject) { - importSpecifiers.push( - ts.factory.createImportSpecifier( - false, - ts.factory.createIdentifier('schema'), - ts.factory.createIdentifier('$schema'), - ), - ); + if (model.declarations.some(isEnum)) { + // enums require referencing the schema object + importSpecifiers.push( + ts.factory.createImportSpecifier( + false, + ts.factory.createIdentifier('schema'), + ts.factory.createIdentifier('$schema'), + ), + ); + } } if (schemaType) { @@ -1243,7 +1246,7 @@ export class TsSchemaGenerator { const statements: ts.Statement[] = []; // generate: import { SchemaType as $Schema } from './schema'; - statements.push(this.generateSchemaTypeImport(false, true)); + statements.push(this.generateSchemaImport(model, false, true)); // generate: import { CreateArgs as $CreateArgs, ... } from '@zenstackhq/runtime'; const inputTypes = [