Skip to content

Commit

Permalink
fix(prisma): fix casing and fix path when outputDir options is given
Browse files Browse the repository at this point in the history
Closes: #1815
  • Loading branch information
Romakita committed Apr 7, 2022
1 parent 28df061 commit 2cf987a
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 16 deletions.
4 changes: 1 addition & 3 deletions packages/orm/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -61,4 +59,4 @@
"tsed",
"prisma"
]
}
}
2 changes: 1 addition & 1 deletion packages/orm/prisma/scripts/backup-index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "../.schema";
export * from "../cjs/.schema";
5 changes: 2 additions & 3 deletions packages/orm/prisma/src/cli/generator.ts
Original file line number Diff line number Diff line change
@@ -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"]
}),
Expand Down
10 changes: 9 additions & 1 deletion packages/orm/prisma/src/cli/prismaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 "";
}
3 changes: 2 additions & 1 deletion packages/orm/prisma/src/generator/domain/DmmfModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion packages/orm/prisma/src/generator/generateCode.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
3 changes: 3 additions & 0 deletions packages/orm/prisma/src/generator/utils/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export function generateIndex(project: Project, baseDirPath: string, hasEnum: bo

project.createSourceFile(indexFile, undefined, {overwrite: true}).addExportDeclarations(
[
{
moduleSpecifier: "./client"
},
{
moduleSpecifier: "./interfaces"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/orm/prisma/test/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ generator tsed {
emitDMMF = true
}

model User {
model user {
/// @TsED.Groups("!creation")
/// Comment
id Int @id @default(autoincrement())
Expand All @@ -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[]
Expand All @@ -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?
}

Expand Down
1 change: 1 addition & 0 deletions packages/orm/prisma/test/snapshots/generate_code/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./client";
export * from "./interfaces";
export * from "./enums";
export * from "./models";
Expand Down

0 comments on commit 2cf987a

Please sign in to comment.