From 4c7fbd480214f1e2508fc9a520c571f6274dce8f Mon Sep 17 00:00:00 2001 From: Yiming Date: Fri, 12 Apr 2024 09:43:42 +0800 Subject: [PATCH 1/3] fix: model meta generator doesn't correctly identify relation names (#1244) --- packages/sdk/src/model-meta-generator.ts | 2 +- .../tests/regression/issue-1241.test.ts | 88 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/integration/tests/regression/issue-1241.test.ts diff --git a/packages/sdk/src/model-meta-generator.ts b/packages/sdk/src/model-meta-generator.ts index 99029e610..15e3abe40 100644 --- a/packages/sdk/src/model-meta-generator.ts +++ b/packages/sdk/src/model-meta-generator.ts @@ -206,7 +206,7 @@ function getBackLink(field: DataModelField) { } function getRelationName(field: DataModelField) { - const relAttr = field.attributes.find((attr) => attr.decl.ref?.name === 'relation'); + const relAttr = field.attributes.find((attr) => attr.decl.ref?.name === '@relation'); const relName = relAttr && relAttr.args?.[0] && getLiteral(relAttr.args?.[0].value); return relName; } diff --git a/tests/integration/tests/regression/issue-1241.test.ts b/tests/integration/tests/regression/issue-1241.test.ts new file mode 100644 index 000000000..3a53f567c --- /dev/null +++ b/tests/integration/tests/regression/issue-1241.test.ts @@ -0,0 +1,88 @@ +import { loadSchema } from '@zenstackhq/testtools'; +import { randomBytes } from 'crypto'; + +describe('issue 1241', () => { + it('regression', async () => { + const { enhance, prisma } = await loadSchema( + ` + model User { + id String @id @default(uuid()) + todos Todo[] + + @@auth + @@allow('all', true) + } + + model Todo { + id String @id @default(uuid()) + + user_id String + user User @relation(fields: [user_id], references: [id]) + + images File[] @relation("todo_images") + documents File[] @relation("todo_documents") + + @@allow('all', true) + } + + model File { + id String @id @default(uuid()) + s3_key String @unique + label String + + todo_image_id String? + todo_image Todo? @relation("todo_images", fields: [todo_image_id], references: [id]) + + todo_document_id String? + todo_document Todo? @relation("todo_documents", fields: [todo_document_id], references: [id]) + + @@allow('all', true) + } + `, + { logPrismaQuery: true } + ); + + const user = await prisma.user.create({ + data: {}, + }); + await prisma.todo.create({ + data: { + user_id: user.id, + + images: { + create: new Array(3).fill(null).map((_, i) => ({ + s3_key: randomBytes(8).toString('hex'), + label: `img-label-${i + 1}`, + })), + }, + + documents: { + create: new Array(3).fill(null).map((_, i) => ({ + s3_key: randomBytes(8).toString('hex'), + label: `doc-label-${i + 1}`, + })), + }, + }, + }); + + const db = enhance(); + + const todo = await db.todo.findFirst({ where: {}, include: { documents: true } }); + await expect( + db.todo.update({ + where: { id: todo.id }, + data: { + documents: { + update: todo.documents.map((doc: any) => { + return { + where: { s3_key: doc.s3_key }, + data: { label: 'updated' }, + }; + }), + }, + }, + include: { documents: true }, + }) + ).toResolveTruthy(); + }); +}); From 3e3e44c9de1d0afbaf02aebe70156ba778c2bfdf Mon Sep 17 00:00:00 2001 From: Yiming Date: Fri, 12 Apr 2024 09:44:13 +0800 Subject: [PATCH 2/3] chore: bump version (#1245) --- package.json | 2 +- packages/ide/jetbrains/build.gradle.kts | 2 +- packages/ide/jetbrains/package.json | 2 +- packages/language/package.json | 2 +- packages/misc/redwood/package.json | 2 +- packages/plugins/openapi/package.json | 2 +- packages/plugins/swr/package.json | 2 +- packages/plugins/tanstack-query/package.json | 2 +- packages/plugins/trpc/package.json | 2 +- packages/runtime/package.json | 2 +- packages/schema/package.json | 2 +- packages/sdk/package.json | 2 +- packages/server/package.json | 2 +- packages/testtools/package.json | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 627428d5e..bb6220a60 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zenstack-monorepo", - "version": "1.12.2", + "version": "1.12.3", "description": "", "scripts": { "build": "pnpm -r build", diff --git a/packages/ide/jetbrains/build.gradle.kts b/packages/ide/jetbrains/build.gradle.kts index f62ecdc48..b57e88259 100644 --- a/packages/ide/jetbrains/build.gradle.kts +++ b/packages/ide/jetbrains/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "dev.zenstack" -version = "1.12.2" +version = "1.12.3" repositories { mavenCentral() diff --git a/packages/ide/jetbrains/package.json b/packages/ide/jetbrains/package.json index 36c8a9dba..4770c785a 100644 --- a/packages/ide/jetbrains/package.json +++ b/packages/ide/jetbrains/package.json @@ -1,6 +1,6 @@ { "name": "jetbrains", - "version": "1.12.2", + "version": "1.12.3", "displayName": "ZenStack JetBrains IDE Plugin", "description": "ZenStack JetBrains IDE plugin", "homepage": "https://zenstack.dev", diff --git a/packages/language/package.json b/packages/language/package.json index e5d8f2c0a..9fe87f55b 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/language", - "version": "1.12.2", + "version": "1.12.3", "displayName": "ZenStack modeling language compiler", "description": "ZenStack modeling language compiler", "homepage": "https://zenstack.dev", diff --git a/packages/misc/redwood/package.json b/packages/misc/redwood/package.json index e4ccd33b2..dc1c5eb81 100644 --- a/packages/misc/redwood/package.json +++ b/packages/misc/redwood/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/redwood", "displayName": "ZenStack RedwoodJS Integration", - "version": "1.12.2", + "version": "1.12.3", "description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.", "repository": { "type": "git", diff --git a/packages/plugins/openapi/package.json b/packages/plugins/openapi/package.json index 2dbe524db..72a3d5109 100644 --- a/packages/plugins/openapi/package.json +++ b/packages/plugins/openapi/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/openapi", "displayName": "ZenStack Plugin and Runtime for OpenAPI", - "version": "1.12.2", + "version": "1.12.3", "description": "ZenStack plugin and runtime supporting OpenAPI", "main": "index.js", "repository": { diff --git a/packages/plugins/swr/package.json b/packages/plugins/swr/package.json index 4eada7fc4..68ebc3d0d 100644 --- a/packages/plugins/swr/package.json +++ b/packages/plugins/swr/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/swr", "displayName": "ZenStack plugin for generating SWR hooks", - "version": "1.12.2", + "version": "1.12.3", "description": "ZenStack plugin for generating SWR hooks", "main": "index.js", "repository": { diff --git a/packages/plugins/tanstack-query/package.json b/packages/plugins/tanstack-query/package.json index 2a7bb7bcb..13ed04b1e 100644 --- a/packages/plugins/tanstack-query/package.json +++ b/packages/plugins/tanstack-query/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/tanstack-query", "displayName": "ZenStack plugin for generating tanstack-query hooks", - "version": "1.12.2", + "version": "1.12.3", "description": "ZenStack plugin for generating tanstack-query hooks", "main": "index.js", "exports": { diff --git a/packages/plugins/trpc/package.json b/packages/plugins/trpc/package.json index e32edfdff..f8e7cd354 100644 --- a/packages/plugins/trpc/package.json +++ b/packages/plugins/trpc/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/trpc", "displayName": "ZenStack plugin for tRPC", - "version": "1.12.2", + "version": "1.12.3", "description": "ZenStack plugin for tRPC", "main": "index.js", "repository": { diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 3aa0543c0..c351314d9 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/runtime", "displayName": "ZenStack Runtime Library", - "version": "1.12.2", + "version": "1.12.3", "description": "Runtime of ZenStack for both client-side and server-side environments.", "repository": { "type": "git", diff --git a/packages/schema/package.json b/packages/schema/package.json index ea559e4ff..f2e62b4bc 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -3,7 +3,7 @@ "publisher": "zenstack", "displayName": "ZenStack Language Tools", "description": "Build scalable web apps with minimum code by defining authorization and validation rules inside the data schema that closer to the database", - "version": "1.12.2", + "version": "1.12.3", "author": { "name": "ZenStack Team" }, diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 16ffeac15..87e9bffb5 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/sdk", - "version": "1.12.2", + "version": "1.12.3", "description": "ZenStack plugin development SDK", "main": "index.js", "scripts": { diff --git a/packages/server/package.json b/packages/server/package.json index 862ea6f20..9cdfbeda5 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/server", - "version": "1.12.2", + "version": "1.12.3", "displayName": "ZenStack Server-side Adapters", "description": "ZenStack server-side adapters", "homepage": "https://zenstack.dev", diff --git a/packages/testtools/package.json b/packages/testtools/package.json index d3ec5018b..0b6907977 100644 --- a/packages/testtools/package.json +++ b/packages/testtools/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/testtools", - "version": "1.12.2", + "version": "1.12.3", "description": "ZenStack Test Tools", "main": "index.js", "private": true, From 813748160e35913f5b26b79b81886ab9ddb02070 Mon Sep 17 00:00:00 2001 From: Yiming Date: Fri, 12 Apr 2024 15:04:23 +0800 Subject: [PATCH 3/3] fix(openapi): `CreateManyArgs` does not take array as input (#1246) --- packages/plugins/openapi/src/rpc-generator.ts | 10 ++++++- .../tests/baseline/rpc-3.0.0.baseline.yaml | 30 +++++++++++++++++-- .../tests/baseline/rpc-3.1.0.baseline.yaml | 30 +++++++++++++++++-- .../rpc-type-coverage-3.0.0.baseline.yaml | 10 ++++++- .../rpc-type-coverage-3.1.0.baseline.yaml | 10 ++++++- .../schema/src/plugins/zod/transformer.ts | 2 +- 6 files changed, 82 insertions(+), 10 deletions(-) diff --git a/packages/plugins/openapi/src/rpc-generator.ts b/packages/plugins/openapi/src/rpc-generator.ts index 13bb91272..e9c66792c 100644 --- a/packages/plugins/openapi/src/rpc-generator.ts +++ b/packages/plugins/openapi/src/rpc-generator.ts @@ -176,7 +176,15 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase { type: 'object', required: ['data'], properties: { - data: this.ref(`${modelName}CreateManyInput`), + data: this.oneOf( + this.ref(`${modelName}CreateManyInput`), + this.array(this.ref(`${modelName}CreateManyInput`)) + ), + skipDuplicates: { + type: 'boolean', + description: + 'Do not insert records with unique fields or ID fields that already exist.', + }, meta: this.ref('_Meta'), }, }, diff --git a/packages/plugins/openapi/tests/baseline/rpc-3.0.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rpc-3.0.0.baseline.yaml index 0f936771e..eacf4d6d7 100644 --- a/packages/plugins/openapi/tests/baseline/rpc-3.0.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rpc-3.0.0.baseline.yaml @@ -3194,7 +3194,15 @@ components: - data properties: data: - $ref: '#/components/schemas/UserCreateManyInput' + oneOf: + - $ref: '#/components/schemas/UserCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/UserCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' UserFindUniqueArgs: @@ -3374,7 +3382,15 @@ components: - data properties: data: - $ref: '#/components/schemas/ProfileCreateManyInput' + oneOf: + - $ref: '#/components/schemas/ProfileCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/ProfileCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' ProfileFindUniqueArgs: @@ -3554,7 +3570,15 @@ components: - data properties: data: - $ref: '#/components/schemas/Post_ItemCreateManyInput' + oneOf: + - $ref: '#/components/schemas/Post_ItemCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/Post_ItemCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' Post_ItemFindUniqueArgs: diff --git a/packages/plugins/openapi/tests/baseline/rpc-3.1.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rpc-3.1.0.baseline.yaml index d842234ab..9608839dd 100644 --- a/packages/plugins/openapi/tests/baseline/rpc-3.1.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rpc-3.1.0.baseline.yaml @@ -3248,7 +3248,15 @@ components: - data properties: data: - $ref: '#/components/schemas/UserCreateManyInput' + oneOf: + - $ref: '#/components/schemas/UserCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/UserCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' UserFindUniqueArgs: @@ -3428,7 +3436,15 @@ components: - data properties: data: - $ref: '#/components/schemas/ProfileCreateManyInput' + oneOf: + - $ref: '#/components/schemas/ProfileCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/ProfileCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' ProfileFindUniqueArgs: @@ -3608,7 +3624,15 @@ components: - data properties: data: - $ref: '#/components/schemas/Post_ItemCreateManyInput' + oneOf: + - $ref: '#/components/schemas/Post_ItemCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/Post_ItemCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' Post_ItemFindUniqueArgs: diff --git a/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.0.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.0.0.baseline.yaml index 6e219e6e3..bcb1e6d4c 100644 --- a/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.0.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.0.0.baseline.yaml @@ -2017,7 +2017,15 @@ components: - data properties: data: - $ref: '#/components/schemas/FooCreateManyInput' + oneOf: + - $ref: '#/components/schemas/FooCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/FooCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' FooFindUniqueArgs: diff --git a/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.1.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.1.0.baseline.yaml index e777f7580..21524fad5 100644 --- a/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.1.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.1.0.baseline.yaml @@ -2049,7 +2049,15 @@ components: - data properties: data: - $ref: '#/components/schemas/FooCreateManyInput' + oneOf: + - $ref: '#/components/schemas/FooCreateManyInput' + - type: array + items: + $ref: '#/components/schemas/FooCreateManyInput' + skipDuplicates: + type: boolean + description: Do not insert records with unique fields or ID fields that already + exist. meta: $ref: '#/components/schemas/_Meta' FooFindUniqueArgs: diff --git a/packages/schema/src/plugins/zod/transformer.ts b/packages/schema/src/plugins/zod/transformer.ts index 878eff82b..e5066c1e4 100644 --- a/packages/schema/src/plugins/zod/transformer.ts +++ b/packages/schema/src/plugins/zod/transformer.ts @@ -485,7 +485,7 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`; imports.push( `import { ${modelName}CreateManyInputObjectSchema } from '../objects/${modelName}CreateManyInput.schema'` ); - codeBody += `createMany: z.object({ data: z.union([${modelName}CreateManyInputObjectSchema, z.array(${modelName}CreateManyInputObjectSchema)]) }),`; + codeBody += `createMany: z.object({ data: z.union([${modelName}CreateManyInputObjectSchema, z.array(${modelName}CreateManyInputObjectSchema)]), skipDuplicates: z.boolean().optional() }),`; operations.push(['createMany', origModelName]); }