@tsed/prisma 8.0.1 → 8.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -11
- package/readme.md +5 -5
- package/src/cli/prismaGenerator.ts +50 -0
- package/src/dev.ts +10 -0
- package/src/generator/domain/DmmfEnum.ts +42 -0
- package/src/generator/domain/DmmfField.spec.ts +40 -0
- package/src/generator/domain/DmmfField.ts +52 -0
- package/src/generator/domain/DmmfModel.ts +94 -0
- package/src/generator/domain/ScalarTsTypes.ts +54 -0
- package/src/generator/domain/TransformContext.ts +7 -0
- package/src/generator/generateCode.spec.ts +16 -0
- package/src/generator/generateCode.ts +51 -0
- package/src/generator/transform/transformEnumsToEnums.spec.ts +44 -0
- package/src/generator/transform/transformEnumsToEnums.ts +21 -0
- package/src/generator/transform/transformFieldToDecorators.spec.ts +222 -0
- package/src/generator/transform/transformFieldToDecorators.ts +72 -0
- package/src/generator/transform/transformFieldToProperty.ts +16 -0
- package/src/generator/transform/transformModelToClass.spec.ts +495 -0
- package/src/generator/transform/transformModelToClass.ts +19 -0
- package/src/generator/transform/transformScalarToType.spec.ts +125 -0
- package/src/generator/transform/transformScalarToType.ts +50 -0
- package/src/generator/utils/__snapshots__/generateEnums.spec.ts.snap +10 -0
- package/src/generator/utils/__snapshots__/generateModels.spec.ts.snap +113 -0
- package/src/generator/utils/__snapshots__/generatePrismaService.spec.ts.snap +28 -0
- package/src/generator/utils/__snapshots__/generateRespositories.spec.ts.snap +151 -0
- package/src/generator/utils/generateClientIndex.ts +20 -0
- package/src/generator/utils/generateDocuments.ts +20 -0
- package/src/generator/utils/generateEnums.spec.ts +15 -0
- package/src/generator/utils/generateEnums.ts +25 -0
- package/src/generator/utils/generateIndex.ts +28 -0
- package/src/generator/utils/generateInterfaces.ts +33 -0
- package/src/generator/utils/generateModels.spec.ts +30 -0
- package/src/generator/utils/generateModels.ts +28 -0
- package/src/generator/utils/generateOutputsBarrelFile.ts +14 -0
- package/src/generator/utils/generatePrismaService.spec.ts +12 -0
- package/src/generator/utils/generatePrismaService.ts +90 -0
- package/src/generator/utils/generateRepositories.ts +207 -0
- package/src/generator/utils/generateRespositories.spec.ts +29 -0
- package/src/generator/utils/isCircularRef.spec.ts +22 -0
- package/src/generator/utils/isCircularRef.ts +33 -0
- package/src/generator/utils/parseDocumentationAttributes.spec.ts +73 -0
- package/src/generator/utils/parseDocumentationAttributes.ts +64 -0
- package/src/generator/utils/removeDir.ts +15 -0
- package/src/generator/utils/resolveExtension.ts +7 -0
- package/src/generator/utils/saveProject.ts +8 -0
- package/src/generator.ts +18 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
"name": "@tsed/prisma",
|
|
3
3
|
"description": "Generate Ts.ED JsonSchema based on Prisma models",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.0.
|
|
5
|
+
"version": "8.0.3",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
9
9
|
"typings": "./lib/types/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"@tsed/source": "./src/index.ts",
|
|
12
13
|
"types": "./lib/types/index.d.ts",
|
|
13
14
|
"import": "./lib/esm/index.js",
|
|
14
15
|
"default": "./lib/esm/index.js"
|
|
@@ -39,13 +40,13 @@
|
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@prisma/client": "^5.20.0",
|
|
42
|
-
"@tsed/barrels": "8.0.
|
|
43
|
-
"@tsed/core": "8.0.
|
|
44
|
-
"@tsed/di": "8.0.
|
|
45
|
-
"@tsed/json-mapper": "8.0.
|
|
43
|
+
"@tsed/barrels": "8.0.3",
|
|
44
|
+
"@tsed/core": "8.0.3",
|
|
45
|
+
"@tsed/di": "8.0.3",
|
|
46
|
+
"@tsed/json-mapper": "8.0.3",
|
|
46
47
|
"@tsed/monorepo-utils": "2.3.9",
|
|
47
|
-
"@tsed/schema": "8.0.
|
|
48
|
-
"@tsed/typescript": "8.0.
|
|
48
|
+
"@tsed/schema": "8.0.3",
|
|
49
|
+
"@tsed/typescript": "8.0.3",
|
|
49
50
|
"@types/change-case": "^2.3.1",
|
|
50
51
|
"@types/pluralize": "0.0.33",
|
|
51
52
|
"eslint": "9.12.0",
|
|
@@ -55,10 +56,10 @@
|
|
|
55
56
|
},
|
|
56
57
|
"peerDependencies": {
|
|
57
58
|
"@prisma/client": ">=5.0.0",
|
|
58
|
-
"@tsed/core": "8.0.
|
|
59
|
-
"@tsed/di": "8.0.
|
|
60
|
-
"@tsed/json-mapper": "8.0.
|
|
61
|
-
"@tsed/schema": "8.0.
|
|
59
|
+
"@tsed/core": "8.0.3",
|
|
60
|
+
"@tsed/di": "8.0.3",
|
|
61
|
+
"@tsed/json-mapper": "8.0.3",
|
|
62
|
+
"@tsed/schema": "8.0.3"
|
|
62
63
|
},
|
|
63
64
|
"keywords": [
|
|
64
65
|
"TypeScript",
|
package/readme.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p style="text-align: center" align="center">
|
|
2
|
-
<a href="https://tsed.
|
|
2
|
+
<a href="https://tsed.dev" target="_blank"><img src="https://tsed.dev/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
<div align="center">
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
</div>
|
|
16
16
|
|
|
17
17
|
<div align="center">
|
|
18
|
-
<a href="https://tsed.
|
|
18
|
+
<a href="https://tsed.dev/">Website</a>
|
|
19
19
|
<span> • </span>
|
|
20
|
-
<a href="https://tsed.
|
|
20
|
+
<a href="https://tsed.dev/tutorials/prisma.html">Tutorial</a>
|
|
21
21
|
<span> • </span>
|
|
22
22
|
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
|
|
23
23
|
<span> • </span>
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
|
|
29
29
|
## Installation
|
|
30
30
|
|
|
31
|
-
See our [documentation](https://tsed.
|
|
31
|
+
See our [documentation](https://tsed.dev/tutorials/prisma.html) for more information.
|
|
32
32
|
|
|
33
33
|
## Contributors
|
|
34
34
|
|
|
35
|
-
Please read [contributing guidelines here](https://tsed.
|
|
35
|
+
Please read [contributing guidelines here](https://tsed.dev/contributing.html)
|
|
36
36
|
|
|
37
37
|
<a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
38
38
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {GeneratorOptions} from "@prisma/generator-helper";
|
|
2
|
+
import internals from "@prisma/internals";
|
|
3
|
+
import fs from "fs-extra";
|
|
4
|
+
import path, {join} from "path";
|
|
5
|
+
|
|
6
|
+
import {generateCode} from "../generator/generateCode.js";
|
|
7
|
+
import removeDir from "../generator/utils/removeDir.js";
|
|
8
|
+
|
|
9
|
+
function parseStringBoolean(stringBoolean: string | string[] | undefined) {
|
|
10
|
+
return Boolean(stringBoolean ? stringBoolean === "true" : undefined);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function toUnixPath(maybeWindowsPath: string) {
|
|
14
|
+
return maybeWindowsPath.split("\\").join("/");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GenerateOptions {
|
|
18
|
+
defaultOutput: string;
|
|
19
|
+
packageDir: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function generate({defaultOutput, packageDir}: GenerateOptions) {
|
|
23
|
+
return async (options: GeneratorOptions) => {
|
|
24
|
+
const outputDir = internals.parseEnvValue(options.generator.output!);
|
|
25
|
+
await fs.mkdir(outputDir, {recursive: true});
|
|
26
|
+
await removeDir(outputDir, true);
|
|
27
|
+
|
|
28
|
+
const generatorConfig = options.generator.config;
|
|
29
|
+
const prismaClientProvider = options.otherGenerators.find((it) => internals.parseEnvValue(it.provider) === "prisma-client-js")!;
|
|
30
|
+
const prismaClientPath = internals.parseEnvValue(prismaClientProvider.output!);
|
|
31
|
+
|
|
32
|
+
await generateCode(options.dmmf, {
|
|
33
|
+
emitTranspiledCode: parseStringBoolean(generatorConfig.emitTranspiledCode),
|
|
34
|
+
outputDirPath: outputDir,
|
|
35
|
+
prismaClientPath: prismaClientPath.includes("node_modules")
|
|
36
|
+
? "@prisma/client"
|
|
37
|
+
: toUnixPath(path.relative(outputDir, prismaClientPath))
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (outputDir === defaultOutput) {
|
|
41
|
+
await fs.copy(join(packageDir, "scripts", "backup-index.esm.js"), join(packageDir, "lib", "esm", "index.js"));
|
|
42
|
+
await fs.copy(join(packageDir, "scripts", "backup-index.d.mts"), join(packageDir, "lib", "types", "index.d.ts"));
|
|
43
|
+
await fs.writeJson(`${outputDir}/package.json`, {
|
|
44
|
+
type: "module"
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return "";
|
|
49
|
+
};
|
|
50
|
+
}
|
package/src/dev.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {DMMF} from "@prisma/generator-helper";
|
|
2
|
+
import {ImportDeclarationStructure, StructureKind} from "ts-morph";
|
|
3
|
+
|
|
4
|
+
export class DmmfEnum {
|
|
5
|
+
readonly model: DMMF.DatamodelEnum;
|
|
6
|
+
readonly modelType: DMMF.SchemaEnum;
|
|
7
|
+
|
|
8
|
+
#imports = new Map<string, ImportDeclarationStructure>();
|
|
9
|
+
|
|
10
|
+
constructor({model, modelType}: {model: DMMF.DatamodelEnum; modelType: DMMF.SchemaEnum}) {
|
|
11
|
+
this.model = model;
|
|
12
|
+
this.modelType = modelType;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get name() {
|
|
16
|
+
return this.model.name;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get values() {
|
|
20
|
+
return this.modelType.values;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static getEnums(dmmf: DMMF.Document, enumsMap: Map<string, DMMF.DatamodelEnum>) {
|
|
24
|
+
const enums = dmmf.schema.enumTypes.model || [];
|
|
25
|
+
|
|
26
|
+
return enums.map(
|
|
27
|
+
(modelType) =>
|
|
28
|
+
new DmmfEnum({
|
|
29
|
+
modelType,
|
|
30
|
+
model: enumsMap.get(modelType.name) as unknown as DMMF.DatamodelEnum
|
|
31
|
+
})
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static symbolName(name: string) {
|
|
36
|
+
return `${name}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toString() {
|
|
40
|
+
return DmmfEnum.symbolName(this.name);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {DmmfField} from "./DmmfField.js";
|
|
2
|
+
import {DmmfModel} from "./DmmfModel.js";
|
|
3
|
+
|
|
4
|
+
describe("DmmfField", () => {
|
|
5
|
+
it("should create a new instance of DmmfField", () => {
|
|
6
|
+
const dmmfField = new DmmfField({
|
|
7
|
+
field: {
|
|
8
|
+
name: "name",
|
|
9
|
+
isRequired: true,
|
|
10
|
+
type: "string",
|
|
11
|
+
isList: false,
|
|
12
|
+
kind: "scalar"
|
|
13
|
+
},
|
|
14
|
+
schemaArg: {
|
|
15
|
+
isNullable: false
|
|
16
|
+
},
|
|
17
|
+
model: new DmmfModel({
|
|
18
|
+
model: {
|
|
19
|
+
name: "model",
|
|
20
|
+
fields: []
|
|
21
|
+
},
|
|
22
|
+
modelType: {
|
|
23
|
+
fields: []
|
|
24
|
+
},
|
|
25
|
+
isInputType: true
|
|
26
|
+
})
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
expect(dmmfField).toBeInstanceOf(DmmfField);
|
|
30
|
+
expect(dmmfField.name).toBe("name");
|
|
31
|
+
expect(dmmfField.isRequired).toBe(true);
|
|
32
|
+
expect(dmmfField.type).toBe("string");
|
|
33
|
+
expect(dmmfField.isList).toBe(false);
|
|
34
|
+
expect(dmmfField.kind).toBe("scalar");
|
|
35
|
+
expect(dmmfField.isNullable).toBe(false);
|
|
36
|
+
expect(dmmfField.location).toBe("scalar");
|
|
37
|
+
expect(dmmfField.getAdditionalDecorators()).toEqual([]);
|
|
38
|
+
expect(dmmfField.toString()).toBe("name");
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {DMMF} from "@prisma/generator-helper";
|
|
2
|
+
|
|
3
|
+
import {parseDocumentationAttributes} from "../utils/parseDocumentationAttributes.js";
|
|
4
|
+
import type {DmmfModel} from "./DmmfModel.js";
|
|
5
|
+
|
|
6
|
+
export class DmmfField {
|
|
7
|
+
readonly model: DmmfModel;
|
|
8
|
+
readonly field: DMMF.Field;
|
|
9
|
+
readonly schemaArg: DMMF.SchemaArg;
|
|
10
|
+
|
|
11
|
+
constructor({field, schemaArg, model}: any) {
|
|
12
|
+
this.field = field;
|
|
13
|
+
this.model = model;
|
|
14
|
+
this.schemaArg = schemaArg;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get name() {
|
|
18
|
+
return this.field.name;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get isRequired() {
|
|
22
|
+
return this.field.isRequired;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get type() {
|
|
26
|
+
return this.field.type;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get isList() {
|
|
30
|
+
return this.field.isList;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get kind() {
|
|
34
|
+
return this.field.kind;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get isNullable() {
|
|
38
|
+
return this.schemaArg.isNullable;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get location(): "enumTypes" | "inputObjectTypes" | "scalar" {
|
|
42
|
+
return this.field.kind === "enum" ? "enumTypes" : this.field.kind === "object" ? "inputObjectTypes" : "scalar";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getAdditionalDecorators() {
|
|
46
|
+
return parseDocumentationAttributes(this.field.documentation);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
toString() {
|
|
50
|
+
return this.name;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {DMMF} from "@prisma/generator-helper";
|
|
2
|
+
import {toMap} from "@tsed/core";
|
|
3
|
+
import {pascalCase} from "change-case";
|
|
4
|
+
import {ImportDeclarationStructure, StructureKind} from "ts-morph";
|
|
5
|
+
|
|
6
|
+
import {resolveExtension} from "../utils/resolveExtension.js";
|
|
7
|
+
import {DmmfField} from "./DmmfField.js";
|
|
8
|
+
|
|
9
|
+
export class DmmfModel {
|
|
10
|
+
readonly isInputType: boolean;
|
|
11
|
+
readonly model: DMMF.Model;
|
|
12
|
+
readonly modelType: DMMF.InputType | DMMF.OutputType;
|
|
13
|
+
#imports = new Map<string, ImportDeclarationStructure>();
|
|
14
|
+
|
|
15
|
+
constructor({isInputType, model, modelType}: any) {
|
|
16
|
+
this.model = model;
|
|
17
|
+
this.modelType = modelType;
|
|
18
|
+
this.isInputType = isInputType;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get name() {
|
|
22
|
+
return this.model.name;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get fields() {
|
|
26
|
+
const dataField = toMap<string, DMMF.SchemaArg>(this.modelType.fields as any, "name");
|
|
27
|
+
|
|
28
|
+
return this.model.fields.map((field) => {
|
|
29
|
+
return new DmmfField({
|
|
30
|
+
model: this,
|
|
31
|
+
field,
|
|
32
|
+
schemaArg: dataField.get(field.name)
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get importDeclarations() {
|
|
38
|
+
return [...this.#imports.values()];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static getModels(dmmf: DMMF.Document, modelsMap: Map<string, DMMF.Model>, typesMap?: Map<string, DMMF.Model>): DmmfModel[] {
|
|
42
|
+
const inputObjectsTypes = dmmf.schema.inputObjectTypes.model || [];
|
|
43
|
+
const outputObjectTypes = dmmf.schema.outputObjectTypes.model || [];
|
|
44
|
+
|
|
45
|
+
const factory = (isInputType: boolean) => (modelType: DMMF.InputType | DMMF.OutputType) => {
|
|
46
|
+
let model = modelsMap.get(modelType.name) || typesMap?.get(modelType.name);
|
|
47
|
+
|
|
48
|
+
if (model) {
|
|
49
|
+
return new DmmfModel({
|
|
50
|
+
modelType,
|
|
51
|
+
model,
|
|
52
|
+
isInputType
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return undefined;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const inputs: DmmfModel[] = inputObjectsTypes.map(factory(true)).filter(Boolean) as DmmfModel[];
|
|
60
|
+
const outputs: DmmfModel[] = outputObjectTypes.map(factory(false)).filter(Boolean) as DmmfModel[];
|
|
61
|
+
|
|
62
|
+
return [...inputs, ...outputs];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static symbolName(name: string) {
|
|
66
|
+
return pascalCase(`${name}Model`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
addImportDeclaration(moduleSpecifier: string, name: string, isTypeOnly = false) {
|
|
70
|
+
if (!this.#imports.has(moduleSpecifier)) {
|
|
71
|
+
moduleSpecifier = resolveExtension(moduleSpecifier);
|
|
72
|
+
|
|
73
|
+
this.#imports.set(moduleSpecifier, {
|
|
74
|
+
kind: StructureKind.ImportDeclaration,
|
|
75
|
+
moduleSpecifier: moduleSpecifier,
|
|
76
|
+
namedImports: [],
|
|
77
|
+
isTypeOnly
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const moduleDeclaration = this.#imports.get(moduleSpecifier)!;
|
|
82
|
+
const nameImports = moduleDeclaration.namedImports as any[];
|
|
83
|
+
|
|
84
|
+
if (!nameImports.includes(name)) {
|
|
85
|
+
nameImports.push(name);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
toString() {
|
|
92
|
+
return DmmfModel.symbolName(this.name);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {DecoratorStructure, StructureKind} from "ts-morph";
|
|
2
|
+
|
|
3
|
+
export enum PrismaScalars {
|
|
4
|
+
String = "String",
|
|
5
|
+
Boolean = "Boolean",
|
|
6
|
+
Int = "Int",
|
|
7
|
+
Float = "Float",
|
|
8
|
+
BigInt = "BigInt",
|
|
9
|
+
Decimal = "Decimal",
|
|
10
|
+
DateTime = "DateTime",
|
|
11
|
+
Json = "Json",
|
|
12
|
+
Bytes = "Bytes"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const ScalarTsTypes: Record<string, string> = {
|
|
16
|
+
[PrismaScalars.String]: "string",
|
|
17
|
+
[PrismaScalars.Boolean]: "boolean",
|
|
18
|
+
[PrismaScalars.Int]: "number",
|
|
19
|
+
[PrismaScalars.Float]: "number",
|
|
20
|
+
[PrismaScalars.Decimal]: "number",
|
|
21
|
+
[PrismaScalars.BigInt]: "bigint",
|
|
22
|
+
[PrismaScalars.DateTime]: "Date",
|
|
23
|
+
[PrismaScalars.Json]: "any",
|
|
24
|
+
[PrismaScalars.Bytes]: "Buffer"
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const ScalarJsClasses: Record<string, string> = {
|
|
28
|
+
[PrismaScalars.String]: "String",
|
|
29
|
+
[PrismaScalars.Boolean]: "Boolean",
|
|
30
|
+
[PrismaScalars.Int]: "Number",
|
|
31
|
+
[PrismaScalars.Float]: "Number",
|
|
32
|
+
[PrismaScalars.Decimal]: "Number",
|
|
33
|
+
[PrismaScalars.BigInt]: "BigInt",
|
|
34
|
+
[PrismaScalars.DateTime]: "Date",
|
|
35
|
+
[PrismaScalars.Json]: "Object",
|
|
36
|
+
[PrismaScalars.Bytes]: "Buffer"
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const ScalarDecorators: Record<string, Omit<DecoratorStructure, "kind">[]> = {
|
|
40
|
+
[PrismaScalars.String]: [],
|
|
41
|
+
[PrismaScalars.Boolean]: [],
|
|
42
|
+
[PrismaScalars.Int]: [
|
|
43
|
+
{
|
|
44
|
+
name: "Integer",
|
|
45
|
+
arguments: []
|
|
46
|
+
}
|
|
47
|
+
],
|
|
48
|
+
[PrismaScalars.Float]: [],
|
|
49
|
+
[PrismaScalars.BigInt]: [],
|
|
50
|
+
[PrismaScalars.Decimal]: [],
|
|
51
|
+
[PrismaScalars.DateTime]: [{name: "Format", arguments: ['"date-time"']}],
|
|
52
|
+
[PrismaScalars.Json]: [],
|
|
53
|
+
[PrismaScalars.Bytes]: []
|
|
54
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {createDmmfFixture} from "../__mock__/createDmmfFixture.js";
|
|
2
|
+
import {createProjectFixture} from "../__mock__/createProjectFixture.js";
|
|
3
|
+
import {generateCode} from "./generateCode.js";
|
|
4
|
+
|
|
5
|
+
describe("generateCode", () => {
|
|
6
|
+
it("should generate all codes", async () => {
|
|
7
|
+
const {baseDir} = createProjectFixture("generate_code");
|
|
8
|
+
const dmmf = createDmmfFixture();
|
|
9
|
+
|
|
10
|
+
await generateCode(dmmf, {
|
|
11
|
+
emitTranspiledCode: false,
|
|
12
|
+
outputDirPath: baseDir,
|
|
13
|
+
prismaClientPath: "@prisma/client"
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {DMMF} from "@prisma/generator-helper";
|
|
2
|
+
import {CompilerOptions, ModuleKind, Project, ScriptTarget} from "ts-morph";
|
|
3
|
+
|
|
4
|
+
import {generateClientIndex} from "./utils/generateClientIndex.js";
|
|
5
|
+
import {generateEnums} from "./utils/generateEnums.js";
|
|
6
|
+
import {generateIndex} from "./utils/generateIndex.js";
|
|
7
|
+
import {generateInterfaces} from "./utils/generateInterfaces.js";
|
|
8
|
+
import {generateModels} from "./utils/generateModels.js";
|
|
9
|
+
import {generatePrismaService} from "./utils/generatePrismaService.js";
|
|
10
|
+
import {generateRepositories} from "./utils/generateRepositories.js";
|
|
11
|
+
import {saveProject} from "./utils/saveProject.js";
|
|
12
|
+
|
|
13
|
+
const baseCompilerOptions: CompilerOptions = {
|
|
14
|
+
target: ScriptTarget.ESNext,
|
|
15
|
+
module: ModuleKind.ESNext,
|
|
16
|
+
emitDecoratorMetadata: true,
|
|
17
|
+
experimentalDecorators: true,
|
|
18
|
+
esModuleInterop: true
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface GenerateCodeOptions {
|
|
22
|
+
emitTranspiledCode: boolean;
|
|
23
|
+
outputDirPath: string;
|
|
24
|
+
prismaClientPath: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function generateCode(dmmf: DMMF.Document, options: GenerateCodeOptions) {
|
|
28
|
+
const baseDirPath = options.outputDirPath;
|
|
29
|
+
const emitTranspiledCode = options.emitTranspiledCode ? true : options.outputDirPath.includes("node_modules");
|
|
30
|
+
|
|
31
|
+
const project = new Project({
|
|
32
|
+
compilerOptions: {
|
|
33
|
+
...baseCompilerOptions,
|
|
34
|
+
...(emitTranspiledCode && {declaration: true})
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const hasEnum = generateEnums(dmmf, project, baseDirPath);
|
|
39
|
+
generateModels(dmmf, project, baseDirPath);
|
|
40
|
+
generateInterfaces(project, baseDirPath);
|
|
41
|
+
generateClientIndex(project, baseDirPath, options);
|
|
42
|
+
generatePrismaService(project, baseDirPath);
|
|
43
|
+
generateRepositories(dmmf, project, baseDirPath);
|
|
44
|
+
generateIndex(project, baseDirPath, hasEnum);
|
|
45
|
+
|
|
46
|
+
if (emitTranspiledCode) {
|
|
47
|
+
await project.emit();
|
|
48
|
+
} else {
|
|
49
|
+
await saveProject(project);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {DmmfEnum} from "../domain/DmmfEnum.js";
|
|
2
|
+
import {transformEnumsToEnums} from "./transformEnumsToEnums.js";
|
|
3
|
+
|
|
4
|
+
describe("transformEnumsToEnums()", () => {
|
|
5
|
+
it("should transform Prisma Enum to a TS Enum", () => {
|
|
6
|
+
const enumModel = new DmmfEnum({
|
|
7
|
+
model: {
|
|
8
|
+
name: "Role",
|
|
9
|
+
values: [
|
|
10
|
+
{
|
|
11
|
+
name: "ADMIN",
|
|
12
|
+
dbName: "dbName"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: "USER",
|
|
16
|
+
dbName: "dbName"
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
modelType: {
|
|
21
|
+
name: "Role",
|
|
22
|
+
values: ["ADMIN", "USER"]
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(transformEnumsToEnums(enumModel)).toEqual({
|
|
27
|
+
isExported: true,
|
|
28
|
+
kind: 7,
|
|
29
|
+
leadingTrivia: "\n",
|
|
30
|
+
members: [
|
|
31
|
+
{
|
|
32
|
+
name: "ADMIN",
|
|
33
|
+
value: "ADMIN"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "USER",
|
|
37
|
+
value: "USER"
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
name: "Role",
|
|
41
|
+
trailingTrivia: "\n"
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {EnumMemberStructure, OptionalKind, StatementStructures, StructureKind} from "ts-morph";
|
|
2
|
+
|
|
3
|
+
import {DmmfEnum} from "../domain/DmmfEnum.js";
|
|
4
|
+
|
|
5
|
+
export function transformEnumsToEnums(enumModel: DmmfEnum): StatementStructures {
|
|
6
|
+
const members: OptionalKind<EnumMemberStructure>[] = enumModel.values.map((value) => {
|
|
7
|
+
return {
|
|
8
|
+
name: value,
|
|
9
|
+
value: value
|
|
10
|
+
};
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
kind: StructureKind.Enum,
|
|
15
|
+
name: enumModel.toString(),
|
|
16
|
+
trailingTrivia: "\n",
|
|
17
|
+
leadingTrivia: "\n",
|
|
18
|
+
isExported: true,
|
|
19
|
+
members
|
|
20
|
+
};
|
|
21
|
+
}
|