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

Open
wants to merge 1 commit into
base: beta
Choose a base branch
from
Open
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 @@
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,7 @@
TSType += " | null";
}

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

Check failure on line 49 in packages/orm/prisma/src/generator/transform/transformScalarToType.ts

View workflow job for this annotation

GitHub Actions / lint (20.12.2)

Delete `⏎⏎`

return TSType;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`generateModels > should generate models (info) 1`] = `
"import { Info } from "../client/index.js";
"import { Info } from "../client/index";
import { Required, Property } from "@tsed/schema";

export class InfoModel implements Info {
Expand All @@ -18,9 +18,10 @@ export class InfoModel implements Info {
`;

exports[`generateModels > should generate models (post) 1`] = `
"import { Post } from "../client/index.js";
"import { Post } from "../client/index";
import { Integer, Required, Property, Allow } from "@tsed/schema";
import { UserModel } from "./UserModel.js";
import { UserModel } from "./UserModel";
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 @@ -42,10 +43,11 @@ export class PostModel implements Post {
`;

exports[`generateModels > should generate models (user) 1`] = `
"import { User } from "../client/index.js";
"import { User } from "../client/index";
import { Integer, Required, Property, Groups, Format, Email, Description, Allow, Enum, CollectionOf } from "@tsed/schema";
import { Role } from "../enums/index.js";
import { PostModel } from "./PostModel.js";
import type { Relation } from "@tsed/core";
import { Role } from "../enums/index";
import { PostModel } from "./PostModel";

export class UserModel implements User {
@Property(Number)
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")
}
Loading