@tsed/prisma 7.27.2 → 7.29.0
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/lib/esm/cli/dev.js +9 -7
- package/lib/esm/cli/generator.js +8 -7
- package/lib/esm/cli/prismaGenerator.js +21 -23
- package/lib/esm/generator/domain/DmmfEnum.js +28 -30
- package/lib/esm/generator/domain/DmmfField.js +37 -40
- package/lib/esm/generator/domain/DmmfModel.js +68 -70
- package/lib/esm/generator/domain/ScalarTsTypes.js +43 -43
- package/lib/esm/generator/domain/TransformContext.js +2 -1
- package/lib/esm/generator/generateCode.js +27 -27
- package/lib/esm/generator/transform/transformEnumsToEnums.js +14 -13
- package/lib/esm/generator/transform/transformFieldToDecorators.js +45 -44
- package/lib/esm/generator/transform/transformFieldToProperty.js +9 -8
- package/lib/esm/generator/transform/transformModelToClass.js +12 -11
- package/lib/esm/generator/transform/transformScalarToType.js +33 -37
- package/lib/esm/generator/utils/generateClientIndex.js +7 -8
- package/lib/esm/generator/utils/generateDocuments.js +10 -11
- package/lib/esm/generator/utils/generateEnums.js +12 -13
- package/lib/esm/generator/utils/generateIndex.js +20 -15
- package/lib/esm/generator/utils/generateInterfaces.js +26 -23
- package/lib/esm/generator/utils/generateModels.js +14 -15
- package/lib/esm/generator/utils/generateOutputsBarrelFile.js +7 -6
- package/lib/esm/generator/utils/generatePrismaService.js +72 -55
- package/lib/esm/generator/utils/generateRepositories.js +161 -148
- package/lib/esm/generator/utils/isCircularRef.js +21 -20
- package/lib/esm/generator/utils/parseDocumentationAttributes.js +48 -37
- package/lib/esm/generator/utils/removeDir.js +10 -11
- package/lib/esm/generator/utils/saveProject.js +6 -7
- package/lib/esm/generator.js +2 -1
- package/lib/esm/index.js +2 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -13
|
@@ -5,155 +5,168 @@ import { DmmfModel } from "../domain/DmmfModel.js";
|
|
|
5
5
|
import { generateOutputsBarrelFile } from "./generateOutputsBarrelFile.js";
|
|
6
6
|
import pluralize from "pluralize";
|
|
7
7
|
import { pascalCase, camelCase } from "change-case";
|
|
8
|
-
function addDelegatedMethod({
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}]
|
|
24
|
-
});
|
|
25
|
-
if (returnType) {
|
|
26
|
-
method.setBodyText(`const obj = await this.collection.${name}(args);
|
|
8
|
+
function addDelegatedMethod({ name, hasQuestionToken, repository, model, returnType }) {
|
|
9
|
+
const method = repository.addMethod({
|
|
10
|
+
name: name,
|
|
11
|
+
isAsync: true,
|
|
12
|
+
returnType: returnType ? `Promise<${returnType}>` : undefined,
|
|
13
|
+
parameters: [
|
|
14
|
+
{
|
|
15
|
+
name: "args",
|
|
16
|
+
type: `Prisma.${model}${pascalCase(name)}Args`,
|
|
17
|
+
hasQuestionToken
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
});
|
|
21
|
+
if (returnType) {
|
|
22
|
+
method.setBodyText(`const obj = await this.collection.${name}(args);
|
|
27
23
|
return this.deserialize<${returnType}>(obj);`);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
method.setBodyText(`return this.collection.${name}(args)`);
|
|
27
|
+
}
|
|
31
28
|
}
|
|
32
29
|
export function generateRepositories(dmmf, project, baseDirPath) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
30
|
+
const modelsMap = toMap(dmmf.datamodel.models, "name");
|
|
31
|
+
const models = DmmfModel.getModels(dmmf, modelsMap);
|
|
32
|
+
const repoDirPath = path.resolve(baseDirPath, "repositories");
|
|
33
|
+
const repoDirectory = project.createDirectory(repoDirPath);
|
|
34
|
+
const repositoriesIndex = repoDirectory.createSourceFile(`index.ts`, undefined, { overwrite: true });
|
|
35
|
+
const exportedModels = models.map((model) => {
|
|
36
|
+
const name = pascalCase(`${pluralize(model.name)}Repository`);
|
|
37
|
+
const modelName = model.toString();
|
|
38
|
+
const sourceFile = repoDirectory.createSourceFile(`${name}.ts`, undefined, { overwrite: true });
|
|
39
|
+
sourceFile.addImportDeclarations([
|
|
40
|
+
{
|
|
41
|
+
moduleSpecifier: "@tsed/core",
|
|
42
|
+
namedImports: ["isArray"]
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
moduleSpecifier: "@tsed/json-mapper",
|
|
46
|
+
namedImports: ["deserialize"]
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
moduleSpecifier: "@tsed/di",
|
|
50
|
+
namedImports: ["Injectable", "Inject"]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
moduleSpecifier: "../services/PrismaService",
|
|
54
|
+
namedImports: ["PrismaService"]
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
moduleSpecifier: "../client",
|
|
58
|
+
namedImports: ["Prisma", model.name]
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
moduleSpecifier: "../models",
|
|
62
|
+
namedImports: [model.toString()]
|
|
63
|
+
}
|
|
64
|
+
]);
|
|
65
|
+
const repository = sourceFile.addClass({
|
|
66
|
+
name,
|
|
67
|
+
isExported: true,
|
|
68
|
+
decorators: [
|
|
69
|
+
{
|
|
70
|
+
name: "Injectable",
|
|
71
|
+
arguments: []
|
|
72
|
+
}
|
|
73
|
+
]
|
|
74
|
+
});
|
|
75
|
+
repository.addProperty({
|
|
76
|
+
name: "prisma",
|
|
77
|
+
type: "PrismaService",
|
|
78
|
+
scope: Scope.Protected,
|
|
79
|
+
decorators: [
|
|
80
|
+
{
|
|
81
|
+
name: "Inject",
|
|
82
|
+
arguments: []
|
|
83
|
+
}
|
|
84
|
+
]
|
|
85
|
+
});
|
|
86
|
+
repository
|
|
87
|
+
.addGetAccessor({
|
|
88
|
+
name: "collection"
|
|
89
|
+
})
|
|
90
|
+
.setBodyText(`return this.prisma.${camelCase(model.name)}`);
|
|
91
|
+
repository
|
|
92
|
+
.addGetAccessor({
|
|
93
|
+
name: "groupBy"
|
|
94
|
+
})
|
|
95
|
+
.setBodyText(`return this.collection.groupBy.bind(this.collection)`);
|
|
96
|
+
repository
|
|
97
|
+
.addMethod({
|
|
98
|
+
name: "deserialize",
|
|
99
|
+
scope: Scope.Protected,
|
|
100
|
+
returnType: "T",
|
|
101
|
+
parameters: [
|
|
102
|
+
{
|
|
103
|
+
name: "obj",
|
|
104
|
+
type: `null | ${model.name} | ${model.name}[]`
|
|
105
|
+
}
|
|
106
|
+
],
|
|
107
|
+
typeParameters: ["T"]
|
|
108
|
+
})
|
|
109
|
+
.setBodyText(`return deserialize<T>(obj, {type: ${modelName}, collectionType: isArray(obj) ? Array : undefined})`);
|
|
110
|
+
addDelegatedMethod({
|
|
111
|
+
repository,
|
|
112
|
+
name: "findUnique",
|
|
113
|
+
model: model.name,
|
|
114
|
+
returnType: `${modelName} | null`
|
|
115
|
+
});
|
|
116
|
+
addDelegatedMethod({
|
|
117
|
+
repository,
|
|
118
|
+
name: "findFirst",
|
|
119
|
+
model: model.name,
|
|
120
|
+
returnType: `${modelName} | null`
|
|
121
|
+
});
|
|
122
|
+
addDelegatedMethod({
|
|
123
|
+
repository,
|
|
124
|
+
name: "findMany",
|
|
125
|
+
model: model.name,
|
|
126
|
+
returnType: `${modelName}[]`,
|
|
127
|
+
hasQuestionToken: true
|
|
128
|
+
});
|
|
129
|
+
addDelegatedMethod({
|
|
130
|
+
repository,
|
|
131
|
+
name: "create",
|
|
132
|
+
model: model.name,
|
|
133
|
+
returnType: `${modelName}`
|
|
134
|
+
});
|
|
135
|
+
addDelegatedMethod({
|
|
136
|
+
repository,
|
|
137
|
+
name: "update",
|
|
138
|
+
model: model.name,
|
|
139
|
+
returnType: `${modelName}`
|
|
140
|
+
});
|
|
141
|
+
addDelegatedMethod({
|
|
142
|
+
repository,
|
|
143
|
+
name: "upsert",
|
|
144
|
+
model: model.name,
|
|
145
|
+
returnType: `${modelName}`
|
|
146
|
+
});
|
|
147
|
+
addDelegatedMethod({
|
|
148
|
+
repository,
|
|
149
|
+
name: "delete",
|
|
150
|
+
model: model.name,
|
|
151
|
+
returnType: `${modelName}`
|
|
152
|
+
});
|
|
153
|
+
addDelegatedMethod({
|
|
154
|
+
repository,
|
|
155
|
+
name: "deleteMany",
|
|
156
|
+
model: model.name
|
|
157
|
+
});
|
|
158
|
+
addDelegatedMethod({
|
|
159
|
+
repository,
|
|
160
|
+
name: "updateMany",
|
|
161
|
+
model: model.name
|
|
162
|
+
});
|
|
163
|
+
addDelegatedMethod({
|
|
164
|
+
repository,
|
|
165
|
+
name: "aggregate",
|
|
166
|
+
model: pascalCase(model.name)
|
|
167
|
+
});
|
|
168
|
+
return name;
|
|
122
169
|
});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
model: model.name,
|
|
127
|
-
returnType: `${modelName}`
|
|
128
|
-
});
|
|
129
|
-
addDelegatedMethod({
|
|
130
|
-
repository,
|
|
131
|
-
name: "upsert",
|
|
132
|
-
model: model.name,
|
|
133
|
-
returnType: `${modelName}`
|
|
134
|
-
});
|
|
135
|
-
addDelegatedMethod({
|
|
136
|
-
repository,
|
|
137
|
-
name: "delete",
|
|
138
|
-
model: model.name,
|
|
139
|
-
returnType: `${modelName}`
|
|
140
|
-
});
|
|
141
|
-
addDelegatedMethod({
|
|
142
|
-
repository,
|
|
143
|
-
name: "deleteMany",
|
|
144
|
-
model: model.name
|
|
145
|
-
});
|
|
146
|
-
addDelegatedMethod({
|
|
147
|
-
repository,
|
|
148
|
-
name: "updateMany",
|
|
149
|
-
model: model.name
|
|
150
|
-
});
|
|
151
|
-
addDelegatedMethod({
|
|
152
|
-
repository,
|
|
153
|
-
name: "aggregate",
|
|
154
|
-
model: pascalCase(model.name)
|
|
155
|
-
});
|
|
156
|
-
return name;
|
|
157
|
-
});
|
|
158
|
-
generateOutputsBarrelFile(repositoriesIndex, exportedModels);
|
|
159
|
-
}
|
|
170
|
+
generateOutputsBarrelFile(repositoriesIndex, exportedModels);
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=generateRepositories.js.map
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
export function isCircularRef(modelName, fieldType, ctx) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
if (modelName === fieldType) {
|
|
3
|
+
return true;
|
|
4
|
+
}
|
|
5
|
+
const relation1 = ctx.modelsMap.get(modelName);
|
|
6
|
+
const relation2 = ctx.modelsMap.get(fieldType);
|
|
7
|
+
return Boolean(relation1 && relation2 && hasModelInFields(relation1, fieldType, ctx) && hasModelInFields(relation2, modelName, ctx));
|
|
8
8
|
}
|
|
9
9
|
function hasModelInFields(model, relation, ctx, inspected = []) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
10
|
+
return !!model.fields.find((field) => {
|
|
11
|
+
if (field.type === relation) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (!inspected.includes(field.type)) {
|
|
15
|
+
const nextModel = ctx.modelsMap.get(field.type);
|
|
16
|
+
if (nextModel) {
|
|
17
|
+
inspected = [...inspected, field.type];
|
|
18
|
+
return hasModelInFields(nextModel, relation, ctx, inspected);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=isCircularRef.js.map
|
|
@@ -3,41 +3,52 @@ export const attributeNameRegex = /(?:\.)+([A-Za-z])+(?:\()+/;
|
|
|
3
3
|
export const attributeArgsRegex = /(?:\()+([A-Za-z])+\:+(.+)+(?:\))+/;
|
|
4
4
|
export const argsRegex = /(?:\()+((.+))+(?:\))+/;
|
|
5
5
|
export function parseDocumentationAttributes(documentation) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
return documentation.split("\n").map(current => {
|
|
10
|
-
const attribute = current.match(attributeRegex)?.[0];
|
|
11
|
-
if (!attribute) {
|
|
12
|
-
return;
|
|
6
|
+
if (!documentation) {
|
|
7
|
+
return [];
|
|
13
8
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
9
|
+
return documentation
|
|
10
|
+
.split("\n")
|
|
11
|
+
.map((current) => {
|
|
12
|
+
const attribute = current.match(attributeRegex)?.[0];
|
|
13
|
+
if (!attribute) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const attributeName = current.match(attributeNameRegex)?.[0]?.slice(1, -1);
|
|
17
|
+
const rawAttributeArgs = attribute.match(attributeArgsRegex)?.[0]?.slice(1, -1);
|
|
18
|
+
const args = [];
|
|
19
|
+
if (rawAttributeArgs) {
|
|
20
|
+
const splitRawArgsArray = rawAttributeArgs.split(",").map((it) => it.trim());
|
|
21
|
+
const parsedAttributeArgs = splitRawArgsArray &&
|
|
22
|
+
Object.fromEntries(splitRawArgsArray.map((it) => {
|
|
23
|
+
const [key, value] = it.split(": ");
|
|
24
|
+
return [key, JSON.parse(value)];
|
|
25
|
+
}));
|
|
26
|
+
args.push(parsedAttributeArgs);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const inputs = attribute
|
|
30
|
+
.match(argsRegex)?.[0]
|
|
31
|
+
?.slice(1, -1)
|
|
32
|
+
?.split(",")
|
|
33
|
+
.map((it) => it.trim());
|
|
34
|
+
args.push(...(inputs || []));
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
name: attributeName,
|
|
38
|
+
content: attribute,
|
|
39
|
+
arguments: args
|
|
40
|
+
};
|
|
41
|
+
})
|
|
42
|
+
.map((options) => {
|
|
43
|
+
if (options?.name === "Ignore") {
|
|
44
|
+
const args = options.arguments.join(" && ");
|
|
45
|
+
return {
|
|
46
|
+
...options,
|
|
47
|
+
arguments: args.length > 0 ? [`(value: any, ctx: any) => ${args}`] : []
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return options;
|
|
51
|
+
})
|
|
52
|
+
.filter(Boolean);
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=parseDocumentationAttributes.js.map
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { promises as fs } from "fs";
|
|
3
3
|
export default async function removeDir(dirPath, onlyContent) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
4
|
+
const dirEntries = await fs.readdir(dirPath, { withFileTypes: true });
|
|
5
|
+
await Promise.all(dirEntries.map(async (dirEntry) => {
|
|
6
|
+
const fullPath = path.join(dirPath, dirEntry.name);
|
|
7
|
+
return dirEntry.isDirectory() ? await removeDir(fullPath, false) : await fs.unlink(fullPath);
|
|
8
|
+
}));
|
|
9
|
+
if (!onlyContent) {
|
|
10
|
+
await fs.rmdir(dirPath);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=removeDir.js.map
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export async function saveProject(project) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
2
|
+
for (const file of project.getSourceFiles()) {
|
|
3
|
+
file.formatText({ indentSize: 2 });
|
|
4
|
+
}
|
|
5
|
+
await project.save();
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=saveProject.js.map
|
package/lib/esm/generator.js
CHANGED
package/lib/esm/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from "./generator.js";
|
|
1
|
+
export * from "./generator.js";
|
|
2
|
+
//# sourceMappingURL=index.js.map
|