diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index f666a58e8..a902c6550 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -47,7 +47,7 @@ jobs: strategy: matrix: - node-version: [24.x] + node-version: [22.x, 24.x] provider: [sqlite, postgresql, mysql] steps: diff --git a/package.json b/package.json index 6d7b7861e..00398b36f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "zenstack-v3", "displayName": "ZenStack", "description": "ZenStack", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/auth-adapters/better-auth/package.json b/packages/auth-adapters/better-auth/package.json index 18a293f81..08e806e97 100644 --- a/packages/auth-adapters/better-auth/package.json +++ b/packages/auth-adapters/better-auth/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/better-auth", "displayName": "ZenStack Better Auth Adapter", "description": "ZenStack Better Auth Adapter. This adapter is modified from better-auth's Prisma adapter.", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/cli/package.json b/packages/cli/package.json index 2ac5adc9f..02650d855 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/cli", "displayName": "ZenStack CLI", "description": "FullStack database toolkit with built-in access control and automatic API generation.", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", @@ -110,7 +110,7 @@ } }, "engines": { - "node": ">=20" + "node": ">=22" }, "funding": "https://github.com/sponsors/zenstackhq" } \ No newline at end of file diff --git a/packages/clients/client-helpers/package.json b/packages/clients/client-helpers/package.json index 4e6fc7e4f..43c235378 100644 --- a/packages/clients/client-helpers/package.json +++ b/packages/clients/client-helpers/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/client-helpers", "displayName": "ZenStack Client Helpers", "description": "Helpers for implementing clients that consume ZenStack's CRUD service", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/clients/fetch-client/package.json b/packages/clients/fetch-client/package.json index 2f2f86b6d..f86c226ff 100644 --- a/packages/clients/fetch-client/package.json +++ b/packages/clients/fetch-client/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/fetch-client", "displayName": "ZenStack Fetch Client", "description": "Simple fetch-based client for consuming ZenStack's RPC-style CRUD API", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/clients/tanstack-query/package.json b/packages/clients/tanstack-query/package.json index f5403617a..e0bdb8515 100644 --- a/packages/clients/tanstack-query/package.json +++ b/packages/clients/tanstack-query/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/tanstack-query", "displayName": "ZenStack TanStack Query Integration", "description": "TanStack Query Client for consuming ZenStack v3's CRUD service", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/common-helpers/package.json b/packages/common-helpers/package.json index 44bb72108..638b02c4a 100644 --- a/packages/common-helpers/package.json +++ b/packages/common-helpers/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/common-helpers", "displayName": "ZenStack Common Helpers", "description": "ZenStack Common Helpers", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/config/eslint-config/package.json b/packages/config/eslint-config/package.json index ac15fc5fa..ddc601ae4 100644 --- a/packages/config/eslint-config/package.json +++ b/packages/config/eslint-config/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/eslint-config", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "private": true, "license": "MIT" diff --git a/packages/config/tsdown-config/package.json b/packages/config/tsdown-config/package.json index 4a0e4826d..35e896dce 100644 --- a/packages/config/tsdown-config/package.json +++ b/packages/config/tsdown-config/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/tsdown-config", - "version": "3.10.1", + "version": "3.10.2", "private": true, "type": "module", "license": "MIT", diff --git a/packages/config/typescript-config/package.json b/packages/config/typescript-config/package.json index 1b08be16c..55e740d2d 100644 --- a/packages/config/typescript-config/package.json +++ b/packages/config/typescript-config/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/typescript-config", - "version": "3.10.1", + "version": "3.10.2", "private": true, "license": "MIT" } diff --git a/packages/config/vitest-config/package.json b/packages/config/vitest-config/package.json index 564433054..624de4d11 100644 --- a/packages/config/vitest-config/package.json +++ b/packages/config/vitest-config/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/vitest-config", "type": "module", - "version": "3.10.1", + "version": "3.10.2", "private": true, "license": "MIT", "exports": { diff --git a/packages/create-zenstack/package.json b/packages/create-zenstack/package.json index fc4169ed4..74f37f09a 100644 --- a/packages/create-zenstack/package.json +++ b/packages/create-zenstack/package.json @@ -2,7 +2,7 @@ "name": "create-zenstack", "displayName": "Create ZenStack", "description": "Create a new ZenStack project", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/ide/vscode/package.json b/packages/ide/vscode/package.json index 44050804a..a997f11c1 100644 --- a/packages/ide/vscode/package.json +++ b/packages/ide/vscode/package.json @@ -1,7 +1,7 @@ { "name": "zenstack-v3", "publisher": "zenstack", - "version": "3.10.1", + "version": "3.10.2", "displayName": "ZenStack V3 Language Tools", "description": "VSCode extension for ZenStack (v3) ZModel language", "private": true, diff --git a/packages/language/package.json b/packages/language/package.json index fda3d8f28..a323203e7 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/language", "displayName": "ZenStack Language Tooling", "description": "ZenStack ZModel language specification", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", @@ -92,5 +92,8 @@ "langium-cli": "catalog:", "tmp": "catalog:" }, + "engines": { + "node": ">=22" + }, "funding": "https://github.com/sponsors/zenstackhq" } diff --git a/packages/orm/package.json b/packages/orm/package.json index 587dbb8e1..26c460a0e 100644 --- a/packages/orm/package.json +++ b/packages/orm/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/orm", "displayName": "ZenStack ORM", "description": "ZenStack ORM", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", @@ -171,5 +171,8 @@ "tsx": "catalog:", "zod": "^4.1.0" }, + "engines": { + "node": ">=22" + }, "funding": "https://github.com/sponsors/zenstackhq" } diff --git a/packages/plugins/policy/package.json b/packages/plugins/policy/package.json index 4cc848e90..f0b15f6b5 100644 --- a/packages/plugins/policy/package.json +++ b/packages/plugins/policy/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/plugin-policy", "displayName": "ZenStack Access Policy Plugin", "description": "ZenStack plugin that enforces access control policies defined in the schema", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/plugins/policy/src/expression-transformer.ts b/packages/plugins/policy/src/expression-transformer.ts index b6cd6dba5..753d27216 100644 --- a/packages/plugins/policy/src/expression-transformer.ts +++ b/packages/plugins/policy/src/expression-transformer.ts @@ -343,9 +343,12 @@ export class ExpressionTransformer { const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context); // Map ZModel operator to SQL operator string const sqlOp = op === '==' ? '=' : op; - // `before()` enums are text in the VALUES table and pg can't compare text to a native enum + // enums read from a VALUES table (`before()` rows, create input) are text, and pg can't + // compare text to a native enum const asText = - this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context); + this.isBeforeEnumField(normalizedLeft, context) || + this.isBeforeEnumField(normalizedRight, context) || + this.isCreateInputEnumComparison(normalizedLeft, normalizedRight, context); const operand = (node: OperationNode) => asText ? this.dialect.castText(new ExpressionWrapper(node)) : new ExpressionWrapper(node); return this.dialect @@ -1233,6 +1236,76 @@ export class ExpressionTransformer { return !!fieldDef && QueryUtils.isEnum(this.schema, fieldDef.type); } + /** + * Whether a comparison in a create rule needs both operands compared as text. + * + * Create rules are evaluated before the insert, against a `VALUES` table built from the input + * (see `enforcePreCreatePolicyForOne`). That table carries enum fields as `text`, while stored + * columns have the native enum type, and PostgreSQL has no `text = "Enum"` operator (42883). + * + * Returns true only when one operand is an enum field of the entity being created and the + * other is a stored field of the same enum, e.g. for `model Grant { permission Permission }`: + * - `permission == anchor.defaultPermission` (bare top-level field vs. related row) + * - `anchor.approvals?[permission == this.permission]` (stored row vs. `this`) + * + * Not for literals (`permission == EDIT`): the name mapper still translates `@map`-ed literals. + * Not for enums with `@map`-ed values: the input holds names (`ACTIVE`) but stored columns + * hold mapped values (`active`), so a text comparison would be wrong (#2867). + */ + private isCreateInputEnumComparison(left: Expression, right: Expression, context: ExpressionTransformerContext) { + // only create rules read from the input `VALUES` table + if (context.operation !== 'create') { + return false; + } + const leftInput = this.getCreateInputEnumField(left, context); + const rightInput = this.getCreateInputEnumField(right, context); + // exactly one operand must be create input: if both are, both are text; if neither is, + // both are stored native enums. Either way the types already match. + if (!leftInput === !rightInput) { + return false; + } + const inputDef = (leftInput ?? rightInput)!; + // the other operand must be a stored field of the same enum (a literal resolves to no field) + const storedDef = this.getFieldDefFromFieldRef(leftInput ? right : left, context); + return storedDef?.type === inputDef.type && !this.hasMappedEnumValues(inputDef.type); + } + + /** + * If the expression reads an enum field of the entity being created (i.e. a column of the input + * `VALUES` table), returns that field's definition; otherwise `undefined`. + * + * - `this.field` always refers to the entity the rule belongs to. A longer path such as + * `this.anchor.defaultPermission` goes through a relation and reads a stored row. + * - a bare `field` refers to the entity being created only at the top level of the rule. Inside + * a collection predicate (`relation?[field == ...]`) it refers to the related stored row. + */ + private getCreateInputEnumField(expr: Expression, context: ExpressionTransformerContext) { + let field: string | undefined; + if (ExpressionUtils.isMember(expr) && ExpressionUtils.isThis(expr.receiver) && expr.members.length === 1) { + // `this.field` + field = expr.members[0]; + } else if ( + ExpressionUtils.isField(expr) && + // still on the model the rule belongs to ... + context.modelOrType === context.thisType && + // ... and not inside a collection predicate: there (incl. self-relations, where the + // model is the same) a bare field is a stored row, aliased differently + context.alias === context.thisAlias + ) { + // bare top-level `field` + field = expr.field; + } + // `this` and top-level bare fields both resolve against the model being created + const fieldDef = field ? QueryUtils.getField(this.schema, context.thisType, field) : undefined; + return fieldDef && QueryUtils.isEnum(this.schema, fieldDef.type) ? fieldDef : undefined; + } + + // whether any member of the enum carries `@map`, i.e. its stored values differ from its names + private hasMappedEnumValues(enumType: string) { + const enumDef = QueryUtils.getEnum(this.schema, enumType); + return Object.values(enumDef?.fields ?? {}).some((f) => f.attributes?.some((attr) => attr.name === '@map')); + } + private getFieldDefFromFieldRef(expr: Expression, context: ExpressionTransformerContext): FieldDef | undefined { return this.resolveFieldRef(expr, context)?.fieldDef; } diff --git a/packages/plugins/soft-delete/package.json b/packages/plugins/soft-delete/package.json index 8c27a5bc4..259e406e7 100644 --- a/packages/plugins/soft-delete/package.json +++ b/packages/plugins/soft-delete/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/plugin-soft-delete", "displayName": "ZenStack Soft Delete Plugin", "description": "ZenStack plugin that implements soft-delete by intercepting Kysely queries", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/schema/package.json b/packages/schema/package.json index e7a43cc86..90b53de02 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/schema", "displayName": "ZenStack Schema Object Model", "description": "TypeScript representation of ZModel schema", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 4f49f634b..b51c4cace 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/sdk", "displayName": "ZenStack SDK", "description": "Utilities for building ZenStack plugins", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/server/package.json b/packages/server/package.json index ddcc7ffe7..d9f423b66 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/server", "displayName": "ZenStack Automatic CRUD Server", "description": "ZenStack automatic CRUD API handlers and server adapters for popular frameworks", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/packages/testtools/package.json b/packages/testtools/package.json index c845e37aa..78a0c7a56 100644 --- a/packages/testtools/package.json +++ b/packages/testtools/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/testtools", "displayName": "ZenStack Test Tools", "description": "ZenStack Test Tools", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", @@ -55,7 +55,8 @@ }, "peerDependencies": { "better-sqlite3": "catalog:", - "pg": "catalog:" + "pg": "catalog:", + "vitest": "^4.0.14" }, "devDependencies": { "@types/better-sqlite3": "catalog:", @@ -65,7 +66,8 @@ "@zenstackhq/eslint-config": "workspace:*", "@zenstackhq/tsdown-config": "workspace:*", "@zenstackhq/typescript-config": "workspace:*", - "typescript": "catalog:" + "typescript": "catalog:", + "vitest": "^4.0.14" }, "funding": "https://github.com/sponsors/zenstackhq" } diff --git a/packages/zod/package.json b/packages/zod/package.json index 7b7fcaf44..fb413a5c2 100644 --- a/packages/zod/package.json +++ b/packages/zod/package.json @@ -2,7 +2,7 @@ "name": "@zenstackhq/zod", "displayName": "ZenStack Zod Integration", "description": "Automatically deriving Zod schemas from ZModel schemas", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "author": { "name": "ZenStack Team", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8f9f69741..9d153c351 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1020,6 +1020,9 @@ importers: typescript: specifier: 'catalog:' version: 6.0.3 + vitest: + specifier: ^4.0.14 + version: 4.0.14(@edge-runtime/vm@5.0.0)(@types/node@20.19.24)(happy-dom@20.8.9)(jiti@2.7.0)(jsdom@27.1.0)(lightningcss@1.30.2)(terser@5.44.0)(tsx@4.22.4)(yaml@2.9.0) packages/zod: dependencies: diff --git a/samples/orm/package.json b/samples/orm/package.json index 957f1e2f4..5201ee9f6 100644 --- a/samples/orm/package.json +++ b/samples/orm/package.json @@ -1,6 +1,6 @@ { "name": "sample-orm", - "version": "3.10.1", + "version": "3.10.2", "description": "", "main": "index.js", "private": true, diff --git a/samples/taskforge/package.json b/samples/taskforge/package.json index 97bcfc28c..9c8eadd2c 100644 --- a/samples/taskforge/package.json +++ b/samples/taskforge/package.json @@ -1,6 +1,6 @@ { "name": "taskforge", - "version": "3.10.1", + "version": "3.10.2", "type": "module", "private": true, "description": "A CLI for a team collaboration / project-tracking platform, built on ZenStack v3 (ORM) and better-auth.", diff --git a/tests/e2e/package.json b/tests/e2e/package.json index deaf767dc..010da3537 100644 --- a/tests/e2e/package.json +++ b/tests/e2e/package.json @@ -1,6 +1,6 @@ { "name": "e2e", - "version": "3.10.1", + "version": "3.10.2", "private": true, "type": "module", "scripts": { diff --git a/tests/regression/package.json b/tests/regression/package.json index 44a6f9cb6..42f2316fc 100644 --- a/tests/regression/package.json +++ b/tests/regression/package.json @@ -1,6 +1,6 @@ { "name": "regression", - "version": "3.10.1", + "version": "3.10.2", "private": true, "type": "module", "scripts": { diff --git a/tests/regression/test/issue-2659.test.ts b/tests/regression/test/issue-2659.test.ts new file mode 100644 index 000000000..ef03d6bcd --- /dev/null +++ b/tests/regression/test/issue-2659.test.ts @@ -0,0 +1,33 @@ +import fs from 'node:fs'; +import { builtinModules } from 'node:module'; +import path from 'node:path'; +import { describe, expect, it } from 'vitest'; + +// https://github.com/zenstackhq/zenstack/issues/2659 +describe('Regression for issue 2659', () => { + it('testtools declares every package it imports at runtime', () => { + const pkgDir = path.resolve(__dirname, '../../../packages/testtools'); + const pkg = JSON.parse(fs.readFileSync(path.join(pkgDir, 'package.json'), 'utf-8')); + const declared = new Set([...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.peerDependencies ?? {})]); + + const srcDir = path.join(pkgDir, 'src'); + const imported = new Set(); + for (const file of fs.readdirSync(srcDir)) { + if (!file.endsWith('.ts') || file.endsWith('.d.ts')) { + continue; + } + const content = fs.readFileSync(path.join(srcDir, file), 'utf-8'); + for (const m of content.matchAll(/^import\s+(?!type\s)[^'"]*?from\s+['"]([^'"]+)['"]/gm)) { + const spec = m[1]!; + if (spec.startsWith('.') || spec.startsWith('node:') || builtinModules.includes(spec)) { + continue; + } + const parts = spec.split('/'); + imported.add(spec.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]!); + } + } + + const missing = [...imported].filter((name) => !declared.has(name)); + expect(missing).toEqual([]); + }); +}); diff --git a/tests/regression/test/issue-2866.test.ts b/tests/regression/test/issue-2866.test.ts new file mode 100644 index 000000000..488e68219 --- /dev/null +++ b/tests/regression/test/issue-2866.test.ts @@ -0,0 +1,148 @@ +import { createPolicyTestClient } from '@zenstackhq/testtools'; +import { describe, expect, it } from 'vitest'; + +// https://github.com/zenstackhq/zenstack/issues/2866 +describe('Regression for issue #2866', () => { + const schema = ` +enum Permission { + VIEW + EDIT +} + +model Anchor { + id Int @id + defaultPermission Permission @default(VIEW) + approvals Approval[] + roles Role[] + grants Grant[] + @@allow('all', true) +} + +model Approval { + id Int @id + anchorId Int + anchor Anchor @relation(fields: [anchorId], references: [id]) + permission Permission @default(VIEW) + permissions Permission[] + @@allow('all', true) +} + +model Role { + id Int @id + anchorId Int + anchor Anchor @relation(fields: [anchorId], references: [id]) + permissions Permission[] + @@allow('read', true) + @@allow('create', anchor.approvals?[permissions == this.permissions]) + @@allow('update', anchor.approvals?[permissions != this.permissions]) +} + +model Grant { + id Int @id + anchorId Int + anchor Anchor @relation(fields: [anchorId], references: [id]) + permission Permission + @@allow('read', true) + @@allow('create', permission == anchor.defaultPermission || anchor.approvals?[permission == this.permission]) +} +`; + + async function setup() { + const db = await createPolicyTestClient(schema, { provider: 'postgresql', usePrismaPush: true }); + const raw = db.$unuseAll(); + await raw.anchor.create({ data: { id: 1 } }); + await raw.anchor.create({ data: { id: 2 } }); + await raw.approval.create({ data: { id: 1, anchorId: 1, permissions: ['VIEW', 'EDIT'] } }); + await raw.approval.create({ data: { id: 2, anchorId: 2, permission: 'EDIT' } }); + return db; + } + + it('compares a stored enum array with the create input', async () => { + const db = await setup(); + + await expect( + db.role.create({ data: { id: 1, anchorId: 1, permissions: ['VIEW', 'EDIT'] } }), + ).resolves.toMatchObject({ permissions: ['VIEW', 'EDIT'] }); + + // array equality keeps order and multiplicity + await expect( + db.role.create({ data: { id: 2, anchorId: 1, permissions: ['EDIT', 'VIEW'] } }), + ).toBeRejectedByPolicy(); + await expect(db.role.create({ data: { id: 3, anchorId: 1, permissions: ['VIEW'] } })).toBeRejectedByPolicy(); + await expect( + db.role.create({ data: { id: 4, anchorId: 1, permissions: ['VIEW', 'EDIT', 'EDIT'] } }), + ).toBeRejectedByPolicy(); + }); + + it('compares stored enum arrays on update', async () => { + const db = await setup(); + const raw = db.$unuseAll(); + await raw.role.create({ data: { id: 1, anchorId: 1, permissions: ['VIEW', 'EDIT'] } }); + await raw.role.create({ data: { id: 2, anchorId: 1, permissions: ['VIEW'] } }); + + await expect(db.role.update({ where: { id: 1 }, data: { permissions: ['EDIT'] } })).toBeRejectedNotFound(); + await expect(db.role.update({ where: { id: 2 }, data: { permissions: ['EDIT'] } })).resolves.toMatchObject({ + permissions: ['EDIT'], + }); + }); + + it('compares a stored enum with the create input', async () => { + const db = await setup(); + + // top-level field compared with a relation field + await expect(db.grant.create({ data: { id: 1, anchorId: 1, permission: 'VIEW' } })).resolves.toMatchObject({ + permission: 'VIEW', + }); + // `this` field compared inside a collection predicate + await expect(db.grant.create({ data: { id: 2, anchorId: 2, permission: 'EDIT' } })).resolves.toMatchObject({ + permission: 'EDIT', + }); + await expect(db.grant.create({ data: { id: 3, anchorId: 1, permission: 'EDIT' } })).toBeRejectedByPolicy(); + }); + + it('treats self-relation rows as stored rows', async () => { + const db = await createPolicyTestClient( + ` +enum Status { + ACTIVE @map("active") + INACTIVE @map("inactive") +} + +enum Kind { + FILE + DIR +} + +model Folder { + id Int @id + status Status + kind Kind + parentId Int? + parent Folder? @relation("tree", fields: [parentId], references: [id]) + children Folder[] @relation("tree") + @@allow('read', true) + @@allow('create', parentId == null || parent.children?[status != ACTIVE] || parent.children?[kind == this.kind]) +} +`, + { provider: 'postgresql', usePrismaPush: true }, + ); + const raw = db.$unuseAll(); + await raw.folder.create({ data: { id: 1, status: 'INACTIVE', kind: 'DIR' } }); + await raw.folder.create({ data: { id: 2, status: 'ACTIVE', kind: 'DIR', parentId: 1 } }); + await raw.folder.create({ data: { id: 3, status: 'INACTIVE', kind: 'DIR' } }); + await raw.folder.create({ data: { id: 4, status: 'INACTIVE', kind: 'DIR', parentId: 3 } }); + + // the only child is ACTIVE and of another kind + await expect( + db.folder.create({ data: { id: 10, status: 'INACTIVE', kind: 'FILE', parentId: 1 } }), + ).toBeRejectedByPolicy(); + // a child that is not ACTIVE + await expect( + db.folder.create({ data: { id: 11, status: 'INACTIVE', kind: 'FILE', parentId: 3 } }), + ).resolves.toMatchObject({ id: 11 }); + // a child of the same kind + await expect( + db.folder.create({ data: { id: 12, status: 'INACTIVE', kind: 'DIR', parentId: 1 } }), + ).resolves.toMatchObject({ id: 12 }); + }); +}); diff --git a/tests/runtimes/bun/package.json b/tests/runtimes/bun/package.json index 2f2d157bd..e428c8751 100644 --- a/tests/runtimes/bun/package.json +++ b/tests/runtimes/bun/package.json @@ -1,6 +1,6 @@ { "name": "bun-e2e", - "version": "3.10.1", + "version": "3.10.2", "private": true, "type": "module", "scripts": { diff --git a/tests/runtimes/edge-runtime/package.json b/tests/runtimes/edge-runtime/package.json index e02e15de6..40ab06d2f 100644 --- a/tests/runtimes/edge-runtime/package.json +++ b/tests/runtimes/edge-runtime/package.json @@ -1,6 +1,6 @@ { "name": "edge-runtime-e2e", - "version": "3.10.1", + "version": "3.10.2", "private": true, "type": "module", "scripts": {