From 84050893f391687b773564521308edcb10cce6c3 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Sat, 14 Dec 2024 10:31:11 +0100 Subject: [PATCH] fix(prisma): resolve multiple enums on model not correctly imported Closes: #2939 --- packages/orm/prisma/package.json | 1 + .../prisma/src/generator/domain/DmmfModel.ts | 7 +-- .../__snapshots__/generateModels.spec.ts.snap | 6 +-- .../test/{postgres => enums}/package.json | 1 + .../prisma/test/enums/prisma/schema.prisma | 38 ++++++++++++++ .../orm/prisma/test/mongo-esm/package.json | 2 +- .../orm/prisma/test/mongo/package-lock.json | 35 ------------- packages/orm/prisma/test/mongo/package.json | 9 ---- .../prisma/test/mongo/prisma/schema.prisma | 34 ------------- .../prisma/test/mongo/prisma/tsconfig.json | 8 --- .../prisma/test/postgres/package-lock.json | 35 ------------- .../prisma/test/postgres/prisma/schema.prisma | 50 ------------------- .../orm/prisma/test/postgres/tsconfig.json | 8 --- .../generate_code/models/PostModel.ts | 2 +- .../generate_code/models/UserModel.ts | 2 +- 15 files changed, 50 insertions(+), 188 deletions(-) rename packages/orm/prisma/test/{postgres => enums}/package.json (87%) create mode 100644 packages/orm/prisma/test/enums/prisma/schema.prisma delete mode 100644 packages/orm/prisma/test/mongo/package-lock.json delete mode 100644 packages/orm/prisma/test/mongo/package.json delete mode 100644 packages/orm/prisma/test/mongo/prisma/schema.prisma delete mode 100644 packages/orm/prisma/test/mongo/prisma/tsconfig.json delete mode 100644 packages/orm/prisma/test/postgres/package-lock.json delete mode 100644 packages/orm/prisma/test/postgres/prisma/schema.prisma delete mode 100644 packages/orm/prisma/test/postgres/tsconfig.json diff --git a/packages/orm/prisma/package.json b/packages/orm/prisma/package.json index 2b3e902bb11..90e77c3e2fd 100644 --- a/packages/orm/prisma/package.json +++ b/packages/orm/prisma/package.json @@ -24,6 +24,7 @@ "build": "rm -rf lib && yarn build:ts && monorepo build-hybrid", "build:ts": "tsc --build tsconfig.json", "test": "vitest run", + "generate:enums": "yarn build && cd test/enums && prisma -v && prisma generate", "generate:postgres:esm": "yarn build && cd test/postgres-esm && prisma -v && prisma generate", "generate:mongo:esm": "yarn build && cd test/mongo-esm && prisma -v && prisma generate", "generate:circular:esm": "yarn build && cd test/circular-ref && prisma -v && prisma generate", diff --git a/packages/orm/prisma/src/generator/domain/DmmfModel.ts b/packages/orm/prisma/src/generator/domain/DmmfModel.ts index 7c133789336..740573ec08a 100644 --- a/packages/orm/prisma/src/generator/domain/DmmfModel.ts +++ b/packages/orm/prisma/src/generator/domain/DmmfModel.ts @@ -67,9 +67,9 @@ export class DmmfModel { } addImportDeclaration(moduleSpecifier: string, name: string, isTypeOnly = false) { - if (!this.#imports.has(moduleSpecifier)) { - moduleSpecifier = resolveExtension(moduleSpecifier); + moduleSpecifier = resolveExtension(moduleSpecifier); + if (!this.#imports.has(moduleSpecifier)) { this.#imports.set(moduleSpecifier, { kind: StructureKind.ImportDeclaration, moduleSpecifier: moduleSpecifier, @@ -79,10 +79,11 @@ export class DmmfModel { } const moduleDeclaration = this.#imports.get(moduleSpecifier)!; - const nameImports = moduleDeclaration.namedImports as any[]; + const nameImports = moduleDeclaration.namedImports as string[]; if (!nameImports.includes(name)) { nameImports.push(name); + moduleDeclaration.namedImports = nameImports.sort(); } return this; diff --git a/packages/orm/prisma/src/generator/utils/__snapshots__/generateModels.spec.ts.snap b/packages/orm/prisma/src/generator/utils/__snapshots__/generateModels.spec.ts.snap index 3a532db4438..51b1d465076 100644 --- a/packages/orm/prisma/src/generator/utils/__snapshots__/generateModels.spec.ts.snap +++ b/packages/orm/prisma/src/generator/utils/__snapshots__/generateModels.spec.ts.snap @@ -2,7 +2,7 @@ exports[`generateModels > should generate models (info) 1`] = ` "import { Info } from "../client/index.js"; -import { Required, Property } from "@tsed/schema"; +import { Property, Required } from "@tsed/schema"; export class InfoModel implements Info { @Property(String) @@ -19,7 +19,7 @@ export class InfoModel implements Info { exports[`generateModels > should generate models (post) 1`] = ` "import { Post } from "../client/index.js"; -import { Integer, Required, Property, Allow } from "@tsed/schema"; +import { Allow, Integer, Property, Required } from "@tsed/schema"; import { UserModel } from "./UserModel.js"; import type { Relation } from "@tsed/core"; @@ -44,7 +44,7 @@ export class PostModel implements Post { exports[`generateModels > should generate models (user) 1`] = ` "import { User } from "../client/index.js"; -import { Integer, Required, Property, Groups, Format, Email, Description, Allow, Enum, CollectionOf } from "@tsed/schema"; +import { Allow, CollectionOf, Description, Email, Enum, Format, Groups, Integer, Property, Required } from "@tsed/schema"; import type { Relation } from "@tsed/core"; import { Role } from "../enums/index.js"; import { PostModel } from "./PostModel.js"; diff --git a/packages/orm/prisma/test/postgres/package.json b/packages/orm/prisma/test/enums/package.json similarity index 87% rename from packages/orm/prisma/test/postgres/package.json rename to packages/orm/prisma/test/enums/package.json index 9a29502f7e1..45e544aa396 100644 --- a/packages/orm/prisma/test/postgres/package.json +++ b/packages/orm/prisma/test/enums/package.json @@ -1,5 +1,6 @@ { "name": "@tsed/prisma-test", + "type": "module", "devDependencies": { "prisma": "^4.0.0" }, diff --git a/packages/orm/prisma/test/enums/prisma/schema.prisma b/packages/orm/prisma/test/enums/prisma/schema.prisma new file mode 100644 index 00000000000..aa2791ff81a --- /dev/null +++ b/packages/orm/prisma/test/enums/prisma/schema.prisma @@ -0,0 +1,38 @@ + +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} + +generator client { + provider = "prisma-client-js" + binaryTargets = ["native", "windows", "debian-openssl-1.1.x"] + output = "../prisma/generated/client" +} + +generator tsed { + provider = "node ../../lib/esm/generator.js" + output = "../prisma/generated/tsed" + emitDMMF = true +} + +model User { + id Int @id @default(autoincrement()) + email String @unique + name String? + role Role @default(USER) + provider Provider @default(EMAIL) +} + +enum Role { + USER + ADMIN +} + +enum Provider { + EMAIL + FACEBOOK + GOOGLE + GITHUB + MICROSOFT +} diff --git a/packages/orm/prisma/test/mongo-esm/package.json b/packages/orm/prisma/test/mongo-esm/package.json index d72ff0fbc66..45e544aa396 100644 --- a/packages/orm/prisma/test/mongo-esm/package.json +++ b/packages/orm/prisma/test/mongo-esm/package.json @@ -1,6 +1,6 @@ { "name": "@tsed/prisma-test", - "type": "commonjs", + "type": "module", "devDependencies": { "prisma": "^4.0.0" }, diff --git a/packages/orm/prisma/test/mongo/package-lock.json b/packages/orm/prisma/test/mongo/package-lock.json deleted file mode 100644 index c9f3a033a41..00000000000 --- a/packages/orm/prisma/test/mongo/package-lock.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "name": "@tsed/prisma-test", - "requires": true, - "lockfileVersion": 1, - "dependencies": { - "@prisma/client": { - "version": "2.22.1", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-2.22.1.tgz", - "integrity": "sha512-JQjbsY6QSfFiovXHEp5WeJHa5p2CuR1ZFPAeYXmUsOAQOaMCrhgQmKAL6w2Q3SRA7ALqPjrKywN9/QfBc4Kp1A==", - "requires": { - "@prisma/engines-version": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c" - } - }, - "@prisma/engines": { - "version": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c", - "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c.tgz", - "integrity": "sha512-KmWdogrsfsSLYvfqY3cS3QcDGzaEFklE+T6dNJf+k/KPQum4A29IwDalafMwh5cMN8ivZobUbowNSwWJrMT08Q==", - "dev": true - }, - "@prisma/engines-version": { - "version": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c", - "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c.tgz", - "integrity": "sha512-OkkVwk6iTzTbwwl8JIKAENyxmh4TFORal55QMKQzrHEY8UzbD0M90mQnmziz3PAopQUZgTFFMlaPAq1WNrLMtA==" - }, - "prisma": { - "version": "2.22.1", - "resolved": "https://registry.npmjs.org/prisma/-/prisma-2.22.1.tgz", - "integrity": "sha512-hwvCM3zyxgSda/+/p+GW7nz93jRebtMU01wAG7YVVnl0OKU+dpw1wPvPFmQRldkZHk8fTCleYmjc24WaSdVPZQ==", - "dev": true, - "requires": { - "@prisma/engines": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c" - } - } - } -} diff --git a/packages/orm/prisma/test/mongo/package.json b/packages/orm/prisma/test/mongo/package.json deleted file mode 100644 index 9a29502f7e1..00000000000 --- a/packages/orm/prisma/test/mongo/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "@tsed/prisma-test", - "devDependencies": { - "prisma": "^4.0.0" - }, - "dependencies": { - "@prisma/client": "^4.0.0" - } -} diff --git a/packages/orm/prisma/test/mongo/prisma/schema.prisma b/packages/orm/prisma/test/mongo/prisma/schema.prisma deleted file mode 100644 index ce074bfac60..00000000000 --- a/packages/orm/prisma/test/mongo/prisma/schema.prisma +++ /dev/null @@ -1,34 +0,0 @@ - -datasource db { - provider = "mongodb" - url = env("DATABASE_URL") -} - -generator client { - provider = "prisma-client-js" - binaryTargets = ["native", "windows", "debian-openssl-1.1.x"] - previewFeatures = ["interactiveTransactions"] - output = "../prisma/generated/client" -} - -generator tsed { - provider = "node ../../lib/cjs/generator.js" - output = "../prisma/generated/tsed" - emitDMMF = true -} - -model User { - /// @TsED.Groups("!creation") - id String @id @default(auto()) @map("_id") @db.ObjectId - /// @TsED.Email() - email String @unique - info Info - /// @TsED.Groups("!creation") - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt -} - -type Info { - firstName String - lastName String -} diff --git a/packages/orm/prisma/test/mongo/prisma/tsconfig.json b/packages/orm/prisma/test/mongo/prisma/tsconfig.json deleted file mode 100644 index fa0a429c11d..00000000000 --- a/packages/orm/prisma/test/mongo/prisma/tsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "@tsed/typescript/tsconfig.node.json", - "compilerOptions": { - "baseUrl": ".", - "noEmit": true - }, - "include": ["."] -} diff --git a/packages/orm/prisma/test/postgres/package-lock.json b/packages/orm/prisma/test/postgres/package-lock.json deleted file mode 100644 index c9f3a033a41..00000000000 --- a/packages/orm/prisma/test/postgres/package-lock.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "name": "@tsed/prisma-test", - "requires": true, - "lockfileVersion": 1, - "dependencies": { - "@prisma/client": { - "version": "2.22.1", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-2.22.1.tgz", - "integrity": "sha512-JQjbsY6QSfFiovXHEp5WeJHa5p2CuR1ZFPAeYXmUsOAQOaMCrhgQmKAL6w2Q3SRA7ALqPjrKywN9/QfBc4Kp1A==", - "requires": { - "@prisma/engines-version": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c" - } - }, - "@prisma/engines": { - "version": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c", - "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c.tgz", - "integrity": "sha512-KmWdogrsfsSLYvfqY3cS3QcDGzaEFklE+T6dNJf+k/KPQum4A29IwDalafMwh5cMN8ivZobUbowNSwWJrMT08Q==", - "dev": true - }, - "@prisma/engines-version": { - "version": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c", - "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c.tgz", - "integrity": "sha512-OkkVwk6iTzTbwwl8JIKAENyxmh4TFORal55QMKQzrHEY8UzbD0M90mQnmziz3PAopQUZgTFFMlaPAq1WNrLMtA==" - }, - "prisma": { - "version": "2.22.1", - "resolved": "https://registry.npmjs.org/prisma/-/prisma-2.22.1.tgz", - "integrity": "sha512-hwvCM3zyxgSda/+/p+GW7nz93jRebtMU01wAG7YVVnl0OKU+dpw1wPvPFmQRldkZHk8fTCleYmjc24WaSdVPZQ==", - "dev": true, - "requires": { - "@prisma/engines": "2.22.0-21.60cc71d884972ab4e897f0277c4b84383dddaf6c" - } - } - } -} diff --git a/packages/orm/prisma/test/postgres/prisma/schema.prisma b/packages/orm/prisma/test/postgres/prisma/schema.prisma deleted file mode 100644 index 9900b67d25b..00000000000 --- a/packages/orm/prisma/test/postgres/prisma/schema.prisma +++ /dev/null @@ -1,50 +0,0 @@ - -datasource db { - provider = "postgresql" - url = env("DATABASE_URL") -} - -generator client { - provider = "prisma-client-js" - binaryTargets = ["native", "windows", "debian-openssl-1.1.x"] - output = "../prisma/generated/client" -} - -generator tsed { - provider = "node ../../lib/cjs/generator.js" - output = "../prisma/generated/tsed" - emitDMMF = true -} - -model user { - /// @TsED.Groups("!creation") - /// Comment - id Int @id @default(autoincrement()) - createdAt DateTime @default(now()) - /// @TsED.Email() - /// @TsED.Description("User email. This email must be unique!") - email String @unique - weight Float? - is18 Boolean? - name String? - successorId Int? @unique - successor user? @relation("BlogOwnerHistory", fields: [successorId], references: [id]) - predecessor user? @relation("BlogOwnerHistory") - role Role @default(USER) - posts Post[] - keywords String[] - biography Json - /// @TsED.Ignore(ctx.endpoint === true) - ignored String -} - -model Post { - id Int @id @default(autoincrement()) - user user? @relation(fields: [userId], references: [id]) - userId Int? -} - -enum Role { - USER - ADMIN -} diff --git a/packages/orm/prisma/test/postgres/tsconfig.json b/packages/orm/prisma/test/postgres/tsconfig.json deleted file mode 100644 index fa0a429c11d..00000000000 --- a/packages/orm/prisma/test/postgres/tsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "@tsed/typescript/tsconfig.node.json", - "compilerOptions": { - "baseUrl": ".", - "noEmit": true - }, - "include": ["."] -} diff --git a/packages/orm/prisma/test/snapshots/generate_code/models/PostModel.ts b/packages/orm/prisma/test/snapshots/generate_code/models/PostModel.ts index c826163da8f..5f513a466e1 100644 --- a/packages/orm/prisma/test/snapshots/generate_code/models/PostModel.ts +++ b/packages/orm/prisma/test/snapshots/generate_code/models/PostModel.ts @@ -1,5 +1,5 @@ import { Post } from "../client/index.js"; -import { Integer, Required, Property, Allow } from "@tsed/schema"; +import { Allow, Integer, Property, Required } from "@tsed/schema"; import { UserModel } from "./UserModel.js"; import type { Relation } from "@tsed/core"; diff --git a/packages/orm/prisma/test/snapshots/generate_code/models/UserModel.ts b/packages/orm/prisma/test/snapshots/generate_code/models/UserModel.ts index bffb6656699..c8bc5a731e8 100644 --- a/packages/orm/prisma/test/snapshots/generate_code/models/UserModel.ts +++ b/packages/orm/prisma/test/snapshots/generate_code/models/UserModel.ts @@ -1,5 +1,5 @@ import { User } from "../client/index.js"; -import { Integer, Required, Property, Groups, Format, Email, Description, Allow, Enum, CollectionOf } from "@tsed/schema"; +import { Allow, CollectionOf, Description, Email, Enum, Format, Groups, Integer, Property, Required } from "@tsed/schema"; import type { Relation } from "@tsed/core"; import { Role } from "../enums/index.js"; import { PostModel } from "./PostModel.js";