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): resolve several issues with prisma generate for ESM #2741

Merged
merged 3 commits into from
Jul 8, 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
2 changes: 1 addition & 1 deletion packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
coverageThreshold: {
global: {
statements: 97.52,
branches: 92.65,
branches: 92.45,
functions: 95.41,
lines: 97.52
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from "./interfaces/AnyDecorator.js";
export * from "./interfaces/DecoratorParameters.js";
export * from "./interfaces/MetadataTypes.js";
export * from "./interfaces/ValueOf.js";
export * from "./interfaces/Relation.js";
export * from "./utils/catchError.js";
export * from "./utils/decorators/decorateMethodsOf.js";
export * from "./utils/decorators/decoratorArgs.js";
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/interfaces/Relation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Relation<T> = T;
2 changes: 1 addition & 1 deletion packages/orm/prisma/scripts/backup-index.esm.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./.schema/index.js";
export * from "../.schema/index.js";
40 changes: 40 additions & 0 deletions packages/orm/prisma/src/generator/domain/DmmfField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {DmmfField} from "./DmmfField.js";
import {DmmfModel} from "./DmmfModel.js";

describe("DmmfField", () => {
it("should create a new instance of DmmfField", () => {
const dmmfField = new DmmfField({
field: {
name: "name",
isRequired: true,
type: "string",
isList: false,
kind: "scalar"
},
schemaArg: {
isNullable: false
},
model: new DmmfModel({
model: {
name: "model",
fields: []
},
modelType: {
fields: []
},
isInputType: true
})
});

expect(dmmfField).toBeInstanceOf(DmmfField);
expect(dmmfField.name).toBe("name");
expect(dmmfField.isRequired).toBe(true);
expect(dmmfField.type).toBe("string");
expect(dmmfField.isList).toBe(false);
expect(dmmfField.kind).toBe("scalar");
expect(dmmfField.isNullable).toBe(false);
expect(dmmfField.location).toBe("scalar");
expect(dmmfField.getAdditionalDecorators()).toEqual([]);
expect(dmmfField.toString()).toBe("name");
});
});
15 changes: 6 additions & 9 deletions packages/orm/prisma/src/generator/domain/DmmfModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,23 @@ export class DmmfModel {
return pascalCase(`${name}Model`);
}

addImportDeclaration(moduleSpecifier: string, name: string, isDefault = false) {
addImportDeclaration(moduleSpecifier: string, name: string, isTypeOnly = false) {
if (!this.#imports.has(moduleSpecifier)) {
moduleSpecifier = resolveExtension(moduleSpecifier);

this.#imports.set(moduleSpecifier, {
kind: StructureKind.ImportDeclaration,
moduleSpecifier: moduleSpecifier,
namedImports: []
namedImports: [],
isTypeOnly
});
}

const moduleDeclaration = this.#imports.get(moduleSpecifier)!;
const nameImports = moduleDeclaration.namedImports as any[];

if (isDefault) {
moduleDeclaration.defaultImport = name;
} else {
const nameImports = moduleDeclaration.namedImports as any[];
if (!nameImports.includes(name)) {
nameImports.push(name);
}
if (!nameImports.includes(name)) {
nameImports.push(name);
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {DmmfModel} from "../domain/DmmfModel.js";
import {ScalarDecorators, ScalarJsClasses} from "../domain/ScalarTsTypes.js";
import {TransformContext} from "../domain/TransformContext.js";
import {isCircularRef} from "../utils/isCircularRef.js";
import {isEsm} from "../utils/sourceType.js";

function createDecorator(name: string, args: string[]): DecoratorStructure {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function transformFieldToProperty(field: DmmfField, ctx: TransformContext
kind: StructureKind.Property,
name: field.name,
trailingTrivia: "\n",
type: transformScalarToType(field),
type: transformScalarToType(field, ctx),
decorators: transformFieldToDecorators(field, ctx)
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ describe("transformModelToClass()", () => {
kind: 30,
name: "role",
trailingTrivia: "\n",
type: "Role"
type: "Relation<Role>"
}
],
trailingTrivia: "\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,100 +1,125 @@
import {createContextFixture} from "../../__mock__/createContextFixture.js";
import {transformScalarToType} from "./transformScalarToType.js";
import {createDmmfFieldFixture} from "../../__mock__/createDmmfFieldFixture.js";
import {PrismaScalars} from "../domain/ScalarTsTypes.js";

describe("transformScalarToType()", () => {
it("should transform User to User (not null + circular ref)", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "object",
type: "Role",
isRequired: true,
isNullable: false
});
// @ts-ignore
field.model.name = "User";
expect(transformScalarToType(field, ctx)).toEqual("Relation<RoleModel>");
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("./RoleModel", "RoleModel");
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("@tsed/core", "Relation", true);
});
it("should transform String to string", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "scalar",
type: PrismaScalars.String
});
expect(transformScalarToType(field)).toEqual("string | null");
expect(transformScalarToType(field, ctx)).toEqual("string | null");
});

it("should transform String to string (isInputType)", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "scalar",
type: PrismaScalars.String,
isInputType: true
});

expect(transformScalarToType(field)).toEqual("string | undefined");
expect(transformScalarToType(field, ctx)).toEqual("string | undefined");
});

it("should transform String to string (Required)", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "scalar",
type: PrismaScalars.String,
isRequired: true
});
expect(transformScalarToType(field)).toEqual("string");
expect(transformScalarToType(field, ctx)).toEqual("string");
});

it("should transform String to string (Required + null)", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "scalar",
type: PrismaScalars.String,
isRequired: true,
isNullable: true
});
expect(transformScalarToType(field)).toEqual("string | null");
expect(transformScalarToType(field, ctx)).toEqual("string | null");
});

it("should transform String to string (Required + null + List)", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "scalar",
type: PrismaScalars.String,
isRequired: true,
isNullable: true,
isList: true
});
expect(transformScalarToType(field)).toEqual("string[]");
expect(transformScalarToType(field, ctx)).toEqual("string[]");
});

it("should transform Int to number", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "scalar",
type: PrismaScalars.Int
});
expect(transformScalarToType(field)).toEqual("number | null");
expect(transformScalarToType(field, ctx)).toEqual("number | null");
});

it("should transform DateTime to Date", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "scalar",
type: PrismaScalars.DateTime
});
expect(transformScalarToType(field)).toEqual("Date | null");
expect(transformScalarToType(field, ctx)).toEqual("Date | null");
});

it("should transform enumTypes to Date", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "enum",
type: "Role"
});
expect(transformScalarToType(field)).toEqual("Role | null");
expect(transformScalarToType(field, ctx)).toEqual("Role | null");
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("../enums/index", "Role");
});

it("should transform User to User (self-ref)", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "object",
type: "User"
});
// @ts-ignore
field.model.name = "User";
expect(transformScalarToType(field)).toEqual("UserModel | null");
expect(transformScalarToType(field, ctx)).toEqual("UserModel | null");
expect(field.model.addImportDeclaration).not.toHaveBeenCalled();
});

it("should transform User to User", () => {
const ctx = createContextFixture();
const field = createDmmfFieldFixture({
kind: "object",
type: "Role"
});
// @ts-ignore
field.model.name = "User";
expect(transformScalarToType(field)).toEqual("RoleModel | null");
expect(transformScalarToType(field, ctx)).toEqual("RoleModel | null");
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("./RoleModel", "RoleModel");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {DmmfField} from "../domain/DmmfField.js";
import {DmmfModel} from "../domain/DmmfModel.js";
import {ScalarTsTypes} from "../domain/ScalarTsTypes.js";
import {DmmfEnum} from "../domain/DmmfEnum.js";
import type {TransformContext} from "../domain/TransformContext.js";
import {isCircularRef} from "../utils/isCircularRef.js";

export function transformScalarToType(field: DmmfField) {
export function transformScalarToType(field: DmmfField, ctx: TransformContext): string {
const {isRequired, isNullable, type, isList, location, model} = field;
let TSType: string = type;
const hasCircularRef = isCircularRef(field.model.name, field.type, ctx);

switch (location) {
case "scalar":
Expand All @@ -21,7 +24,6 @@ export function transformScalarToType(field: DmmfField) {
if (field.model.name !== field.type) {
field.model.addImportDeclaration(`./${DmmfModel.symbolName(field.type)}`, DmmfModel.symbolName(field.type));
}

break;
}

Expand All @@ -39,5 +41,10 @@ export function transformScalarToType(field: DmmfField) {
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
@@ -1,6 +1,23 @@
import {parseDocumentationAttributes} from "./parseDocumentationAttributes.js";

describe("parseDocumentationAttributes", () => {
it('should parse @TsED.Groups(type: "test", fix: "other")', () => {
expect(parseDocumentationAttributes('/// @TsED.Groups(type: "test", fix: "other")')).toEqual([
{
arguments: [
{
type: "test",
fix: "other"
}
],
content: '@TsED.Groups(type: "test", fix: "other")',
name: "Groups"
}
]);
});
it("should ignore undefined comment", () => {
expect(parseDocumentationAttributes(undefined)).toEqual([]);
});
it("should parse @TsED.Email()", () => {
expect(parseDocumentationAttributes("/// @TsED.Email()")).toEqual([
{
Expand All @@ -10,7 +27,6 @@ describe("parseDocumentationAttributes", () => {
}
]);
});

it("should parse @TsED.Ignore(endpoint = true)", () => {
expect(parseDocumentationAttributes("/// @TsED.Ignore(ctx.endpoint === true)")).toEqual([
{
Expand All @@ -20,7 +36,6 @@ describe("parseDocumentationAttributes", () => {
}
]);
});

it("should parse @TsED.Ignore()", () => {
expect(parseDocumentationAttributes("/// @TsED.Ignore()")).toEqual([
{
Expand All @@ -30,7 +45,6 @@ describe("parseDocumentationAttributes", () => {
}
]);
});

it('should parse @TsED.Groups("!creation")', () => {
expect(parseDocumentationAttributes('/// @TsED.Groups("!creation")')).toEqual([
{
Expand All @@ -56,8 +70,4 @@ describe("parseDocumentationAttributes", () => {
it("should ignore other comments", () => {
expect(parseDocumentationAttributes("/// comments")).toEqual([]);
});

it("should ignore undefined comment", () => {
expect(parseDocumentationAttributes(undefined)).toEqual([]);
});
});
Loading