From fa0dafb98f2f9c034731f380ac190e048d0c0d3f Mon Sep 17 00:00:00 2001 From: Yiming Date: Sun, 11 Feb 2024 22:05:45 +0800 Subject: [PATCH 01/17] fix: incorrect validation errors for abstract models with validation rules (#991) --- .../validator/datamodel-validator.ts | 22 ++++---- .../validation/attribute-validation.test.ts | 9 ++++ .../validation/datamodel-validation.test.ts | 13 +---- .../tests/regression/issue-965.test.ts | 53 +++++++++++++++++++ 4 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 tests/integration/tests/regression/issue-965.test.ts diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index ce1886f5e..3096d5257 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -6,7 +6,7 @@ import { isStringLiteral, ReferenceExpr, } from '@zenstackhq/language/ast'; -import { analyzePolicies, getLiteral, getModelIdFields, getModelUniqueFields } from '@zenstackhq/sdk'; +import { getLiteral, getModelIdFields, getModelUniqueFields } from '@zenstackhq/sdk'; import { AstNode, DiagnosticInfo, getDocument, ValidationAcceptor } from 'langium'; import { IssueCodes, SCALAR_TYPES } from '../constants'; import { AstValidator } from '../types'; @@ -34,23 +34,19 @@ export default class DataModelValidator implements AstValidator { const modelUniqueFields = getModelUniqueFields(dm); if ( + !dm.isAbstract && idFields.length === 0 && modelLevelIds.length === 0 && uniqueFields.length === 0 && modelUniqueFields.length === 0 ) { - const { allows, denies, hasFieldValidation } = analyzePolicies(dm); - if (allows.length > 0 || denies.length > 0 || hasFieldValidation) { - // TODO: relax this requirement to require only @unique fields - // when access policies or field valdaition is used, require an @id field - accept( - 'error', - 'Model must include a field with @id or @unique attribute, or a model-level @@id or @@unique attribute to use access policies', - { - node: dm, - } - ); - } + accept( + 'error', + 'Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.', + { + node: dm, + } + ); } else if (idFields.length > 0 && modelLevelIds.length > 0) { accept('error', 'Model cannot have both field-level @id and model-level @@id attributes', { node: dm, diff --git a/packages/schema/tests/schema/validation/attribute-validation.test.ts b/packages/schema/tests/schema/validation/attribute-validation.test.ts index 8b7886334..6d8d02b14 100644 --- a/packages/schema/tests/schema/validation/attribute-validation.test.ts +++ b/packages/schema/tests/schema/validation/attribute-validation.test.ts @@ -251,6 +251,7 @@ describe('Attribute tests', () => { ${prelude} model _String { + id String @id _string String @db.String _string1 String @db.String(1) _text String @db.Text @@ -275,6 +276,7 @@ describe('Attribute tests', () => { } model _Boolean { + id String @id _boolean Boolean @db.Boolean _bit Boolean @db.Bit _bit1 Boolean @db.Bit(1) @@ -283,6 +285,7 @@ describe('Attribute tests', () => { } model _Int { + id String @id _int Int @db.Int _integer Int @db.Integer _smallInt Int @db.SmallInt @@ -298,12 +301,14 @@ describe('Attribute tests', () => { } model _BigInt { + id String @id _bigInt BigInt @db.BigInt _unsignedBigInt BigInt @db.UnsignedBigInt _int8 BigInt @db.Int8 } model _FloatDecimal { + id String @id _float Float @db.Float _decimal Decimal @db.Decimal _decimal1 Decimal @db.Decimal(10, 2) @@ -318,6 +323,7 @@ describe('Attribute tests', () => { } model _DateTime { + id String @id _dateTime DateTime @db.DateTime _dateTime2 DateTime @db.DateTime2 _smallDateTime DateTime @db.SmallDateTime @@ -334,11 +340,13 @@ describe('Attribute tests', () => { } model _Json { + id String @id _json Json @db.Json _jsonb Json @db.JsonB } model _Bytes { + id String @id _bytes Bytes @db.Bytes _byteA Bytes @db.ByteA _longBlob Bytes @db.LongBlob @@ -1118,6 +1126,7 @@ describe('Attribute tests', () => { } model M { + id String @id e E @default(E1) } `); diff --git a/packages/schema/tests/schema/validation/datamodel-validation.test.ts b/packages/schema/tests/schema/validation/datamodel-validation.test.ts index e1f06d268..4212441fe 100644 --- a/packages/schema/tests/schema/validation/datamodel-validation.test.ts +++ b/packages/schema/tests/schema/validation/datamodel-validation.test.ts @@ -120,16 +120,8 @@ describe('Data Model Validation Tests', () => { }); it('id field', async () => { - // no need for '@id' field when there's no access policy or field validation - await loadModel(` - ${prelude} - model M { - x Int - } - `); - const err = - 'Model must include a field with @id or @unique attribute, or a model-level @@id or @@unique attribute to use access policies'; + 'Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.'; expect( await loadModelWithError(` @@ -630,9 +622,8 @@ describe('Data Model Validation Tests', () => { b String } `); - expect(errors.length).toBe(1); - expect(errors[0]).toEqual(`Model A cannot be extended because it's not abstract`); + expect(errors).toContain(`Model A cannot be extended because it's not abstract`); // relation incomplete from multiple level inheritance expect( diff --git a/tests/integration/tests/regression/issue-965.test.ts b/tests/integration/tests/regression/issue-965.test.ts new file mode 100644 index 000000000..79bd92075 --- /dev/null +++ b/tests/integration/tests/regression/issue-965.test.ts @@ -0,0 +1,53 @@ +import { loadModel, loadModelWithError } from '@zenstackhq/testtools'; + +describe('Regression: issue 965', () => { + it('regression1', async () => { + await loadModel(` + abstract model Base { + id String @id @default(cuid()) + } + + abstract model A { + URL String? @url + } + + abstract model B { + anotherURL String? @url + } + + abstract model C { + oneMoreURL String? @url + } + + model D extends Base, A, B { + } + + model E extends Base, B, C { + }`); + }); + + it('regression2', async () => { + await expect( + loadModelWithError(` + abstract model A { + URL String? @url + } + + abstract model B { + anotherURL String? @url + } + + abstract model C { + oneMoreURL String? @url + } + + model D extends A, B { + } + + model E extends B, C { + }`) + ).resolves.toContain( + 'Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.' + ); + }); +}); From da3388190020041965ff104a346f932a8d32b59d Mon Sep 17 00:00:00 2001 From: Yiming Date: Sun, 11 Feb 2024 22:30:13 +0800 Subject: [PATCH 02/17] fix: Zmodel linker doesn't recursively visit base types when building resolution scopes (#992) --- .../src/language-server/zmodel-linker.ts | 15 ++++++++---- .../tests/regression/issue-971.test.ts | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 tests/integration/tests/regression/issue-971.test.ts diff --git a/packages/schema/src/language-server/zmodel-linker.ts b/packages/schema/src/language-server/zmodel-linker.ts index ef97cf4b6..69fdf67c2 100644 --- a/packages/schema/src/language-server/zmodel-linker.ts +++ b/packages/schema/src/language-server/zmodel-linker.ts @@ -519,12 +519,17 @@ export class ZModelLinker extends DefaultLinker { private resolveDataModel(node: DataModel, document: LangiumDocument, extraScopes: ScopeProvider[]) { if (node.superTypes.length > 0) { - const providers = node.superTypes.map( - (superType) => (name: string) => superType.ref?.fields.find((f) => f.name === name) - ); - extraScopes = [...providers, ...extraScopes]; + const superTypeProviders: ScopeProvider[] = []; + // build scope providers for super types recursively with breadth-first search + const queue = node.superTypes.map((t) => t.ref!); + while (queue.length > 0) { + const superType = queue.shift()!; + const provider = (name: string) => superType.fields.find((f) => f.name === name); + superTypeProviders.push(provider); + queue.push(...superType.superTypes.map((t) => t.ref!)); + } + extraScopes = [...superTypeProviders, ...extraScopes]; } - return this.resolveDefault(node, document, extraScopes); } diff --git a/tests/integration/tests/regression/issue-971.test.ts b/tests/integration/tests/regression/issue-971.test.ts new file mode 100644 index 000000000..40990aa6a --- /dev/null +++ b/tests/integration/tests/regression/issue-971.test.ts @@ -0,0 +1,23 @@ +import { loadSchema } from '@zenstackhq/testtools'; + +describe('Regression: issue 971', () => { + it('regression', async () => { + await loadSchema( + ` + abstract model Level1 { + id String @id @default(cuid()) + URL String? + @@validate(URL != null, "URL must be provided") // works + } + abstract model Level2 extends Level1 { + @@validate(URL != null, "URL must be provided") // works + } + abstract model Level3 extends Level2 { + @@validate(URL != null, "URL must be provided") // doesn't work + } + model Foo extends Level3 { + } + ` + ); + }); +}); From a4d3f15746269257bc7fb56332766e3f598e2996 Mon Sep 17 00:00:00 2001 From: Yiming Date: Sun, 11 Feb 2024 23:24:07 +0800 Subject: [PATCH 03/17] fix: @omit doesn't remove fields inside to-many relation (#993) --- packages/runtime/src/enhancements/omit.ts | 15 ++++-- .../enhancements/with-omit/with-omit.test.ts | 48 +++++++++++++++++++ .../tests/regression/issue-992.test.ts | 45 +++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/integration/tests/regression/issue-992.test.ts diff --git a/packages/runtime/src/enhancements/omit.ts b/packages/runtime/src/enhancements/omit.ts index 8b2937845..2df81b40a 100644 --- a/packages/runtime/src/enhancements/omit.ts +++ b/packages/runtime/src/enhancements/omit.ts @@ -54,11 +54,18 @@ class OmitHandler extends DefaultPrismaProxyHandler { continue; } - if (fieldInfo.attributes?.find((attr) => attr.name === '@omit')) { + const shouldOmit = fieldInfo.attributes?.find((attr) => attr.name === '@omit'); + if (shouldOmit) { delete entityData[field]; - } else if (fieldInfo.isDataModel) { - // recurse - await this.doPostProcess(entityData[field], fieldInfo.type); + } + + if (fieldInfo.isDataModel) { + const items = + fieldInfo.isArray && Array.isArray(entityData[field]) ? entityData[field] : [entityData[field]]; + for (const item of items) { + // recurse + await this.doPostProcess(item, fieldInfo.type); + } } } } diff --git a/tests/integration/tests/enhancements/with-omit/with-omit.test.ts b/tests/integration/tests/enhancements/with-omit/with-omit.test.ts index 61d44b440..e2db0a3f8 100644 --- a/tests/integration/tests/enhancements/with-omit/with-omit.test.ts +++ b/tests/integration/tests/enhancements/with-omit/with-omit.test.ts @@ -105,4 +105,52 @@ describe('Omit test', () => { expect(r1.password).toBeUndefined(); expect(r1.profile.image).toBeUndefined(); }); + + it('to-many', async () => { + const { withOmit } = await loadSchema( + ` + model User { + id String @id @default(cuid()) + posts Post[] + + @@allow('all', true) + } + + model Post { + id String @id @default(cuid()) + user User @relation(fields: [userId], references: [id]) + userId String + images Image[] + + @@allow('all', true) + } + + model Image { + id String @id @default(cuid()) + post Post @relation(fields: [postId], references: [id]) + postId String + url String @omit + + @@allow('all', true) + } + ` + ); + + const db = withOmit(); + const r = await db.user.create({ + include: { posts: { include: { images: true } } }, + data: { + posts: { + create: [ + { images: { create: { url: 'img1' } } }, + { images: { create: [{ url: 'img2' }, { url: 'img3' }] } }, + ], + }, + }, + }); + + expect(r.posts[0].images[0].url).toBeUndefined(); + expect(r.posts[1].images[0].url).toBeUndefined(); + expect(r.posts[1].images[1].url).toBeUndefined(); + }); }); diff --git a/tests/integration/tests/regression/issue-992.test.ts b/tests/integration/tests/regression/issue-992.test.ts new file mode 100644 index 000000000..40a1aac47 --- /dev/null +++ b/tests/integration/tests/regression/issue-992.test.ts @@ -0,0 +1,45 @@ +import { loadSchema } from '@zenstackhq/testtools'; + +describe('Regression: issue 992', () => { + it('regression', async () => { + const { enhance, prisma } = await loadSchema( + ` + model Product { + id String @id @default(cuid()) + category Category @relation(fields: [categoryId], references: [id]) + categoryId String + + deleted Int @default(0) @omit + @@deny('read', deleted != 0) + @@allow('all', true) + } + + model Category { + id String @id @default(cuid()) + products Product[] + @@allow('all', true) + } + ` + ); + + await prisma.category.create({ + data: { + products: { + create: [ + { + deleted: 0, + }, + { + deleted: 0, + }, + ], + }, + }, + }); + + const db = enhance(); + const category = await db.category.findFirst({ include: { products: true } }); + expect(category.products[0].deleted).toBeUndefined(); + expect(category.products[1].deleted).toBeUndefined(); + }); +}); From d5303676e44d24460c1c232a3954778175884156 Mon Sep 17 00:00:00 2001 From: Yiming Date: Sun, 11 Feb 2024 23:24:30 +0800 Subject: [PATCH 04/17] chore: bump version (#994) --- 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 16b18ea0a..3c08bc9c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zenstack-monorepo", - "version": "1.8.2", + "version": "1.9.1", "description": "", "scripts": { "build": "pnpm -r build", diff --git a/packages/ide/jetbrains/build.gradle.kts b/packages/ide/jetbrains/build.gradle.kts index 2643f4e2a..b3074746e 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.8.2" +version = "1.9.0" repositories { mavenCentral() diff --git a/packages/ide/jetbrains/package.json b/packages/ide/jetbrains/package.json index ca7e7c57e..b08392ade 100644 --- a/packages/ide/jetbrains/package.json +++ b/packages/ide/jetbrains/package.json @@ -1,6 +1,6 @@ { "name": "jetbrains", - "version": "1.8.2", + "version": "1.9.1", "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 ff2cfd5ec..1d06a04c7 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/language", - "version": "1.8.2", + "version": "1.9.1", "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 47cddae1b..011e0dd64 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.8.2", + "version": "1.9.1", "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 a910ab0e8..6584ad022 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.8.2", + "version": "1.9.1", "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 223433bec..351048b69 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.8.2", + "version": "1.9.1", "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 3d5a6d94b..ffbfd7bc0 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.8.2", + "version": "1.9.1", "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 6620e365e..215039ba0 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.8.2", + "version": "1.9.1", "description": "ZenStack plugin for tRPC", "main": "index.js", "repository": { diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 4c0125474..494cf907f 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/runtime", "displayName": "ZenStack Runtime Library", - "version": "1.8.2", + "version": "1.9.1", "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 d7726b3be..7fc652b14 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.8.2", + "version": "1.9.1", "author": { "name": "ZenStack Team" }, diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 8fa8cf619..b4ccab542 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/sdk", - "version": "1.8.2", + "version": "1.9.1", "description": "ZenStack plugin development SDK", "main": "index.js", "scripts": { diff --git a/packages/server/package.json b/packages/server/package.json index f4b4b68ab..af441d4f1 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/server", - "version": "1.8.2", + "version": "1.9.1", "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 107b4d659..8acf19707 100644 --- a/packages/testtools/package.json +++ b/packages/testtools/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/testtools", - "version": "1.8.2", + "version": "1.9.1", "description": "ZenStack Test Tools", "main": "index.js", "private": true, From 541cd973081cbbf2d9e2e571ee8f971bc859150c Mon Sep 17 00:00:00 2001 From: Yiming Date: Mon, 12 Feb 2024 19:42:18 +0800 Subject: [PATCH 05/17] fix: supports for complex usage of "@@index" in zmodel (#995) --- package.json | 2 +- packages/ide/jetbrains/package.json | 2 +- packages/language/package.json | 6 +- packages/language/src/generated/ast.ts | 53 ++++++---- packages/language/src/generated/grammar.ts | 97 ++++++------------- packages/language/src/generated/module.ts | 8 +- packages/language/src/zmodel.langium | 6 +- packages/language/syntaxes/zmodel.tmLanguage | 2 +- .../language/syntaxes/zmodel.tmLanguage.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 | 4 +- .../function-invocation-validator.ts | 1 + .../zmodel-completion-provider.ts | 10 +- .../src/language-server/zmodel-module.ts | 7 ++ .../src/plugins/prisma/prisma-builder.ts | 6 +- .../src/plugins/prisma/schema-generator.ts | 8 +- packages/schema/src/res/stdlib.zmodel | 97 ++++++++++++++++++- .../tests/generator/prisma-builder.test.ts | 2 +- .../schema/tests/schema/all-features.zmodel | 4 +- packages/schema/tests/schema/parser.test.ts | 2 - .../validation/attribute-validation.test.ts | 10 +- packages/sdk/package.json | 2 +- packages/sdk/src/constants.ts | 1 + packages/sdk/src/zmodel-code-generator.ts | 8 +- packages/server/package.json | 2 +- packages/testtools/package.json | 4 +- pnpm-lock.yaml | 38 +++++--- 32 files changed, 245 insertions(+), 151 deletions(-) diff --git a/package.json b/package.json index 3c08bc9c3..2d2d2e264 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zenstack-monorepo", - "version": "1.9.1", + "version": "1.9.0", "description": "", "scripts": { "build": "pnpm -r build", diff --git a/packages/ide/jetbrains/package.json b/packages/ide/jetbrains/package.json index b08392ade..7305853c2 100644 --- a/packages/ide/jetbrains/package.json +++ b/packages/ide/jetbrains/package.json @@ -1,6 +1,6 @@ { "name": "jetbrains", - "version": "1.9.1", + "version": "1.9.0", "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 1d06a04c7..a80768913 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/language", - "version": "1.9.1", + "version": "1.9.0", "displayName": "ZenStack modeling language compiler", "description": "ZenStack modeling language compiler", "homepage": "https://zenstack.dev", @@ -19,11 +19,11 @@ "author": "ZenStack Team", "license": "MIT", "devDependencies": { - "langium-cli": "1.2.0", + "langium-cli": "1.3.1", "plist2": "^1.1.3" }, "dependencies": { - "langium": "1.2.0" + "langium": "1.3.1" }, "contributes": { "languages": [ diff --git a/packages/language/src/generated/ast.ts b/packages/language/src/generated/ast.ts index 7463fb9da..a95a748d9 100644 --- a/packages/language/src/generated/ast.ts +++ b/packages/language/src/generated/ast.ts @@ -1,10 +1,24 @@ /****************************************************************************** - * This file was generated by langium-cli 1.2.0. + * This file was generated by langium-cli 1.3.1. * DO NOT EDIT MANUALLY! ******************************************************************************/ /* eslint-disable */ -import { AstNode, AbstractAstReflection, Reference, ReferenceInfo, TypeMetaData } from 'langium'; +import type { AstNode, Reference, ReferenceInfo, TypeMetaData } from 'langium'; +import { AbstractAstReflection } from 'langium'; + +export const ZModelTerminals = { + WS: /\s+/, + INTERNAL_ATTRIBUTE_NAME: /@@@([_a-zA-Z][\w_]*\.)*[_a-zA-Z][\w_]*/, + MODEL_ATTRIBUTE_NAME: /@@([_a-zA-Z][\w_]*\.)*[_a-zA-Z][\w_]*/, + FIELD_ATTRIBUTE_NAME: /@([_a-zA-Z][\w_]*\.)*[_a-zA-Z][\w_]*/, + ID: /[_a-zA-Z][\w_]*/, + STRING: /"(\\.|[^"\\])*"|'(\\.|[^'\\])*'/, + NUMBER: /[+-]?[0-9]+(\.[0-9]+)?/, + TRIPLE_SLASH_COMMENT: /\/\/\/[^\n\r]*/, + ML_COMMENT: /\/\*[\s\S]*?\*\//, + SL_COMMENT: /\/\/[^\n\r]*/, +}; export type AbstractDeclaration = Attribute | DataModel | DataSource | Enum | FunctionDecl | GeneratorDecl | Plugin; @@ -64,10 +78,10 @@ export function isReferenceTarget(item: unknown): item is ReferenceTarget { return reflection.isInstance(item, ReferenceTarget); } -export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'sort' | 'view' | string; +export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'view' | string; export function isRegularID(item: unknown): item is RegularID { - return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'sort' || item === 'view' || item === 'import' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item))); + return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'view' || item === 'import' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item))); } export type TypeDeclaration = DataModel | Enum; @@ -81,7 +95,6 @@ export function isTypeDeclaration(item: unknown): item is TypeDeclaration { export interface Argument extends AstNode { readonly $container: InvocationExpr; readonly $type: 'Argument'; - name?: RegularID value: Expression } @@ -92,7 +105,7 @@ export function isArgument(item: unknown): item is Argument { } export interface ArrayExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'ArrayExpr'; items: Array } @@ -163,7 +176,7 @@ export function isAttributeParamType(item: unknown): item is AttributeParamType } export interface BinaryExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'BinaryExpr'; left: Expression operator: '!' | '!=' | '&&' | '<' | '<=' | '==' | '>' | '>=' | '?' | '^' | 'in' | '||' @@ -177,7 +190,7 @@ export function isBinaryExpr(item: unknown): item is BinaryExpr { } export interface BooleanLiteral extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'BooleanLiteral'; value: Boolean } @@ -189,7 +202,7 @@ export function isBooleanLiteral(item: unknown): item is BooleanLiteral { } export interface ConfigArrayExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'ConfigArrayExpr'; items: Array } @@ -440,7 +453,7 @@ export function isInternalAttribute(item: unknown): item is InternalAttribute { } export interface InvocationExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'InvocationExpr'; args: Array function: Reference @@ -453,7 +466,7 @@ export function isInvocationExpr(item: unknown): item is InvocationExpr { } export interface MemberAccessExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'MemberAccessExpr'; member: Reference operand: Expression @@ -490,7 +503,7 @@ export function isModelImport(item: unknown): item is ModelImport { } export interface NullExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'NullExpr'; value: 'null' } @@ -502,7 +515,7 @@ export function isNullExpr(item: unknown): item is NullExpr { } export interface NumberLiteral extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'NumberLiteral'; value: string } @@ -514,7 +527,7 @@ export function isNumberLiteral(item: unknown): item is NumberLiteral { } export interface ObjectExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'ObjectExpr'; fields: Array } @@ -554,8 +567,8 @@ export function isPluginField(item: unknown): item is PluginField { export interface ReferenceArg extends AstNode { readonly $container: ReferenceExpr; readonly $type: 'ReferenceArg'; - name: 'sort' - value: 'Asc' | 'Desc' + name: string + value: Expression } export const ReferenceArg = 'ReferenceArg'; @@ -565,7 +578,7 @@ export function isReferenceArg(item: unknown): item is ReferenceArg { } export interface ReferenceExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'ReferenceExpr'; args: Array target: Reference @@ -578,7 +591,7 @@ export function isReferenceExpr(item: unknown): item is ReferenceExpr { } export interface StringLiteral extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'StringLiteral'; value: string } @@ -590,7 +603,7 @@ export function isStringLiteral(item: unknown): item is StringLiteral { } export interface ThisExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'ThisExpr'; value: 'this' } @@ -602,7 +615,7 @@ export function isThisExpr(item: unknown): item is ThisExpr { } export interface UnaryExpr extends AstNode { - readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType; + readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType; readonly $type: 'UnaryExpr'; operand: Expression operator: '!' diff --git a/packages/language/src/generated/grammar.ts b/packages/language/src/generated/grammar.ts index 5dbe02014..45aa3ff97 100644 --- a/packages/language/src/generated/grammar.ts +++ b/packages/language/src/generated/grammar.ts @@ -1,9 +1,10 @@ /****************************************************************************** - * This file was generated by langium-cli 1.2.0. + * This file was generated by langium-cli 1.3.1. * DO NOT EDIT MANUALLY! ******************************************************************************/ -import { loadGrammarFromJson, Grammar } from 'langium'; +import type { Grammar } from 'langium'; +import { loadGrammarFromJson } from 'langium'; let loadedZModelGrammar: Grammar | undefined; export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModelGrammar = loadGrammarFromJson(`{ @@ -1052,8 +1053,11 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "feature": "name", "operator": "=", "terminal": { - "$type": "Keyword", - "value": "sort" + "$type": "RuleCall", + "rule": { + "$ref": "#/rules@62" + }, + "arguments": [] } }, { @@ -1065,17 +1069,11 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "feature": "value", "operator": "=", "terminal": { - "$type": "Alternatives", - "elements": [ - { - "$type": "Keyword", - "value": "Asc" - }, - { - "$type": "Keyword", - "value": "Desc" - } - ] + "$type": "RuleCall", + "rule": { + "$ref": "#/rules@8" + }, + "arguments": [] } } ] @@ -1865,43 +1863,16 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "$type": "ParserRule", "name": "Argument", "definition": { - "$type": "Group", - "elements": [ - { - "$type": "Group", - "elements": [ - { - "$type": "Assignment", - "feature": "name", - "operator": "=", - "terminal": { - "$type": "RuleCall", - "rule": { - "$ref": "#/rules@46" - }, - "arguments": [] - } - }, - { - "$type": "Keyword", - "value": ":" - } - ], - "cardinality": "?" + "$type": "Assignment", + "feature": "value", + "operator": "=", + "terminal": { + "$type": "RuleCall", + "rule": { + "$ref": "#/rules@8" }, - { - "$type": "Assignment", - "feature": "value", - "operator": "=", - "terminal": { - "$type": "RuleCall", - "rule": { - "$ref": "#/rules@8" - }, - "arguments": [] - } - } - ] + "arguments": [] + } }, "definesHiddenTokens": false, "entry": false, @@ -2723,10 +2694,6 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "$type": "Keyword", "value": "in" }, - { - "$type": "Keyword", - "value": "sort" - }, { "$type": "Keyword", "value": "view" @@ -3452,7 +3419,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "WS", "definition": { "$type": "RegexToken", - "regex": "\\\\s+" + "regex": "/\\\\s+/" }, "fragment": false }, @@ -3461,7 +3428,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "INTERNAL_ATTRIBUTE_NAME", "definition": { "$type": "RegexToken", - "regex": "@@@([_a-zA-Z][\\\\w_]*\\\\.)*[_a-zA-Z][\\\\w_]*" + "regex": "/@@@([_a-zA-Z][\\\\w_]*\\\\.)*[_a-zA-Z][\\\\w_]*/" }, "fragment": false, "hidden": false @@ -3471,7 +3438,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "MODEL_ATTRIBUTE_NAME", "definition": { "$type": "RegexToken", - "regex": "@@([_a-zA-Z][\\\\w_]*\\\\.)*[_a-zA-Z][\\\\w_]*" + "regex": "/@@([_a-zA-Z][\\\\w_]*\\\\.)*[_a-zA-Z][\\\\w_]*/" }, "fragment": false, "hidden": false @@ -3481,7 +3448,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "FIELD_ATTRIBUTE_NAME", "definition": { "$type": "RegexToken", - "regex": "@([_a-zA-Z][\\\\w_]*\\\\.)*[_a-zA-Z][\\\\w_]*" + "regex": "/@([_a-zA-Z][\\\\w_]*\\\\.)*[_a-zA-Z][\\\\w_]*/" }, "fragment": false, "hidden": false @@ -3491,7 +3458,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "ID", "definition": { "$type": "RegexToken", - "regex": "[_a-zA-Z][\\\\w_]*" + "regex": "/[_a-zA-Z][\\\\w_]*/" }, "fragment": false, "hidden": false @@ -3501,7 +3468,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "STRING", "definition": { "$type": "RegexToken", - "regex": "\\"(\\\\\\\\.|[^\\"\\\\\\\\])*\\"|'(\\\\\\\\.|[^'\\\\\\\\])*'" + "regex": "/\\"(\\\\\\\\.|[^\\"\\\\\\\\])*\\"|'(\\\\\\\\.|[^'\\\\\\\\])*'/" }, "fragment": false, "hidden": false @@ -3511,7 +3478,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "NUMBER", "definition": { "$type": "RegexToken", - "regex": "[+-]?[0-9]+(\\\\.[0-9]+)?" + "regex": "/[+-]?[0-9]+(\\\\.[0-9]+)?/" }, "fragment": false, "hidden": false @@ -3521,7 +3488,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "TRIPLE_SLASH_COMMENT", "definition": { "$type": "RegexToken", - "regex": "\\\\/\\\\/\\\\/[^\\\\n\\\\r]*" + "regex": "/\\\\/\\\\/\\\\/[^\\\\n\\\\r]*/" }, "fragment": false, "hidden": false @@ -3532,7 +3499,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "ML_COMMENT", "definition": { "$type": "RegexToken", - "regex": "\\\\/\\\\*[\\\\s\\\\S]*?\\\\*\\\\/" + "regex": "/\\\\/\\\\*[\\\\s\\\\S]*?\\\\*\\\\//" }, "fragment": false }, @@ -3542,7 +3509,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel "name": "SL_COMMENT", "definition": { "$type": "RegexToken", - "regex": "\\\\/\\\\/[^\\\\n\\\\r]*" + "regex": "/\\\\/\\\\/[^\\\\n\\\\r]*/" }, "fragment": false } diff --git a/packages/language/src/generated/module.ts b/packages/language/src/generated/module.ts index ac0995108..b96dd1dee 100644 --- a/packages/language/src/generated/module.ts +++ b/packages/language/src/generated/module.ts @@ -1,17 +1,17 @@ /****************************************************************************** - * This file was generated by langium-cli 1.2.0. + * This file was generated by langium-cli 1.3.1. * DO NOT EDIT MANUALLY! ******************************************************************************/ -import { LangiumGeneratedServices, LangiumGeneratedSharedServices, LangiumSharedServices, LangiumServices, LanguageMetaData, Module } from 'langium'; +import type { LangiumGeneratedServices, LangiumGeneratedSharedServices, LangiumSharedServices, LangiumServices, LanguageMetaData, Module } from 'langium'; import { ZModelAstReflection } from './ast'; import { ZModelGrammar } from './grammar'; -export const ZModelLanguageMetaData: LanguageMetaData = { +export const ZModelLanguageMetaData = { languageId: 'zmodel', fileExtensions: ['.zmodel'], caseInsensitive: false -}; +} as const satisfies LanguageMetaData; export const ZModelGeneratedSharedModule: Module = { AstReflection: () => new ZModelAstReflection() diff --git a/packages/language/src/zmodel.langium b/packages/language/src/zmodel.langium index da445c792..8fcc72c34 100644 --- a/packages/language/src/zmodel.langium +++ b/packages/language/src/zmodel.langium @@ -80,7 +80,7 @@ fragment ReferenceArgList: args+=ReferenceArg (',' args+=ReferenceArg)*; ReferenceArg: - name=('sort') ':' value=('Asc' | 'Desc'); + name=ID ':' value=Expression; ObjectExpr: @@ -172,7 +172,7 @@ fragment ArgumentList: args+=Argument (',' args+=Argument)*; Argument: - (name=RegularID ':')? value=Expression; + value=Expression; // model DataModel: @@ -224,7 +224,7 @@ FunctionParamType: // https://github.com/langium/langium/discussions/1012 RegularID returns string: // include keywords that we'd like to work as ID in most places - ID | 'model' | 'enum' | 'attribute' | 'datasource' | 'plugin' | 'abstract' | 'in' | 'sort' | 'view' | 'import'; + ID | 'model' | 'enum' | 'attribute' | 'datasource' | 'plugin' | 'abstract' | 'in' | 'view' | 'import'; // attribute Attribute: diff --git a/packages/language/syntaxes/zmodel.tmLanguage b/packages/language/syntaxes/zmodel.tmLanguage index cf70fb761..6102b919d 100644 --- a/packages/language/syntaxes/zmodel.tmLanguage +++ b/packages/language/syntaxes/zmodel.tmLanguage @@ -20,7 +20,7 @@ name keyword.control.zmodel match - \b(Any|Asc|BigInt|Boolean|Bytes|ContextType|DateTime|Decimal|Desc|FieldReference|Float|Int|Json|Null|Object|String|TransitiveFieldReference|Unsupported|abstract|attribute|datasource|enum|extends|false|function|generator|import|in|model|null|plugin|sort|this|true|view)\b + \b(Any|BigInt|Boolean|Bytes|ContextType|DateTime|Decimal|FieldReference|Float|Int|Json|Null|Object|String|TransitiveFieldReference|Unsupported|abstract|attribute|datasource|enum|extends|false|function|generator|import|in|model|null|plugin|this|true|view)\b name diff --git a/packages/language/syntaxes/zmodel.tmLanguage.json b/packages/language/syntaxes/zmodel.tmLanguage.json index 00c737c97..aad6a38c7 100644 --- a/packages/language/syntaxes/zmodel.tmLanguage.json +++ b/packages/language/syntaxes/zmodel.tmLanguage.json @@ -10,7 +10,7 @@ }, { "name": "keyword.control.zmodel", - "match": "\\b(Any|Asc|BigInt|Boolean|Bytes|ContextType|DateTime|Decimal|Desc|FieldReference|Float|Int|Json|Null|Object|String|TransitiveFieldReference|Unsupported|abstract|attribute|datasource|enum|extends|false|function|generator|import|in|model|null|plugin|sort|this|true|view)\\b" + "match": "\\b(Any|BigInt|Boolean|Bytes|ContextType|DateTime|Decimal|FieldReference|Float|Int|Json|Null|Object|String|TransitiveFieldReference|Unsupported|abstract|attribute|datasource|enum|extends|false|function|generator|import|in|model|null|plugin|this|true|view)\\b" }, { "name": "string.quoted.double.zmodel", diff --git a/packages/misc/redwood/package.json b/packages/misc/redwood/package.json index 011e0dd64..e477bc7b8 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.9.1", + "version": "1.9.0", "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 6584ad022..96a336f58 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.9.1", + "version": "1.9.0", "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 351048b69..c64538378 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.9.1", + "version": "1.9.0", "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 ffbfd7bc0..0c16ca59d 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.9.1", + "version": "1.9.0", "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 215039ba0..0b53d2aa2 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.9.1", + "version": "1.9.0", "description": "ZenStack plugin for tRPC", "main": "index.js", "repository": { diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 494cf907f..3cb61dd38 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/runtime", "displayName": "ZenStack Runtime Library", - "version": "1.9.1", + "version": "1.9.0", "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 7fc652b14..2451a87c8 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.9.1", + "version": "1.9.0", "author": { "name": "ZenStack Team" }, @@ -87,7 +87,7 @@ "colors": "1.4.0", "commander": "^8.3.0", "get-latest-version": "^5.0.1", - "langium": "1.2.0", + "langium": "1.3.1", "lower-case-first": "^2.0.2", "mixpanel": "^0.17.0", "ora": "^5.4.1", diff --git a/packages/schema/src/language-server/validator/function-invocation-validator.ts b/packages/schema/src/language-server/validator/function-invocation-validator.ts index 3bc364bd2..d8c3df900 100644 --- a/packages/schema/src/language-server/validator/function-invocation-validator.ts +++ b/packages/schema/src/language-server/validator/function-invocation-validator.ts @@ -52,6 +52,7 @@ export default class FunctionInvocationValidator implements AstValidator ExpressionContext.DefaultValue) .with(P.union('@@allow', '@@deny', '@allow', '@deny'), () => ExpressionContext.AccessPolicy) .with('@@validate', () => ExpressionContext.ValidationRule) + .with('@@index', () => ExpressionContext.Index) .otherwise(() => undefined); // get the context allowed for the function diff --git a/packages/schema/src/language-server/zmodel-completion-provider.ts b/packages/schema/src/language-server/zmodel-completion-provider.ts index 742f7087f..70400db64 100644 --- a/packages/schema/src/language-server/zmodel-completion-provider.ts +++ b/packages/schema/src/language-server/zmodel-completion-provider.ts @@ -61,7 +61,7 @@ export class ZModelCompletionProvider extends DefaultCompletionProvider { if (isDataModelAttribute(context.node) || isDataModelFieldAttribute(context.node)) { const completions = this.getCompletionFromHint(context.node); if (completions) { - completions.forEach(acceptor); + completions.forEach((c) => acceptor(context, c)); return; } } @@ -131,7 +131,7 @@ export class ZModelCompletionProvider extends DefaultCompletionProvider { return; } - const customAcceptor = (item: CompletionValueItem) => { + const customAcceptor = (context: CompletionContext, item: CompletionValueItem) => { // attributes starting with @@@ are for internal use only if (item.insertText?.startsWith('@@@') || item.label?.startsWith('@@@')) { return; @@ -156,7 +156,7 @@ export class ZModelCompletionProvider extends DefaultCompletionProvider { return; } } - acceptor(item); + acceptor(context, item); }; super.completionForCrossReference(context, crossRef, customAcceptor); @@ -168,11 +168,11 @@ export class ZModelCompletionProvider extends DefaultCompletionProvider { keyword: any, acceptor: CompletionAcceptor ): MaybePromise { - const customAcceptor = (item: CompletionValueItem) => { + const customAcceptor = (context: CompletionContext, item: CompletionValueItem) => { if (!this.filterKeywordForContext(context, keyword.value)) { return; } - acceptor(item); + acceptor(context, item); }; super.completionForKeyword(context, keyword, customAcceptor); } diff --git a/packages/schema/src/language-server/zmodel-module.ts b/packages/schema/src/language-server/zmodel-module.ts index 07dc223e0..c0c66ce43 100644 --- a/packages/schema/src/language-server/zmodel-module.ts +++ b/packages/schema/src/language-server/zmodel-module.ts @@ -2,12 +2,15 @@ import { ZModelGeneratedModule, ZModelGeneratedSharedModule } from '@zenstackhq/ import { DefaultConfigurationProvider, DefaultDocumentBuilder, + DefaultFuzzyMatcher, DefaultIndexManager, DefaultLangiumDocumentFactory, DefaultLangiumDocuments, DefaultLanguageServer, + DefaultNodeKindProvider, DefaultServiceRegistry, DefaultSharedModuleContext, + DefaultWorkspaceSymbolProvider, LangiumDefaultSharedServices, LangiumServices, LangiumSharedServices, @@ -77,6 +80,7 @@ export const ZModelModule: Module { @@ -85,6 +89,9 @@ export function createSharedModule( lsp: { Connection: () => context.connection, LanguageServer: (services) => new DefaultLanguageServer(services), + WorkspaceSymbolProvider: (services) => new DefaultWorkspaceSymbolProvider(services), + NodeKindProvider: () => new DefaultNodeKindProvider(), + FuzzyMatcher: () => new DefaultFuzzyMatcher(), }, workspace: { LangiumDocuments: (services) => new DefaultLangiumDocuments(services), diff --git a/packages/schema/src/plugins/prisma/prisma-builder.ts b/packages/schema/src/plugins/prisma/prisma-builder.ts index 64777b62e..ea6317504 100644 --- a/packages/schema/src/plugins/prisma/prisma-builder.ts +++ b/packages/schema/src/plugins/prisma/prisma-builder.ts @@ -288,7 +288,7 @@ export class FieldReference { } export class FieldReferenceArg { - constructor(public name: 'sort', public value: 'Asc' | 'Desc') {} + constructor(public name: string, public value: string) {} toString(): string { return `${this.name}: ${this.value}`; @@ -304,10 +304,10 @@ export class FunctionCall { } export class FunctionCallArg { - constructor(public name: string | undefined, public value: string) {} + constructor(public value: string) {} toString(): string { - return this.name ? `${this.name}: ${this.value}` : this.value; + return this.value; } } diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 98dfa717e..2aa426b57 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -217,6 +217,10 @@ export default class PrismaSchemaGenerator { return JSON.stringify(expr.value); } + private exprToText(expr: Expression) { + return new ZModelCodeGenerator({ quote: 'double' }).generate(expr); + } + private generateGenerator(prisma: PrismaModel, decl: GeneratorDecl) { const generator = prisma.addGenerator( decl.name, @@ -368,7 +372,7 @@ export default class PrismaSchemaGenerator { 'FieldReference', new PrismaFieldReference( resolved(node.target).name, - node.args.map((arg) => new PrismaFieldReferenceArg(arg.name, arg.value)) + node.args.map((arg) => new PrismaFieldReferenceArg(arg.name, this.exprToText(arg.value))) ) ); } else if (isInvocationExpr(node)) { @@ -391,7 +395,7 @@ export default class PrismaSchemaGenerator { throw new PluginError(name, 'Function call argument must be literal or null'); }); - return new PrismaFunctionCallArg(arg.name, val); + return new PrismaFunctionCallArg(val); }) ); } diff --git a/packages/schema/src/res/stdlib.zmodel b/packages/schema/src/res/stdlib.zmodel index be241fe2c..a3bee6b2e 100644 --- a/packages/schema/src/res/stdlib.zmodel +++ b/packages/schema/src/res/stdlib.zmodel @@ -61,6 +61,9 @@ enum ExpressionContext { // used in @@validate ValidationRule + + // used in @@index + Index } /** @@ -200,7 +203,7 @@ attribute @@@completionHint(_ values: String[]) * @param sort: Allows you to specify in what order the entries of the ID are stored in the database. The available options are Asc and Desc. * @param clustered: Defines whether the ID is clustered or non-clustered. Defaults to true. */ -attribute @id(map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma +attribute @id(map: String?, length: Int?, sort: SortOrder?, clustered: Boolean?) @@@prisma /** * Defines a default value for a field. @@ -215,7 +218,7 @@ attribute @default(_ value: ContextType, map: String?) @@@prisma * @param sort: Allows you to specify in what order the entries of the constraint are stored in the database. The available options are Asc and Desc. * @param clustered: Boolean Defines whether the constraint is clustered or non-clustered. Defaults to false. */ -attribute @unique(map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma +attribute @unique(map: String?, length: Int?, sort: SortOrder?, clustered: Boolean?) @@@prisma /** * Defines a multi-field ID (composite ID) on the model. @@ -227,7 +230,7 @@ attribute @unique(map: String?, length: Int?, sort: String?, clustered: Boolean? * @param sort: Allows you to specify in what order the entries of the ID are stored in the database. The available options are Asc and Desc. * @param clustered: Defines whether the ID is clustered or non-clustered. Defaults to true. */ -attribute @@id(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma +attribute @@id(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: SortOrder?, clustered: Boolean?) @@@prisma /** * Defines a compound unique constraint for the specified fields. @@ -238,7 +241,7 @@ attribute @@id(_ fields: FieldReference[], name: String?, map: String?, length: * @param sort: Allows you to specify in what order the entries of the constraint are stored in the database. The available options are Asc and Desc. * @param clustered: Boolean Defines whether the constraint is clustered or non-clustered. Defaults to false. */ -attribute @@unique(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma +attribute @@unique(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: SortOrder?, clustered: Boolean?) @@@prisma /** * Index types @@ -252,6 +255,84 @@ enum IndexType { Brin } +/** + * Operator class for index + */ +enum IndexOperatorClass { + // GIN + ArrayOps + JsonbOps + JsonbPathOps + + // Gist + InetOps + + // SpGist + TextOps + + // BRIN + BitMinMaxOps + VarBitMinMaxOps + BpcharBloomOps + BpcharMinMaxOps + ByteaBloomOps + ByteaMinMaxOps + DateBloomOps + DateMinMaxOps + DateMinMaxMultiOps + Float4BloomOps + Float4MinMaxOps + Float4MinMaxMultiOps + Float8BloomOps + Float8MinMaxOps + Float8MinMaxMultiOps + InetInclusionOps + InetBloomOps + InetMinMaxOps + InetMinMaxMultiOps + Int2BloomOps + Int2MinMaxOps + Int2MinMaxMultiOps + Int4BloomOps + Int4MinMaxOps + Int4MinMaxMultiOps + Int8BloomOps + Int8MinMaxOps + Int8MinMaxMultiOps + NumericBloomOps + NumericMinMaxOps + NumericMinMaxMultiOps + OidBloomOps + OidMinMaxOps + OidMinMaxMultiOps + TextBloomOps + TextMinMaxOps + TextMinMaxMultiOps + TimestampBloomOps + TimestampMinMaxOps + TimestampMinMaxMultiOps + TimestampTzBloomOps + TimestampTzMinMaxOps + TimestampTzMinMaxMultiOps + TimeBloomOps + TimeMinMaxOps + TimeMinMaxMultiOps + TimeTzBloomOps + TimeTzMinMaxOps + TimeTzMinMaxMultiOps + UuidBloomOps + UuidMinMaxOps + UuidMinMaxMultiOps +} + +/** + * Index sort order + */ +enum SortOrder { + Asc + Desc +} + /** * Defines an index in the database. * @@ -263,7 +344,7 @@ enum IndexType { * @params clustered: Defines whether the index is clustered or non-clustered. Defaults to false. * @params type: Allows you to specify an index access method. Defaults to BTree. */ -attribute @@index(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: String?, clustered: Boolean?, type: IndexType?) @@@prisma +attribute @@index(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: SortOrder?, clustered: Boolean?, type: IndexType?) @@@prisma /** * Defines meta information about the relation. @@ -598,3 +679,9 @@ attribute @prisma.passthrough(_ text: String) * A utility attribute to allow passthrough of arbitrary attribute text to the generated Prisma schema. */ attribute @@prisma.passthrough(_ text: String) + +/** + * Used for specifying operator classes for GIN index. + */ +function raw(value: String): Any { +} @@@expressionContext([Index]) diff --git a/packages/schema/tests/generator/prisma-builder.test.ts b/packages/schema/tests/generator/prisma-builder.test.ts index 48e465362..a3944401c 100644 --- a/packages/schema/tests/generator/prisma-builder.test.ts +++ b/packages/schema/tests/generator/prisma-builder.test.ts @@ -102,7 +102,7 @@ describe('Prisma Builder Tests', () => { undefined, new AttributeArgValue( 'FunctionCall', - new FunctionCall('dbgenerated', [new FunctionCallArg(undefined, '"timestamp_id()"')]) + new FunctionCall('dbgenerated', [new FunctionCallArg('"timestamp_id()"')]) ) ), ]), diff --git a/packages/schema/tests/schema/all-features.zmodel b/packages/schema/tests/schema/all-features.zmodel index c47a7cf79..b567093fe 100644 --- a/packages/schema/tests/schema/all-features.zmodel +++ b/packages/schema/tests/schema/all-features.zmodel @@ -40,7 +40,7 @@ model Space extends Base { createdAt DateTime @default(now()) updatedAt DateTime @updatedAt name String @length(4, 50) - slug String @unique @length(4, 16) + slug String @length(4, 16) owner User? @relation(fields: [ownerId], references: [id]) ownerId String? members SpaceUser[] @@ -58,6 +58,8 @@ model Space extends Base { // space admin can update and delete @@allow('update,delete', members?[user == auth() && role == ADMIN]) + + @@index([slug(ops: raw("gin_trgm_ops"))], type: Gin) } /* diff --git a/packages/schema/tests/schema/parser.test.ts b/packages/schema/tests/schema/parser.test.ts index 9b4150cd5..25ada5ceb 100644 --- a/packages/schema/tests/schema/parser.test.ts +++ b/packages/schema/tests/schema/parser.test.ts @@ -224,7 +224,6 @@ describe('Parsing Tests', () => { expect(((model.attributes[1].args[0].value as ArrayExpr).items[0] as ReferenceExpr).args[0]).toEqual( expect.objectContaining({ name: 'sort', - value: 'Asc', }) ); @@ -232,7 +231,6 @@ describe('Parsing Tests', () => { expect((model.attributes[2].args[0].value as ReferenceExpr).args[0]).toEqual( expect.objectContaining({ name: 'sort', - value: 'Desc', }) ); }); diff --git a/packages/schema/tests/schema/validation/attribute-validation.test.ts b/packages/schema/tests/schema/validation/attribute-validation.test.ts index 6d8d02b14..b86637b58 100644 --- a/packages/schema/tests/schema/validation/attribute-validation.test.ts +++ b/packages/schema/tests/schema/validation/attribute-validation.test.ts @@ -161,11 +161,11 @@ describe('Attribute tests', () => { model A { x Int y String - @@id([x, y], name: 'x_y', map: '_x_y', length: 10, sort: 'Asc', clustered: true) + @@id([x, y], name: 'x_y', map: '_x_y', length: 10, sort: Asc, clustered: true) } model B { - id String @id(map: '_id', length: 10, sort: 'Asc', clustered: true) + id String @id(map: '_id', length: 10, sort: Asc, clustered: true) } `); @@ -175,7 +175,7 @@ describe('Attribute tests', () => { id String @id x Int y String - @@unique([x, y], name: 'x_y', map: '_x_y', length: 10, sort: 'Asc', clustered: true) + @@unique([x, y], name: 'x_y', map: '_x_y', length: 10, sort: Asc, clustered: true) } `); @@ -193,7 +193,7 @@ describe('Attribute tests', () => { ${prelude} model A { id String @id - x Int @unique(map: '_x', length: 10, sort: 'Asc', clustered: true) + x Int @unique(map: '_x', length: 10, sort: Asc, clustered: true) } `); @@ -222,7 +222,7 @@ describe('Attribute tests', () => { id String @id x Int y String - @@index([x(sort: Asc), y(sort: Desc)], name: 'myindex', map: '_myindex', length: 10, sort: 'asc', clustered: true, type: BTree) + @@index([x(sort: Asc), y(sort: Desc)], name: 'myindex', map: '_myindex', length: 10, sort: Asc, clustered: true, type: BTree) } `); diff --git a/packages/sdk/package.json b/packages/sdk/package.json index b4ccab542..99d7ba495 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/sdk", - "version": "1.9.1", + "version": "1.9.0", "description": "ZenStack plugin development SDK", "main": "index.js", "scripts": { diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index e038c6958..1e0d22d67 100644 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -12,6 +12,7 @@ export enum ExpressionContext { DefaultValue = 'DefaultValue', AccessPolicy = 'AccessPolicy', ValidationRule = 'ValidationRule', + Index = 'Index', } export const STD_LIB_MODULE_NAME = 'stdlib.zmodel'; diff --git a/packages/sdk/src/zmodel-code-generator.ts b/packages/sdk/src/zmodel-code-generator.ts index 1b1f001e1..96aaa87d9 100644 --- a/packages/sdk/src/zmodel-code-generator.ts +++ b/packages/sdk/src/zmodel-code-generator.ts @@ -49,6 +49,7 @@ export interface ZModelCodeOptions { binaryExprNumberOfSpaces: number; unaryExprNumberOfSpaces: number; indent: number; + quote: 'single' | 'double'; } // a registry of generation handlers marked with @gen @@ -75,6 +76,7 @@ export class ZModelCodeGenerator { binaryExprNumberOfSpaces: options?.binaryExprNumberOfSpaces ?? 1, unaryExprNumberOfSpaces: options?.unaryExprNumberOfSpaces ?? 0, indent: options?.indent ?? 4, + quote: options?.quote ?? 'single', }; } @@ -224,7 +226,7 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ @gen(StringLiteral) private _generateLiteralExpr(ast: LiteralExpr) { - return `'${ast.value}'`; + return this.options.quote === 'single' ? `'${ast.value}'` : `"${ast.value}"`; } @gen(NumberLiteral) @@ -265,7 +267,7 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ @gen(ReferenceArg) private _generateReferenceArg(ast: ReferenceArg) { - return `${ast.name}:${ast.value}`; + return `${ast.name}:${this.generate(ast.value)}`; } @gen(MemberAccessExpr) @@ -321,7 +323,7 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ } private argument(ast: Argument) { - return `${ast.name ? ast.name + ': ' : ''}${this.generate(ast.value)}`; + return this.generate(ast.value); } private get binaryExprSpace() { diff --git a/packages/server/package.json b/packages/server/package.json index af441d4f1..7ed1e3e29 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/server", - "version": "1.9.1", + "version": "1.9.0", "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 8acf19707..0aafe452e 100644 --- a/packages/testtools/package.json +++ b/packages/testtools/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/testtools", - "version": "1.9.1", + "version": "1.9.0", "description": "ZenStack Test Tools", "main": "index.js", "private": true, @@ -24,7 +24,7 @@ "@zenstackhq/runtime": "workspace:*", "@zenstackhq/sdk": "workspace:*", "json5": "^2.2.3", - "langium": "1.2.0", + "langium": "1.3.1", "pg": "^8.11.1", "tmp": "^0.2.1", "vscode-uri": "^3.0.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d501a52ca..9dcef261b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -69,12 +69,12 @@ importers: packages/language: dependencies: langium: - specifier: 1.2.0 - version: 1.2.0 + specifier: 1.3.1 + version: 1.3.1 devDependencies: langium-cli: - specifier: 1.2.0 - version: 1.2.0 + specifier: 1.3.1 + version: 1.3.1 plist2: specifier: ^1.1.3 version: 1.1.3 @@ -484,8 +484,8 @@ importers: specifier: ^5.0.1 version: 5.0.1 langium: - specifier: 1.2.0 - version: 1.2.0 + specifier: 1.3.1 + version: 1.3.1 lower-case-first: specifier: ^2.0.2 version: 2.0.2 @@ -739,8 +739,8 @@ importers: specifier: ^2.2.3 version: 2.2.3 langium: - specifier: 1.2.0 - version: 1.2.0 + specifier: 1.3.1 + version: 1.3.1 pg: specifier: ^8.11.1 version: 8.11.1 @@ -10232,8 +10232,8 @@ packages: resolution: {integrity: sha512-dWl0Dbjm6Xm+kDxhPQJsCBTxrJzuGl0aP9rhr+TG8D3l+GL90N8O8lYUi7dTSAN2uuDqCtNgb6aEuQH5wsiV8Q==} dev: true - /langium-cli@1.2.0: - resolution: {integrity: sha512-DPyJUd4Hj8+OBNEcAQyJtW6e38+UPd758gTI7Ep0r/sDogrwJ/GJHx5nGA+r0ygpNcDPG+mS9Hw8Y05uCNNcoQ==} + /langium-cli@1.3.1: + resolution: {integrity: sha512-9faKpioKCjBD0Z4y165+wQlDFiDHOXYBlhPVgbV+neSnSB70belZLNfykAVa564360h7Br/5PogR5jW2n/tOKw==} engines: {node: '>=14.0.0'} hasBin: true dependencies: @@ -10241,12 +10241,20 @@ packages: commander: 10.0.1 fs-extra: 11.1.1 jsonschema: 1.4.1 - langium: 1.2.0 + langium: 1.3.1 + langium-railroad: 1.3.0 lodash: 4.17.21 dev: true - /langium@1.2.0: - resolution: {integrity: sha512-jFSptpFljYo9ZTHrq/GZflMUXiKo5KBNtsaIJtnIzDm9zC2FxsxejEFAtNL09262RVQt+zFeF/2iLAShFTGitw==} + /langium-railroad@1.3.0: + resolution: {integrity: sha512-I3gx79iF+Qpn2UjzfHLf2GENAD9mPdSZHL3juAZLBsxznw4se7MBrJX32oPr/35DTjU9q99wFCQoCXu7mcf+Bg==} + dependencies: + langium: 1.3.1 + railroad-diagrams: 1.0.0 + dev: true + + /langium@1.3.1: + resolution: {integrity: sha512-xC+DnAunl6cZIgYjRpgm3s1kYAB5/Wycsj24iYaXG9uai7SgvMaFZSrRvdA5rUK/lSta/CRvgF+ZFoEKEOFJ5w==} engines: {node: '>=14.0.0'} dependencies: chevrotain: 10.4.2 @@ -12619,6 +12627,10 @@ packages: resolution: {integrity: sha512-pNsHDxbGORSvuSScqNJ+3Km6QAVqk8CfsCBIEoDgpqLrkD2f3QM4I7d1ozJJ172OmIcoUcerZaNWqtLkRXTV3A==} dev: true + /railroad-diagrams@1.0.0: + resolution: {integrity: sha512-cz93DjNeLY0idrCNOH6PviZGRN9GJhsdm9hpn1YCS879fj4W+x5IFJhhkRZcwVgMmFF7R82UA/7Oh+R8lLZg6A==} + dev: true + /randombytes@2.1.0: resolution: {integrity: sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==} dependencies: From 43eb61508fbde4431831343566dd637dff7a6d49 Mon Sep 17 00:00:00 2001 From: Yiming Date: Mon, 12 Feb 2024 21:49:37 +0800 Subject: [PATCH 06/17] fix: generate suspense queries in tanstack-query plugin (#996) --- .../plugins/tanstack-query/src/generator.ts | 171 ++++++++++-------- .../tanstack-query/src/runtime-v5/react.ts | 57 ++++++ 2 files changed, 152 insertions(+), 76 deletions(-) diff --git a/packages/plugins/tanstack-query/src/generator.ts b/packages/plugins/tanstack-query/src/generator.ts index bf0c88e0a..10852e826 100644 --- a/packages/plugins/tanstack-query/src/generator.ts +++ b/packages/plugins/tanstack-query/src/generator.ts @@ -78,68 +78,88 @@ function generateQueryHook( overrideReturnType?: string, overrideInputType?: string, overrideTypeParameters?: string[], - infinite = false, - optimisticUpdate = false + supportInfinite = false, + supportOptimistic = false ) { - const capOperation = upperCaseFirst(operation); - - const argsType = overrideInputType ?? `Prisma.${model}${capOperation}Args`; - const inputType = `Prisma.SelectSubset`; - - let defaultReturnType = `Prisma.${model}GetPayload`; - if (optimisticUpdate) { - defaultReturnType += '& { $optimistic?: boolean }'; + const generateModes: ('' | 'Infinite' | 'Suspense' | 'SuspenseInfinite')[] = ['']; + if (supportInfinite) { + generateModes.push('Infinite'); } - if (returnArray) { - defaultReturnType = `Array<${defaultReturnType}>`; + + if (target === 'react' && version === 'v5') { + // react-query v5 supports suspense query + generateModes.push('Suspense'); + if (supportInfinite) { + generateModes.push('SuspenseInfinite'); + } } - const returnType = overrideReturnType ?? defaultReturnType; - const optionsType = makeQueryOptions(target, 'TQueryFnData', 'TData', infinite, version); + for (const generateMode of generateModes) { + const capOperation = upperCaseFirst(operation); - const func = sf.addFunction({ - name: `use${infinite ? 'Infinite' : ''}${capOperation}${model}`, - typeParameters: overrideTypeParameters ?? [ - `TArgs extends ${argsType}`, - `TQueryFnData = ${returnType} `, - 'TData = TQueryFnData', - 'TError = DefaultError', - ], - parameters: [ - { - name: optionalInput ? 'args?' : 'args', - type: inputType, - }, - { - name: 'options?', - type: optionsType, - }, - ...(optimisticUpdate - ? [ - { - name: 'optimisticUpdate', - type: 'boolean', - initializer: 'true', - }, - ] - : []), - ], - isExported: true, - }); + const argsType = overrideInputType ?? `Prisma.${model}${capOperation}Args`; + const inputType = `Prisma.SelectSubset`; - if (version === 'v5' && infinite && ['react', 'svelte'].includes(target)) { - // initialPageParam and getNextPageParam options are required in v5 - func.addStatements([`options = options ?? { initialPageParam: undefined, getNextPageParam: () => null };`]); - } + const infinite = generateMode.includes('Infinite'); + const suspense = generateMode.includes('Suspense'); + const optimistic = + supportOptimistic && + // infinite queries are not subject to optimistic updates + !infinite; + + let defaultReturnType = `Prisma.${model}GetPayload`; + if (optimistic) { + defaultReturnType += '& { $optimistic?: boolean }'; + } + if (returnArray) { + defaultReturnType = `Array<${defaultReturnType}>`; + } + + const returnType = overrideReturnType ?? defaultReturnType; + const optionsType = makeQueryOptions(target, 'TQueryFnData', 'TData', infinite, suspense, version); + + const func = sf.addFunction({ + name: `use${generateMode}${capOperation}${model}`, + typeParameters: overrideTypeParameters ?? [ + `TArgs extends ${argsType}`, + `TQueryFnData = ${returnType} `, + 'TData = TQueryFnData', + 'TError = DefaultError', + ], + parameters: [ + { + name: optionalInput ? 'args?' : 'args', + type: inputType, + }, + { + name: 'options?', + type: optionsType, + }, + ...(optimistic + ? [ + { + name: 'optimisticUpdate', + type: 'boolean', + initializer: 'true', + }, + ] + : []), + ], + isExported: true, + }); + + if (version === 'v5' && infinite && ['react', 'svelte'].includes(target)) { + // initialPageParam and getNextPageParam options are required in v5 + func.addStatements([`options = options ?? { initialPageParam: undefined, getNextPageParam: () => null };`]); + } - func.addStatements([ - makeGetContext(target), - `return ${ - infinite ? 'useInfiniteModelQuery' : 'useModelQuery' - }('${model}', \`\${endpoint}/${lowerCaseFirst( - model - )}/${operation}\`, args, options, fetch${optimisticUpdate ? ', optimisticUpdate' : ''});`, - ]); + func.addStatements([ + makeGetContext(target), + `return use${generateMode}ModelQuery('${model}', \`\${endpoint}/${lowerCaseFirst( + model + )}/${operation}\`, args, options, fetch${optimistic ? ', optimisticUpdate' : ''});`, + ]); + } } function generateMutationHook( @@ -313,23 +333,8 @@ function generateModelHooks( undefined, undefined, undefined, - false, - true - ); - // infinite findMany - generateQueryHook( - target, - version, - sf, - model.name, - 'findMany', - true, true, - undefined, - undefined, - undefined, - true, - false + true ); } @@ -565,19 +570,29 @@ function makeBaseImports(target: TargetFramework, version: TanStackVersion) { `type DefaultError = Error;`, ]; switch (target) { - case 'react': + case 'react': { + const suspense = + version === 'v5' + ? [ + `import { useSuspenseModelQuery, useSuspenseInfiniteModelQuery } from '${runtimeImportBase}/${target}';`, + `import type { UseSuspenseQueryOptions, UseSuspenseInfiniteQueryOptions } from '@tanstack/react-query';`, + ] + : []; return [ `import type { UseMutationOptions, UseQueryOptions, UseInfiniteQueryOptions, InfiniteData } from '@tanstack/react-query';`, `import { getHooksContext } from '${runtimeImportBase}/${target}';`, ...shared, + ...suspense, ]; - case 'vue': + } + case 'vue': { return [ `import type { UseMutationOptions, UseQueryOptions, UseInfiniteQueryOptions, InfiniteData } from '@tanstack/vue-query';`, `import { getHooksContext } from '${runtimeImportBase}/${target}';`, ...shared, ]; - case 'svelte': + } + case 'svelte': { return [ `import { derived } from 'svelte/store';`, `import type { MutationOptions, CreateQueryOptions, CreateInfiniteQueryOptions } from '@tanstack/svelte-query';`, @@ -587,6 +602,7 @@ function makeBaseImports(target: TargetFramework, version: TanStackVersion) { `import { getHooksContext } from '${runtimeImportBase}/${target}';`, ...shared, ]; + } default: throw new PluginError(name, `Unsupported target: ${target}`); } @@ -597,6 +613,7 @@ function makeQueryOptions( returnType: string, dataType: string, infinite: boolean, + suspense: boolean, version: TanStackVersion ) { switch (target) { @@ -604,8 +621,10 @@ function makeQueryOptions( return infinite ? version === 'v4' ? `Omit, 'queryKey'>` - : `Omit>, 'queryKey'>` - : `Omit, 'queryKey'>`; + : `Omit>, 'queryKey'>` + : `Omit, 'queryKey'>`; case 'vue': return `Omit, 'queryKey'>`; case 'svelte': diff --git a/packages/plugins/tanstack-query/src/runtime-v5/react.ts b/packages/plugins/tanstack-query/src/runtime-v5/react.ts index 4871e8229..375cb2676 100644 --- a/packages/plugins/tanstack-query/src/runtime-v5/react.ts +++ b/packages/plugins/tanstack-query/src/runtime-v5/react.ts @@ -4,10 +4,14 @@ import { useMutation, useQuery, useQueryClient, + useSuspenseInfiniteQuery, + useSuspenseQuery, type InfiniteData, type UseInfiniteQueryOptions, type UseMutationOptions, type UseQueryOptions, + UseSuspenseInfiniteQueryOptions, + UseSuspenseQueryOptions, } from '@tanstack/react-query-v5'; import type { ModelMeta } from '@zenstackhq/runtime/cross'; import { createContext, useContext } from 'react'; @@ -71,6 +75,33 @@ export function useModelQuery( }); } +/** + * Creates a react-query suspense query. + * + * @param model The name of the model under query. + * @param url The request URL. + * @param args The request args object, URL-encoded and appended as "?q=" parameter + * @param options The react-query options object + * @param fetch The fetch function to use for sending the HTTP request + * @param optimisticUpdate Whether to enable automatic optimistic update + * @returns useSuspenseQuery hook + */ +export function useSuspenseModelQuery( + model: string, + url: string, + args?: unknown, + options?: Omit, 'queryKey'>, + fetch?: FetchFn, + optimisticUpdate = false +) { + const reqUrl = makeUrl(url, args); + return useSuspenseQuery({ + queryKey: getQueryKey(model, url, args, false, optimisticUpdate), + queryFn: () => fetcher(reqUrl, undefined, fetch, false), + ...options, + }); +} + /** * Creates a react-query infinite query. * @@ -97,6 +128,32 @@ export function useInfiniteModelQuery( }); } +/** + * Creates a react-query infinite suspense query. + * + * @param model The name of the model under query. + * @param url The request URL. + * @param args The initial request args object, URL-encoded and appended as "?q=" parameter + * @param options The react-query infinite query options object + * @param fetch The fetch function to use for sending the HTTP request + * @returns useSuspenseInfiniteQuery hook + */ +export function useSuspenseInfiniteModelQuery( + model: string, + url: string, + args: unknown, + options: Omit>, 'queryKey'>, + fetch?: FetchFn +) { + return useSuspenseInfiniteQuery({ + queryKey: getQueryKey(model, url, args, true), + queryFn: ({ pageParam }) => { + return fetcher(makeUrl(url, pageParam ?? args), undefined, fetch, false); + }, + ...options, + }); +} + /** * Creates a react-query mutation * From 613ac8d2cd638272bcc7b24e0fb96e60c0d43acc Mon Sep 17 00:00:00 2001 From: Yiming Date: Tue, 13 Feb 2024 09:25:43 +0800 Subject: [PATCH 07/17] fix: use zod parse result data as mutation input (#997) --- .../src/enhancements/policy/handler.ts | 26 ++++++-- .../src/enhancements/policy/policy-utils.ts | 21 +++++++ packages/schema/src/plugins/zod/generator.ts | 6 +- .../with-policy/field-validation.test.ts | 59 +++++++++++++++++++ 4 files changed, 106 insertions(+), 6 deletions(-) diff --git a/packages/runtime/src/enhancements/policy/handler.ts b/packages/runtime/src/enhancements/policy/handler.ts index e9f4daae0..698dcd364 100644 --- a/packages/runtime/src/enhancements/policy/handler.ts +++ b/packages/runtime/src/enhancements/policy/handler.ts @@ -249,7 +249,7 @@ export class PolicyProxyHandler implements Pr // there's no nested write and we've passed input check, proceed with the create directly // validate zod schema if any - this.validateCreateInputSchema(this.model, args.data); + args.data = this.validateCreateInputSchema(this.model, args.data); // make a create args only containing data and ID selection const createArgs: any = { data: args.data, select: this.utils.makeIdSelection(this.model) }; @@ -305,12 +305,20 @@ export class PolicyProxyHandler implements Pr // visit the create payload const visitor = new NestedWriteVisitor(this.modelMeta, { create: async (model, args, context) => { - this.validateCreateInputSchema(model, args); + const validateResult = this.validateCreateInputSchema(model, args); + if (validateResult !== args) { + this.utils.replace(args, validateResult); + } pushIdFields(model, context); }, createMany: async (model, args, context) => { - enumerate(args.data).forEach((item) => this.validateCreateInputSchema(model, item)); + enumerate(args.data).forEach((item) => { + const r = this.validateCreateInputSchema(model, item); + if (r !== item) { + this.utils.replace(item, r); + } + }); pushIdFields(model, context); }, @@ -319,7 +327,9 @@ export class PolicyProxyHandler implements Pr throw this.utils.validationError(`'where' field is required for connectOrCreate`); } - this.validateCreateInputSchema(model, args.create); + if (args.create) { + args.create = this.validateCreateInputSchema(model, args.create); + } const existing = await this.utils.checkExistence(db, model, args.where); if (existing) { @@ -468,6 +478,9 @@ export class PolicyProxyHandler implements Pr parseResult.error ); } + return parseResult.data; + } else { + return data; } } @@ -495,7 +508,10 @@ export class PolicyProxyHandler implements Pr CrudFailureReason.ACCESS_POLICY_VIOLATION ); } else if (inputCheck === true) { - this.validateCreateInputSchema(this.model, item); + const r = this.validateCreateInputSchema(this.model, item); + if (r !== item) { + this.utils.replace(item, r); + } } else if (inputCheck === undefined) { // static policy check is not possible, need to do post-create check needPostCreateCheck = true; diff --git a/packages/runtime/src/enhancements/policy/policy-utils.ts b/packages/runtime/src/enhancements/policy/policy-utils.ts index 388f9cd90..63b83b79f 100644 --- a/packages/runtime/src/enhancements/policy/policy-utils.ts +++ b/packages/runtime/src/enhancements/policy/policy-utils.ts @@ -1276,6 +1276,27 @@ export class PolicyUtil { return value ? deepcopy(value) : {}; } + /** + * Replace content of `target` object with `withObject` in-place. + */ + replace(target: any, withObject: any) { + if (!target || typeof target !== 'object' || !withObject || typeof withObject !== 'object') { + return; + } + + // remove missing keys + for (const key of Object.keys(target)) { + if (!(key in withObject)) { + delete target[key]; + } + } + + // overwrite keys + for (const [key, value] of Object.entries(withObject)) { + target[key] = value; + } + } + /** * Picks properties from an object. */ diff --git a/packages/schema/src/plugins/zod/generator.ts b/packages/schema/src/plugins/zod/generator.ts index 2727a781f..d1af70882 100644 --- a/packages/schema/src/plugins/zod/generator.ts +++ b/packages/schema/src/plugins/zod/generator.ts @@ -395,7 +395,7 @@ async function generateModelSchema(model: DataModel, project: Project, output: s //////////////////////////////////////////////// // schema for validating prisma create input (all fields optional) - let prismaCreateSchema = makePartial('baseSchema'); + let prismaCreateSchema = makePassthrough(makePartial('baseSchema')); if (refineFuncName) { prismaCreateSchema = `${refineFuncName}(${prismaCreateSchema})`; } @@ -501,3 +501,7 @@ function makeOmit(schema: string, fields: string[]) { function makeMerge(schema1: string, schema2: string): string { return `${schema1}.merge(${schema2})`; } + +function makePassthrough(schema: string) { + return `${schema}.passthrough()`; +} diff --git a/tests/integration/tests/enhancements/with-policy/field-validation.test.ts b/tests/integration/tests/enhancements/with-policy/field-validation.test.ts index 8727f1561..55b9c5cee 100644 --- a/tests/integration/tests/enhancements/with-policy/field-validation.test.ts +++ b/tests/integration/tests/enhancements/with-policy/field-validation.test.ts @@ -35,6 +35,8 @@ describe('With Policy: field validation', () => { text3 String @length(min: 3) text4 String @length(max: 5) text5 String? @endsWith('xyz') + text6 String? @trim @lower + text7 String? @upper @@allow('all', true) } @@ -495,4 +497,61 @@ describe('With Policy: field validation', () => { }) ).toResolveTruthy(); }); + + it('string transformation', async () => { + await db.user.create({ + data: { + id: '1', + password: 'abc123!@#', + email: 'who@myorg.com', + handle: 'user1', + }, + }); + + await expect( + db.userData.create({ + data: { + userId: '1', + a: 1, + b: 0, + c: -1, + d: 0, + text1: 'abc123', + text2: 'def', + text3: 'aaa', + text4: 'abcab', + text6: ' AbC ', + text7: 'abc', + }, + }) + ).resolves.toMatchObject({ text6: 'abc', text7: 'ABC' }); + + await expect( + db.user.create({ + data: { + id: '2', + password: 'abc123!@#', + email: 'who@myorg.com', + handle: 'user2', + userData: { + create: { + a: 1, + b: 0, + c: -1, + d: 0, + text1: 'abc123', + text2: 'def', + text3: 'aaa', + text4: 'abcab', + text6: ' AbC ', + text7: 'abc', + }, + }, + }, + include: { userData: true }, + }) + ).resolves.toMatchObject({ + userData: expect.objectContaining({ text6: 'abc', text7: 'ABC' }), + }); + }); }); From ad1829132d140d2b8cbfff8e3281e29493ed9477 Mon Sep 17 00:00:00 2001 From: Yiming Date: Tue, 13 Feb 2024 10:31:21 +0800 Subject: [PATCH 08/17] chore: skipping several tests that hang intermittently (#998) --- tests/integration/tests/cli/init.test.ts | 8 ++++---- .../integration/tests/frameworks/trpc/generation.test.ts | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/integration/tests/cli/init.test.ts b/tests/integration/tests/cli/init.test.ts index 96492b286..ab6fcc747 100644 --- a/tests/integration/tests/cli/init.test.ts +++ b/tests/integration/tests/cli/init.test.ts @@ -9,7 +9,9 @@ import { createProgram } from '../../../../packages/schema/src/cli'; import { execSync } from '../../../../packages/schema/src/utils/exec-utils'; import { createNpmrc } from './share'; -describe('CLI init command tests', () => { +// Skipping these tests as they seem to cause hangs intermittently when running with other tests +// eslint-disable-next-line jest/no-disabled-tests +describe.skip('CLI init command tests', () => { let origDir: string; beforeEach(() => { @@ -39,9 +41,7 @@ describe('CLI init command tests', () => { checkDependency('@zenstackhq/runtime', false, true); }); - // Disabled because it blows up memory on MAC, not sure why ... - // eslint-disable-next-line jest/no-disabled-tests - it.skip('init project t3 yarn std', async () => { + it('init project t3 yarn std', async () => { execSync('npx --yes create-t3-app@latest --prisma --CI --noGit .', 'inherit', { npm_config_user_agent: 'yarn', npm_config_cache: getWorkspaceNpmCacheFolder(__dirname), diff --git a/tests/integration/tests/frameworks/trpc/generation.test.ts b/tests/integration/tests/frameworks/trpc/generation.test.ts index 5e15d9943..e4cd0ede1 100644 --- a/tests/integration/tests/frameworks/trpc/generation.test.ts +++ b/tests/integration/tests/frameworks/trpc/generation.test.ts @@ -21,6 +21,7 @@ describe('tRPC Routers Generation Tests', () => { `${path.join(__dirname, '../../../../../.build/zenstackhq-sdk-' + ver + '.tgz')}`, `${path.join(__dirname, '../../../../../.build/zenstackhq-runtime-' + ver + '.tgz')}`, `${path.join(__dirname, '../../../../../.build/zenstackhq-trpc-' + ver + '.tgz')}`, + `${path.join(__dirname, '../../../../../.build/zenstackhq-server-' + ver + '.tgz')}`, ]; const deps = depPkgs.join(' '); From b4579c7f3a5d47e1f61c5268d3ac8efbebc7557d Mon Sep 17 00:00:00 2001 From: Yiming Date: Sun, 18 Feb 2024 10:46:59 +0800 Subject: [PATCH 09/17] chore: update JetBrains extension changelog (#1007) --- packages/ide/jetbrains/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/ide/jetbrains/CHANGELOG.md b/packages/ide/jetbrains/CHANGELOG.md index 4f4625001..1fa15f2eb 100644 --- a/packages/ide/jetbrains/CHANGELOG.md +++ b/packages/ide/jetbrains/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog ## [Unreleased] +### Added +- Added support to complex usage of `@@index` attribute like `@@index([content(ops: raw("gin_trgm_ops"))], type: Gin)`. +### Fixed +- Fixed several ZModel validation issues related to model inheritance. +## 1.7.0 ### Added - Auto-completion is now supported inside attributes. From b2e1635cb1857afebde286a0c077c0f561d0bbec Mon Sep 17 00:00:00 2001 From: Yiming Date: Sun, 18 Feb 2024 17:57:51 -0800 Subject: [PATCH 10/17] fix: vue-query typing issue (#1009) --- packages/plugins/tanstack-query/src/runtime/vue.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugins/tanstack-query/src/runtime/vue.ts b/packages/plugins/tanstack-query/src/runtime/vue.ts index a0f1055e8..b0a35f5f3 100644 --- a/packages/plugins/tanstack-query/src/runtime/vue.ts +++ b/packages/plugins/tanstack-query/src/runtime/vue.ts @@ -61,7 +61,7 @@ export function useModelQuery( model: string, url: string, args?: unknown, - options?: UseQueryOptions, + options?: Omit, 'queryKey'>, fetch?: FetchFn, optimisticUpdate = false ) { @@ -87,7 +87,7 @@ export function useInfiniteModelQuery( model: string, url: string, args?: unknown, - options?: UseInfiniteQueryOptions, + options?: Omit, 'queryKey'>, fetch?: FetchFn ) { return useInfiniteQuery({ From 0704f9db945fc922746ecd480ae833fd64415784 Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Mon, 19 Feb 2024 10:01:41 -0600 Subject: [PATCH 11/17] fix: zenstack cli errors while using bun/bunx during docker build (#1011) --- packages/schema/src/cli/actions/repl.ts | 8 +++++++- packages/schema/src/plugins/prisma/schema-generator.ts | 10 +++++----- packages/schema/src/utils/exec-utils.ts | 9 +++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/schema/src/cli/actions/repl.ts b/packages/schema/src/cli/actions/repl.ts index 6ca3c3503..df15e30fb 100644 --- a/packages/schema/src/cli/actions/repl.ts +++ b/packages/schema/src/cli/actions/repl.ts @@ -2,7 +2,6 @@ /* eslint-disable @typescript-eslint/no-var-requires */ import colors from 'colors'; import path from 'path'; -import prettyRepl from 'pretty-repl'; import { inspect } from 'util'; // inspired by: https://github.com/Kinjalrk2k/prisma-console @@ -11,6 +10,13 @@ import { inspect } from 'util'; * CLI action for starting a REPL session */ export async function repl(projectPath: string, options: { prismaClient?: string; debug?: boolean; table?: boolean }) { + if (!process?.stdout?.isTTY && process?.versions?.bun) { + console.error('REPL on Bun is only available in a TTY terminal at this time. Please use npm/npx to run the command in this context instead of bun/bunx.'); + return; + } + + const prettyRepl = await import('pretty-repl') + console.log('Welcome to ZenStack REPL. See help with the ".help" command.'); console.log('Global variables:'); console.log(` ${colors.blue('db')} to access enhanced PrismaClient`); diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 2aa426b57..881983caa 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -47,7 +47,7 @@ import stripColor from 'strip-color'; import { name } from '.'; import { getStringLiteral } from '../../language-server/validator/utils'; import telemetry from '../../telemetry'; -import { execSync } from '../../utils/exec-utils'; +import { execPackage } from '../../utils/exec-utils'; import { findPackageJson } from '../../utils/pkg-utils'; import { ModelFieldType, @@ -127,7 +127,7 @@ export default class PrismaSchemaGenerator { if (options.format === true) { try { // run 'prisma format' - await execSync(`npx prisma format --schema ${outFile}`); + await execPackage(`prisma format --schema ${outFile}`); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -136,18 +136,18 @@ export default class PrismaSchemaGenerator { const generateClient = options.generateClient !== false; if (generateClient) { - let generateCmd = `npx prisma generate --schema "${outFile}"`; + let generateCmd = `prisma generate --schema "${outFile}"`; if (typeof options.generateArgs === 'string') { generateCmd += ` ${options.generateArgs}`; } try { // run 'prisma generate' - await execSync(generateCmd, 'ignore'); + await execPackage(generateCmd, 'ignore'); } catch { await this.trackPrismaSchemaError(outFile); try { // run 'prisma generate' again with output to the console - await execSync(generateCmd); + await execPackage(generateCmd); } catch { // noop } diff --git a/packages/schema/src/utils/exec-utils.ts b/packages/schema/src/utils/exec-utils.ts index f355ae2b4..8f0508dbb 100644 --- a/packages/schema/src/utils/exec-utils.ts +++ b/packages/schema/src/utils/exec-utils.ts @@ -7,3 +7,12 @@ export function execSync(cmd: string, stdio: StdioOptions = 'inherit', env?: Rec const mergedEnv = { ...process.env, ...env }; _exec(cmd, { encoding: 'utf-8', stdio, env: mergedEnv }); } + +/** + * Utility for running package commands through npx/bunx + */ +export function execPackage(cmd: string, stdio: StdioOptions = 'inherit', env?: Record): void { + const packageManager = process?.versions?.bun ? 'bunx' : 'npx'; + const mergedEnv = { ...process.env, ...env }; + _exec(`${packageManager} ${cmd}`, { encoding: 'utf-8', stdio, env: mergedEnv }); +} \ No newline at end of file From d7b75e9c1da943267ae79381e5ee605b864a4f26 Mon Sep 17 00:00:00 2001 From: Jason Kleinberg Date: Mon, 19 Feb 2024 12:38:38 -0500 Subject: [PATCH 12/17] Allows enum types for id fields (#1010) --- .../validator/datamodel-validator.ts | 10 +- packages/schema/tests/schema/stdlib.test.ts | 2 +- .../validation/datamodel-validation.test.ts | 415 ++++++++++-------- .../validation/datasource-validation.test.ts | 25 +- packages/schema/tests/utils.ts | 38 +- 5 files changed, 286 insertions(+), 204 deletions(-) diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index 3096d5257..379d4e46a 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -5,6 +5,7 @@ import { isDataModel, isStringLiteral, ReferenceExpr, + isEnum, } from '@zenstackhq/language/ast'; import { getLiteral, getModelIdFields, getModelUniqueFields } from '@zenstackhq/sdk'; import { AstNode, DiagnosticInfo, getDocument, ValidationAcceptor } from 'langium'; @@ -61,8 +62,13 @@ export default class DataModelValidator implements AstValidator { if (idField.type.optional) { accept('error', 'Field with @id attribute must not be optional', { node: idField }); } - if (idField.type.array || !idField.type.type || !SCALAR_TYPES.includes(idField.type.type)) { - accept('error', 'Field with @id attribute must be of scalar type', { node: idField }); + + const isArray = idField.type.array; + const isScalar = SCALAR_TYPES.includes(idField.type.type as typeof SCALAR_TYPES[number]) + const isValidType = isScalar || isEnum(idField.type.reference?.ref) + + if (isArray || !isValidType) { + accept('error', 'Field with @id attribute must be of scalar or enum type', { node: idField }); } }); } diff --git a/packages/schema/tests/schema/stdlib.test.ts b/packages/schema/tests/schema/stdlib.test.ts index f4b1cc1fe..ad637be7a 100644 --- a/packages/schema/tests/schema/stdlib.test.ts +++ b/packages/schema/tests/schema/stdlib.test.ts @@ -24,7 +24,7 @@ describe('Stdlib Tests', () => { }` ); } - throw new SchemaLoadingError(validationErrors.map((e) => e.message)); + throw new SchemaLoadingError(validationErrors); } }); }); diff --git a/packages/schema/tests/schema/validation/datamodel-validation.test.ts b/packages/schema/tests/schema/validation/datamodel-validation.test.ts index 4212441fe..78e31204d 100644 --- a/packages/schema/tests/schema/validation/datamodel-validation.test.ts +++ b/packages/schema/tests/schema/validation/datamodel-validation.test.ts @@ -1,4 +1,4 @@ -import { loadModel, loadModelWithError } from '../../utils'; +import { loadModel, safelyLoadModel, errorLike } from '../../utils'; describe('Data Model Validation Tests', () => { const prelude = ` @@ -9,20 +9,20 @@ describe('Data Model Validation Tests', () => { `; it('duplicated fields', async () => { - expect( - await loadModelWithError(` - ${prelude} - model M { - id String @id - x Int - x String - } + const result = await safelyLoadModel(` + ${ prelude } + model M { + id String @id + x Int + x String + } `) - ).toContain('Duplicated declaration name "x"'); + + expect(result).toMatchObject(errorLike('Duplicated declaration name "x"')); }); it('scalar types', async () => { - await loadModel(` + const result = await safelyLoadModel(` ${prelude} model M { id String @id @@ -38,33 +38,36 @@ describe('Data Model Validation Tests', () => { i Bytes } `); + expect(result).toMatchObject({ status: 'fulfilled' }); }); it('Unsupported type valid arg', async () => { - await loadModel(` + const result = await safelyLoadModel(` ${prelude} model M { id String @id a Unsupported('foo') } `); + + expect(result).toMatchObject({ status: 'fulfilled' }); }); it('Unsupported type invalid arg', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id a Unsupported(123) } `) - ).toContain('Unsupported type argument must be a string literal'); + ).toMatchObject(errorLike('Unsupported type argument must be a string literal')); }); it('Unsupported type used in expression', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id @@ -72,208 +75,258 @@ describe('Data Model Validation Tests', () => { @@allow('all', a == 'a') } `) - ).toContain('Field of "Unsupported" type cannot be used in expressions'); + ).toMatchObject(errorLike('Field of "Unsupported" type cannot be used in expressions')); }); it('mix array and optional', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Int[]? } `) - ).toContain('Optional lists are not supported. Use either `Type[]` or `Type?`'); + ).toMatchObject(errorLike('Optional lists are not supported. Use either `Type[]` or `Type?`')); }); it('unresolved field type', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Integer } `) - ).toContain(`Could not resolve reference to TypeDeclaration named 'Integer'.`); + ).toMatchObject(errorLike(`Could not resolve reference to TypeDeclaration named 'Integer'.`)); expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Integer[] } `) - ).toContain(`Could not resolve reference to TypeDeclaration named 'Integer'.`); + ).toMatchObject(errorLike(`Could not resolve reference to TypeDeclaration named 'Integer'.`)); expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Integer? } `) - ).toContain(`Could not resolve reference to TypeDeclaration named 'Integer'.`); + ).toMatchObject(errorLike(`Could not resolve reference to TypeDeclaration named 'Integer'.`)); }); - it('id field', async () => { + describe('id field', () => { const err = 'Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.'; - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int - @@allow('all', x > 0) - } - `) - ).toContain(err); + it('should error when there are no unique fields', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int + @@allow('all', x > 0) + } + `) + expect(result).toMatchObject(errorLike(err)); + }) - // @unique used as id - await loadModel(` - ${prelude} - model M { - id Int @unique - x Int - @@allow('all', x > 0) - } - `); + it('should should use @unique when there is no @id', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + id Int @unique + x Int + @@allow('all', x > 0) + } + `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }) // @@unique used as id - await loadModel(` - ${prelude} + it('should suceed when @@unique used as id', async () => { + const result = await safelyLoadModel(` + ${ prelude } model M { x Int @@unique([x]) @@allow('all', x > 0) } `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }) + + it('should succeed when @id is an enum type', async () => { + const result = await safelyLoadModel(` + ${ prelude } + enum E { + A + B + } + model M { + id E @id + } + `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }) + + it('should succeed when @@id is an enum type', async () => { + const result = await safelyLoadModel(` + ${ prelude } + enum E { + A + B + } + model M { + x Int + y E + @@id([x, y]) + } + `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int - @@deny('all', x <= 0) - } - `) - ).toContain(err); + it('should error when there are no id fields, even when denying access', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int + @@deny('all', x <= 0) + } + `) + + expect(result).toMatchObject(errorLike(err)); + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int @gt(0) - } - `) - ).toContain(err); + it('should error when there are not id fields, without access restrictions', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int @gt(0) + } + `) + + expect(result).toMatchObject(errorLike(err)); + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int @id - y Int @id - } - `) - ).toContain(`Model can include at most one field with @id attribute`); + it('should error when there is more than one field marked as @id', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int @id + y Int @id + } + `) + expect(result).toMatchObject(errorLike(`Model can include at most one field with @id attribute`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int @id - y Int - @@id([x, y]) - } - `) - ).toContain(`Model cannot have both field-level @id and model-level @@id attributes`); + it('should error when both @id and @@id are used', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int @id + y Int + @@id([x, y]) + } + `) + expect(result).toMatchObject(errorLike(`Model cannot have both field-level @id and model-level @@id attributes`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int? @id - } - `) - ).toContain(`Field with @id attribute must not be optional`); + it('should error when @id used on optional field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int? @id + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int? - @@id([x]) - } - `) - ).toContain(`Field with @id attribute must not be optional`); + it('should error when @@id used on optional field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int? + @@id([x]) + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int[] @id - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @id used on list field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int[] @id + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int[] - @@id([x]) - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @@id used on list field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Int[] + @@id([x]) + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Json @id - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @id used on a Json field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Json @id + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model M { - x Json - @@id([x]) - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @@id used on a Json field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model M { + x Json + @@id([x]) + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model Id { - id String @id - } - model M { - myId Id @id - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @id used on a reference field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model Id { + id String @id + } + model M { + myId Id @id + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)) + }) - expect( - await loadModelWithError(` - ${prelude} - model Id { - id String @id - } - model M { - myId Id - @@id([myId]) - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @@id used on a reference field', async () => { + const result = await safelyLoadModel(` + ${ prelude } + model Id { + id String @id + } + model M { + myId Id + @@id([myId]) + } + `) + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)) + }) }); it('relation', async () => { @@ -326,7 +379,7 @@ describe('Data Model Validation Tests', () => { // one-to-one incomplete expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -337,11 +390,11 @@ describe('Data Model Validation Tests', () => { id String @id } `) - ).toContain(`The relation field "b" on model "A" is missing an opposite relation field on model "B"`); + ).toMatchObject(errorLike(`The relation field "b" on model "A" is missing an opposite relation field on model "B"`)); // one-to-one ambiguous expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -354,11 +407,11 @@ describe('Data Model Validation Tests', () => { a1 A } `) - ).toContain(`Fields "a", "a1" on model "B" refer to the same relation to model "A"`); + ).toMatchObject(errorLike(`Fields "a", "a1" on model "B" refer to the same relation to model "A"`)); // fields or references missing expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -371,11 +424,11 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain(`Both "fields" and "references" must be provided`); + ).toMatchObject(errorLike(`Both "fields" and "references" must be provided`)); // one-to-one inconsistent attribute expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -388,11 +441,11 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain(`"fields" and "references" must be provided only on one side of relation field`); + ).toMatchObject(errorLike(`"fields" and "references" must be provided only on one side of relation field`)); // references mismatch expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { myId Int @id @@ -405,11 +458,11 @@ describe('Data Model Validation Tests', () => { aId String @unique } `) - ).toContain(`values of "references" and "fields" must have the same type`); + ).toMatchObject(errorLike(`values of "references" and "fields" must have the same type`)); // "fields" and "references" typing consistency expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id Int @id @@ -422,11 +475,11 @@ describe('Data Model Validation Tests', () => { aId String @unique } `) - ).toContain(`values of "references" and "fields" must have the same type`); + ).toMatchObject(errorLike(`values of "references" and "fields" must have the same type`)); // one-to-one missing @unique expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -439,13 +492,11 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain( - `Field "aId" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute` - ); + ).toMatchObject(errorLike(`Field "aId" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`)); // missing @relation expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -457,13 +508,11 @@ describe('Data Model Validation Tests', () => { a A } `) - ).toContain( - `Field for one side of relation must carry @relation attribute with both "fields" and "references" fields` - ); + ).toMatchObject(errorLike(`Field for one side of relation must carry @relation attribute with both "fields" and "references" fields`)); // wrong relation owner field type expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -476,11 +525,11 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain(`Relation field needs to be list or optional`); + ).toMatchObject(errorLike(`Relation field needs to be list or optional`)); // unresolved field expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -492,7 +541,7 @@ describe('Data Model Validation Tests', () => { a A @relation(fields: [aId], references: [id]) } `) - ).toContain(`Could not resolve reference to ReferenceTarget named 'aId'.`); + ).toMatchObject(errorLike(`Could not resolve reference to ReferenceTarget named 'aId'.`)); // enum as foreign key await loadModel(` @@ -607,7 +656,7 @@ describe('Data Model Validation Tests', () => { }); it('abstract base type', async () => { - const errors = await loadModelWithError(` + const errors = await safelyLoadModel(` ${prelude} abstract model Base { @@ -623,11 +672,11 @@ describe('Data Model Validation Tests', () => { } `); - expect(errors).toContain(`Model A cannot be extended because it's not abstract`); + expect(errors).toMatchObject(errorLike(`Model A cannot be extended because it's not abstract`)); // relation incomplete from multiple level inheritance expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model User { id Int @id @default(autoincrement()) @@ -647,6 +696,6 @@ describe('Data Model Validation Tests', () => { a String } `) - ).toContain(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`); + ).toMatchObject(errorLike(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`)); }); }); diff --git a/packages/schema/tests/schema/validation/datasource-validation.test.ts b/packages/schema/tests/schema/validation/datasource-validation.test.ts index 19be1f076..469ba5ac1 100644 --- a/packages/schema/tests/schema/validation/datasource-validation.test.ts +++ b/packages/schema/tests/schema/validation/datasource-validation.test.ts @@ -1,18 +1,21 @@ -import { loadModel, loadModelWithError } from '../../utils'; +import { loadModel, loadModelWithError, safelyLoadModel } from '../../utils'; describe('Datasource Validation Tests', () => { it('missing fields', async () => { - expect( - await loadModelWithError(` + const result = await safelyLoadModel(` datasource db { } - `) - ).toEqual( - expect.arrayContaining([ - 'datasource must include a "provider" field', - 'datasource must include a "url" field', - ]) - ); + `); + + expect(result).toMatchObject({ + status: 'rejected', + reason: { + cause: [ + { message: 'datasource must include a "provider" field' }, + { message: 'datasource must include a "url" field' }, + ] + } + }) }); it('dup fields', async () => { @@ -41,7 +44,7 @@ describe('Datasource Validation Tests', () => { provider = 'abc' } `) - ).toContainEqual(expect.stringContaining('Provider "abc" is not supported')); + ).toContain('Provider "abc" is not supported'); }); it('invalid url value', async () => { diff --git a/packages/schema/tests/utils.ts b/packages/schema/tests/utils.ts index f88aae6e2..7369838f5 100644 --- a/packages/schema/tests/utils.ts +++ b/packages/schema/tests/utils.ts @@ -7,9 +7,21 @@ import { URI } from 'vscode-uri'; import { createZModelServices } from '../src/language-server/zmodel-module'; import { mergeBaseModel } from '../src/utils/ast-utils'; -export class SchemaLoadingError extends Error { - constructor(public readonly errors: string[]) { - super('Schema error:\n' + errors.join('\n')); +type Errorish = Error | { message: string; stack?: string } | string; + +export class SchemaLoadingError extends Error { + cause: Errors + constructor(public readonly errors: Errors) { + const stack = errors.find((e): e is typeof e & { stack: string } => typeof e === 'object' && 'stack' in e)?.stack; + const message = errors.map((e) => (typeof e === 'string' ? e : e.message)).join('\n'); + + super(`Schema error:\n${ message }`); + + if (stack) { + const shiftedStack = stack.split('\n').slice(1).join('\n'); + this.stack = shiftedStack + } + this.cause = errors } } @@ -23,11 +35,11 @@ export async function loadModel(content: string, validate = true, verbose = true const doc = shared.workspace.LangiumDocuments.getOrCreateDocument(URI.file(docPath)); if (doc.parseResult.lexerErrors.length > 0) { - throw new SchemaLoadingError(doc.parseResult.lexerErrors.map((e) => e.message)); + throw new SchemaLoadingError(doc.parseResult.lexerErrors); } if (doc.parseResult.parserErrors.length > 0) { - throw new SchemaLoadingError(doc.parseResult.parserErrors.map((e) => e.message)); + throw new SchemaLoadingError(doc.parseResult.parserErrors); } await shared.workspace.DocumentBuilder.build([stdLib, doc], { @@ -46,7 +58,7 @@ export async function loadModel(content: string, validate = true, verbose = true ); } } - throw new SchemaLoadingError(validationErrors.map((e) => e.message)); + throw new SchemaLoadingError(validationErrors); } const model = (await doc.parseResult.value) as Model; @@ -65,7 +77,19 @@ export async function loadModelWithError(content: string, verbose = false) { if (!(err instanceof SchemaLoadingError)) { throw err; } - return (err as SchemaLoadingError).errors; + return (err as SchemaLoadingError).message; } throw new Error('No error is thrown'); } + +export async function safelyLoadModel(content: string, validate = true, verbose = false) { + const [ result ] = await Promise.allSettled([ loadModel(content, validate, verbose) ]); + + return result +} + +export const errorLike = (msg: string) => ({ + reason: { + message: expect.stringContaining(msg) + }, +}) From 43982315c6053161e817e566be8c9f69ded71c73 Mon Sep 17 00:00:00 2001 From: Jason Kleinberg Date: Mon, 19 Feb 2024 18:16:20 -0500 Subject: [PATCH 13/17] Minor language server fixes (#1013) --- .../validator/schema-validator.ts | 3 ++- packages/schema/src/res/stdlib.zmodel | 4 ++-- .../validation/attribute-validation.test.ts | 20 +++++++++++++++++++ .../validation/schema-validation.test.ts | 14 +++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/schema/src/language-server/validator/schema-validator.ts b/packages/schema/src/language-server/validator/schema-validator.ts index b80bf890d..6f868c614 100644 --- a/packages/schema/src/language-server/validator/schema-validator.ts +++ b/packages/schema/src/language-server/validator/schema-validator.ts @@ -52,8 +52,9 @@ export default class SchemaValidator implements AstValidator { private validateImports(model: Model, accept: ValidationAcceptor) { model.imports.forEach((imp) => { const importedModel = resolveImport(this.documents, imp); + const importPath = imp.path.endsWith('.zmodel') ? imp.path : `${imp.path}.zmodel`; if (!importedModel) { - accept('error', `Cannot find model file ${imp.path}.zmodel`, { node: imp }); + accept('error', `Cannot find model file ${importPath}`, { node: imp }); } }); } diff --git a/packages/schema/src/res/stdlib.zmodel b/packages/schema/src/res/stdlib.zmodel index a3bee6b2e..145ffed60 100644 --- a/packages/schema/src/res/stdlib.zmodel +++ b/packages/schema/src/res/stdlib.zmodel @@ -390,7 +390,7 @@ attribute @updatedAt() @@@targetField([DateTimeField]) @@@prisma /** * Add full text index (MySQL only). */ -attribute @@fulltext(_ fields: FieldReference[]) @@@prisma +attribute @@fulltext(_ fields: FieldReference[], map: String?) @@@prisma // String type modifiers @@ -479,7 +479,7 @@ attribute @db.Bytes() @@@targetField([BytesField]) @@@prisma attribute @db.ByteA() @@@targetField([BytesField]) @@@prisma attribute @db.LongBlob() @@@targetField([BytesField]) @@@prisma attribute @db.Binary() @@@targetField([BytesField]) @@@prisma -attribute @db.VarBinary() @@@targetField([BytesField]) @@@prisma +attribute @db.VarBinary(_ x: Int?) @@@targetField([BytesField]) @@@prisma attribute @db.TinyBlob() @@@targetField([BytesField]) @@@prisma attribute @db.Blob() @@@targetField([BytesField]) @@@prisma attribute @db.MediumBlob() @@@targetField([BytesField]) @@@prisma diff --git a/packages/schema/tests/schema/validation/attribute-validation.test.ts b/packages/schema/tests/schema/validation/attribute-validation.test.ts index b86637b58..8eb674b2f 100644 --- a/packages/schema/tests/schema/validation/attribute-validation.test.ts +++ b/packages/schema/tests/schema/validation/attribute-validation.test.ts @@ -226,6 +226,25 @@ describe('Attribute tests', () => { } `); + await loadModel(` + ${ prelude } + model A { + id String @id + x String + y String + z String + @@fulltext([x, y, z]) + } + + model B { + id String @id + x String + y String + z String + @@fulltext([x, y, z], map: "n") + } + `); + await loadModel(` ${prelude} model A { @@ -352,6 +371,7 @@ describe('Attribute tests', () => { _longBlob Bytes @db.LongBlob _binary Bytes @db.Binary _varBinary Bytes @db.VarBinary + _varBinarySized Bytes @db.VarBinary(100) _tinyBlob Bytes @db.TinyBlob _blob Bytes @db.Blob _mediumBlob Bytes @db.MediumBlob diff --git a/packages/schema/tests/schema/validation/schema-validation.test.ts b/packages/schema/tests/schema/validation/schema-validation.test.ts index 5f1cc6254..ca0efa697 100644 --- a/packages/schema/tests/schema/validation/schema-validation.test.ts +++ b/packages/schema/tests/schema/validation/schema-validation.test.ts @@ -39,6 +39,20 @@ describe('Toplevel Schema Validation Tests', () => { ).toContain('Cannot find model file models/abc.zmodel'); }); + it('not existing import with extension', async () => { + expect( + await loadModelWithError(` + import 'models/abc.zmodel' + datasource db1 { + provider = 'postgresql' + url = env('DATABASE_URL') + } + + model X {id String @id } + `) + ).toContain('Cannot find model file models/abc.zmodel'); + }) + it('multiple auth models', async () => { expect( await loadModelWithError(` From 9384a86e157d9b76b2bd47bef5bf3b72567c6b28 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Tue, 20 Feb 2024 09:24:13 -0800 Subject: [PATCH 14/17] chore: merge from dev --- packages/ide/jetbrains/CHANGELOG.md | 5 + .../plugins/tanstack-query/src/runtime/vue.ts | 4 +- packages/schema/src/cli/actions/repl.ts | 8 +- .../validator/datamodel-validator.ts | 10 +- .../validator/schema-validator.ts | 3 +- .../src/plugins/prisma/schema-generator.ts | 10 +- packages/schema/src/res/stdlib.zmodel | 4 +- packages/schema/src/utils/exec-utils.ts | 8 + packages/schema/tests/schema/stdlib.test.ts | 2 +- .../validation/attribute-validation.test.ts | 20 + .../validation/datamodel-validation.test.ts | 427 ++++++++++-------- .../validation/datasource-validation.test.ts | 25 +- .../validation/schema-validation.test.ts | 14 + packages/schema/tests/utils.ts | 38 +- 14 files changed, 364 insertions(+), 214 deletions(-) diff --git a/packages/ide/jetbrains/CHANGELOG.md b/packages/ide/jetbrains/CHANGELOG.md index 4f4625001..1fa15f2eb 100644 --- a/packages/ide/jetbrains/CHANGELOG.md +++ b/packages/ide/jetbrains/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog ## [Unreleased] +### Added +- Added support to complex usage of `@@index` attribute like `@@index([content(ops: raw("gin_trgm_ops"))], type: Gin)`. +### Fixed +- Fixed several ZModel validation issues related to model inheritance. +## 1.7.0 ### Added - Auto-completion is now supported inside attributes. diff --git a/packages/plugins/tanstack-query/src/runtime/vue.ts b/packages/plugins/tanstack-query/src/runtime/vue.ts index a0f1055e8..b0a35f5f3 100644 --- a/packages/plugins/tanstack-query/src/runtime/vue.ts +++ b/packages/plugins/tanstack-query/src/runtime/vue.ts @@ -61,7 +61,7 @@ export function useModelQuery( model: string, url: string, args?: unknown, - options?: UseQueryOptions, + options?: Omit, 'queryKey'>, fetch?: FetchFn, optimisticUpdate = false ) { @@ -87,7 +87,7 @@ export function useInfiniteModelQuery( model: string, url: string, args?: unknown, - options?: UseInfiniteQueryOptions, + options?: Omit, 'queryKey'>, fetch?: FetchFn ) { return useInfiniteQuery({ diff --git a/packages/schema/src/cli/actions/repl.ts b/packages/schema/src/cli/actions/repl.ts index 6ca3c3503..df15e30fb 100644 --- a/packages/schema/src/cli/actions/repl.ts +++ b/packages/schema/src/cli/actions/repl.ts @@ -2,7 +2,6 @@ /* eslint-disable @typescript-eslint/no-var-requires */ import colors from 'colors'; import path from 'path'; -import prettyRepl from 'pretty-repl'; import { inspect } from 'util'; // inspired by: https://github.com/Kinjalrk2k/prisma-console @@ -11,6 +10,13 @@ import { inspect } from 'util'; * CLI action for starting a REPL session */ export async function repl(projectPath: string, options: { prismaClient?: string; debug?: boolean; table?: boolean }) { + if (!process?.stdout?.isTTY && process?.versions?.bun) { + console.error('REPL on Bun is only available in a TTY terminal at this time. Please use npm/npx to run the command in this context instead of bun/bunx.'); + return; + } + + const prettyRepl = await import('pretty-repl') + console.log('Welcome to ZenStack REPL. See help with the ".help" command.'); console.log('Global variables:'); console.log(` ${colors.blue('db')} to access enhanced PrismaClient`); diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index ac727b917..ec5c3ef0b 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -5,6 +5,7 @@ import { isDataModel, isStringLiteral, ReferenceExpr, + isEnum, } from '@zenstackhq/language/ast'; import { getLiteral, getModelIdFields, getModelUniqueFields, isDelegateModel } from '@zenstackhq/sdk'; import { AstNode, DiagnosticInfo, getDocument, ValidationAcceptor } from 'langium'; @@ -61,8 +62,13 @@ export default class DataModelValidator implements AstValidator { if (idField.type.optional) { accept('error', 'Field with @id attribute must not be optional', { node: idField }); } - if (idField.type.array || !idField.type.type || !SCALAR_TYPES.includes(idField.type.type)) { - accept('error', 'Field with @id attribute must be of scalar type', { node: idField }); + + const isArray = idField.type.array; + const isScalar = SCALAR_TYPES.includes(idField.type.type as typeof SCALAR_TYPES[number]) + const isValidType = isScalar || isEnum(idField.type.reference?.ref) + + if (isArray || !isValidType) { + accept('error', 'Field with @id attribute must be of scalar or enum type', { node: idField }); } }); } diff --git a/packages/schema/src/language-server/validator/schema-validator.ts b/packages/schema/src/language-server/validator/schema-validator.ts index d3722638e..9e0512547 100644 --- a/packages/schema/src/language-server/validator/schema-validator.ts +++ b/packages/schema/src/language-server/validator/schema-validator.ts @@ -52,8 +52,9 @@ export default class SchemaValidator implements AstValidator { private validateImports(model: Model, accept: ValidationAcceptor) { model.imports.forEach((imp) => { const importedModel = resolveImport(this.documents, imp); + const importPath = imp.path.endsWith('.zmodel') ? imp.path : `${imp.path}.zmodel`; if (!importedModel) { - accept('error', `Cannot find model file ${imp.path}.zmodel`, { node: imp }); + accept('error', `Cannot find model file ${importPath}`, { node: imp }); } }); } diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 72d2a02e6..7e4e52af1 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -56,7 +56,7 @@ import { upperCaseFirst } from 'upper-case-first'; import { name } from '.'; import { getStringLiteral } from '../../language-server/validator/utils'; import telemetry from '../../telemetry'; -import { execSync } from '../../utils/exec-utils'; +import { execPackage } from '../../utils/exec-utils'; import { findPackageJson } from '../../utils/pkg-utils'; import { AttributeArgValue, @@ -142,7 +142,7 @@ export class PrismaSchemaGenerator { if (options.format === true) { try { // run 'prisma format' - await execSync(`npx prisma format --schema ${outFile}`, { stdio: 'ignore' }); + await execPackage(`prisma format --schema ${outFile}`, {stdio: 'ignore'}); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -151,18 +151,18 @@ export class PrismaSchemaGenerator { const generateClient = options.generateClient !== false; if (generateClient) { - let generateCmd = `npx prisma generate --schema "${outFile}"`; + let generateCmd = `prisma generate --schema "${outFile}"`; if (typeof options.generateArgs === 'string') { generateCmd += ` ${options.generateArgs}`; } try { // run 'prisma generate' - await execSync(generateCmd, { stdio: 'ignore' }); + await execPackage(generateCmd, {stdio: 'ignore'}); } catch { await this.trackPrismaSchemaError(outFile); try { // run 'prisma generate' again with output to the console - await execSync(generateCmd); + await execPackage(generateCmd); } catch { // noop } diff --git a/packages/schema/src/res/stdlib.zmodel b/packages/schema/src/res/stdlib.zmodel index fd470efb8..266b5c517 100644 --- a/packages/schema/src/res/stdlib.zmodel +++ b/packages/schema/src/res/stdlib.zmodel @@ -390,7 +390,7 @@ attribute @updatedAt() @@@targetField([DateTimeField]) @@@prisma /** * Add full text index (MySQL only). */ -attribute @@fulltext(_ fields: FieldReference[]) @@@prisma +attribute @@fulltext(_ fields: FieldReference[], map: String?) @@@prisma // String type modifiers @@ -479,7 +479,7 @@ attribute @db.Bytes() @@@targetField([BytesField]) @@@prisma attribute @db.ByteA() @@@targetField([BytesField]) @@@prisma attribute @db.LongBlob() @@@targetField([BytesField]) @@@prisma attribute @db.Binary() @@@targetField([BytesField]) @@@prisma -attribute @db.VarBinary() @@@targetField([BytesField]) @@@prisma +attribute @db.VarBinary(_ x: Int?) @@@targetField([BytesField]) @@@prisma attribute @db.TinyBlob() @@@targetField([BytesField]) @@@prisma attribute @db.Blob() @@@targetField([BytesField]) @@@prisma attribute @db.MediumBlob() @@@targetField([BytesField]) @@@prisma diff --git a/packages/schema/src/utils/exec-utils.ts b/packages/schema/src/utils/exec-utils.ts index d88e42b3d..03060a4f9 100644 --- a/packages/schema/src/utils/exec-utils.ts +++ b/packages/schema/src/utils/exec-utils.ts @@ -8,3 +8,11 @@ export function execSync(cmd: string, options?: Omit & { const mergedEnv = env ? { ...process.env, ...env } : undefined; _exec(cmd, { encoding: 'utf-8', stdio: options?.stdio ?? 'inherit', env: mergedEnv, ...restOptions }); } + +/** + * Utility for running package commands through npx/bunx + */ +export function execPackage(cmd: string, options?: Omit & { env?: Record }): void { + const packageManager = process?.versions?.bun ? 'bunx' : 'npx'; + execSync(`${packageManager} ${cmd}`, options) +} \ No newline at end of file diff --git a/packages/schema/tests/schema/stdlib.test.ts b/packages/schema/tests/schema/stdlib.test.ts index f4b1cc1fe..ad637be7a 100644 --- a/packages/schema/tests/schema/stdlib.test.ts +++ b/packages/schema/tests/schema/stdlib.test.ts @@ -24,7 +24,7 @@ describe('Stdlib Tests', () => { }` ); } - throw new SchemaLoadingError(validationErrors.map((e) => e.message)); + throw new SchemaLoadingError(validationErrors); } }); }); diff --git a/packages/schema/tests/schema/validation/attribute-validation.test.ts b/packages/schema/tests/schema/validation/attribute-validation.test.ts index ac87665b1..611f8dc60 100644 --- a/packages/schema/tests/schema/validation/attribute-validation.test.ts +++ b/packages/schema/tests/schema/validation/attribute-validation.test.ts @@ -226,6 +226,25 @@ describe('Attribute tests', () => { } `); + await loadModel(` + ${ prelude } + model A { + id String @id + x String + y String + z String + @@fulltext([x, y, z]) + } + + model B { + id String @id + x String + y String + z String + @@fulltext([x, y, z], map: "n") + } + `); + await loadModel(` ${prelude} model A { @@ -352,6 +371,7 @@ describe('Attribute tests', () => { _longBlob Bytes @db.LongBlob _binary Bytes @db.Binary _varBinary Bytes @db.VarBinary + _varBinarySized Bytes @db.VarBinary(100) _tinyBlob Bytes @db.TinyBlob _blob Bytes @db.Blob _mediumBlob Bytes @db.MediumBlob diff --git a/packages/schema/tests/schema/validation/datamodel-validation.test.ts b/packages/schema/tests/schema/validation/datamodel-validation.test.ts index 19535d5dd..955f315c3 100644 --- a/packages/schema/tests/schema/validation/datamodel-validation.test.ts +++ b/packages/schema/tests/schema/validation/datamodel-validation.test.ts @@ -1,4 +1,4 @@ -import { loadModel, loadModelWithError } from '../../utils'; +import { loadModel, safelyLoadModel, errorLike } from '../../utils'; describe('Data Model Validation Tests', () => { const prelude = ` @@ -9,20 +9,20 @@ describe('Data Model Validation Tests', () => { `; it('duplicated fields', async () => { - expect( - await loadModelWithError(` - ${prelude} - model M { - id String @id - x Int - x String - } - `) - ).toContain('Duplicated declaration name "x"'); + const result = await safelyLoadModel(` + ${prelude} + model M { + id String @id + x Int + x String + } + `); + + expect(result).toMatchObject(errorLike('Duplicated declaration name "x"')); }); it('scalar types', async () => { - await loadModel(` + const result = await safelyLoadModel(` ${prelude} model M { id String @id @@ -38,33 +38,36 @@ describe('Data Model Validation Tests', () => { i Bytes } `); + expect(result).toMatchObject({ status: 'fulfilled' }); }); it('Unsupported type valid arg', async () => { - await loadModel(` + const result = await safelyLoadModel(` ${prelude} model M { id String @id a Unsupported('foo') } `); + + expect(result).toMatchObject({ status: 'fulfilled' }); }); it('Unsupported type invalid arg', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id a Unsupported(123) } `) - ).toContain('Unsupported type argument must be a string literal'); + ).toMatchObject(errorLike('Unsupported type argument must be a string literal')); }); it('Unsupported type used in expression', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id @@ -72,79 +75,83 @@ describe('Data Model Validation Tests', () => { @@allow('all', a == 'a') } `) - ).toContain('Field of "Unsupported" type cannot be used in expressions'); + ).toMatchObject(errorLike('Field of "Unsupported" type cannot be used in expressions')); }); it('mix array and optional', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Int[]? } `) - ).toContain('Optional lists are not supported. Use either `Type[]` or `Type?`'); + ).toMatchObject(errorLike('Optional lists are not supported. Use either `Type[]` or `Type?`')); }); it('unresolved field type', async () => { expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Integer } `) - ).toContain(`Could not resolve reference to TypeDeclaration named 'Integer'.`); + ).toMatchObject(errorLike(`Could not resolve reference to TypeDeclaration named 'Integer'.`)); expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Integer[] } `) - ).toContain(`Could not resolve reference to TypeDeclaration named 'Integer'.`); + ).toMatchObject(errorLike(`Could not resolve reference to TypeDeclaration named 'Integer'.`)); expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model M { id String @id x Integer? } `) - ).toContain(`Could not resolve reference to TypeDeclaration named 'Integer'.`); + ).toMatchObject(errorLike(`Could not resolve reference to TypeDeclaration named 'Integer'.`)); }); - it('id field', async () => { + describe('id field', () => { const err = 'Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.'; - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int - @@allow('all', x > 0) - } - `) - ).toContain(err); + it('should error when there are no unique fields', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int + @@allow('all', x > 0) + } + `); + expect(result).toMatchObject(errorLike(err)); + }); - // @unique used as id - await loadModel(` - ${prelude} - model M { - id Int @unique - x Int - @@allow('all', x > 0) - } - `); + it('should should use @unique when there is no @id', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + id Int @unique + x Int + @@allow('all', x > 0) + } + `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }); // @@unique used as id - await loadModel(` + it('should suceed when @@unique used as id', async () => { + const result = await safelyLoadModel(` ${prelude} model M { x Int @@ -152,128 +159,176 @@ describe('Data Model Validation Tests', () => { @@allow('all', x > 0) } `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int - @@deny('all', x <= 0) - } - `) - ).toContain(err); + it('should succeed when @id is an enum type', async () => { + const result = await safelyLoadModel(` + ${prelude} + enum E { + A + B + } + model M { + id E @id + } + `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int @gt(0) - } - `) - ).toContain(err); + it('should succeed when @@id is an enum type', async () => { + const result = await safelyLoadModel(` + ${prelude} + enum E { + A + B + } + model M { + x Int + y E + @@id([x, y]) + } + `); + expect(result).toMatchObject({ status: 'fulfilled' }); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int @id - y Int @id - } - `) - ).toContain(`Model can include at most one field with @id attribute`); + it('should error when there are no id fields, even when denying access', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int + @@deny('all', x <= 0) + } + `); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int @id - y Int - @@id([x, y]) - } - `) - ).toContain(`Model cannot have both field-level @id and model-level @@id attributes`); + expect(result).toMatchObject(errorLike(err)); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int? @id - } - `) - ).toContain(`Field with @id attribute must not be optional`); + it('should error when there are not id fields, without access restrictions', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int @gt(0) + } + `); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int? - @@id([x]) - } - `) - ).toContain(`Field with @id attribute must not be optional`); + expect(result).toMatchObject(errorLike(err)); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int[] @id - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when there is more than one field marked as @id', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int @id + y Int @id + } + `); + expect(result).toMatchObject(errorLike(`Model can include at most one field with @id attribute`)); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Int[] - @@id([x]) - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when both @id and @@id are used', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int @id + y Int + @@id([x, y]) + } + `); + expect(result).toMatchObject( + errorLike(`Model cannot have both field-level @id and model-level @@id attributes`) + ); + }); + + it('should error when @id used on optional field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int? @id + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`)); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Json @id - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @@id used on optional field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int? + @@id([x]) + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`)); + }); - expect( - await loadModelWithError(` - ${prelude} - model M { - x Json - @@id([x]) - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @id used on list field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int[] @id + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)); + }); - expect( - await loadModelWithError(` - ${prelude} - model Id { - id String @id - } - model M { - myId Id @id - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @@id used on list field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Int[] + @@id([x]) + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)); + }); - expect( - await loadModelWithError(` - ${prelude} - model Id { - id String @id - } - model M { - myId Id - @@id([myId]) - } - `) - ).toContain(`Field with @id attribute must be of scalar type`); + it('should error when @id used on a Json field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Json @id + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)); + }); + + it('should error when @@id used on a Json field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model M { + x Json + @@id([x]) + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)); + }); + + it('should error when @id used on a reference field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model Id { + id String @id + } + model M { + myId Id @id + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)); + }); + + it('should error when @@id used on a reference field', async () => { + const result = await safelyLoadModel(` + ${prelude} + model Id { + id String @id + } + model M { + myId Id + @@id([myId]) + } + `); + expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`)); + }); }); it('relation', async () => { @@ -326,7 +381,7 @@ describe('Data Model Validation Tests', () => { // one-to-one incomplete expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -337,11 +392,13 @@ describe('Data Model Validation Tests', () => { id String @id } `) - ).toContain(`The relation field "b" on model "A" is missing an opposite relation field on model "B"`); + ).toMatchObject( + errorLike(`The relation field "b" on model "A" is missing an opposite relation field on model "B"`) + ); // one-to-one ambiguous expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -354,11 +411,11 @@ describe('Data Model Validation Tests', () => { a1 A } `) - ).toContain(`Fields "a", "a1" on model "B" refer to the same relation to model "A"`); + ).toMatchObject(errorLike(`Fields "a", "a1" on model "B" refer to the same relation to model "A"`)); // fields or references missing expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -371,11 +428,11 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain(`Both "fields" and "references" must be provided`); + ).toMatchObject(errorLike(`Both "fields" and "references" must be provided`)); // one-to-one inconsistent attribute expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -388,11 +445,11 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain(`"fields" and "references" must be provided only on one side of relation field`); + ).toMatchObject(errorLike(`"fields" and "references" must be provided only on one side of relation field`)); // references mismatch expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { myId Int @id @@ -405,11 +462,11 @@ describe('Data Model Validation Tests', () => { aId String @unique } `) - ).toContain(`values of "references" and "fields" must have the same type`); + ).toMatchObject(errorLike(`values of "references" and "fields" must have the same type`)); // "fields" and "references" typing consistency expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id Int @id @@ -422,11 +479,11 @@ describe('Data Model Validation Tests', () => { aId String @unique } `) - ).toContain(`values of "references" and "fields" must have the same type`); + ).toMatchObject(errorLike(`values of "references" and "fields" must have the same type`)); // one-to-one missing @unique expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -439,13 +496,15 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain( - `Field "aId" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute` + ).toMatchObject( + errorLike( + `Field "aId" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute` + ) ); // missing @relation expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -457,13 +516,15 @@ describe('Data Model Validation Tests', () => { a A } `) - ).toContain( - `Field for one side of relation must carry @relation attribute with both "fields" and "references" fields` + ).toMatchObject( + errorLike( + `Field for one side of relation must carry @relation attribute with both "fields" and "references" fields` + ) ); // wrong relation owner field type expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -476,11 +537,11 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toContain(`Relation field needs to be list or optional`); + ).toMatchObject(errorLike(`Relation field needs to be list or optional`)); // unresolved field expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model A { id String @id @@ -492,7 +553,7 @@ describe('Data Model Validation Tests', () => { a A @relation(fields: [aId], references: [id]) } `) - ).toContain(`Could not resolve reference to ReferenceTarget named 'aId'.`); + ).toMatchObject(errorLike(`Could not resolve reference to ReferenceTarget named 'aId'.`)); // enum as foreign key await loadModel(` @@ -607,7 +668,7 @@ describe('Data Model Validation Tests', () => { }); it('abstract base type', async () => { - const errors = await loadModelWithError(` + const errors = await safelyLoadModel(` ${prelude} abstract model Base { @@ -623,13 +684,13 @@ describe('Data Model Validation Tests', () => { } `); - expect(errors[0]).toEqual( - 'Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.' + expect(errors).toMatchObject( + errorLike(`Model A cannot be extended because it's neither abstract nor marked as "@@delegate"`) ); // relation incomplete from multiple level inheritance expect( - await loadModelWithError(` + await safelyLoadModel(` ${prelude} model User { id Int @id @default(autoincrement()) @@ -649,6 +710,8 @@ describe('Data Model Validation Tests', () => { a String } `) - ).toContain(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`); + ).toMatchObject( + errorLike(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`) + ); }); }); diff --git a/packages/schema/tests/schema/validation/datasource-validation.test.ts b/packages/schema/tests/schema/validation/datasource-validation.test.ts index 19be1f076..469ba5ac1 100644 --- a/packages/schema/tests/schema/validation/datasource-validation.test.ts +++ b/packages/schema/tests/schema/validation/datasource-validation.test.ts @@ -1,18 +1,21 @@ -import { loadModel, loadModelWithError } from '../../utils'; +import { loadModel, loadModelWithError, safelyLoadModel } from '../../utils'; describe('Datasource Validation Tests', () => { it('missing fields', async () => { - expect( - await loadModelWithError(` + const result = await safelyLoadModel(` datasource db { } - `) - ).toEqual( - expect.arrayContaining([ - 'datasource must include a "provider" field', - 'datasource must include a "url" field', - ]) - ); + `); + + expect(result).toMatchObject({ + status: 'rejected', + reason: { + cause: [ + { message: 'datasource must include a "provider" field' }, + { message: 'datasource must include a "url" field' }, + ] + } + }) }); it('dup fields', async () => { @@ -41,7 +44,7 @@ describe('Datasource Validation Tests', () => { provider = 'abc' } `) - ).toContainEqual(expect.stringContaining('Provider "abc" is not supported')); + ).toContain('Provider "abc" is not supported'); }); it('invalid url value', async () => { diff --git a/packages/schema/tests/schema/validation/schema-validation.test.ts b/packages/schema/tests/schema/validation/schema-validation.test.ts index 5f1cc6254..ca0efa697 100644 --- a/packages/schema/tests/schema/validation/schema-validation.test.ts +++ b/packages/schema/tests/schema/validation/schema-validation.test.ts @@ -39,6 +39,20 @@ describe('Toplevel Schema Validation Tests', () => { ).toContain('Cannot find model file models/abc.zmodel'); }); + it('not existing import with extension', async () => { + expect( + await loadModelWithError(` + import 'models/abc.zmodel' + datasource db1 { + provider = 'postgresql' + url = env('DATABASE_URL') + } + + model X {id String @id } + `) + ).toContain('Cannot find model file models/abc.zmodel'); + }) + it('multiple auth models', async () => { expect( await loadModelWithError(` diff --git a/packages/schema/tests/utils.ts b/packages/schema/tests/utils.ts index 4dcd45170..dc424b93a 100644 --- a/packages/schema/tests/utils.ts +++ b/packages/schema/tests/utils.ts @@ -7,9 +7,21 @@ import { URI } from 'vscode-uri'; import { createZModelServices } from '../src/language-server/zmodel-module'; import { mergeBaseModel } from '../src/utils/ast-utils'; -export class SchemaLoadingError extends Error { - constructor(public readonly errors: string[]) { - super('Schema error:\n' + errors.join('\n')); +type Errorish = Error | { message: string; stack?: string } | string; + +export class SchemaLoadingError extends Error { + cause: Errors + constructor(public readonly errors: Errors) { + const stack = errors.find((e): e is typeof e & { stack: string } => typeof e === 'object' && 'stack' in e)?.stack; + const message = errors.map((e) => (typeof e === 'string' ? e : e.message)).join('\n'); + + super(`Schema error:\n${ message }`); + + if (stack) { + const shiftedStack = stack.split('\n').slice(1).join('\n'); + this.stack = shiftedStack + } + this.cause = errors } } @@ -23,11 +35,11 @@ export async function loadModel(content: string, validate = true, verbose = true const doc = shared.workspace.LangiumDocuments.getOrCreateDocument(URI.file(docPath)); if (doc.parseResult.lexerErrors.length > 0) { - throw new SchemaLoadingError(doc.parseResult.lexerErrors.map((e) => e.message)); + throw new SchemaLoadingError(doc.parseResult.lexerErrors); } if (doc.parseResult.parserErrors.length > 0) { - throw new SchemaLoadingError(doc.parseResult.parserErrors.map((e) => e.message)); + throw new SchemaLoadingError(doc.parseResult.parserErrors); } await shared.workspace.DocumentBuilder.build([stdLib, doc], { @@ -46,7 +58,7 @@ export async function loadModel(content: string, validate = true, verbose = true ); } } - throw new SchemaLoadingError(validationErrors.map((e) => e.message)); + throw new SchemaLoadingError(validationErrors); } const model = (await doc.parseResult.value) as Model; @@ -65,7 +77,19 @@ export async function loadModelWithError(content: string, verbose = false) { if (!(err instanceof SchemaLoadingError)) { throw err; } - return (err as SchemaLoadingError).errors; + return (err as SchemaLoadingError).message; } throw new Error('No error is thrown'); } + +export async function safelyLoadModel(content: string, validate = true, verbose = false) { + const [ result ] = await Promise.allSettled([ loadModel(content, validate, verbose) ]); + + return result +} + +export const errorLike = (msg: string) => ({ + reason: { + message: expect.stringContaining(msg) + }, +}) From 9cb91f3cacfc8e49589f0b10e6c878ffd56ee372 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Tue, 20 Feb 2024 09:25:29 -0800 Subject: [PATCH 15/17] fix styling --- packages/schema/src/plugins/prisma/schema-generator.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 7e4e52af1..7eebc3040 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -142,7 +142,7 @@ export class PrismaSchemaGenerator { if (options.format === true) { try { // run 'prisma format' - await execPackage(`prisma format --schema ${outFile}`, {stdio: 'ignore'}); + await execPackage(`prisma format --schema ${outFile}`, { stdio: 'ignore' }); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -157,7 +157,7 @@ export class PrismaSchemaGenerator { } try { // run 'prisma generate' - await execPackage(generateCmd, {stdio: 'ignore'}); + await execPackage(generateCmd, { stdio: 'ignore' }); } catch { await this.trackPrismaSchemaError(outFile); try { From 10cb00a8ead120846698435c60825172af35f644 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:48:00 -0800 Subject: [PATCH 16/17] try run tests in parallel --- tests/integration/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/package.json b/tests/integration/package.json index cace90307..40627f354 100644 --- a/tests/integration/package.json +++ b/tests/integration/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "lint": "eslint . --ext .ts", - "test": "ZENSTACK_TEST=1 jest --runInBand" + "test": "ZENSTACK_TEST=1 jest" }, "keywords": [], "author": "", From da53753f1bc912272c1a7e640bcecff0faf5a596 Mon Sep 17 00:00:00 2001 From: Yiming Date: Tue, 20 Feb 2024 15:22:56 -0800 Subject: [PATCH 17/17] chore: improve test run speed (#1018) --- .github/workflows/build-test.yml | 2 +- .gitignore | 1 + CONTRIBUTING.md | 8 + jest.config.ts | 6 +- package.json | 6 +- packages/testtools/package.json | 2 +- packages/testtools/src/.npmrc.template | 1 - packages/testtools/src/package.template.json | 21 -- packages/testtools/src/schema.ts | 71 +++-- pnpm-lock.yaml | 262 +++++++++++++++++++ script/test-scaffold.ts | 24 ++ test-setup.ts | 9 + tests/integration/global-setup.js | 10 - tests/integration/jest.config.ts | 26 +- tests/integration/tests/cli/generate.test.ts | 6 +- tests/integration/tests/cli/plugins.test.ts | 6 +- tests/integration/tests/schema/todo.zmodel | 2 +- 17 files changed, 369 insertions(+), 94 deletions(-) delete mode 100644 packages/testtools/src/.npmrc.template delete mode 100644 packages/testtools/src/package.template.json create mode 100644 script/test-scaffold.ts create mode 100644 test-setup.ts delete mode 100644 tests/integration/global-setup.js diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index f92512bc8..404f42d3a 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -97,4 +97,4 @@ jobs: run: pnpm install --frozen-lockfile - name: Test - run: pnpm run test-ci + run: pnpm run test-scaffold && pnpm run test-ci diff --git a/.gitignore b/.gitignore index 9b5f3d7c8..307f58a86 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ dist .npmcache coverage .build +.test diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f1f733983..97319e08c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -35,6 +35,14 @@ I want to think you first for considering contributing to ZenStack 🙏🏻. It' pnpm build ``` +1. Scaffold the project used for testing + + ```bash + pnpm test-scaffold + ``` + + You only need to run this command once. + 1. Run tests ```bash diff --git a/jest.config.ts b/jest.config.ts index 917cf52f6..222e6fb2c 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -3,15 +3,19 @@ * https://jestjs.io/docs/configuration */ +import path from 'path'; + export default { // Automatically clear mock calls, instances, contexts and results before every test clearMocks: true, + globalSetup: path.join(__dirname, './test-setup.ts'), + // Indicates whether the coverage information should be collected while executing the test collectCoverage: true, // The directory where Jest should output its coverage files - coverageDirectory: 'tests/coverage', + coverageDirectory: path.join(__dirname, '.test/coverage'), // An array of regexp pattern strings used to skip coverage collection coveragePathIgnorePatterns: ['/node_modules/', '/tests/'], diff --git a/package.json b/package.json index 2d2d2e264..42168988a 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,9 @@ "scripts": { "build": "pnpm -r build", "lint": "pnpm -r lint", - "test": "ZENSTACK_TEST=1 pnpm -r run test --silent --forceExit", - "test-ci": "ZENSTACK_TEST=1 pnpm -r run test --silent --forceExit", + "test": "ZENSTACK_TEST=1 pnpm -r --parallel run test --silent --forceExit", + "test-ci": "ZENSTACK_TEST=1 pnpm -r --parallel run test --silent --forceExit", + "test-scaffold": "tsx script/test-scaffold.ts", "publish-all": "pnpm --filter \"./packages/**\" -r publish --access public", "publish-preview": "pnpm --filter \"./packages/**\" -r publish --force --registry https://preview.registry.zenstack.dev/", "unpublish-preview": "pnpm --recursive --shell-mode exec -- npm unpublish -f --registry https://preview.registry.zenstack.dev/ \"\\$PNPM_PACKAGE_NAME\"" @@ -30,6 +31,7 @@ "ts-jest": "^29.1.1", "ts-node": "^10.9.1", "tsup": "^8.0.1", + "tsx": "^4.7.1", "typescript": "^5.3.2" } } diff --git a/packages/testtools/package.json b/packages/testtools/package.json index 0aafe452e..38198a59e 100644 --- a/packages/testtools/package.json +++ b/packages/testtools/package.json @@ -11,7 +11,7 @@ "scripts": { "clean": "rimraf dist", "lint": "eslint src --ext ts", - "build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./LICENSE ./README.md dist && copyfiles -u 1 src/package.template.json src/.npmrc.template dist && pnpm pack dist --pack-destination '../../../.build'", + "build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./LICENSE ./README.md dist && pnpm pack dist --pack-destination '../../../.build'", "watch": "tsc --watch", "prepublishOnly": "pnpm build" }, diff --git a/packages/testtools/src/.npmrc.template b/packages/testtools/src/.npmrc.template deleted file mode 100644 index 14f2c2865..000000000 --- a/packages/testtools/src/.npmrc.template +++ /dev/null @@ -1 +0,0 @@ -cache=/.npmcache diff --git a/packages/testtools/src/package.template.json b/packages/testtools/src/package.template.json deleted file mode 100644 index 8ea542361..000000000 --- a/packages/testtools/src/package.template.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "test-run", - "version": "1.0.0", - "description": "", - "main": "index.js", - "keywords": [], - "author": "", - "license": "ISC", - "dependencies": { - "@prisma/client": "^4.8.0", - "@zenstackhq/runtime": "file:/packages/runtime/dist", - "@zenstackhq/swr": "file:/packages/plugins/swr/dist", - "@zenstackhq/trpc": "file:/packages/plugins/trpc/dist", - "@zenstackhq/openapi": "file:/packages/plugins/openapi/dist", - "prisma": "^4.8.0", - "typescript": "^4.9.3", - "zenstack": "file:/packages/schema/dist", - "zod": "^3.22.4", - "decimal.js": "^10.4.2" - } -} diff --git a/packages/testtools/src/schema.ts b/packages/testtools/src/schema.ts index f69a845cc..c570c6a30 100644 --- a/packages/testtools/src/schema.ts +++ b/packages/testtools/src/schema.ts @@ -35,14 +35,23 @@ export type FullDbClientContract = Record & { }; export function run(cmd: string, env?: Record, cwd?: string) { - const start = Date.now(); - execSync(cmd, { - stdio: 'pipe', - encoding: 'utf-8', - env: { ...process.env, DO_NOT_TRACK: '1', ...env }, - cwd, - }); - console.log('Execution took', Date.now() - start, 'ms', '-', cmd); + try { + const start = Date.now(); + execSync(cmd, { + stdio: 'pipe', + encoding: 'utf-8', + env: { ...process.env, DO_NOT_TRACK: '1', ...env }, + cwd, + }); + console.log('Execution took', Date.now() - start, 'ms', '-', cmd); + } catch (err) { + console.error('Command failed:', cmd, err); + throw err; + } +} + +export function installPackage(pkg: string, dev = false) { + run(`npm install ${dev ? '-D' : ''} --no-audit --no-fund ${pkg}`); } function normalizePath(p: string) { @@ -86,17 +95,17 @@ generator js { plugin meta { provider = '@core/model-meta' - preserveTsFiles = true + // preserveTsFiles = true } plugin policy { provider = '@core/access-policy' - preserveTsFiles = true + // preserveTsFiles = true } plugin zod { provider = '@core/zod' - preserveTsFiles = true + // preserveTsFiles = true modelOnly = ${!options.fullZod} } `; @@ -138,21 +147,29 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { const { name: projectRoot } = tmp.dirSync({ unsafeCleanup: true }); - const root = getWorkspaceRoot(__dirname); + const workspaceRoot = getWorkspaceRoot(__dirname); - if (!root) { + if (!workspaceRoot) { throw new Error('Could not find workspace root'); } - const pkgContent = fs.readFileSync(path.join(__dirname, 'package.template.json'), { encoding: 'utf-8' }); - fs.writeFileSync(path.join(projectRoot, 'package.json'), pkgContent.replaceAll('', root)); - - const npmrcContent = fs.readFileSync(path.join(__dirname, '.npmrc.template'), { encoding: 'utf-8' }); - fs.writeFileSync(path.join(projectRoot, '.npmrc'), npmrcContent.replaceAll('', root)); - console.log('Workdir:', projectRoot); process.chdir(projectRoot); + // copy project structure from scaffold (prepared by test-setup.ts) + fs.cpSync(path.join(workspaceRoot, '.test/scaffold'), projectRoot, { recursive: true, force: true }); + + // install local deps + const localInstallDeps = [ + 'packages/schema/dist', + 'packages/runtime/dist', + 'packages/plugins/swr/dist', + 'packages/plugins/trpc/dist', + 'packages/plugins/openapi/dist', + ]; + + run(`npm i --no-audit --no-fund ${localInstallDeps.map((d) => path.join(workspaceRoot, d)).join(' ')}`); + let zmodelPath = path.join(projectRoot, 'schema.zmodel'); const files = schema.split(FILE_SPLITTER); @@ -189,16 +206,16 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { } } - run('npm install'); - const outputArg = opt.output ? ` --output ${opt.output}` : ''; if (opt.customSchemaFilePath) { - run(`npx zenstack generate --schema ${zmodelPath} --no-dependency-check${outputArg}`, { + run(`npx zenstack generate --no-version-check --schema ${zmodelPath} --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules', }); } else { - run(`npx zenstack generate --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules' }); + run(`npx zenstack generate --no-version-check --no-dependency-check${outputArg}`, { + NODE_PATH: './node_modules', + }); } if (opt.pushDb) { @@ -209,10 +226,10 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { opt.extraDependencies?.push('@prisma/extension-pulse'); } - opt.extraDependencies?.forEach((dep) => { - console.log(`Installing dependency ${dep}`); - run(`npm install ${dep}`); - }); + if (opt.extraDependencies) { + console.log(`Installing dependency ${opt.extraDependencies.join(' ')}`); + installPackage(opt.extraDependencies.join(' ')); + } opt.copyDependencies?.forEach((dep) => { const pkgJson = JSON.parse(fs.readFileSync(path.join(dep, 'package.json'), { encoding: 'utf-8' })); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9dcef261b..b432b95c7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -53,6 +53,9 @@ importers: tsup: specifier: ^8.0.1 version: 8.0.1(ts-node@10.9.1)(typescript@5.3.2) + tsx: + specifier: ^4.7.1 + version: 4.7.1 typescript: specifier: ^5.3.2 version: 5.3.2 @@ -1594,6 +1597,15 @@ packages: tslib: 2.6.0 dev: true + /@esbuild/aix-ppc64@0.19.12: + resolution: {integrity: sha512-bmoCYyWdEL3wDQIVbcyzRyeKLgk2WtWLTWz1ZIAZF/EGbNOwSA6ew3PftJ1PqMiOOGu0OyFMzG53L0zqIpPeNA==} + engines: {node: '>=12'} + cpu: [ppc64] + os: [aix] + requiresBuild: true + dev: true + optional: true + /@esbuild/android-arm64@0.17.19: resolution: {integrity: sha512-KBMWvEZooR7+kzY0BtbTQn0OAYY7CsiydT63pVEaPtVYF0hXbUaOyZog37DKxK7NF3XacBJOpYT4adIJh+avxA==} engines: {node: '>=12'} @@ -1612,6 +1624,15 @@ packages: dev: true optional: true + /@esbuild/android-arm64@0.19.12: + resolution: {integrity: sha512-P0UVNGIienjZv3f5zq0DP3Nt2IE/3plFzuaS96vihvD0Hd6H/q4WXUGpCxD/E8YrSXfNyRPbpTq+T8ZQioSuPA==} + engines: {node: '>=12'} + cpu: [arm64] + os: [android] + requiresBuild: true + dev: true + optional: true + /@esbuild/android-arm64@0.19.4: resolution: {integrity: sha512-mRsi2vJsk4Bx/AFsNBqOH2fqedxn5L/moT58xgg51DjX1la64Z3Npicut2VbhvDFO26qjWtPMsVxCd80YTFVeg==} engines: {node: '>=12'} @@ -1648,6 +1669,15 @@ packages: dev: true optional: true + /@esbuild/android-arm@0.19.12: + resolution: {integrity: sha512-qg/Lj1mu3CdQlDEEiWrlC4eaPZ1KztwGJ9B6J+/6G+/4ewxJg7gqj8eVYWvao1bXrqGiW2rsBZFSX3q2lcW05w==} + engines: {node: '>=12'} + cpu: [arm] + os: [android] + requiresBuild: true + dev: true + optional: true + /@esbuild/android-arm@0.19.4: resolution: {integrity: sha512-uBIbiYMeSsy2U0XQoOGVVcpIktjLMEKa7ryz2RLr7L/vTnANNEsPVAh4xOv7ondGz6ac1zVb0F8Jx20rQikffQ==} engines: {node: '>=12'} @@ -1675,6 +1705,15 @@ packages: dev: true optional: true + /@esbuild/android-x64@0.19.12: + resolution: {integrity: sha512-3k7ZoUW6Q6YqhdhIaq/WZ7HwBpnFBlW905Fa4s4qWJyiNOgT1dOqDiVAQFwBH7gBRZr17gLrlFCRzF6jFh7Kew==} + engines: {node: '>=12'} + cpu: [x64] + os: [android] + requiresBuild: true + dev: true + optional: true + /@esbuild/android-x64@0.19.4: resolution: {integrity: sha512-4iPufZ1TMOD3oBlGFqHXBpa3KFT46aLl6Vy7gwed0ZSYgHaZ/mihbYb4t7Z9etjkC9Al3ZYIoOaHrU60gcMy7g==} engines: {node: '>=12'} @@ -1702,6 +1741,15 @@ packages: dev: true optional: true + /@esbuild/darwin-arm64@0.19.12: + resolution: {integrity: sha512-B6IeSgZgtEzGC42jsI+YYu9Z3HKRxp8ZT3cqhvliEHovq8HSX2YX8lNocDn79gCKJXOSaEot9MVYky7AKjCs8g==} + engines: {node: '>=12'} + cpu: [arm64] + os: [darwin] + requiresBuild: true + dev: true + optional: true + /@esbuild/darwin-arm64@0.19.4: resolution: {integrity: sha512-Lviw8EzxsVQKpbS+rSt6/6zjn9ashUZ7Tbuvc2YENgRl0yZTktGlachZ9KMJUsVjZEGFVu336kl5lBgDN6PmpA==} engines: {node: '>=12'} @@ -1729,6 +1777,15 @@ packages: dev: true optional: true + /@esbuild/darwin-x64@0.19.12: + resolution: {integrity: sha512-hKoVkKzFiToTgn+41qGhsUJXFlIjxI/jSYeZf3ugemDYZldIXIxhvwN6erJGlX4t5h417iFuheZ7l+YVn05N3A==} + engines: {node: '>=12'} + cpu: [x64] + os: [darwin] + requiresBuild: true + dev: true + optional: true + /@esbuild/darwin-x64@0.19.4: resolution: {integrity: sha512-YHbSFlLgDwglFn0lAO3Zsdrife9jcQXQhgRp77YiTDja23FrC2uwnhXMNkAucthsf+Psr7sTwYEryxz6FPAVqw==} engines: {node: '>=12'} @@ -1756,6 +1813,15 @@ packages: dev: true optional: true + /@esbuild/freebsd-arm64@0.19.12: + resolution: {integrity: sha512-4aRvFIXmwAcDBw9AueDQ2YnGmz5L6obe5kmPT8Vd+/+x/JMVKCgdcRwH6APrbpNXsPz+K653Qg8HB/oXvXVukA==} + engines: {node: '>=12'} + cpu: [arm64] + os: [freebsd] + requiresBuild: true + dev: true + optional: true + /@esbuild/freebsd-arm64@0.19.4: resolution: {integrity: sha512-vz59ijyrTG22Hshaj620e5yhs2dU1WJy723ofc+KUgxVCM6zxQESmWdMuVmUzxtGqtj5heHyB44PjV/HKsEmuQ==} engines: {node: '>=12'} @@ -1783,6 +1849,15 @@ packages: dev: true optional: true + /@esbuild/freebsd-x64@0.19.12: + resolution: {integrity: sha512-EYoXZ4d8xtBoVN7CEwWY2IN4ho76xjYXqSXMNccFSx2lgqOG/1TBPW0yPx1bJZk94qu3tX0fycJeeQsKovA8gg==} + engines: {node: '>=12'} + cpu: [x64] + os: [freebsd] + requiresBuild: true + dev: true + optional: true + /@esbuild/freebsd-x64@0.19.4: resolution: {integrity: sha512-3sRbQ6W5kAiVQRBWREGJNd1YE7OgzS0AmOGjDmX/qZZecq8NFlQsQH0IfXjjmD0XtUYqr64e0EKNFjMUlPL3Cw==} engines: {node: '>=12'} @@ -1810,6 +1885,15 @@ packages: dev: true optional: true + /@esbuild/linux-arm64@0.19.12: + resolution: {integrity: sha512-EoTjyYyLuVPfdPLsGVVVC8a0p1BFFvtpQDB/YLEhaXyf/5bczaGeN15QkR+O4S5LeJ92Tqotve7i1jn35qwvdA==} + engines: {node: '>=12'} + cpu: [arm64] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-arm64@0.19.4: resolution: {integrity: sha512-ZWmWORaPbsPwmyu7eIEATFlaqm0QGt+joRE9sKcnVUG3oBbr/KYdNE2TnkzdQwX6EDRdg/x8Q4EZQTXoClUqqA==} engines: {node: '>=12'} @@ -1837,6 +1921,15 @@ packages: dev: true optional: true + /@esbuild/linux-arm@0.19.12: + resolution: {integrity: sha512-J5jPms//KhSNv+LO1S1TX1UWp1ucM6N6XuL6ITdKWElCu8wXP72l9MM0zDTzzeikVyqFE6U8YAV9/tFyj0ti+w==} + engines: {node: '>=12'} + cpu: [arm] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-arm@0.19.4: resolution: {integrity: sha512-z/4ArqOo9EImzTi4b6Vq+pthLnepFzJ92BnofU1jgNlcVb+UqynVFdoXMCFreTK7FdhqAzH0vmdwW5373Hm9pg==} engines: {node: '>=12'} @@ -1864,6 +1957,15 @@ packages: dev: true optional: true + /@esbuild/linux-ia32@0.19.12: + resolution: {integrity: sha512-Thsa42rrP1+UIGaWz47uydHSBOgTUnwBwNq59khgIwktK6x60Hivfbux9iNR0eHCHzOLjLMLfUMLCypBkZXMHA==} + engines: {node: '>=12'} + cpu: [ia32] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-ia32@0.19.4: resolution: {integrity: sha512-EGc4vYM7i1GRUIMqRZNCTzJh25MHePYsnQfKDexD8uPTCm9mK56NIL04LUfX2aaJ+C9vyEp2fJ7jbqFEYgO9lQ==} engines: {node: '>=12'} @@ -1900,6 +2002,15 @@ packages: dev: true optional: true + /@esbuild/linux-loong64@0.19.12: + resolution: {integrity: sha512-LiXdXA0s3IqRRjm6rV6XaWATScKAXjI4R4LoDlvO7+yQqFdlr1Bax62sRwkVvRIrwXxvtYEHHI4dm50jAXkuAA==} + engines: {node: '>=12'} + cpu: [loong64] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-loong64@0.19.4: resolution: {integrity: sha512-WVhIKO26kmm8lPmNrUikxSpXcgd6HDog0cx12BUfA2PkmURHSgx9G6vA19lrlQOMw+UjMZ+l3PpbtzffCxFDRg==} engines: {node: '>=12'} @@ -1927,6 +2038,15 @@ packages: dev: true optional: true + /@esbuild/linux-mips64el@0.19.12: + resolution: {integrity: sha512-fEnAuj5VGTanfJ07ff0gOA6IPsvrVHLVb6Lyd1g2/ed67oU1eFzL0r9WL7ZzscD+/N6i3dWumGE1Un4f7Amf+w==} + engines: {node: '>=12'} + cpu: [mips64el] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-mips64el@0.19.4: resolution: {integrity: sha512-keYY+Hlj5w86hNp5JJPuZNbvW4jql7c1eXdBUHIJGTeN/+0QFutU3GrS+c27L+NTmzi73yhtojHk+lr2+502Mw==} engines: {node: '>=12'} @@ -1954,6 +2074,15 @@ packages: dev: true optional: true + /@esbuild/linux-ppc64@0.19.12: + resolution: {integrity: sha512-nYJA2/QPimDQOh1rKWedNOe3Gfc8PabU7HT3iXWtNUbRzXS9+vgB0Fjaqr//XNbd82mCxHzik2qotuI89cfixg==} + engines: {node: '>=12'} + cpu: [ppc64] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-ppc64@0.19.4: resolution: {integrity: sha512-tQ92n0WMXyEsCH4m32S21fND8VxNiVazUbU4IUGVXQpWiaAxOBvtOtbEt3cXIV3GEBydYsY8pyeRMJx9kn3rvw==} engines: {node: '>=12'} @@ -1981,6 +2110,15 @@ packages: dev: true optional: true + /@esbuild/linux-riscv64@0.19.12: + resolution: {integrity: sha512-2MueBrlPQCw5dVJJpQdUYgeqIzDQgw3QtiAHUC4RBz9FXPrskyyU3VI1hw7C0BSKB9OduwSJ79FTCqtGMWqJHg==} + engines: {node: '>=12'} + cpu: [riscv64] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-riscv64@0.19.4: resolution: {integrity: sha512-tRRBey6fG9tqGH6V75xH3lFPpj9E8BH+N+zjSUCnFOX93kEzqS0WdyJHkta/mmJHn7MBaa++9P4ARiU4ykjhig==} engines: {node: '>=12'} @@ -2008,6 +2146,15 @@ packages: dev: true optional: true + /@esbuild/linux-s390x@0.19.12: + resolution: {integrity: sha512-+Pil1Nv3Umes4m3AZKqA2anfhJiVmNCYkPchwFJNEJN5QxmTs1uzyy4TvmDrCRNT2ApwSari7ZIgrPeUx4UZDg==} + engines: {node: '>=12'} + cpu: [s390x] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-s390x@0.19.4: resolution: {integrity: sha512-152aLpQqKZYhThiJ+uAM4PcuLCAOxDsCekIbnGzPKVBRUDlgaaAfaUl5NYkB1hgY6WN4sPkejxKlANgVcGl9Qg==} engines: {node: '>=12'} @@ -2035,6 +2182,15 @@ packages: dev: true optional: true + /@esbuild/linux-x64@0.19.12: + resolution: {integrity: sha512-B71g1QpxfwBvNrfyJdVDexenDIt1CiDN1TIXLbhOw0KhJzE78KIFGX6OJ9MrtC0oOqMWf+0xop4qEU8JrJTwCg==} + engines: {node: '>=12'} + cpu: [x64] + os: [linux] + requiresBuild: true + dev: true + optional: true + /@esbuild/linux-x64@0.19.4: resolution: {integrity: sha512-Mi4aNA3rz1BNFtB7aGadMD0MavmzuuXNTaYL6/uiYIs08U7YMPETpgNn5oue3ICr+inKwItOwSsJDYkrE9ekVg==} engines: {node: '>=12'} @@ -2062,6 +2218,15 @@ packages: dev: true optional: true + /@esbuild/netbsd-x64@0.19.12: + resolution: {integrity: sha512-3ltjQ7n1owJgFbuC61Oj++XhtzmymoCihNFgT84UAmJnxJfm4sYCiSLTXZtE00VWYpPMYc+ZQmB6xbSdVh0JWA==} + engines: {node: '>=12'} + cpu: [x64] + os: [netbsd] + requiresBuild: true + dev: true + optional: true + /@esbuild/netbsd-x64@0.19.4: resolution: {integrity: sha512-9+Wxx1i5N/CYo505CTT7T+ix4lVzEdz0uCoYGxM5JDVlP2YdDC1Bdz+Khv6IbqmisT0Si928eAxbmGkcbiuM/A==} engines: {node: '>=12'} @@ -2089,6 +2254,15 @@ packages: dev: true optional: true + /@esbuild/openbsd-x64@0.19.12: + resolution: {integrity: sha512-RbrfTB9SWsr0kWmb9srfF+L933uMDdu9BIzdA7os2t0TXhCRjrQyCeOt6wVxr79CKD4c+p+YhCj31HBkYcXebw==} + engines: {node: '>=12'} + cpu: [x64] + os: [openbsd] + requiresBuild: true + dev: true + optional: true + /@esbuild/openbsd-x64@0.19.4: resolution: {integrity: sha512-MFsHleM5/rWRW9EivFssop+OulYVUoVcqkyOkjiynKBCGBj9Lihl7kh9IzrreDyXa4sNkquei5/DTP4uCk25xw==} engines: {node: '>=12'} @@ -2116,6 +2290,15 @@ packages: dev: true optional: true + /@esbuild/sunos-x64@0.19.12: + resolution: {integrity: sha512-HKjJwRrW8uWtCQnQOz9qcU3mUZhTUQvi56Q8DPTLLB+DawoiQdjsYq+j+D3s9I8VFtDr+F9CjgXKKC4ss89IeA==} + engines: {node: '>=12'} + cpu: [x64] + os: [sunos] + requiresBuild: true + dev: true + optional: true + /@esbuild/sunos-x64@0.19.4: resolution: {integrity: sha512-6Xq8SpK46yLvrGxjp6HftkDwPP49puU4OF0hEL4dTxqCbfx09LyrbUj/D7tmIRMj5D5FCUPksBbxyQhp8tmHzw==} engines: {node: '>=12'} @@ -2143,6 +2326,15 @@ packages: dev: true optional: true + /@esbuild/win32-arm64@0.19.12: + resolution: {integrity: sha512-URgtR1dJnmGvX864pn1B2YUYNzjmXkuJOIqG2HdU62MVS4EHpU2946OZoTMnRUHklGtJdJZ33QfzdjGACXhn1A==} + engines: {node: '>=12'} + cpu: [arm64] + os: [win32] + requiresBuild: true + dev: true + optional: true + /@esbuild/win32-arm64@0.19.4: resolution: {integrity: sha512-PkIl7Jq4mP6ke7QKwyg4fD4Xvn8PXisagV/+HntWoDEdmerB2LTukRZg728Yd1Fj+LuEX75t/hKXE2Ppk8Hh1w==} engines: {node: '>=12'} @@ -2170,6 +2362,15 @@ packages: dev: true optional: true + /@esbuild/win32-ia32@0.19.12: + resolution: {integrity: sha512-+ZOE6pUkMOJfmxmBZElNOx72NKpIa/HFOMGzu8fqzQJ5kgf6aTGrcJaFsNiVMH4JKpMipyK+7k0n2UXN7a8YKQ==} + engines: {node: '>=12'} + cpu: [ia32] + os: [win32] + requiresBuild: true + dev: true + optional: true + /@esbuild/win32-ia32@0.19.4: resolution: {integrity: sha512-ga676Hnvw7/ycdKB53qPusvsKdwrWzEyJ+AtItHGoARszIqvjffTwaaW3b2L6l90i7MO9i+dlAW415INuRhSGg==} engines: {node: '>=12'} @@ -2197,6 +2398,15 @@ packages: dev: true optional: true + /@esbuild/win32-x64@0.19.12: + resolution: {integrity: sha512-T1QyPSDCyMXaO3pzBkF96E8xMkiRYbUEZADd29SyPGabqxMViNoii+NcK7eWJAEoU6RZyEm5lVSIjTmcdoB9HA==} + engines: {node: '>=12'} + cpu: [x64] + os: [win32] + requiresBuild: true + dev: true + optional: true + /@esbuild/win32-x64@0.19.4: resolution: {integrity: sha512-HP0GDNla1T3ZL8Ko/SHAS2GgtjOg+VmWnnYLhuTksr++EnduYB0f3Y2LzHsUwb2iQ13JGoY6G3R8h6Du/WG6uA==} engines: {node: '>=12'} @@ -7770,6 +7980,37 @@ packages: '@esbuild/win32-x64': 0.18.14 dev: true + /esbuild@0.19.12: + resolution: {integrity: sha512-aARqgq8roFBj054KvQr5f1sFu0D65G+miZRCuJyJ0G13Zwx7vRar5Zhn2tkQNzIXcBrNVsv/8stehpj+GAjgbg==} + engines: {node: '>=12'} + hasBin: true + requiresBuild: true + optionalDependencies: + '@esbuild/aix-ppc64': 0.19.12 + '@esbuild/android-arm': 0.19.12 + '@esbuild/android-arm64': 0.19.12 + '@esbuild/android-x64': 0.19.12 + '@esbuild/darwin-arm64': 0.19.12 + '@esbuild/darwin-x64': 0.19.12 + '@esbuild/freebsd-arm64': 0.19.12 + '@esbuild/freebsd-x64': 0.19.12 + '@esbuild/linux-arm': 0.19.12 + '@esbuild/linux-arm64': 0.19.12 + '@esbuild/linux-ia32': 0.19.12 + '@esbuild/linux-loong64': 0.19.12 + '@esbuild/linux-mips64el': 0.19.12 + '@esbuild/linux-ppc64': 0.19.12 + '@esbuild/linux-riscv64': 0.19.12 + '@esbuild/linux-s390x': 0.19.12 + '@esbuild/linux-x64': 0.19.12 + '@esbuild/netbsd-x64': 0.19.12 + '@esbuild/openbsd-x64': 0.19.12 + '@esbuild/sunos-x64': 0.19.12 + '@esbuild/win32-arm64': 0.19.12 + '@esbuild/win32-ia32': 0.19.12 + '@esbuild/win32-x64': 0.19.12 + dev: true + /esbuild@0.19.4: resolution: {integrity: sha512-x7jL0tbRRpv4QUyuDMjONtWFciygUxWaUM1kMX2zWxI0X2YWOt7MSA0g4UdeSiHM8fcYVzpQhKYOycZwxTdZkA==} engines: {node: '>=12'} @@ -8597,6 +8838,12 @@ packages: get-intrinsic: 1.2.1 dev: true + /get-tsconfig@4.7.2: + resolution: {integrity: sha512-wuMsz4leaj5hbGgg4IvDU0bqJagpftG5l5cXIAvo8uZrqn0NJqwtfupTN00VnkQJPcIRrxYrm1Ue24btpCha2A==} + dependencies: + resolve-pkg-maps: 1.0.0 + dev: true + /giget@1.1.3: resolution: {integrity: sha512-zHuCeqtfgqgDwvXlR84UNgnJDuUHQcNI5OqWqFxxuk2BshuKbYhJWdxBsEo4PvKqoGh23lUAIvBNpChMLv7/9Q==} hasBin: true @@ -12958,6 +13205,10 @@ packages: engines: {node: '>=8'} dev: true + /resolve-pkg-maps@1.0.0: + resolution: {integrity: sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==} + dev: true + /resolve.exports@2.0.2: resolution: {integrity: sha512-X2UW6Nw3n/aMgDVy+0rSqgHlv39WZAlZrXCdnbyEiKm17DSqHX4MmQMaST3FbeWR5FTuRcUwYAziZajji0Y7mg==} engines: {node: '>=10'} @@ -14255,6 +14506,17 @@ packages: typescript: 5.3.2 dev: true + /tsx@4.7.1: + resolution: {integrity: sha512-8d6VuibXHtlN5E3zFkgY8u4DX7Y3Z27zvvPKVmLon/D4AjuKzarkUBTLDBgj9iTQ0hg5xM7c/mYiRVM+HETf0g==} + engines: {node: '>=18.0.0'} + hasBin: true + dependencies: + esbuild: 0.19.12 + get-tsconfig: 4.7.2 + optionalDependencies: + fsevents: 2.3.3 + dev: true + /tty-table@4.2.1: resolution: {integrity: sha512-xz0uKo+KakCQ+Dxj1D/tKn2FSyreSYWzdkL/BYhgN6oMW808g8QRMuh1atAV9fjTPbWBjfbkKQpI/5rEcnAc7g==} engines: {node: '>=8.0.0'} diff --git a/script/test-scaffold.ts b/script/test-scaffold.ts new file mode 100644 index 000000000..ddf3c999a --- /dev/null +++ b/script/test-scaffold.ts @@ -0,0 +1,24 @@ +import path from 'path'; +import fs from 'fs'; +import { execSync } from 'child_process'; + +const scaffoldPath = path.join(__dirname, '../.test/scaffold'); +if (fs.existsSync(scaffoldPath)) { + fs.rmSync(scaffoldPath, { recursive: true, force: true }); +} +fs.mkdirSync(scaffoldPath, { recursive: true }); + +function run(cmd: string) { + console.log(`Running: ${cmd}, in ${scaffoldPath}`); + try { + execSync(cmd, { cwd: scaffoldPath, stdio: 'ignore' }); + } catch (err) { + console.error(`Test project scaffolding cmd error: ${err}`); + throw err; + } +} + +run('npm init -y'); +run('npm i --no-audit --no-fund typescript prisma @prisma/client zod decimal.js'); + +console.log('Test scaffold setup complete.'); diff --git a/test-setup.ts b/test-setup.ts new file mode 100644 index 000000000..9856ff4b5 --- /dev/null +++ b/test-setup.ts @@ -0,0 +1,9 @@ +import fs from 'fs'; +import path from 'path'; + +export default function globalSetup() { + if (!fs.existsSync(path.join(__dirname, '.test/scaffold/package-lock.json'))) { + console.error(`Test scaffold not found. Please run \`pnpm test-scaffold\` first.`); + process.exit(1); + } +} diff --git a/tests/integration/global-setup.js b/tests/integration/global-setup.js deleted file mode 100644 index 0d4b8e23e..000000000 --- a/tests/integration/global-setup.js +++ /dev/null @@ -1,10 +0,0 @@ -const { execSync } = require('child_process'); - -module.exports = function () { - console.log('npm install'); - execSync('npm install', { - encoding: 'utf-8', - stdio: 'inherit', - cwd: 'test-run', - }); -}; diff --git a/tests/integration/jest.config.ts b/tests/integration/jest.config.ts index 346f6faad..67a118269 100644 --- a/tests/integration/jest.config.ts +++ b/tests/integration/jest.config.ts @@ -1,30 +1,10 @@ +import baseConfig from '../../jest.config'; + /* * For a detailed explanation regarding each configuration property and type check, visit: * https://jestjs.io/docs/configuration */ export default { - // Automatically clear mock calls, instances, contexts and results before every test - clearMocks: true, - - // A map from regular expressions to paths to transformers - transform: { '^.+\\.tsx?$': 'ts-jest' }, - - testTimeout: 300000, - + ...baseConfig, setupFilesAfterEnv: ['./test-setup.ts'], - - // Indicates whether the coverage information should be collected while executing the test - collectCoverage: true, - - // The directory where Jest should output its coverage files - coverageDirectory: 'tests/coverage', - - // An array of regexp pattern strings used to skip coverage collection - coveragePathIgnorePatterns: ['/node_modules/', '/tests/'], - - // Indicates which provider should be used to instrument code for coverage - coverageProvider: 'v8', - - // A list of reporter names that Jest uses when writing coverage reports - coverageReporters: ['json', 'text', 'lcov', 'clover'], }; diff --git a/tests/integration/tests/cli/generate.test.ts b/tests/integration/tests/cli/generate.test.ts index 0367033bd..90f9e2311 100644 --- a/tests/integration/tests/cli/generate.test.ts +++ b/tests/integration/tests/cli/generate.test.ts @@ -1,11 +1,11 @@ /* eslint-disable @typescript-eslint/no-var-requires */ /// +import { installPackage } from '@zenstackhq/testtools'; import * as fs from 'fs'; import path from 'path'; import * as tmp from 'tmp'; import { createProgram } from '../../../../packages/schema/src/cli'; -import { execSync } from '../../../../packages/schema/src/utils/exec-utils'; import { createNpmrc } from './share'; describe('CLI generate command tests', () => { @@ -43,8 +43,8 @@ model Post { // set up project fs.writeFileSync('package.json', JSON.stringify({ name: 'my app', version: '1.0.0' })); createNpmrc(); - execSync('npm install prisma @prisma/client zod'); - execSync(`npm install ${path.join(__dirname, '../../../../packages/runtime/dist')}`); + installPackage('prisma @prisma/client zod'); + installPackage(path.join(__dirname, '../../../../packages/runtime/dist')); // set up schema fs.writeFileSync('schema.zmodel', MODEL, 'utf-8'); diff --git a/tests/integration/tests/cli/plugins.test.ts b/tests/integration/tests/cli/plugins.test.ts index 005a0f69b..716ac224e 100644 --- a/tests/integration/tests/cli/plugins.test.ts +++ b/tests/integration/tests/cli/plugins.test.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-var-requires */ /// -import { getWorkspaceNpmCacheFolder, run } from '@zenstackhq/testtools'; +import { getWorkspaceNpmCacheFolder, installPackage, run } from '@zenstackhq/testtools'; import * as fs from 'fs'; import * as path from 'path'; import * as tmp from 'tmp'; @@ -94,8 +94,8 @@ describe('CLI Plugins Tests', () => { switch (pm) { case 'npm': - run('npm install ' + deps); - run('npm install -D ' + devDeps); + installPackage(deps); + installPackage(devDeps, true); break; // case 'yarn': // run('yarn add ' + deps); diff --git a/tests/integration/tests/schema/todo.zmodel b/tests/integration/tests/schema/todo.zmodel index 733391bd1..079e3b1ef 100644 --- a/tests/integration/tests/schema/todo.zmodel +++ b/tests/integration/tests/schema/todo.zmodel @@ -14,7 +14,7 @@ generator js { plugin zod { provider = '@core/zod' - preserveTsFiles = true + // preserveTsFiles = true } /*