@tsed/prisma 8.0.1 → 8.0.2
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/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
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import {createContextFixture} from "../../__mock__/createContextFixture.js";
|
|
2
|
+
import {createDmmfFieldFixture} from "../../__mock__/createDmmfFieldFixture.js";
|
|
3
|
+
import {PrismaScalars} from "../domain/ScalarTsTypes.js";
|
|
4
|
+
import {transformFieldToDecorators} from "./transformFieldToDecorators.js";
|
|
5
|
+
|
|
6
|
+
describe("transformFieldToDecorators()", () => {
|
|
7
|
+
it("should transform String to decorator", () => {
|
|
8
|
+
const field = createDmmfFieldFixture({
|
|
9
|
+
kind: "scalar",
|
|
10
|
+
type: PrismaScalars.String
|
|
11
|
+
});
|
|
12
|
+
const ctx = createContextFixture();
|
|
13
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
14
|
+
{
|
|
15
|
+
arguments: ["String"],
|
|
16
|
+
kind: 6,
|
|
17
|
+
name: "Property"
|
|
18
|
+
}
|
|
19
|
+
]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should transform String to string (Required)", () => {
|
|
23
|
+
const field = createDmmfFieldFixture({
|
|
24
|
+
kind: "scalar",
|
|
25
|
+
type: PrismaScalars.String,
|
|
26
|
+
isRequired: true
|
|
27
|
+
});
|
|
28
|
+
const ctx = createContextFixture();
|
|
29
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
30
|
+
{
|
|
31
|
+
arguments: ["String"],
|
|
32
|
+
kind: 6,
|
|
33
|
+
name: "Property"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
arguments: [],
|
|
37
|
+
kind: 6,
|
|
38
|
+
name: "Required"
|
|
39
|
+
}
|
|
40
|
+
]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should transform String to string (Required + null)", () => {
|
|
44
|
+
const field = createDmmfFieldFixture({
|
|
45
|
+
kind: "scalar",
|
|
46
|
+
type: PrismaScalars.String,
|
|
47
|
+
isRequired: true,
|
|
48
|
+
isNullable: true
|
|
49
|
+
});
|
|
50
|
+
const ctx = createContextFixture();
|
|
51
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
52
|
+
{
|
|
53
|
+
arguments: ["String"],
|
|
54
|
+
kind: 6,
|
|
55
|
+
name: "Property"
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
arguments: [],
|
|
59
|
+
kind: 6,
|
|
60
|
+
name: "Required"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
arguments: ["null"],
|
|
64
|
+
kind: 6,
|
|
65
|
+
name: "Allow"
|
|
66
|
+
}
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should transform String to string (Required + null + List)", () => {
|
|
71
|
+
const field = createDmmfFieldFixture({
|
|
72
|
+
kind: "scalar",
|
|
73
|
+
type: PrismaScalars.String,
|
|
74
|
+
isRequired: true,
|
|
75
|
+
isNullable: true,
|
|
76
|
+
isList: true
|
|
77
|
+
});
|
|
78
|
+
const ctx = createContextFixture();
|
|
79
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
80
|
+
{
|
|
81
|
+
arguments: ["String"],
|
|
82
|
+
kind: 6,
|
|
83
|
+
name: "CollectionOf"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
arguments: [],
|
|
87
|
+
kind: 6,
|
|
88
|
+
name: "Required"
|
|
89
|
+
}
|
|
90
|
+
]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should transform Int to number", () => {
|
|
94
|
+
const field = createDmmfFieldFixture({
|
|
95
|
+
kind: "scalar",
|
|
96
|
+
type: PrismaScalars.Int
|
|
97
|
+
});
|
|
98
|
+
const ctx = createContextFixture();
|
|
99
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
100
|
+
{
|
|
101
|
+
arguments: ["Number"],
|
|
102
|
+
kind: 6,
|
|
103
|
+
name: "Property"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
arguments: [],
|
|
107
|
+
kind: 6,
|
|
108
|
+
name: "Integer"
|
|
109
|
+
}
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("should transform DateTime to Date", () => {
|
|
114
|
+
const field = createDmmfFieldFixture({
|
|
115
|
+
kind: "scalar",
|
|
116
|
+
type: PrismaScalars.DateTime
|
|
117
|
+
});
|
|
118
|
+
const ctx = createContextFixture();
|
|
119
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
120
|
+
{
|
|
121
|
+
arguments: ["Date"],
|
|
122
|
+
kind: 6,
|
|
123
|
+
name: "Property"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
arguments: ['"date-time"'],
|
|
127
|
+
kind: 6,
|
|
128
|
+
name: "Format"
|
|
129
|
+
}
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("should transform enumTypes to Date", () => {
|
|
134
|
+
const field = createDmmfFieldFixture({
|
|
135
|
+
kind: "enum",
|
|
136
|
+
type: "Role"
|
|
137
|
+
});
|
|
138
|
+
const ctx = createContextFixture();
|
|
139
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
140
|
+
{
|
|
141
|
+
arguments: ["Role"],
|
|
142
|
+
kind: 6,
|
|
143
|
+
name: "Enum"
|
|
144
|
+
}
|
|
145
|
+
]);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("should transform User to User (self-ref)", () => {
|
|
149
|
+
const field = createDmmfFieldFixture({
|
|
150
|
+
kind: "object",
|
|
151
|
+
type: "User"
|
|
152
|
+
});
|
|
153
|
+
// @ts-ignore
|
|
154
|
+
field.model.name = "User";
|
|
155
|
+
const ctx = createContextFixture();
|
|
156
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
157
|
+
{
|
|
158
|
+
arguments: ["() => UserModel"],
|
|
159
|
+
kind: 6,
|
|
160
|
+
name: "Property"
|
|
161
|
+
}
|
|
162
|
+
]);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("should transform User to User", () => {
|
|
166
|
+
const field = createDmmfFieldFixture({
|
|
167
|
+
kind: "object",
|
|
168
|
+
type: "Role"
|
|
169
|
+
});
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
field.model.name = "User";
|
|
172
|
+
const ctx = createContextFixture();
|
|
173
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
174
|
+
{
|
|
175
|
+
arguments: ["() => RoleModel"],
|
|
176
|
+
kind: 6,
|
|
177
|
+
name: "Property"
|
|
178
|
+
}
|
|
179
|
+
]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("should transform User to User (list)", () => {
|
|
183
|
+
const field = createDmmfFieldFixture({
|
|
184
|
+
kind: "object",
|
|
185
|
+
type: "Role",
|
|
186
|
+
isList: true
|
|
187
|
+
});
|
|
188
|
+
// @ts-ignore
|
|
189
|
+
field.model.name = "User";
|
|
190
|
+
const ctx = createContextFixture();
|
|
191
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
192
|
+
{
|
|
193
|
+
arguments: ["() => RoleModel"],
|
|
194
|
+
kind: 6,
|
|
195
|
+
name: "CollectionOf"
|
|
196
|
+
}
|
|
197
|
+
]);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("should transform User to User, Groups", () => {
|
|
201
|
+
const field = createDmmfFieldFixture({
|
|
202
|
+
kind: "object",
|
|
203
|
+
type: "Role",
|
|
204
|
+
documentation: '// @TsED.Groups("expression")'
|
|
205
|
+
});
|
|
206
|
+
// @ts-ignore
|
|
207
|
+
field.model.name = "User";
|
|
208
|
+
const ctx = createContextFixture();
|
|
209
|
+
expect(transformFieldToDecorators(field, ctx)).toEqual([
|
|
210
|
+
{
|
|
211
|
+
arguments: ["() => RoleModel"],
|
|
212
|
+
kind: 6,
|
|
213
|
+
name: "Property"
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
arguments: ['"expression"'],
|
|
217
|
+
kind: 6,
|
|
218
|
+
name: "Groups"
|
|
219
|
+
}
|
|
220
|
+
]);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {DecoratorStructure, StructureKind} from "ts-morph";
|
|
2
|
+
|
|
3
|
+
import {DmmfField} from "../domain/DmmfField.js";
|
|
4
|
+
import {DmmfModel} from "../domain/DmmfModel.js";
|
|
5
|
+
import {ScalarDecorators, ScalarJsClasses} from "../domain/ScalarTsTypes.js";
|
|
6
|
+
import {TransformContext} from "../domain/TransformContext.js";
|
|
7
|
+
import {isCircularRef} from "../utils/isCircularRef.js";
|
|
8
|
+
|
|
9
|
+
function createDecorator(name: string, args: string[]): DecoratorStructure {
|
|
10
|
+
return {
|
|
11
|
+
kind: StructureKind.Decorator,
|
|
12
|
+
name,
|
|
13
|
+
arguments: args
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function transformFieldToDecorators(field: DmmfField, ctx: TransformContext): DecoratorStructure[] {
|
|
18
|
+
const hasCircularRef = isCircularRef(field.model.name, field.type, ctx);
|
|
19
|
+
|
|
20
|
+
const decorators: DecoratorStructure[] = [...(ScalarDecorators[field.type] || [])].map((obj) => {
|
|
21
|
+
field.model.addImportDeclaration("@tsed/schema", obj.name);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
kind: StructureKind.Decorator,
|
|
25
|
+
...obj
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (field.isRequired) {
|
|
30
|
+
field.model.addImportDeclaration("@tsed/schema", "Required");
|
|
31
|
+
decorators.push(createDecorator("Required", []));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (field.location === "enumTypes") {
|
|
35
|
+
field.model.addImportDeclaration("@tsed/schema", "Enum");
|
|
36
|
+
decorators.push(createDecorator("Enum", [field.type]));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (field.isNullable && !field.isList) {
|
|
40
|
+
field.model.addImportDeclaration("@tsed/schema", "Allow");
|
|
41
|
+
decorators.push(createDecorator("Allow", ["null"]));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (field.isList && field.location !== "enumTypes") {
|
|
45
|
+
let classType = ScalarJsClasses[field.type] || DmmfModel.symbolName(field.type);
|
|
46
|
+
|
|
47
|
+
if (hasCircularRef) {
|
|
48
|
+
classType = `() => ${classType}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
field.model.addImportDeclaration("@tsed/schema", "CollectionOf");
|
|
52
|
+
decorators.unshift(createDecorator("CollectionOf", [classType]));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (field.location !== "enumTypes" && !field.isList) {
|
|
56
|
+
let classType = ScalarJsClasses[field.type] || DmmfModel.symbolName(field.type);
|
|
57
|
+
|
|
58
|
+
if (hasCircularRef) {
|
|
59
|
+
classType = `() => ${classType}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
field.model.addImportDeclaration("@tsed/schema", "Property");
|
|
63
|
+
decorators.unshift(createDecorator("Property", [classType]));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
field.getAdditionalDecorators().forEach((item) => {
|
|
67
|
+
field.model.addImportDeclaration("@tsed/schema", item.name);
|
|
68
|
+
decorators.push(createDecorator(item.name, item.arguments));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return decorators;
|
|
72
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {PropertyDeclarationStructure, StructureKind} from "ts-morph";
|
|
2
|
+
|
|
3
|
+
import {DmmfField} from "../domain/DmmfField.js";
|
|
4
|
+
import {TransformContext} from "../domain/TransformContext.js";
|
|
5
|
+
import {transformFieldToDecorators} from "./transformFieldToDecorators.js";
|
|
6
|
+
import {transformScalarToType} from "./transformScalarToType.js";
|
|
7
|
+
|
|
8
|
+
export function transformFieldToProperty(field: DmmfField, ctx: TransformContext): PropertyDeclarationStructure {
|
|
9
|
+
return {
|
|
10
|
+
kind: StructureKind.Property,
|
|
11
|
+
name: field.name,
|
|
12
|
+
trailingTrivia: "\n",
|
|
13
|
+
type: transformScalarToType(field, ctx),
|
|
14
|
+
decorators: transformFieldToDecorators(field, ctx)
|
|
15
|
+
};
|
|
16
|
+
}
|