@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
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import {createContextFixture} from "../../__mock__/createContextFixture.js";
|
|
2
|
+
import {createDmmfModelFixture} from "../../__mock__/createDmmfModelFixture.js";
|
|
3
|
+
import {DmmfModel} from "../domain/DmmfModel.js";
|
|
4
|
+
import {transformModelToClass} from "./transformModelToClass.js";
|
|
5
|
+
|
|
6
|
+
describe("transformModelToClass()", () => {
|
|
7
|
+
it("should transform Prisma model to a TS Model (InputType)", () => {
|
|
8
|
+
const model = new DmmfModel({
|
|
9
|
+
isInputType: true,
|
|
10
|
+
model: {
|
|
11
|
+
name: "User",
|
|
12
|
+
fields: [
|
|
13
|
+
{
|
|
14
|
+
name: "id",
|
|
15
|
+
isRequired: false,
|
|
16
|
+
isNullable: false,
|
|
17
|
+
type: "String",
|
|
18
|
+
isList: false,
|
|
19
|
+
location: ""
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
},
|
|
23
|
+
modelType: {
|
|
24
|
+
name: "user",
|
|
25
|
+
fields: [
|
|
26
|
+
{
|
|
27
|
+
name: "id",
|
|
28
|
+
isNullable: false,
|
|
29
|
+
type: "String",
|
|
30
|
+
isList: false,
|
|
31
|
+
location: ""
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const ctx = createContextFixture();
|
|
38
|
+
|
|
39
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
40
|
+
implements: ["User"],
|
|
41
|
+
isExported: true,
|
|
42
|
+
kind: 1,
|
|
43
|
+
leadingTrivia: "\n",
|
|
44
|
+
name: "UserModel",
|
|
45
|
+
properties: [
|
|
46
|
+
{
|
|
47
|
+
decorators: [
|
|
48
|
+
{
|
|
49
|
+
arguments: ["String"],
|
|
50
|
+
kind: 6,
|
|
51
|
+
name: "Property"
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
kind: 30,
|
|
55
|
+
name: "id",
|
|
56
|
+
trailingTrivia: "\n",
|
|
57
|
+
type: "string | undefined"
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
trailingTrivia: "\n"
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
it("should transform Prisma model to a TS Model (InputType + required)", () => {
|
|
64
|
+
const model = new DmmfModel({
|
|
65
|
+
isInputType: true,
|
|
66
|
+
model: {
|
|
67
|
+
name: "User",
|
|
68
|
+
fields: [
|
|
69
|
+
{
|
|
70
|
+
name: "id",
|
|
71
|
+
isRequired: true,
|
|
72
|
+
isNullable: false,
|
|
73
|
+
type: "String",
|
|
74
|
+
isList: false,
|
|
75
|
+
location: ""
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
},
|
|
79
|
+
modelType: {
|
|
80
|
+
name: "user",
|
|
81
|
+
fields: [
|
|
82
|
+
{
|
|
83
|
+
name: "id",
|
|
84
|
+
isNullable: false,
|
|
85
|
+
type: "String",
|
|
86
|
+
isList: false,
|
|
87
|
+
location: ""
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const ctx = createContextFixture();
|
|
94
|
+
|
|
95
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
96
|
+
implements: ["User"],
|
|
97
|
+
isExported: true,
|
|
98
|
+
kind: 1,
|
|
99
|
+
leadingTrivia: "\n",
|
|
100
|
+
name: "UserModel",
|
|
101
|
+
properties: [
|
|
102
|
+
{
|
|
103
|
+
decorators: [
|
|
104
|
+
{
|
|
105
|
+
arguments: ["String"],
|
|
106
|
+
kind: 6,
|
|
107
|
+
name: "Property"
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
arguments: [],
|
|
111
|
+
kind: 6,
|
|
112
|
+
name: "Required"
|
|
113
|
+
}
|
|
114
|
+
],
|
|
115
|
+
kind: 30,
|
|
116
|
+
name: "id",
|
|
117
|
+
trailingTrivia: "\n",
|
|
118
|
+
type: "string"
|
|
119
|
+
}
|
|
120
|
+
],
|
|
121
|
+
trailingTrivia: "\n"
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
it("should transform Prisma model to a TS Model (OutputType)", () => {
|
|
125
|
+
const model = new DmmfModel({
|
|
126
|
+
model: {
|
|
127
|
+
name: "User",
|
|
128
|
+
fields: [
|
|
129
|
+
{
|
|
130
|
+
name: "id",
|
|
131
|
+
isRequired: false,
|
|
132
|
+
type: "String",
|
|
133
|
+
isList: false,
|
|
134
|
+
location: ""
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
},
|
|
138
|
+
modelType: {
|
|
139
|
+
name: "user",
|
|
140
|
+
fields: [
|
|
141
|
+
{
|
|
142
|
+
name: "id",
|
|
143
|
+
isNullable: true,
|
|
144
|
+
type: "String",
|
|
145
|
+
isList: false,
|
|
146
|
+
location: ""
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
},
|
|
150
|
+
isInputType: false
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const ctx = createContextFixture();
|
|
154
|
+
|
|
155
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
156
|
+
implements: ["User"],
|
|
157
|
+
isExported: true,
|
|
158
|
+
kind: 1,
|
|
159
|
+
leadingTrivia: "\n",
|
|
160
|
+
name: "UserModel",
|
|
161
|
+
properties: [
|
|
162
|
+
{
|
|
163
|
+
decorators: [
|
|
164
|
+
{
|
|
165
|
+
arguments: ["String"],
|
|
166
|
+
kind: 6,
|
|
167
|
+
name: "Property"
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
arguments: ["null"],
|
|
171
|
+
kind: 6,
|
|
172
|
+
name: "Allow"
|
|
173
|
+
}
|
|
174
|
+
],
|
|
175
|
+
kind: 30,
|
|
176
|
+
name: "id",
|
|
177
|
+
trailingTrivia: "\n",
|
|
178
|
+
type: "string | null"
|
|
179
|
+
}
|
|
180
|
+
],
|
|
181
|
+
trailingTrivia: "\n"
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
it("should transform Prisma model to a TS Model (OutputType + required)", () => {
|
|
185
|
+
const model = new DmmfModel({
|
|
186
|
+
model: {
|
|
187
|
+
name: "User",
|
|
188
|
+
fields: [
|
|
189
|
+
{
|
|
190
|
+
name: "id",
|
|
191
|
+
isRequired: true,
|
|
192
|
+
isNullable: false,
|
|
193
|
+
type: "String",
|
|
194
|
+
isList: false,
|
|
195
|
+
location: ""
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
},
|
|
199
|
+
modelType: {
|
|
200
|
+
name: "user",
|
|
201
|
+
fields: [
|
|
202
|
+
{
|
|
203
|
+
name: "id",
|
|
204
|
+
isNullable: false,
|
|
205
|
+
type: "String",
|
|
206
|
+
isList: false,
|
|
207
|
+
location: ""
|
|
208
|
+
}
|
|
209
|
+
]
|
|
210
|
+
},
|
|
211
|
+
isInputType: false
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const ctx = createContextFixture();
|
|
215
|
+
|
|
216
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
217
|
+
implements: ["User"],
|
|
218
|
+
isExported: true,
|
|
219
|
+
kind: 1,
|
|
220
|
+
leadingTrivia: "\n",
|
|
221
|
+
name: "UserModel",
|
|
222
|
+
properties: [
|
|
223
|
+
{
|
|
224
|
+
decorators: [
|
|
225
|
+
{
|
|
226
|
+
arguments: ["String"],
|
|
227
|
+
kind: 6,
|
|
228
|
+
name: "Property"
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
arguments: [],
|
|
232
|
+
kind: 6,
|
|
233
|
+
name: "Required"
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
kind: 30,
|
|
237
|
+
name: "id",
|
|
238
|
+
trailingTrivia: "\n",
|
|
239
|
+
type: "string"
|
|
240
|
+
}
|
|
241
|
+
],
|
|
242
|
+
trailingTrivia: "\n"
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
it("should transform Prisma model to a TS Model (List)", () => {
|
|
246
|
+
const model = new DmmfModel({
|
|
247
|
+
isInputType: true,
|
|
248
|
+
model: {
|
|
249
|
+
name: "User",
|
|
250
|
+
fields: [
|
|
251
|
+
{
|
|
252
|
+
name: "ids",
|
|
253
|
+
isRequired: false,
|
|
254
|
+
type: "String",
|
|
255
|
+
isList: true,
|
|
256
|
+
location: ""
|
|
257
|
+
}
|
|
258
|
+
]
|
|
259
|
+
},
|
|
260
|
+
modelType: {
|
|
261
|
+
name: "user",
|
|
262
|
+
fields: [
|
|
263
|
+
{
|
|
264
|
+
name: "ids",
|
|
265
|
+
isNullable: false,
|
|
266
|
+
type: "String",
|
|
267
|
+
location: ""
|
|
268
|
+
}
|
|
269
|
+
]
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const ctx = createContextFixture();
|
|
274
|
+
|
|
275
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
276
|
+
implements: ["User"],
|
|
277
|
+
isExported: true,
|
|
278
|
+
kind: 1,
|
|
279
|
+
leadingTrivia: "\n",
|
|
280
|
+
name: "UserModel",
|
|
281
|
+
properties: [
|
|
282
|
+
{
|
|
283
|
+
decorators: [
|
|
284
|
+
{
|
|
285
|
+
arguments: ["String"],
|
|
286
|
+
kind: 6,
|
|
287
|
+
name: "CollectionOf"
|
|
288
|
+
}
|
|
289
|
+
],
|
|
290
|
+
kind: 30,
|
|
291
|
+
name: "ids",
|
|
292
|
+
trailingTrivia: "\n",
|
|
293
|
+
type: "string[] | undefined"
|
|
294
|
+
}
|
|
295
|
+
],
|
|
296
|
+
trailingTrivia: "\n"
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
it("should transform Prisma model to a TS Model (List + required)", () => {
|
|
300
|
+
const model = new DmmfModel({
|
|
301
|
+
isInputType: true,
|
|
302
|
+
model: {
|
|
303
|
+
name: "User",
|
|
304
|
+
fields: [
|
|
305
|
+
{
|
|
306
|
+
name: "ids",
|
|
307
|
+
isRequired: true,
|
|
308
|
+
type: "String",
|
|
309
|
+
isList: true,
|
|
310
|
+
location: ""
|
|
311
|
+
}
|
|
312
|
+
]
|
|
313
|
+
},
|
|
314
|
+
modelType: {
|
|
315
|
+
name: "user",
|
|
316
|
+
fields: [
|
|
317
|
+
{
|
|
318
|
+
name: "ids",
|
|
319
|
+
isNullable: false,
|
|
320
|
+
type: "String",
|
|
321
|
+
location: ""
|
|
322
|
+
}
|
|
323
|
+
]
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const ctx = createContextFixture();
|
|
328
|
+
|
|
329
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
330
|
+
implements: ["User"],
|
|
331
|
+
isExported: true,
|
|
332
|
+
kind: 1,
|
|
333
|
+
leadingTrivia: "\n",
|
|
334
|
+
name: "UserModel",
|
|
335
|
+
properties: [
|
|
336
|
+
{
|
|
337
|
+
decorators: [
|
|
338
|
+
{
|
|
339
|
+
arguments: ["String"],
|
|
340
|
+
kind: 6,
|
|
341
|
+
name: "CollectionOf"
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
arguments: [],
|
|
345
|
+
kind: 6,
|
|
346
|
+
name: "Required"
|
|
347
|
+
}
|
|
348
|
+
],
|
|
349
|
+
kind: 30,
|
|
350
|
+
name: "ids",
|
|
351
|
+
trailingTrivia: "\n",
|
|
352
|
+
type: "string[]"
|
|
353
|
+
}
|
|
354
|
+
],
|
|
355
|
+
trailingTrivia: "\n"
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
it("should transform Prisma model to a TS Model (Enum)", () => {
|
|
359
|
+
const model = new DmmfModel({
|
|
360
|
+
isInputType: true,
|
|
361
|
+
model: {
|
|
362
|
+
name: "User",
|
|
363
|
+
fields: [
|
|
364
|
+
{
|
|
365
|
+
name: "role",
|
|
366
|
+
isRequired: true,
|
|
367
|
+
type: "Role",
|
|
368
|
+
isList: false,
|
|
369
|
+
kind: "enum"
|
|
370
|
+
}
|
|
371
|
+
]
|
|
372
|
+
},
|
|
373
|
+
modelType: {
|
|
374
|
+
name: "user",
|
|
375
|
+
fields: [
|
|
376
|
+
{
|
|
377
|
+
name: "role",
|
|
378
|
+
isNullable: false,
|
|
379
|
+
type: "Role"
|
|
380
|
+
}
|
|
381
|
+
]
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
const ctx = createContextFixture();
|
|
386
|
+
|
|
387
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
388
|
+
implements: ["User"],
|
|
389
|
+
isExported: true,
|
|
390
|
+
kind: 1,
|
|
391
|
+
leadingTrivia: "\n",
|
|
392
|
+
name: "UserModel",
|
|
393
|
+
properties: [
|
|
394
|
+
{
|
|
395
|
+
decorators: [
|
|
396
|
+
{
|
|
397
|
+
arguments: [],
|
|
398
|
+
kind: 6,
|
|
399
|
+
name: "Required"
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
arguments: ["Role"],
|
|
403
|
+
kind: 6,
|
|
404
|
+
name: "Enum"
|
|
405
|
+
}
|
|
406
|
+
],
|
|
407
|
+
kind: 30,
|
|
408
|
+
name: "role",
|
|
409
|
+
trailingTrivia: "\n",
|
|
410
|
+
type: "Relation<Role>"
|
|
411
|
+
}
|
|
412
|
+
],
|
|
413
|
+
trailingTrivia: "\n"
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
it("should transform Prisma model to a TS Model (List + Enum)", () => {
|
|
417
|
+
const model = new DmmfModel({
|
|
418
|
+
isInputType: true,
|
|
419
|
+
model: {
|
|
420
|
+
name: "User",
|
|
421
|
+
fields: [
|
|
422
|
+
{
|
|
423
|
+
name: "role",
|
|
424
|
+
isRequired: true,
|
|
425
|
+
type: "Role",
|
|
426
|
+
isList: true,
|
|
427
|
+
kind: "enum"
|
|
428
|
+
}
|
|
429
|
+
]
|
|
430
|
+
},
|
|
431
|
+
modelType: {
|
|
432
|
+
name: "user",
|
|
433
|
+
fields: [
|
|
434
|
+
{
|
|
435
|
+
name: "role",
|
|
436
|
+
isNullable: false,
|
|
437
|
+
type: "Role"
|
|
438
|
+
}
|
|
439
|
+
]
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const ctx = createContextFixture();
|
|
444
|
+
|
|
445
|
+
expect(transformModelToClass(model, ctx)).toEqual({
|
|
446
|
+
implements: ["User"],
|
|
447
|
+
isExported: true,
|
|
448
|
+
kind: 1,
|
|
449
|
+
leadingTrivia: "\n",
|
|
450
|
+
name: "UserModel",
|
|
451
|
+
properties: [
|
|
452
|
+
{
|
|
453
|
+
decorators: [
|
|
454
|
+
{
|
|
455
|
+
arguments: [],
|
|
456
|
+
kind: 6,
|
|
457
|
+
name: "Required"
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
arguments: ["Role"],
|
|
461
|
+
kind: 6,
|
|
462
|
+
name: "Enum"
|
|
463
|
+
}
|
|
464
|
+
],
|
|
465
|
+
kind: 30,
|
|
466
|
+
name: "role",
|
|
467
|
+
trailingTrivia: "\n",
|
|
468
|
+
type: "Role[]"
|
|
469
|
+
}
|
|
470
|
+
],
|
|
471
|
+
trailingTrivia: "\n"
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
it("should transform Prisma model to a TS Model and exclude fields with immediate relation", () => {
|
|
475
|
+
const model = createDmmfModelFixture();
|
|
476
|
+
const ctx = createContextFixture();
|
|
477
|
+
const output: any = transformModelToClass(model, ctx);
|
|
478
|
+
|
|
479
|
+
expect(output.properties.map((p: any) => p.name)).toEqual([
|
|
480
|
+
"id",
|
|
481
|
+
"createdAt",
|
|
482
|
+
"email",
|
|
483
|
+
"weight",
|
|
484
|
+
"is18",
|
|
485
|
+
"name",
|
|
486
|
+
"successorId",
|
|
487
|
+
"successor",
|
|
488
|
+
"predecessor",
|
|
489
|
+
"role",
|
|
490
|
+
"posts",
|
|
491
|
+
"keywords",
|
|
492
|
+
"biography"
|
|
493
|
+
]);
|
|
494
|
+
});
|
|
495
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {StatementStructures, StructureKind} from "ts-morph";
|
|
2
|
+
|
|
3
|
+
import {DmmfModel} from "../domain/DmmfModel.js";
|
|
4
|
+
import {TransformContext} from "../domain/TransformContext.js";
|
|
5
|
+
import {transformFieldToProperty} from "./transformFieldToProperty.js";
|
|
6
|
+
|
|
7
|
+
export function transformModelToClass(model: DmmfModel, ctx: TransformContext): StatementStructures {
|
|
8
|
+
model.addImportDeclaration("../client/index", model.name);
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
kind: StructureKind.Class,
|
|
12
|
+
name: model.toString(),
|
|
13
|
+
trailingTrivia: "\n",
|
|
14
|
+
leadingTrivia: "\n",
|
|
15
|
+
isExported: true,
|
|
16
|
+
implements: [model.name],
|
|
17
|
+
properties: model.fields.map((field) => transformFieldToProperty(field, ctx))
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {createContextFixture} from "../../__mock__/createContextFixture.js";
|
|
2
|
+
import {createDmmfFieldFixture} from "../../__mock__/createDmmfFieldFixture.js";
|
|
3
|
+
import {PrismaScalars} from "../domain/ScalarTsTypes.js";
|
|
4
|
+
import {transformScalarToType} from "./transformScalarToType.js";
|
|
5
|
+
|
|
6
|
+
describe("transformScalarToType()", () => {
|
|
7
|
+
it("should transform User to User (not null + circular ref)", () => {
|
|
8
|
+
const ctx = createContextFixture();
|
|
9
|
+
const field = createDmmfFieldFixture({
|
|
10
|
+
kind: "object",
|
|
11
|
+
type: "Role",
|
|
12
|
+
isRequired: true,
|
|
13
|
+
isNullable: false
|
|
14
|
+
});
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
field.model.name = "User";
|
|
17
|
+
expect(transformScalarToType(field, ctx)).toEqual("Relation<RoleModel>");
|
|
18
|
+
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("./RoleModel", "RoleModel");
|
|
19
|
+
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("@tsed/core", "Relation", true);
|
|
20
|
+
});
|
|
21
|
+
it("should transform String to string", () => {
|
|
22
|
+
const ctx = createContextFixture();
|
|
23
|
+
const field = createDmmfFieldFixture({
|
|
24
|
+
kind: "scalar",
|
|
25
|
+
type: PrismaScalars.String
|
|
26
|
+
});
|
|
27
|
+
expect(transformScalarToType(field, ctx)).toEqual("string | null");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should transform String to string (isInputType)", () => {
|
|
31
|
+
const ctx = createContextFixture();
|
|
32
|
+
const field = createDmmfFieldFixture({
|
|
33
|
+
kind: "scalar",
|
|
34
|
+
type: PrismaScalars.String,
|
|
35
|
+
isInputType: true
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
expect(transformScalarToType(field, ctx)).toEqual("string | undefined");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should transform String to string (Required)", () => {
|
|
42
|
+
const ctx = createContextFixture();
|
|
43
|
+
const field = createDmmfFieldFixture({
|
|
44
|
+
kind: "scalar",
|
|
45
|
+
type: PrismaScalars.String,
|
|
46
|
+
isRequired: true
|
|
47
|
+
});
|
|
48
|
+
expect(transformScalarToType(field, ctx)).toEqual("string");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should transform String to string (Required + null)", () => {
|
|
52
|
+
const ctx = createContextFixture();
|
|
53
|
+
const field = createDmmfFieldFixture({
|
|
54
|
+
kind: "scalar",
|
|
55
|
+
type: PrismaScalars.String,
|
|
56
|
+
isRequired: true,
|
|
57
|
+
isNullable: true
|
|
58
|
+
});
|
|
59
|
+
expect(transformScalarToType(field, ctx)).toEqual("string | null");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should transform String to string (Required + null + List)", () => {
|
|
63
|
+
const ctx = createContextFixture();
|
|
64
|
+
const field = createDmmfFieldFixture({
|
|
65
|
+
kind: "scalar",
|
|
66
|
+
type: PrismaScalars.String,
|
|
67
|
+
isRequired: true,
|
|
68
|
+
isNullable: true,
|
|
69
|
+
isList: true
|
|
70
|
+
});
|
|
71
|
+
expect(transformScalarToType(field, ctx)).toEqual("string[]");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should transform Int to number", () => {
|
|
75
|
+
const ctx = createContextFixture();
|
|
76
|
+
const field = createDmmfFieldFixture({
|
|
77
|
+
kind: "scalar",
|
|
78
|
+
type: PrismaScalars.Int
|
|
79
|
+
});
|
|
80
|
+
expect(transformScalarToType(field, ctx)).toEqual("number | null");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should transform DateTime to Date", () => {
|
|
84
|
+
const ctx = createContextFixture();
|
|
85
|
+
const field = createDmmfFieldFixture({
|
|
86
|
+
kind: "scalar",
|
|
87
|
+
type: PrismaScalars.DateTime
|
|
88
|
+
});
|
|
89
|
+
expect(transformScalarToType(field, ctx)).toEqual("Date | null");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("should transform enumTypes to Date", () => {
|
|
93
|
+
const ctx = createContextFixture();
|
|
94
|
+
const field = createDmmfFieldFixture({
|
|
95
|
+
kind: "enum",
|
|
96
|
+
type: "Role"
|
|
97
|
+
});
|
|
98
|
+
expect(transformScalarToType(field, ctx)).toEqual("Role | null");
|
|
99
|
+
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("../enums/index", "Role");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("should transform User to User (self-ref)", () => {
|
|
103
|
+
const ctx = createContextFixture();
|
|
104
|
+
const field = createDmmfFieldFixture({
|
|
105
|
+
kind: "object",
|
|
106
|
+
type: "User"
|
|
107
|
+
});
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
field.model.name = "User";
|
|
110
|
+
expect(transformScalarToType(field, ctx)).toEqual("Relation<UserModel> | null");
|
|
111
|
+
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("@tsed/core", "Relation", true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should transform Role to User", () => {
|
|
115
|
+
const ctx = createContextFixture();
|
|
116
|
+
const field = createDmmfFieldFixture({
|
|
117
|
+
kind: "object",
|
|
118
|
+
type: "Role"
|
|
119
|
+
});
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
field.model.name = "User";
|
|
122
|
+
expect(transformScalarToType(field, ctx)).toEqual("Relation<RoleModel> | null");
|
|
123
|
+
expect(field.model.addImportDeclaration).toHaveBeenCalledWith("./RoleModel", "RoleModel");
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {DmmfEnum} from "../domain/DmmfEnum.js";
|
|
2
|
+
import {DmmfField} from "../domain/DmmfField.js";
|
|
3
|
+
import {DmmfModel} from "../domain/DmmfModel.js";
|
|
4
|
+
import {ScalarTsTypes} from "../domain/ScalarTsTypes.js";
|
|
5
|
+
import type {TransformContext} from "../domain/TransformContext.js";
|
|
6
|
+
import {isCircularRef} from "../utils/isCircularRef.js";
|
|
7
|
+
|
|
8
|
+
export function transformScalarToType(field: DmmfField, ctx: TransformContext): string {
|
|
9
|
+
const {isRequired, isNullable, type, isList, location, model} = field;
|
|
10
|
+
let TSType: string = type;
|
|
11
|
+
const hasCircularRef = isCircularRef(field.model.name, field.type, ctx);
|
|
12
|
+
|
|
13
|
+
switch (location) {
|
|
14
|
+
case "scalar":
|
|
15
|
+
TSType = ScalarTsTypes[field.type];
|
|
16
|
+
break;
|
|
17
|
+
case "enumTypes":
|
|
18
|
+
TSType = DmmfEnum.symbolName(type);
|
|
19
|
+
field.model.addImportDeclaration(`../enums/index`, DmmfEnum.symbolName(field.type));
|
|
20
|
+
break;
|
|
21
|
+
case "inputObjectTypes":
|
|
22
|
+
TSType = DmmfModel.symbolName(type);
|
|
23
|
+
|
|
24
|
+
if (field.model.name !== field.type) {
|
|
25
|
+
field.model.addImportDeclaration(`./${DmmfModel.symbolName(field.type)}`, DmmfModel.symbolName(field.type));
|
|
26
|
+
}
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (isList) {
|
|
31
|
+
TSType += "[]";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!isList && hasCircularRef && ["inputObjectTypes", "enumTypes"].includes(location)) {
|
|
35
|
+
hasCircularRef && !isList && field.model.addImportDeclaration("@tsed/core", "Relation", true);
|
|
36
|
+
TSType = `Relation<${TSType}>`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!isRequired) {
|
|
40
|
+
if (model.isInputType) {
|
|
41
|
+
TSType += " | undefined";
|
|
42
|
+
} else {
|
|
43
|
+
TSType += " | null";
|
|
44
|
+
}
|
|
45
|
+
} else if (isNullable && !isList) {
|
|
46
|
+
TSType += " | null";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return TSType;
|
|
50
|
+
}
|