Skip to content

Commit

Permalink
fix(prisma): resolve multiple enums on model not correctly imported
Browse files Browse the repository at this point in the history
Closes: #2939
  • Loading branch information
Romakita committed Dec 14, 2024
1 parent 818bae2 commit 8405089
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 188 deletions.
1 change: 1 addition & 0 deletions packages/orm/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions packages/orm/prisma/src/generator/domain/DmmfModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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";
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@tsed/prisma-test",
"type": "module",
"devDependencies": {
"prisma": "^4.0.0"
},
Expand Down
38 changes: 38 additions & 0 deletions packages/orm/prisma/test/enums/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion packages/orm/prisma/test/mongo-esm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tsed/prisma-test",
"type": "commonjs",
"type": "module",
"devDependencies": {
"prisma": "^4.0.0"
},
Expand Down
35 changes: 0 additions & 35 deletions packages/orm/prisma/test/mongo/package-lock.json

This file was deleted.

9 changes: 0 additions & 9 deletions packages/orm/prisma/test/mongo/package.json

This file was deleted.

34 changes: 0 additions & 34 deletions packages/orm/prisma/test/mongo/prisma/schema.prisma

This file was deleted.

8 changes: 0 additions & 8 deletions packages/orm/prisma/test/mongo/prisma/tsconfig.json

This file was deleted.

35 changes: 0 additions & 35 deletions packages/orm/prisma/test/postgres/package-lock.json

This file was deleted.

50 changes: 0 additions & 50 deletions packages/orm/prisma/test/postgres/prisma/schema.prisma

This file was deleted.

8 changes: 0 additions & 8 deletions packages/orm/prisma/test/postgres/tsconfig.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 8405089

Please sign in to comment.