prisma-generator-express 1.16.7 → 1.18.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/README.md +4 -4
- package/dist/generator.js +8 -129
- package/dist/generator.js.map +1 -1
- package/dist/helpers/generateOperation.js +471 -0
- package/dist/helpers/generateOperation.js.map +1 -0
- package/dist/helpers/generateRouteFile.js +18 -0
- package/dist/helpers/generateRouteFile.js.map +1 -1
- package/package.json +16 -16
- package/src/copy/createOutputValidatorMiddleware.ts +1 -1
- package/src/copy/createValidatorMiddleware.ts +1 -1
- package/src/copy/misc.ts +1 -1
- package/src/copy/parseQueryParams.ts +1 -1
- package/src/copy/routeConfig.ts +5 -3
- package/src/copy/transformZod.ts +15 -16
- package/src/generator.ts +9 -143
- package/src/helpers/generateOperation.ts +515 -0
- package/src/helpers/generateRouteFile.ts +19 -1
- package/dist/helpers/generateAggregate.js +0 -51
- package/dist/helpers/generateAggregate.js.map +0 -1
- package/dist/helpers/generateCount.js +0 -50
- package/dist/helpers/generateCount.js.map +0 -1
- package/dist/helpers/generateCreate.js +0 -49
- package/dist/helpers/generateCreate.js.map +0 -1
- package/dist/helpers/generateCreateMany.js +0 -49
- package/dist/helpers/generateCreateMany.js.map +0 -1
- package/dist/helpers/generateDelete.js +0 -49
- package/dist/helpers/generateDelete.js.map +0 -1
- package/dist/helpers/generateDeleteMany.js +0 -49
- package/dist/helpers/generateDeleteMany.js.map +0 -1
- package/dist/helpers/generateFindFirst.js +0 -56
- package/dist/helpers/generateFindFirst.js.map +0 -1
- package/dist/helpers/generateFindMany.js +0 -56
- package/dist/helpers/generateFindMany.js.map +0 -1
- package/dist/helpers/generateFindUnique.js +0 -56
- package/dist/helpers/generateFindUnique.js.map +0 -1
- package/dist/helpers/generateGroupBy.js +0 -51
- package/dist/helpers/generateGroupBy.js.map +0 -1
- package/dist/helpers/generateUpdate.js +0 -49
- package/dist/helpers/generateUpdate.js.map +0 -1
- package/dist/helpers/generateUpdateMany.js +0 -49
- package/dist/helpers/generateUpdateMany.js.map +0 -1
- package/dist/helpers/generateUpsert.js +0 -49
- package/dist/helpers/generateUpsert.js.map +0 -1
- package/src/helpers/generateAggregate.ts +0 -59
- package/src/helpers/generateCount.ts +0 -58
- package/src/helpers/generateCreate.ts +0 -56
- package/src/helpers/generateCreateMany.ts +0 -55
- package/src/helpers/generateDelete.ts +0 -57
- package/src/helpers/generateDeleteMany.ts +0 -57
- package/src/helpers/generateFindFirst.ts +0 -62
- package/src/helpers/generateFindMany.ts +0 -62
- package/src/helpers/generateFindUnique.ts +0 -62
- package/src/helpers/generateGroupBy.ts +0 -60
- package/src/helpers/generateUpdate.ts +0 -56
- package/src/helpers/generateUpdateMany.ts +0 -56
- package/src/helpers/generateUpsert.ts +0 -57
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { capitalize, toPascalCase } from '../utils/strings'
|
|
3
|
+
|
|
4
|
+
type MiddlewareType = 'queryFull' | 'bodyArgs' | 'partialArgs' | 'emptyParsedQs'
|
|
5
|
+
|
|
6
|
+
export interface OperationConfig {
|
|
7
|
+
operation: string
|
|
8
|
+
prismaMethod: string
|
|
9
|
+
source: 'query' | 'body'
|
|
10
|
+
successStatus: number
|
|
11
|
+
resultVar: 'data' | 'result'
|
|
12
|
+
exportInterface: boolean
|
|
13
|
+
hasPassToNext: boolean
|
|
14
|
+
importsModel: boolean
|
|
15
|
+
argsPartial: boolean
|
|
16
|
+
useCapitalizeForArgs: boolean
|
|
17
|
+
tsIgnoreCall: boolean
|
|
18
|
+
noCastInCall: boolean
|
|
19
|
+
wrapCount: boolean
|
|
20
|
+
useResLocalsValidator: boolean
|
|
21
|
+
importValidatorConfig: boolean
|
|
22
|
+
localsDataType: 'nullable' | 'array' | null
|
|
23
|
+
validatorLocalsType: string
|
|
24
|
+
zodTypeGeneric: string | null
|
|
25
|
+
middlewareType: MiddlewareType
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const OPERATION_CONFIGS: OperationConfig[] = [
|
|
29
|
+
{
|
|
30
|
+
operation: 'FindUnique',
|
|
31
|
+
prismaMethod: 'findUnique',
|
|
32
|
+
source: 'query',
|
|
33
|
+
successStatus: 200,
|
|
34
|
+
resultVar: 'data',
|
|
35
|
+
exportInterface: true,
|
|
36
|
+
hasPassToNext: true,
|
|
37
|
+
importsModel: true,
|
|
38
|
+
argsPartial: false,
|
|
39
|
+
useCapitalizeForArgs: false,
|
|
40
|
+
tsIgnoreCall: false,
|
|
41
|
+
noCastInCall: false,
|
|
42
|
+
wrapCount: false,
|
|
43
|
+
useResLocalsValidator: false,
|
|
44
|
+
importValidatorConfig: false,
|
|
45
|
+
localsDataType: 'nullable',
|
|
46
|
+
validatorLocalsType: 'ZodType',
|
|
47
|
+
zodTypeGeneric: null,
|
|
48
|
+
middlewareType: 'queryFull',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
operation: 'FindFirst',
|
|
52
|
+
prismaMethod: 'findFirst',
|
|
53
|
+
source: 'query',
|
|
54
|
+
successStatus: 200,
|
|
55
|
+
resultVar: 'data',
|
|
56
|
+
exportInterface: true,
|
|
57
|
+
hasPassToNext: true,
|
|
58
|
+
importsModel: true,
|
|
59
|
+
argsPartial: false,
|
|
60
|
+
useCapitalizeForArgs: false,
|
|
61
|
+
tsIgnoreCall: false,
|
|
62
|
+
noCastInCall: false,
|
|
63
|
+
wrapCount: false,
|
|
64
|
+
useResLocalsValidator: false,
|
|
65
|
+
importValidatorConfig: false,
|
|
66
|
+
localsDataType: 'nullable',
|
|
67
|
+
validatorLocalsType: 'ZodType',
|
|
68
|
+
zodTypeGeneric: null,
|
|
69
|
+
middlewareType: 'queryFull',
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
operation: 'FindMany',
|
|
73
|
+
prismaMethod: 'findMany',
|
|
74
|
+
source: 'query',
|
|
75
|
+
successStatus: 200,
|
|
76
|
+
resultVar: 'data',
|
|
77
|
+
exportInterface: true,
|
|
78
|
+
hasPassToNext: true,
|
|
79
|
+
importsModel: true,
|
|
80
|
+
argsPartial: false,
|
|
81
|
+
useCapitalizeForArgs: false,
|
|
82
|
+
tsIgnoreCall: false,
|
|
83
|
+
noCastInCall: false,
|
|
84
|
+
wrapCount: false,
|
|
85
|
+
useResLocalsValidator: false,
|
|
86
|
+
importValidatorConfig: false,
|
|
87
|
+
localsDataType: 'array',
|
|
88
|
+
validatorLocalsType: 'ZodType',
|
|
89
|
+
zodTypeGeneric: null,
|
|
90
|
+
middlewareType: 'queryFull',
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
operation: 'Create',
|
|
94
|
+
prismaMethod: 'create',
|
|
95
|
+
source: 'body',
|
|
96
|
+
successStatus: 201,
|
|
97
|
+
resultVar: 'data',
|
|
98
|
+
exportInterface: false,
|
|
99
|
+
hasPassToNext: false,
|
|
100
|
+
importsModel: false,
|
|
101
|
+
argsPartial: false,
|
|
102
|
+
useCapitalizeForArgs: false,
|
|
103
|
+
tsIgnoreCall: false,
|
|
104
|
+
noCastInCall: false,
|
|
105
|
+
wrapCount: false,
|
|
106
|
+
useResLocalsValidator: false,
|
|
107
|
+
importValidatorConfig: false,
|
|
108
|
+
localsDataType: null,
|
|
109
|
+
validatorLocalsType: 'ZodType',
|
|
110
|
+
zodTypeGeneric: null,
|
|
111
|
+
middlewareType: 'bodyArgs',
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
operation: 'CreateMany',
|
|
115
|
+
prismaMethod: 'createMany',
|
|
116
|
+
source: 'body',
|
|
117
|
+
successStatus: 201,
|
|
118
|
+
resultVar: 'data',
|
|
119
|
+
exportInterface: false,
|
|
120
|
+
hasPassToNext: false,
|
|
121
|
+
importsModel: false,
|
|
122
|
+
argsPartial: false,
|
|
123
|
+
useCapitalizeForArgs: false,
|
|
124
|
+
tsIgnoreCall: false,
|
|
125
|
+
noCastInCall: false,
|
|
126
|
+
wrapCount: false,
|
|
127
|
+
useResLocalsValidator: false,
|
|
128
|
+
importValidatorConfig: false,
|
|
129
|
+
localsDataType: null,
|
|
130
|
+
validatorLocalsType: 'ZodType',
|
|
131
|
+
zodTypeGeneric: null,
|
|
132
|
+
middlewareType: 'bodyArgs',
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
operation: 'Update',
|
|
136
|
+
prismaMethod: 'update',
|
|
137
|
+
source: 'body',
|
|
138
|
+
successStatus: 200,
|
|
139
|
+
resultVar: 'data',
|
|
140
|
+
exportInterface: false,
|
|
141
|
+
hasPassToNext: false,
|
|
142
|
+
importsModel: false,
|
|
143
|
+
argsPartial: false,
|
|
144
|
+
useCapitalizeForArgs: false,
|
|
145
|
+
tsIgnoreCall: false,
|
|
146
|
+
noCastInCall: false,
|
|
147
|
+
wrapCount: false,
|
|
148
|
+
useResLocalsValidator: false,
|
|
149
|
+
importValidatorConfig: false,
|
|
150
|
+
localsDataType: null,
|
|
151
|
+
validatorLocalsType: 'ZodType',
|
|
152
|
+
zodTypeGeneric: null,
|
|
153
|
+
middlewareType: 'bodyArgs',
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
operation: 'UpdateMany',
|
|
157
|
+
prismaMethod: 'updateMany',
|
|
158
|
+
source: 'body',
|
|
159
|
+
successStatus: 200,
|
|
160
|
+
resultVar: 'data',
|
|
161
|
+
exportInterface: false,
|
|
162
|
+
hasPassToNext: false,
|
|
163
|
+
importsModel: false,
|
|
164
|
+
argsPartial: false,
|
|
165
|
+
useCapitalizeForArgs: false,
|
|
166
|
+
tsIgnoreCall: false,
|
|
167
|
+
noCastInCall: false,
|
|
168
|
+
wrapCount: true,
|
|
169
|
+
useResLocalsValidator: false,
|
|
170
|
+
importValidatorConfig: false,
|
|
171
|
+
localsDataType: null,
|
|
172
|
+
validatorLocalsType: 'ZodType',
|
|
173
|
+
zodTypeGeneric: 'UpdateManyResult',
|
|
174
|
+
middlewareType: 'bodyArgs',
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
operation: 'Upsert',
|
|
178
|
+
prismaMethod: 'upsert',
|
|
179
|
+
source: 'body',
|
|
180
|
+
successStatus: 200,
|
|
181
|
+
resultVar: 'data',
|
|
182
|
+
exportInterface: false,
|
|
183
|
+
hasPassToNext: false,
|
|
184
|
+
importsModel: false,
|
|
185
|
+
argsPartial: false,
|
|
186
|
+
useCapitalizeForArgs: false,
|
|
187
|
+
tsIgnoreCall: false,
|
|
188
|
+
noCastInCall: false,
|
|
189
|
+
wrapCount: false,
|
|
190
|
+
useResLocalsValidator: false,
|
|
191
|
+
importValidatorConfig: false,
|
|
192
|
+
localsDataType: null,
|
|
193
|
+
validatorLocalsType: 'ZodType',
|
|
194
|
+
zodTypeGeneric: null,
|
|
195
|
+
middlewareType: 'bodyArgs',
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
operation: 'Delete',
|
|
199
|
+
prismaMethod: 'delete',
|
|
200
|
+
source: 'body',
|
|
201
|
+
successStatus: 200,
|
|
202
|
+
resultVar: 'data',
|
|
203
|
+
exportInterface: false,
|
|
204
|
+
hasPassToNext: false,
|
|
205
|
+
importsModel: false,
|
|
206
|
+
argsPartial: false,
|
|
207
|
+
useCapitalizeForArgs: false,
|
|
208
|
+
tsIgnoreCall: false,
|
|
209
|
+
noCastInCall: false,
|
|
210
|
+
wrapCount: false,
|
|
211
|
+
useResLocalsValidator: false,
|
|
212
|
+
importValidatorConfig: false,
|
|
213
|
+
localsDataType: null,
|
|
214
|
+
validatorLocalsType: 'ZodType',
|
|
215
|
+
zodTypeGeneric: null,
|
|
216
|
+
middlewareType: 'bodyArgs',
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
operation: 'DeleteMany',
|
|
220
|
+
prismaMethod: 'deleteMany',
|
|
221
|
+
source: 'body',
|
|
222
|
+
successStatus: 200,
|
|
223
|
+
resultVar: 'result',
|
|
224
|
+
exportInterface: false,
|
|
225
|
+
hasPassToNext: false,
|
|
226
|
+
importsModel: false,
|
|
227
|
+
argsPartial: false,
|
|
228
|
+
useCapitalizeForArgs: false,
|
|
229
|
+
tsIgnoreCall: false,
|
|
230
|
+
noCastInCall: false,
|
|
231
|
+
wrapCount: false,
|
|
232
|
+
useResLocalsValidator: false,
|
|
233
|
+
importValidatorConfig: false,
|
|
234
|
+
localsDataType: null,
|
|
235
|
+
validatorLocalsType: 'ZodType',
|
|
236
|
+
zodTypeGeneric: null,
|
|
237
|
+
middlewareType: 'bodyArgs',
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
operation: 'Aggregate',
|
|
241
|
+
prismaMethod: 'aggregate',
|
|
242
|
+
source: 'query',
|
|
243
|
+
successStatus: 200,
|
|
244
|
+
resultVar: 'result',
|
|
245
|
+
exportInterface: false,
|
|
246
|
+
hasPassToNext: false,
|
|
247
|
+
importsModel: false,
|
|
248
|
+
argsPartial: true,
|
|
249
|
+
useCapitalizeForArgs: true,
|
|
250
|
+
tsIgnoreCall: false,
|
|
251
|
+
noCastInCall: false,
|
|
252
|
+
wrapCount: false,
|
|
253
|
+
useResLocalsValidator: true,
|
|
254
|
+
importValidatorConfig: true,
|
|
255
|
+
localsDataType: null,
|
|
256
|
+
validatorLocalsType: 'ValidatorConfig',
|
|
257
|
+
zodTypeGeneric: null,
|
|
258
|
+
middlewareType: 'partialArgs',
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
operation: 'Count',
|
|
262
|
+
prismaMethod: 'count',
|
|
263
|
+
source: 'query',
|
|
264
|
+
successStatus: 200,
|
|
265
|
+
resultVar: 'result',
|
|
266
|
+
exportInterface: false,
|
|
267
|
+
hasPassToNext: false,
|
|
268
|
+
importsModel: false,
|
|
269
|
+
argsPartial: true,
|
|
270
|
+
useCapitalizeForArgs: false,
|
|
271
|
+
tsIgnoreCall: false,
|
|
272
|
+
noCastInCall: false,
|
|
273
|
+
wrapCount: false,
|
|
274
|
+
useResLocalsValidator: false,
|
|
275
|
+
importValidatorConfig: false,
|
|
276
|
+
localsDataType: null,
|
|
277
|
+
validatorLocalsType: 'ZodType',
|
|
278
|
+
zodTypeGeneric: null,
|
|
279
|
+
middlewareType: 'emptyParsedQs',
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
operation: 'GroupBy',
|
|
283
|
+
prismaMethod: 'groupBy',
|
|
284
|
+
source: 'query',
|
|
285
|
+
successStatus: 200,
|
|
286
|
+
resultVar: 'result',
|
|
287
|
+
exportInterface: false,
|
|
288
|
+
hasPassToNext: false,
|
|
289
|
+
importsModel: false,
|
|
290
|
+
argsPartial: true,
|
|
291
|
+
useCapitalizeForArgs: false,
|
|
292
|
+
tsIgnoreCall: true,
|
|
293
|
+
noCastInCall: true,
|
|
294
|
+
wrapCount: false,
|
|
295
|
+
useResLocalsValidator: false,
|
|
296
|
+
importValidatorConfig: false,
|
|
297
|
+
localsDataType: null,
|
|
298
|
+
validatorLocalsType: 'ZodType',
|
|
299
|
+
zodTypeGeneric: null,
|
|
300
|
+
middlewareType: 'emptyParsedQs',
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
operation: 'CreateManyAndReturn',
|
|
304
|
+
prismaMethod: 'createManyAndReturn',
|
|
305
|
+
source: 'body',
|
|
306
|
+
successStatus: 201,
|
|
307
|
+
resultVar: 'data',
|
|
308
|
+
exportInterface: false,
|
|
309
|
+
hasPassToNext: false,
|
|
310
|
+
importsModel: false,
|
|
311
|
+
argsPartial: false,
|
|
312
|
+
useCapitalizeForArgs: false,
|
|
313
|
+
tsIgnoreCall: false,
|
|
314
|
+
noCastInCall: false,
|
|
315
|
+
wrapCount: false,
|
|
316
|
+
useResLocalsValidator: false,
|
|
317
|
+
importValidatorConfig: false,
|
|
318
|
+
localsDataType: null,
|
|
319
|
+
validatorLocalsType: 'ZodType',
|
|
320
|
+
zodTypeGeneric: null,
|
|
321
|
+
middlewareType: 'bodyArgs',
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
operation: 'UpdateManyAndReturn',
|
|
325
|
+
prismaMethod: 'updateManyAndReturn',
|
|
326
|
+
source: 'body',
|
|
327
|
+
successStatus: 200,
|
|
328
|
+
resultVar: 'data',
|
|
329
|
+
exportInterface: false,
|
|
330
|
+
hasPassToNext: false,
|
|
331
|
+
importsModel: false,
|
|
332
|
+
argsPartial: false,
|
|
333
|
+
useCapitalizeForArgs: false,
|
|
334
|
+
tsIgnoreCall: false,
|
|
335
|
+
noCastInCall: false,
|
|
336
|
+
wrapCount: false,
|
|
337
|
+
useResLocalsValidator: false,
|
|
338
|
+
importValidatorConfig: false,
|
|
339
|
+
localsDataType: null,
|
|
340
|
+
validatorLocalsType: 'ZodType',
|
|
341
|
+
zodTypeGeneric: null,
|
|
342
|
+
middlewareType: 'bodyArgs',
|
|
343
|
+
},
|
|
344
|
+
]
|
|
345
|
+
|
|
346
|
+
export function generateOperationFunction(
|
|
347
|
+
cfg: OperationConfig,
|
|
348
|
+
model: DMMF.Model,
|
|
349
|
+
prismaImportStatement: string,
|
|
350
|
+
): string {
|
|
351
|
+
const modelName = model.name
|
|
352
|
+
const functionName = `${modelName}${cfg.operation}`
|
|
353
|
+
const interfaceName = `${cfg.operation}Request`
|
|
354
|
+
const middlewareTypeName = `${cfg.operation}Middleware`
|
|
355
|
+
|
|
356
|
+
const argsTypeName = cfg.useCapitalizeForArgs
|
|
357
|
+
? `Prisma.${capitalize(modelName)}${cfg.operation}Args`
|
|
358
|
+
: `Prisma.${modelName}${cfg.operation}Args`
|
|
359
|
+
|
|
360
|
+
const zodType = cfg.zodTypeGeneric ? `ZodType<${cfg.zodTypeGeneric}>` : 'ZodType'
|
|
361
|
+
|
|
362
|
+
const prismaImport = cfg.importsModel
|
|
363
|
+
? prismaImportStatement.replace('{ Prisma }', `{ Prisma, ${modelName} }`)
|
|
364
|
+
: prismaImportStatement
|
|
365
|
+
|
|
366
|
+
const importLines = [
|
|
367
|
+
prismaImport,
|
|
368
|
+
`import { Request, Response, NextFunction } from 'express';`,
|
|
369
|
+
`import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';`,
|
|
370
|
+
]
|
|
371
|
+
|
|
372
|
+
if (cfg.source === 'query') {
|
|
373
|
+
importLines.push(`import { ParsedQs } from 'qs';`)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
importLines.push(`import { ZodType } from 'zod';`)
|
|
377
|
+
|
|
378
|
+
if (cfg.importValidatorConfig) {
|
|
379
|
+
importLines.push(`import { ValidatorConfig } from '../routeConfig';`)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const extraTypes = cfg.wrapCount ? `\ntype UpdateManyResult = { count: number };\n` : ''
|
|
383
|
+
|
|
384
|
+
const queryType = cfg.argsPartial
|
|
385
|
+
? `Partial<${argsTypeName}> & ParsedQs`
|
|
386
|
+
: `${argsTypeName} & ParsedQs`
|
|
387
|
+
|
|
388
|
+
let interfaceBody: string
|
|
389
|
+
if (cfg.source === 'query') {
|
|
390
|
+
const localsEntries: string[] = []
|
|
391
|
+
if (cfg.localsDataType === 'nullable') {
|
|
392
|
+
localsEntries.push(` data?: ${modelName} | null`)
|
|
393
|
+
} else if (cfg.localsDataType === 'array') {
|
|
394
|
+
localsEntries.push(` data?: ${modelName}[]`)
|
|
395
|
+
}
|
|
396
|
+
localsEntries.push(` outputValidator?: ${cfg.validatorLocalsType};`)
|
|
397
|
+
|
|
398
|
+
interfaceBody = [
|
|
399
|
+
` prisma: PrismaClient;`,
|
|
400
|
+
` query: ${queryType};`,
|
|
401
|
+
` outputValidation?: ${zodType};`,
|
|
402
|
+
...(cfg.hasPassToNext ? [` passToNext?: boolean;`] : []),
|
|
403
|
+
` locals?: {`,
|
|
404
|
+
...localsEntries,
|
|
405
|
+
` }`,
|
|
406
|
+
].join('\n')
|
|
407
|
+
} else {
|
|
408
|
+
interfaceBody = [
|
|
409
|
+
` prisma: PrismaClient;`,
|
|
410
|
+
` body: ${argsTypeName};`,
|
|
411
|
+
` outputValidation?: ${zodType};`,
|
|
412
|
+
` locals?: {`,
|
|
413
|
+
` outputValidator?: ${zodType};`,
|
|
414
|
+
` };`,
|
|
415
|
+
].join('\n')
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const exportKw = cfg.exportInterface ? 'export ' : ''
|
|
419
|
+
const interfaceDecl = `${exportKw}interface ${interfaceName} extends Request {\n${interfaceBody}\n}`
|
|
420
|
+
|
|
421
|
+
let middlewareTypeDecl: string
|
|
422
|
+
switch (cfg.middlewareType) {
|
|
423
|
+
case 'queryFull':
|
|
424
|
+
middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, any, ${argsTypeName} & ParsedQs, Record<string, any>>`
|
|
425
|
+
break
|
|
426
|
+
case 'bodyArgs':
|
|
427
|
+
middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>`
|
|
428
|
+
break
|
|
429
|
+
case 'partialArgs':
|
|
430
|
+
middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, Partial<${argsTypeName}>, Record<string, any>>`
|
|
431
|
+
break
|
|
432
|
+
case 'emptyParsedQs':
|
|
433
|
+
middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, {}, ParsedQs>`
|
|
434
|
+
break
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const validatorLine = cfg.useResLocalsValidator
|
|
438
|
+
? `const outputValidator = res.locals.outputValidator?.schema || req.outputValidation;`
|
|
439
|
+
: `const outputValidator = req.locals?.outputValidator || req.outputValidation;`
|
|
440
|
+
|
|
441
|
+
const v = cfg.resultVar
|
|
442
|
+
const callArg = cfg.source === 'body'
|
|
443
|
+
? 'req.body'
|
|
444
|
+
: cfg.noCastInCall
|
|
445
|
+
? 'req.query'
|
|
446
|
+
: `req.query as ${argsTypeName}`
|
|
447
|
+
|
|
448
|
+
const prismaCall = [
|
|
449
|
+
cfg.tsIgnoreCall ? ' // @ts-ignore' : '',
|
|
450
|
+
` const ${v} = await req.prisma.${toPascalCase(modelName)}.${cfg.prismaMethod}(${callArg});`,
|
|
451
|
+
].filter(Boolean).join('\n')
|
|
452
|
+
|
|
453
|
+
const successJson = cfg.wrapCount
|
|
454
|
+
? `{ count: validationResult.data.count }`
|
|
455
|
+
: `validationResult.data`
|
|
456
|
+
|
|
457
|
+
const elseJson = cfg.wrapCount
|
|
458
|
+
? `{ count: ${v}.count }`
|
|
459
|
+
: v
|
|
460
|
+
|
|
461
|
+
let bodyLines: string[]
|
|
462
|
+
|
|
463
|
+
if (cfg.hasPassToNext) {
|
|
464
|
+
bodyLines = [
|
|
465
|
+
` ${validatorLine}`,
|
|
466
|
+
``,
|
|
467
|
+
prismaCall,
|
|
468
|
+
` if (req.passToNext) {`,
|
|
469
|
+
` if (req.locals) req.locals.data = ${v};`,
|
|
470
|
+
` next();`,
|
|
471
|
+
` } else if (outputValidator) {`,
|
|
472
|
+
` const validationResult = outputValidator.safeParse(${v});`,
|
|
473
|
+
` if (validationResult.success) {`,
|
|
474
|
+
` return res.status(${cfg.successStatus}).json(${successJson});`,
|
|
475
|
+
` } else {`,
|
|
476
|
+
` return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });`,
|
|
477
|
+
` }`,
|
|
478
|
+
` } else {`,
|
|
479
|
+
` return res.status(${cfg.successStatus}).json(${elseJson});`,
|
|
480
|
+
` }`,
|
|
481
|
+
]
|
|
482
|
+
} else {
|
|
483
|
+
bodyLines = [
|
|
484
|
+
` ${validatorLine}`,
|
|
485
|
+
``,
|
|
486
|
+
prismaCall,
|
|
487
|
+
``,
|
|
488
|
+
` if (outputValidator) {`,
|
|
489
|
+
` const validationResult = outputValidator.safeParse(${v});`,
|
|
490
|
+
` if (validationResult.success) {`,
|
|
491
|
+
` return res.status(${cfg.successStatus}).json(${successJson});`,
|
|
492
|
+
` } else {`,
|
|
493
|
+
` return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });`,
|
|
494
|
+
` }`,
|
|
495
|
+
` } else {`,
|
|
496
|
+
` return res.status(${cfg.successStatus}).json(${elseJson});`,
|
|
497
|
+
` }`,
|
|
498
|
+
]
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
return `
|
|
502
|
+
${importLines.join('\n')}
|
|
503
|
+
${extraTypes}
|
|
504
|
+
${interfaceDecl}
|
|
505
|
+
|
|
506
|
+
${middlewareTypeDecl}
|
|
507
|
+
|
|
508
|
+
export async function ${functionName}(req: ${interfaceName}, res: Response, next: NextFunction) {
|
|
509
|
+
try {
|
|
510
|
+
${bodyLines.join('\n')}
|
|
511
|
+
} catch(error: unknown) {
|
|
512
|
+
next(error)
|
|
513
|
+
}
|
|
514
|
+
}`
|
|
515
|
+
}
|
|
@@ -17,8 +17,10 @@ import { ${modelName}FindMany } from './${modelName}FindMany';
|
|
|
17
17
|
import { ${modelName}FindUnique } from './${modelName}FindUnique';
|
|
18
18
|
import { ${modelName}Create } from './${modelName}Create';
|
|
19
19
|
import { ${modelName}CreateMany } from './${modelName}CreateMany';
|
|
20
|
+
import { ${modelName}CreateManyAndReturn } from './${modelName}CreateManyAndReturn';
|
|
20
21
|
import { ${modelName}Update } from './${modelName}Update';
|
|
21
22
|
import { ${modelName}UpdateMany } from './${modelName}UpdateMany';
|
|
23
|
+
import { ${modelName}UpdateManyAndReturn } from './${modelName}UpdateManyAndReturn';
|
|
22
24
|
import { ${modelName}Upsert } from './${modelName}Upsert';
|
|
23
25
|
import { ${modelName}Delete } from './${modelName}Delete';
|
|
24
26
|
import { ${modelName}DeleteMany } from './${modelName}DeleteMany';
|
|
@@ -125,6 +127,14 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
|
|
|
125
127
|
}
|
|
126
128
|
}
|
|
127
129
|
|
|
130
|
+
if (config.enableAll || config?.createManyAndReturn) {
|
|
131
|
+
const { before = [], after = [], inputValidator, outputValidator } = config.createManyAndReturn || defaultBeforeAfter;
|
|
132
|
+
setupRoute('/many/return', 'post', before, ${modelName}CreateManyAndReturn as RequestHandler, inputValidator, outputValidator);
|
|
133
|
+
if (after.length) {
|
|
134
|
+
router.use(removeTrailingSlash(basePath) + '/many/return', ...after);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
128
138
|
if (config.enableAll || config?.update) {
|
|
129
139
|
const { before = [], after = [], inputValidator, outputValidator } = config.update || defaultBeforeAfter;
|
|
130
140
|
setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, inputValidator, outputValidator);
|
|
@@ -141,6 +151,14 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
|
|
|
141
151
|
}
|
|
142
152
|
}
|
|
143
153
|
|
|
154
|
+
if (config.enableAll || config?.updateManyAndReturn) {
|
|
155
|
+
const { before = [], after = [], inputValidator, outputValidator } = config.updateManyAndReturn || defaultBeforeAfter;
|
|
156
|
+
setupRoute('/many/return', 'put', before, ${modelName}UpdateManyAndReturn as RequestHandler, inputValidator, outputValidator);
|
|
157
|
+
if (after.length) {
|
|
158
|
+
router.use(removeTrailingSlash(basePath) + '/many/return', ...after);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
144
162
|
if (config.enableAll || config?.upsert) {
|
|
145
163
|
const { before = [], after = [], inputValidator, outputValidator } = config.upsert || defaultBeforeAfter;
|
|
146
164
|
setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, inputValidator, outputValidator);
|
|
@@ -192,4 +210,4 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
|
|
|
192
210
|
return router;
|
|
193
211
|
}
|
|
194
212
|
`
|
|
195
|
-
}
|
|
213
|
+
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateAggregateFunction = void 0;
|
|
4
|
-
const strings_1 = require("../utils/strings");
|
|
5
|
-
const generateAggregateFunction = (options) => {
|
|
6
|
-
const { model, prismaImportStatement } = options;
|
|
7
|
-
const modelName = model.name;
|
|
8
|
-
const functionName = `${modelName}Aggregate`;
|
|
9
|
-
const argsTypeName = `Prisma.${(0, strings_1.capitalize)(modelName)}AggregateArgs`;
|
|
10
|
-
return `
|
|
11
|
-
${prismaImportStatement}
|
|
12
|
-
import { Request, Response, NextFunction } from 'express';
|
|
13
|
-
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core'
|
|
14
|
-
import { ParsedQs } from 'qs'
|
|
15
|
-
import { ZodTypeAny } from 'zod';
|
|
16
|
-
import { ValidatorConfig } from '../routeConfig'
|
|
17
|
-
|
|
18
|
-
interface AggregateRequest extends Request {
|
|
19
|
-
prisma: PrismaClient;
|
|
20
|
-
query: Partial<${argsTypeName}> & ParsedQs;
|
|
21
|
-
outputValidation?: ZodTypeAny;
|
|
22
|
-
locals?: {
|
|
23
|
-
outputValidator?: ValidatorConfig;
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type AggregateMiddleware = RequestHandler<ParamsDictionary, any, Partial<${argsTypeName}>, Record<string, any>>;
|
|
28
|
-
|
|
29
|
-
export async function ${functionName}(req: AggregateRequest, res: Response, next: NextFunction) {
|
|
30
|
-
try {
|
|
31
|
-
const outputValidator = res.locals.outputValidator?.schema || req.outputValidation;
|
|
32
|
-
|
|
33
|
-
const result = await req.prisma.${(0, strings_1.toPascalCase)(modelName)}.aggregate(req.query as ${argsTypeName});
|
|
34
|
-
|
|
35
|
-
if (outputValidator) {
|
|
36
|
-
const validationResult = outputValidator.safeParse(result);
|
|
37
|
-
if (validationResult.success) {
|
|
38
|
-
return res.status(200).json(validationResult.data);
|
|
39
|
-
} else {
|
|
40
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
return res.status(200).json(result);
|
|
44
|
-
}
|
|
45
|
-
} catch(error: unknown) {
|
|
46
|
-
next(error)
|
|
47
|
-
}
|
|
48
|
-
}`;
|
|
49
|
-
};
|
|
50
|
-
exports.generateAggregateFunction = generateAggregateFunction;
|
|
51
|
-
//# sourceMappingURL=generateAggregate.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generateAggregate.js","sourceRoot":"","sources":["../../src/helpers/generateAggregate.ts"],"names":[],"mappings":";;;AACA,8CAA2D;AASpD,MAAM,yBAAyB,GAAG,CAAC,OAGzC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,WAAW,CAAA;IAC5C,MAAM,YAAY,GAAG,UAAU,IAAA,oBAAU,EAAC,SAAS,CAAC,eAAe,CAAA;IAEnE,OAAO;EACP,qBAAqB;;;;;;;;;mBASJ,YAAY;;;;;;;kFAOmD,YAAY;;wBAEtE,YAAY;;;;sCAIE,IAAA,sBAAY,EAAC,SAAS,CAAC,2BAA2B,YAAY;;;;;;;;;;;;;;;EAelG,CAAA;AACF,CAAC,CAAA;AAhDY,QAAA,yBAAyB,6BAgDrC"}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateCountFunction = void 0;
|
|
4
|
-
const strings_1 = require("../utils/strings");
|
|
5
|
-
const generateCountFunction = (options) => {
|
|
6
|
-
const { model, prismaImportStatement } = options;
|
|
7
|
-
const modelName = model.name;
|
|
8
|
-
const functionName = `${modelName}Count`;
|
|
9
|
-
const argsTypeName = `Prisma.${modelName}CountArgs`;
|
|
10
|
-
return `
|
|
11
|
-
${prismaImportStatement}
|
|
12
|
-
import { Request, Response, NextFunction } from 'express';
|
|
13
|
-
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
14
|
-
import { ParsedQs } from 'qs';
|
|
15
|
-
import { ZodTypeAny } from 'zod';
|
|
16
|
-
|
|
17
|
-
interface CountRequest extends Request {
|
|
18
|
-
prisma: PrismaClient;
|
|
19
|
-
query: Partial<${argsTypeName}> & ParsedQs;
|
|
20
|
-
outputValidation?: ZodTypeAny;
|
|
21
|
-
locals?: {
|
|
22
|
-
outputValidator?: ZodTypeAny;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type CountMiddleware = RequestHandler<ParamsDictionary, any, {}, ParsedQs>;
|
|
27
|
-
|
|
28
|
-
export async function ${functionName}(req: CountRequest, res: Response, next: NextFunction) {
|
|
29
|
-
try {
|
|
30
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
31
|
-
|
|
32
|
-
const result = await req.prisma.${(0, strings_1.toPascalCase)(modelName)}.count(req.query as ${argsTypeName});
|
|
33
|
-
|
|
34
|
-
if (outputValidator) {
|
|
35
|
-
const validationResult = outputValidator.safeParse(result);
|
|
36
|
-
if (validationResult.success) {
|
|
37
|
-
return res.status(200).json(validationResult.data);
|
|
38
|
-
} else {
|
|
39
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
return res.status(200).json(result);
|
|
43
|
-
}
|
|
44
|
-
} catch(error: unknown) {
|
|
45
|
-
next(error)
|
|
46
|
-
}
|
|
47
|
-
}`;
|
|
48
|
-
};
|
|
49
|
-
exports.generateCountFunction = generateCountFunction;
|
|
50
|
-
//# sourceMappingURL=generateCount.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generateCount.js","sourceRoot":"","sources":["../../src/helpers/generateCount.ts"],"names":[],"mappings":";;;AACA,8CAA+C;AASxC,MAAM,qBAAqB,GAAG,CAAC,OAGrC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,OAAO,CAAA;IACxC,MAAM,YAAY,GAAG,UAAU,SAAS,WAAW,CAAA;IAEnD,OAAO;EACP,qBAAqB;;;;;;;;mBAQJ,YAAY;;;;;;;;;wBASP,YAAY;;;;sCAIE,IAAA,sBAAY,EAAC,SAAS,CAAC,uBAAuB,YAAY;;;;;;;;;;;;;;;EAe9F,CAAA;AACF,CAAC,CAAA;AA/CY,QAAA,qBAAqB,yBA+CjC"}
|