Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prisma): fix circular reference when column is optional #2865

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/orm/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"test": "vitest run",
"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",
"test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ describe("transformScalarToType()", () => {
});
// @ts-ignore
field.model.name = "User";
expect(transformScalarToType(field, ctx)).toEqual("UserModel | null");
expect(field.model.addImportDeclaration).not.toHaveBeenCalled();
expect(transformScalarToType(field, ctx)).toEqual("Relation<UserModel> | null");
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("@tsed/core", "Relation", true);
});

it("should transform User to User", () => {
it("should transform Role to User", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "object",
type: "Role"
});
// @ts-ignore
field.model.name = "User";
expect(transformScalarToType(field, ctx)).toEqual("RoleModel | null");
expect(transformScalarToType(field, ctx)).toEqual("Relation<RoleModel> | null");
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("./RoleModel", "RoleModel");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export function transformScalarToType(field: DmmfField, ctx: TransformContext):
TSType += "[]";
}

if (!isList && hasCircularRef && ["inputObjectTypes", "enumTypes"].includes(location)) {
hasCircularRef && !isList && field.model.addImportDeclaration("@tsed/core", "Relation", true);
TSType = `Relation<${TSType}>`;
}

if (!isRequired) {
if (model.isInputType) {
TSType += " | undefined";
Expand All @@ -41,10 +46,5 @@ export function transformScalarToType(field: DmmfField, ctx: TransformContext):
TSType += " | null";
}

if (isRequired && !isList && !isNullable && hasCircularRef && ["inputObjectTypes", "enumTypes"].includes(location)) {
hasCircularRef && !isList && field.model.addImportDeclaration("@tsed/core", "Relation", true);
TSType = `Relation<${TSType}>`;
}

return TSType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports[`generateModels > should generate models (post) 1`] = `
"import { Post } from "../client/index.js";
import { Integer, Required, Property, Allow } from "@tsed/schema";
import { UserModel } from "./UserModel.js";
import type { Relation } from "@tsed/core";

export class PostModel implements Post {
@Property(Number)
Expand All @@ -30,7 +31,7 @@ export class PostModel implements Post {

@Property(() => UserModel)
@Allow(null)
user: UserModel | null;
user: Relation<UserModel> | null;

@Property(Number)
@Integer()
Expand All @@ -44,6 +45,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 type { Relation } from "@tsed/core";
import { Role } from "../enums/index.js";
import { PostModel } from "./PostModel.js";

Expand Down Expand Up @@ -84,11 +86,11 @@ export class UserModel implements User {

@Property(() => UserModel)
@Allow(null)
successor: UserModel | null;
successor: Relation<UserModel> | null;

@Property(() => UserModel)
@Allow(null)
predecessor: UserModel | null;
predecessor: Relation<UserModel> | null;

@Required()
@Enum(Role)
Expand Down
10 changes: 10 additions & 0 deletions packages/orm/prisma/test/circular-ref/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@tsed/prisma-test",
"type": "commonjs",
"devDependencies": {
"prisma": "^4.0.0"
},
"dependencies": {
"@prisma/client": "^4.0.0"
}
}
32 changes: 32 additions & 0 deletions packages/orm/prisma/test/circular-ref/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

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 Conversation {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
assignmentId String? @map("assignment_id") @db.Uuid
assignment Assignment? @relation(fields: [assignmentId], references: [id], onDelete: NoAction, onUpdate: NoAction)

@@map("conversation")
}

model Assignment {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
conversations Conversation[]

@@map("assignment")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Post } from "../client/index.js";
import { Integer, Required, Property, Allow } from "@tsed/schema";
import { UserModel } from "./UserModel.js";
import type { Relation } from "@tsed/core";

export class PostModel implements Post {
@Property(Number)
Expand All @@ -10,7 +11,7 @@ export class PostModel implements Post {

@Property(() => UserModel)
@Allow(null)
user: UserModel | null;
user: Relation<UserModel> | null;

@Property(Number)
@Integer()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { User } from "../client/index.js";
import { Integer, Required, Property, Groups, Format, Email, Description, Allow, Enum, CollectionOf } from "@tsed/schema";
import type { Relation } from "@tsed/core";
import { Role } from "../enums/index.js";
import { PostModel } from "./PostModel.js";

Expand Down Expand Up @@ -40,11 +41,11 @@ export class UserModel implements User {

@Property(() => UserModel)
@Allow(null)
successor: UserModel | null;
successor: Relation<UserModel> | null;

@Property(() => UserModel)
@Allow(null)
predecessor: UserModel | null;
predecessor: Relation<UserModel> | null;

@Required()
@Enum(Role)
Expand Down
Loading