From 2cf987aa39417f5cd308fa56b7df975c1c3b6b0b Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Thu, 7 Apr 2022 08:56:04 +0200 Subject: [PATCH] fix(prisma): fix casing and fix path when outputDir options is given Closes: #1815 --- packages/orm/prisma/package.json | 4 +--- packages/orm/prisma/scripts/backup-index.d.ts | 2 +- packages/orm/prisma/src/cli/generator.ts | 5 ++--- packages/orm/prisma/src/cli/prismaGenerator.ts | 10 +++++++++- packages/orm/prisma/src/generator/domain/DmmfModel.ts | 3 ++- packages/orm/prisma/src/generator/generateCode.ts | 1 - .../orm/prisma/src/generator/utils/generateIndex.ts | 3 +++ .../prisma/src/generator/utils/generateRepositories.ts | 4 ++-- packages/orm/prisma/test/prisma/schema.prisma | 8 ++++---- .../orm/prisma/test/snapshots/generate_code/index.ts | 1 + 10 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/orm/prisma/package.json b/packages/orm/prisma/package.json index af769da5c2b..77409744cc5 100644 --- a/packages/orm/prisma/package.json +++ b/packages/orm/prisma/package.json @@ -20,8 +20,6 @@ "build:cjs": "tsc --build tsconfig.compile.json", "build:esm": "tsc --build tsconfig.compile.esm.json", "test": "cross-env NODE_ENV=test yarn jest", - "copy": "cp scripts/backup-index.cjs.js lib/cjs/index.js && cp scripts/backup-index.esm.js lib/esm/index.js && cp scripts/backup-index.d.ts lib/types/index.d.ts", - "prepare": "cp scripts/backup-index.cjs.js lib/cjs/index.js && cp scripts/backup-index.esm.js lib/esm/index.js && cp scripts/backup-index.d.ts lib/types/index.d.ts", "generate": "cd test && prisma -v && prisma generate" }, "private": false, @@ -61,4 +59,4 @@ "tsed", "prisma" ] -} \ No newline at end of file +} diff --git a/packages/orm/prisma/scripts/backup-index.d.ts b/packages/orm/prisma/scripts/backup-index.d.ts index f4aa627148f..d36f277dad6 100644 --- a/packages/orm/prisma/scripts/backup-index.d.ts +++ b/packages/orm/prisma/scripts/backup-index.d.ts @@ -1 +1 @@ -export * from "../.schema"; +export * from "../cjs/.schema"; diff --git a/packages/orm/prisma/src/cli/generator.ts b/packages/orm/prisma/src/cli/generator.ts index d9c1e6a713b..1163a44cc6f 100644 --- a/packages/orm/prisma/src/cli/generator.ts +++ b/packages/orm/prisma/src/cli/generator.ts @@ -1,10 +1,9 @@ import {generatorHandler} from "@prisma/generator-helper"; -import {generate} from "./prismaGenerator"; -import {join} from "path"; +import {generate, defaultOutput} from "./prismaGenerator"; generatorHandler({ onManifest: () => ({ - defaultOutput: join(__dirname, "..", ".schema"), + defaultOutput, prettyName: "Ts.ED integration", requiresGenerators: ["prisma-client-js"] }), diff --git a/packages/orm/prisma/src/cli/prismaGenerator.ts b/packages/orm/prisma/src/cli/prismaGenerator.ts index f57ad42bed0..be6c73890fc 100644 --- a/packages/orm/prisma/src/cli/prismaGenerator.ts +++ b/packages/orm/prisma/src/cli/prismaGenerator.ts @@ -3,7 +3,10 @@ import {parseEnvValue} from "@prisma/sdk"; import {promises as asyncFs} from "fs"; import {generateCode} from "../generator/generateCode"; import removeDir from "../generator/utils/removeDir"; -import path from "path"; +import path, {join} from "path"; + +export const defaultOutput = join(__dirname, "..", ".schema"); +export const packageDir = join(__dirname, "..", "..", ".."); function parseStringBoolean(stringBoolean: string | undefined) { return Boolean(stringBoolean ? stringBoolean === "true" : undefined); @@ -28,5 +31,10 @@ export async function generate(options: GeneratorOptions) { prismaClientPath: prismaClientPath.includes("node_modules") ? "@prisma/client" : toUnixPath(path.relative(outputDir, prismaClientPath)) }); + if (outputDir === defaultOutput) { + await asyncFs.cp(join(packageDir, "scripts", "backup-index.cjs.js"), join(packageDir, "lib", "cjs", "index.js")); + await asyncFs.cp(join(packageDir, "scripts", "backup-index.d.ts"), join(packageDir, "lib", "types", "index.d.ts")); + } + return ""; } diff --git a/packages/orm/prisma/src/generator/domain/DmmfModel.ts b/packages/orm/prisma/src/generator/domain/DmmfModel.ts index c46841b7908..68f4a899ad4 100644 --- a/packages/orm/prisma/src/generator/domain/DmmfModel.ts +++ b/packages/orm/prisma/src/generator/domain/DmmfModel.ts @@ -2,6 +2,7 @@ import {DMMF} from "@prisma/generator-helper"; import {toMap} from "@tsed/core"; import {ImportDeclarationStructure, StructureKind} from "ts-morph"; import {DmmfField} from "./DmmfField"; +import {pascalCase} from "change-case"; export class DmmfModel { readonly isInputType: boolean; @@ -61,7 +62,7 @@ export class DmmfModel { } static symbolName(name: string) { - return `${name}Model`; + return pascalCase(`${name}Model`); } addImportDeclaration(moduleSpecifier: string, name: string, isDefault = false) { diff --git a/packages/orm/prisma/src/generator/generateCode.ts b/packages/orm/prisma/src/generator/generateCode.ts index 15ed8778b0c..8343a833573 100644 --- a/packages/orm/prisma/src/generator/generateCode.ts +++ b/packages/orm/prisma/src/generator/generateCode.ts @@ -1,5 +1,4 @@ import {DMMF} from "@prisma/generator-helper"; -import path from "path"; import {CompilerOptions, ModuleKind, Project, ScriptTarget} from "ts-morph"; import {generateEnums} from "./utils/generateEnums"; import {generateModels} from "./utils/generateModels"; diff --git a/packages/orm/prisma/src/generator/utils/generateIndex.ts b/packages/orm/prisma/src/generator/utils/generateIndex.ts index 608d08a96e9..d1c8a9bd6b1 100644 --- a/packages/orm/prisma/src/generator/utils/generateIndex.ts +++ b/packages/orm/prisma/src/generator/utils/generateIndex.ts @@ -6,6 +6,9 @@ export function generateIndex(project: Project, baseDirPath: string, hasEnum: bo project.createSourceFile(indexFile, undefined, {overwrite: true}).addExportDeclarations( [ + { + moduleSpecifier: "./client" + }, { moduleSpecifier: "./interfaces" }, diff --git a/packages/orm/prisma/src/generator/utils/generateRepositories.ts b/packages/orm/prisma/src/generator/utils/generateRepositories.ts index 89769b28d0c..c4d09b1e79f 100644 --- a/packages/orm/prisma/src/generator/utils/generateRepositories.ts +++ b/packages/orm/prisma/src/generator/utils/generateRepositories.ts @@ -45,7 +45,7 @@ export function generateRepositories(dmmf: DMMF.Document, project: Project, base const repositoriesIndex = repoDirectory.createSourceFile(`index.ts`, undefined, {overwrite: true}); const exportedModels = models.map((model) => { - const name = `${pluralize(model.name)}Repository`; + const name = pascalCase(`${pluralize(model.name)}Repository`); const modelName = model.toString(); const sourceFile = repoDirectory.createSourceFile(`${name}.ts`, undefined, {overwrite: true}); @@ -191,7 +191,7 @@ export function generateRepositories(dmmf: DMMF.Document, project: Project, base addDelegatedMethod({ repository, name: "aggregate", - model: model.name + model: pascalCase(model.name) }); return name; diff --git a/packages/orm/prisma/test/prisma/schema.prisma b/packages/orm/prisma/test/prisma/schema.prisma index e08dadb001e..313ecd469ff 100644 --- a/packages/orm/prisma/test/prisma/schema.prisma +++ b/packages/orm/prisma/test/prisma/schema.prisma @@ -17,7 +17,7 @@ generator tsed { emitDMMF = true } -model User { +model user { /// @TsED.Groups("!creation") /// Comment id Int @id @default(autoincrement()) @@ -29,8 +29,8 @@ model User { is18 Boolean? name String? successorId Int? - successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id]) - predecessor User? @relation("BlogOwnerHistory") + successor user? @relation("BlogOwnerHistory", fields: [successorId], references: [id]) + predecessor user? @relation("BlogOwnerHistory") role Role @default(USER) posts Post[] keywords String[] @@ -41,7 +41,7 @@ model User { model Post { id Int @id @default(autoincrement()) - user User? @relation(fields: [userId], references: [id]) + user user? @relation(fields: [userId], references: [id]) userId Int? } diff --git a/packages/orm/prisma/test/snapshots/generate_code/index.ts b/packages/orm/prisma/test/snapshots/generate_code/index.ts index d3f117a9af6..3fd3f291c08 100644 --- a/packages/orm/prisma/test/snapshots/generate_code/index.ts +++ b/packages/orm/prisma/test/snapshots/generate_code/index.ts @@ -1,3 +1,4 @@ +export * from "./client"; export * from "./interfaces"; export * from "./enums"; export * from "./models";