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.
Files changed (56) hide show
  1. package/README.md +4 -4
  2. package/dist/generator.js +8 -129
  3. package/dist/generator.js.map +1 -1
  4. package/dist/helpers/generateOperation.js +471 -0
  5. package/dist/helpers/generateOperation.js.map +1 -0
  6. package/dist/helpers/generateRouteFile.js +18 -0
  7. package/dist/helpers/generateRouteFile.js.map +1 -1
  8. package/package.json +16 -16
  9. package/src/copy/createOutputValidatorMiddleware.ts +1 -1
  10. package/src/copy/createValidatorMiddleware.ts +1 -1
  11. package/src/copy/misc.ts +1 -1
  12. package/src/copy/parseQueryParams.ts +1 -1
  13. package/src/copy/routeConfig.ts +5 -3
  14. package/src/copy/transformZod.ts +15 -16
  15. package/src/generator.ts +9 -143
  16. package/src/helpers/generateOperation.ts +515 -0
  17. package/src/helpers/generateRouteFile.ts +19 -1
  18. package/dist/helpers/generateAggregate.js +0 -51
  19. package/dist/helpers/generateAggregate.js.map +0 -1
  20. package/dist/helpers/generateCount.js +0 -50
  21. package/dist/helpers/generateCount.js.map +0 -1
  22. package/dist/helpers/generateCreate.js +0 -49
  23. package/dist/helpers/generateCreate.js.map +0 -1
  24. package/dist/helpers/generateCreateMany.js +0 -49
  25. package/dist/helpers/generateCreateMany.js.map +0 -1
  26. package/dist/helpers/generateDelete.js +0 -49
  27. package/dist/helpers/generateDelete.js.map +0 -1
  28. package/dist/helpers/generateDeleteMany.js +0 -49
  29. package/dist/helpers/generateDeleteMany.js.map +0 -1
  30. package/dist/helpers/generateFindFirst.js +0 -56
  31. package/dist/helpers/generateFindFirst.js.map +0 -1
  32. package/dist/helpers/generateFindMany.js +0 -56
  33. package/dist/helpers/generateFindMany.js.map +0 -1
  34. package/dist/helpers/generateFindUnique.js +0 -56
  35. package/dist/helpers/generateFindUnique.js.map +0 -1
  36. package/dist/helpers/generateGroupBy.js +0 -51
  37. package/dist/helpers/generateGroupBy.js.map +0 -1
  38. package/dist/helpers/generateUpdate.js +0 -49
  39. package/dist/helpers/generateUpdate.js.map +0 -1
  40. package/dist/helpers/generateUpdateMany.js +0 -49
  41. package/dist/helpers/generateUpdateMany.js.map +0 -1
  42. package/dist/helpers/generateUpsert.js +0 -49
  43. package/dist/helpers/generateUpsert.js.map +0 -1
  44. package/src/helpers/generateAggregate.ts +0 -59
  45. package/src/helpers/generateCount.ts +0 -58
  46. package/src/helpers/generateCreate.ts +0 -56
  47. package/src/helpers/generateCreateMany.ts +0 -55
  48. package/src/helpers/generateDelete.ts +0 -57
  49. package/src/helpers/generateDeleteMany.ts +0 -57
  50. package/src/helpers/generateFindFirst.ts +0 -62
  51. package/src/helpers/generateFindMany.ts +0 -62
  52. package/src/helpers/generateFindUnique.ts +0 -62
  53. package/src/helpers/generateGroupBy.ts +0 -60
  54. package/src/helpers/generateUpdate.ts +0 -56
  55. package/src/helpers/generateUpdateMany.ts +0 -56
  56. package/src/helpers/generateUpsert.ts +0 -57
package/README.md CHANGED
@@ -173,7 +173,7 @@ const afterFindFirst: RequestHandler = (
173
173
  * 6. Custom middlewares: config.{method}.after[] (not available if req.passToNext is falsy)
174
174
  */
175
175
  const userAccounRouterConfig: RouteConfig<RequestHandler> = {
176
- FindFirst: {
176
+ findFirst: {
177
177
  before: [beforeFindFirst],
178
178
  after: [afterFindFirst],
179
179
  input: {
@@ -210,7 +210,7 @@ The following properties can be attached to the `req` object to control the beha
210
210
  | ---------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------- |
211
211
  | `prisma` | PrismaClient | An instance of PrismaClient that allows the middleware to interact with your database. |
212
212
  | `passToNext` | boolean | Optional, if `true` - the result of a Prisma request will be passed to the next middleware as `if (req.locals) req.locals.data` |
213
- | `outputValidation` | ZodTypeAny | (Optional) A Zod schema used to validate the data returned from the Prisma query before sending it to the client. |
213
+ | `outputValidation` | ZodType | (Optional) A Zod schema used to validate the data returned from the Prisma query before sending it to the client. |
214
214
 
215
215
  ## Router Schema
216
216
 
@@ -277,11 +277,11 @@ type QueryParams = string | ParsedQs | string[] | ParsedQs[] | undefined
277
277
 
278
278
  Recursively converts strings "true", "false", "null", and "number" into correct formats.
279
279
 
280
- ### allow<T extends z.ZodTypeAny>( schema: T, allowedPaths: string[] ): ZodEffects<T, any, any>
280
+ ### allow<T extends z.ZodType>( schema: T, allowedPaths: string[] )
281
281
 
282
282
  Accepts schema and `['array.of.allowed.paths']`. Throws an error if provided something that doesn't fit allowed schema.
283
283
 
284
- ### forbid<T extends z.ZodTypeAny>( schema: T, forbiddenPaths: string[] ): ZodEffects<T, any, any>
284
+ ### forbid<T extends z.ZodType>( schema: T, forbiddenPaths: string[] )
285
285
 
286
286
  Same as `allow` but works in opposite way.
287
287
 
package/dist/generator.js CHANGED
@@ -4,21 +4,9 @@ const generator_helper_1 = require("@prisma/generator-helper");
4
4
  const sdk_1 = require("@prisma/sdk");
5
5
  const constants_1 = require("./constants");
6
6
  const writeFileSafely_1 = require("./utils/writeFileSafely");
7
- const generateFindUnique_1 = require("./helpers/generateFindUnique");
8
7
  const generateImportPrismaStatement_1 = require("./helpers/generateImportPrismaStatement");
9
- const generateFindMany_1 = require("./helpers/generateFindMany");
10
- const generateFindFirst_1 = require("./helpers/generateFindFirst");
11
- const generateCreate_1 = require("./helpers/generateCreate");
12
8
  const generateRouteFile_1 = require("./helpers/generateRouteFile");
13
- const generateCreateMany_1 = require("./helpers/generateCreateMany");
14
- const generateUpdate_1 = require("./helpers/generateUpdate");
15
- const generateUpdateMany_1 = require("./helpers/generateUpdateMany");
16
- const generateUpsert_1 = require("./helpers/generateUpsert");
17
- const generateDelete_1 = require("./helpers/generateDelete");
18
- const generateDeleteMany_1 = require("./helpers/generateDeleteMany");
19
- const generateAggregate_1 = require("./helpers/generateAggregate");
20
- const generateCount_1 = require("./helpers/generateCount");
21
- const generateGroupBy_1 = require("./helpers/generateGroupBy");
9
+ const generateOperation_1 = require("./helpers/generateOperation");
22
10
  const copyFiles_1 = require("./utils/copyFiles");
23
11
  const { version } = require('../package.json');
24
12
  (0, generator_helper_1.generatorHandler)({
@@ -38,123 +26,14 @@ const { version } = require('../package.json');
38
26
  sdk_1.logger.info(`Skipping generation for model ${model.name} as it is marked with 'generator off'.`);
39
27
  continue;
40
28
  }
41
- await (0, writeFileSafely_1.writeFileSafely)({
42
- content: (0, generateFindUnique_1.generateFindUniqueFunction)({
29
+ for (const cfg of generateOperation_1.OPERATION_CONFIGS) {
30
+ await (0, writeFileSafely_1.writeFileSafely)({
31
+ content: (0, generateOperation_1.generateOperationFunction)(cfg, model, prismaImportStatement),
32
+ options,
43
33
  model,
44
- prismaImportStatement,
45
- }),
46
- options,
47
- model,
48
- operation: 'FindUnique',
49
- });
50
- await (0, writeFileSafely_1.writeFileSafely)({
51
- content: (0, generateFindFirst_1.generateFindFirstFunction)({
52
- model,
53
- prismaImportStatement,
54
- }),
55
- options,
56
- model,
57
- operation: 'FindFirst',
58
- });
59
- await (0, writeFileSafely_1.writeFileSafely)({
60
- content: (0, generateFindMany_1.generateFindManyFunction)({
61
- model,
62
- prismaImportStatement,
63
- }),
64
- options,
65
- model,
66
- operation: 'FindMany',
67
- });
68
- await (0, writeFileSafely_1.writeFileSafely)({
69
- content: (0, generateCreate_1.generateCreateFunction)({
70
- model,
71
- prismaImportStatement,
72
- }),
73
- options,
74
- model,
75
- operation: 'Create',
76
- });
77
- await (0, writeFileSafely_1.writeFileSafely)({
78
- content: (0, generateCreateMany_1.generateCreateManyFunction)({
79
- model,
80
- prismaImportStatement,
81
- }),
82
- options,
83
- model,
84
- operation: 'CreateMany',
85
- });
86
- await (0, writeFileSafely_1.writeFileSafely)({
87
- content: (0, generateUpdate_1.generateUpdateFunction)({
88
- model,
89
- prismaImportStatement,
90
- }),
91
- options,
92
- model,
93
- operation: 'Update',
94
- });
95
- await (0, writeFileSafely_1.writeFileSafely)({
96
- content: (0, generateUpdateMany_1.generateUpdateManyFunction)({
97
- model,
98
- prismaImportStatement,
99
- }),
100
- options,
101
- model,
102
- operation: 'UpdateMany',
103
- });
104
- await (0, writeFileSafely_1.writeFileSafely)({
105
- content: (0, generateUpsert_1.generateUpsertFunction)({
106
- model,
107
- prismaImportStatement,
108
- }),
109
- options,
110
- model,
111
- operation: 'Upsert',
112
- });
113
- await (0, writeFileSafely_1.writeFileSafely)({
114
- content: (0, generateDelete_1.generateDeleteFunction)({
115
- model,
116
- prismaImportStatement,
117
- }),
118
- options,
119
- model,
120
- operation: 'Delete',
121
- });
122
- await (0, writeFileSafely_1.writeFileSafely)({
123
- content: (0, generateDeleteMany_1.generateDeleteManyFunction)({
124
- model,
125
- prismaImportStatement,
126
- }),
127
- options,
128
- model,
129
- operation: 'DeleteMany',
130
- });
131
- await (0, writeFileSafely_1.writeFileSafely)({
132
- content: (0, generateAggregate_1.generateAggregateFunction)({
133
- model,
134
- prismaImportStatement,
135
- }),
136
- options,
137
- model,
138
- operation: 'Aggregate',
139
- });
140
- await (0, writeFileSafely_1.writeFileSafely)({
141
- content: (0, generateCount_1.generateCountFunction)({
142
- model,
143
- prismaImportStatement,
144
- }),
145
- options,
146
- model,
147
- operation: 'Count',
148
- });
149
- await (0, writeFileSafely_1.writeFileSafely)({
150
- content: (0, generateGroupBy_1.generateGroupByFunction)({
151
- model,
152
- prismaImportStatement,
153
- }),
154
- options,
155
- model,
156
- operation: 'GroupBy',
157
- });
34
+ operation: cfg.operation,
35
+ });
36
+ }
158
37
  await (0, writeFileSafely_1.writeFileSafely)({
159
38
  content: (0, generateRouteFile_1.generateRouterFunction)({ model }),
160
39
  options,
@@ -1 +1 @@
1
- {"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;AAAA,+DAA6E;AAC7E,qCAAoC;AACpC,2CAA4C;AAC5C,6DAAyD;AACzD,qEAAyE;AACzE,2FAAuF;AACvF,iEAAqE;AACrE,mEAAuE;AACvE,6DAAiE;AACjE,mEAAoE;AACpE,qEAAyE;AACzE,6DAAiE;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,GAAG,0BAAc,aAAa,CAAC,CAAA;QAC3C,OAAO;YACL,OAAO;YACP,aAAa,EAAE,cAAc;YAC7B,UAAU,EAAE,0BAAc;SAC3B,CAAA;IACH,CAAC;IACD,UAAU,EAAE,KAAK,EAAE,OAAyB,EAAE,EAAE;QAC9C,MAAM,qBAAqB,GAAG,IAAA,6DAA6B,EAAC,OAAO,CAAC,CAAA;QAEpE,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;YACxD,IACE,KAAK,CAAC,aAAa;gBACnB,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,eAAe,CAAC,EAC7C,CAAC;gBACD,YAAM,CAAC,IAAI,CACT,iCAAiC,KAAK,CAAC,IAAI,wCAAwC,CACpF,CAAA;gBACD,SAAQ;YACV,CAAC;YAED,KAAK,MAAM,GAAG,IAAI,qCAAiB,EAAE,CAAC;gBACpC,MAAM,IAAA,iCAAe,EAAC;oBACpB,OAAO,EAAE,IAAA,6CAAyB,EAAC,GAAG,EAAE,KAAK,EAAE,qBAAqB,CAAC;oBACrE,OAAO;oBACP,KAAK;oBACL,SAAS,EAAE,GAAG,CAAC,SAAS;iBACzB,CAAC,CAAA;YACJ,CAAC;YAED,MAAM,IAAA,iCAAe,EAAC;gBACpB,OAAO,EAAE,IAAA,0CAAsB,EAAC,EAAE,KAAK,EAAE,CAAC;gBAC1C,OAAO;gBACP,KAAK;gBACL,SAAS,EAAE,OAAO;aACnB,CAAC,CAAA;QACJ,CAAC;QAED,MAAM,IAAA,qBAAS,EAAC,OAAO,CAAC,CAAA;IAC1B,CAAC;CACF,CAAC,CAAA"}
@@ -0,0 +1,471 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OPERATION_CONFIGS = void 0;
4
+ exports.generateOperationFunction = generateOperationFunction;
5
+ const strings_1 = require("../utils/strings");
6
+ exports.OPERATION_CONFIGS = [
7
+ {
8
+ operation: 'FindUnique',
9
+ prismaMethod: 'findUnique',
10
+ source: 'query',
11
+ successStatus: 200,
12
+ resultVar: 'data',
13
+ exportInterface: true,
14
+ hasPassToNext: true,
15
+ importsModel: true,
16
+ argsPartial: false,
17
+ useCapitalizeForArgs: false,
18
+ tsIgnoreCall: false,
19
+ noCastInCall: false,
20
+ wrapCount: false,
21
+ useResLocalsValidator: false,
22
+ importValidatorConfig: false,
23
+ localsDataType: 'nullable',
24
+ validatorLocalsType: 'ZodType',
25
+ zodTypeGeneric: null,
26
+ middlewareType: 'queryFull',
27
+ },
28
+ {
29
+ operation: 'FindFirst',
30
+ prismaMethod: 'findFirst',
31
+ source: 'query',
32
+ successStatus: 200,
33
+ resultVar: 'data',
34
+ exportInterface: true,
35
+ hasPassToNext: true,
36
+ importsModel: true,
37
+ argsPartial: false,
38
+ useCapitalizeForArgs: false,
39
+ tsIgnoreCall: false,
40
+ noCastInCall: false,
41
+ wrapCount: false,
42
+ useResLocalsValidator: false,
43
+ importValidatorConfig: false,
44
+ localsDataType: 'nullable',
45
+ validatorLocalsType: 'ZodType',
46
+ zodTypeGeneric: null,
47
+ middlewareType: 'queryFull',
48
+ },
49
+ {
50
+ operation: 'FindMany',
51
+ prismaMethod: 'findMany',
52
+ source: 'query',
53
+ successStatus: 200,
54
+ resultVar: 'data',
55
+ exportInterface: true,
56
+ hasPassToNext: true,
57
+ importsModel: true,
58
+ argsPartial: false,
59
+ useCapitalizeForArgs: false,
60
+ tsIgnoreCall: false,
61
+ noCastInCall: false,
62
+ wrapCount: false,
63
+ useResLocalsValidator: false,
64
+ importValidatorConfig: false,
65
+ localsDataType: 'array',
66
+ validatorLocalsType: 'ZodType',
67
+ zodTypeGeneric: null,
68
+ middlewareType: 'queryFull',
69
+ },
70
+ {
71
+ operation: 'Create',
72
+ prismaMethod: 'create',
73
+ source: 'body',
74
+ successStatus: 201,
75
+ resultVar: 'data',
76
+ exportInterface: false,
77
+ hasPassToNext: false,
78
+ importsModel: false,
79
+ argsPartial: false,
80
+ useCapitalizeForArgs: false,
81
+ tsIgnoreCall: false,
82
+ noCastInCall: false,
83
+ wrapCount: false,
84
+ useResLocalsValidator: false,
85
+ importValidatorConfig: false,
86
+ localsDataType: null,
87
+ validatorLocalsType: 'ZodType',
88
+ zodTypeGeneric: null,
89
+ middlewareType: 'bodyArgs',
90
+ },
91
+ {
92
+ operation: 'CreateMany',
93
+ prismaMethod: 'createMany',
94
+ source: 'body',
95
+ successStatus: 201,
96
+ resultVar: 'data',
97
+ exportInterface: false,
98
+ hasPassToNext: false,
99
+ importsModel: false,
100
+ argsPartial: false,
101
+ useCapitalizeForArgs: false,
102
+ tsIgnoreCall: false,
103
+ noCastInCall: false,
104
+ wrapCount: false,
105
+ useResLocalsValidator: false,
106
+ importValidatorConfig: false,
107
+ localsDataType: null,
108
+ validatorLocalsType: 'ZodType',
109
+ zodTypeGeneric: null,
110
+ middlewareType: 'bodyArgs',
111
+ },
112
+ {
113
+ operation: 'Update',
114
+ prismaMethod: 'update',
115
+ source: 'body',
116
+ successStatus: 200,
117
+ resultVar: 'data',
118
+ exportInterface: false,
119
+ hasPassToNext: false,
120
+ importsModel: false,
121
+ argsPartial: false,
122
+ useCapitalizeForArgs: false,
123
+ tsIgnoreCall: false,
124
+ noCastInCall: false,
125
+ wrapCount: false,
126
+ useResLocalsValidator: false,
127
+ importValidatorConfig: false,
128
+ localsDataType: null,
129
+ validatorLocalsType: 'ZodType',
130
+ zodTypeGeneric: null,
131
+ middlewareType: 'bodyArgs',
132
+ },
133
+ {
134
+ operation: 'UpdateMany',
135
+ prismaMethod: 'updateMany',
136
+ source: 'body',
137
+ successStatus: 200,
138
+ resultVar: 'data',
139
+ exportInterface: false,
140
+ hasPassToNext: false,
141
+ importsModel: false,
142
+ argsPartial: false,
143
+ useCapitalizeForArgs: false,
144
+ tsIgnoreCall: false,
145
+ noCastInCall: false,
146
+ wrapCount: true,
147
+ useResLocalsValidator: false,
148
+ importValidatorConfig: false,
149
+ localsDataType: null,
150
+ validatorLocalsType: 'ZodType',
151
+ zodTypeGeneric: 'UpdateManyResult',
152
+ middlewareType: 'bodyArgs',
153
+ },
154
+ {
155
+ operation: 'Upsert',
156
+ prismaMethod: 'upsert',
157
+ source: 'body',
158
+ successStatus: 200,
159
+ resultVar: 'data',
160
+ exportInterface: false,
161
+ hasPassToNext: false,
162
+ importsModel: false,
163
+ argsPartial: false,
164
+ useCapitalizeForArgs: false,
165
+ tsIgnoreCall: false,
166
+ noCastInCall: false,
167
+ wrapCount: false,
168
+ useResLocalsValidator: false,
169
+ importValidatorConfig: false,
170
+ localsDataType: null,
171
+ validatorLocalsType: 'ZodType',
172
+ zodTypeGeneric: null,
173
+ middlewareType: 'bodyArgs',
174
+ },
175
+ {
176
+ operation: 'Delete',
177
+ prismaMethod: 'delete',
178
+ source: 'body',
179
+ successStatus: 200,
180
+ resultVar: 'data',
181
+ exportInterface: false,
182
+ hasPassToNext: false,
183
+ importsModel: false,
184
+ argsPartial: false,
185
+ useCapitalizeForArgs: false,
186
+ tsIgnoreCall: false,
187
+ noCastInCall: false,
188
+ wrapCount: false,
189
+ useResLocalsValidator: false,
190
+ importValidatorConfig: false,
191
+ localsDataType: null,
192
+ validatorLocalsType: 'ZodType',
193
+ zodTypeGeneric: null,
194
+ middlewareType: 'bodyArgs',
195
+ },
196
+ {
197
+ operation: 'DeleteMany',
198
+ prismaMethod: 'deleteMany',
199
+ source: 'body',
200
+ successStatus: 200,
201
+ resultVar: 'result',
202
+ exportInterface: false,
203
+ hasPassToNext: false,
204
+ importsModel: false,
205
+ argsPartial: false,
206
+ useCapitalizeForArgs: false,
207
+ tsIgnoreCall: false,
208
+ noCastInCall: false,
209
+ wrapCount: false,
210
+ useResLocalsValidator: false,
211
+ importValidatorConfig: false,
212
+ localsDataType: null,
213
+ validatorLocalsType: 'ZodType',
214
+ zodTypeGeneric: null,
215
+ middlewareType: 'bodyArgs',
216
+ },
217
+ {
218
+ operation: 'Aggregate',
219
+ prismaMethod: 'aggregate',
220
+ source: 'query',
221
+ successStatus: 200,
222
+ resultVar: 'result',
223
+ exportInterface: false,
224
+ hasPassToNext: false,
225
+ importsModel: false,
226
+ argsPartial: true,
227
+ useCapitalizeForArgs: true,
228
+ tsIgnoreCall: false,
229
+ noCastInCall: false,
230
+ wrapCount: false,
231
+ useResLocalsValidator: true,
232
+ importValidatorConfig: true,
233
+ localsDataType: null,
234
+ validatorLocalsType: 'ValidatorConfig',
235
+ zodTypeGeneric: null,
236
+ middlewareType: 'partialArgs',
237
+ },
238
+ {
239
+ operation: 'Count',
240
+ prismaMethod: 'count',
241
+ source: 'query',
242
+ successStatus: 200,
243
+ resultVar: 'result',
244
+ exportInterface: false,
245
+ hasPassToNext: false,
246
+ importsModel: false,
247
+ argsPartial: true,
248
+ useCapitalizeForArgs: false,
249
+ tsIgnoreCall: false,
250
+ noCastInCall: false,
251
+ wrapCount: false,
252
+ useResLocalsValidator: false,
253
+ importValidatorConfig: false,
254
+ localsDataType: null,
255
+ validatorLocalsType: 'ZodType',
256
+ zodTypeGeneric: null,
257
+ middlewareType: 'emptyParsedQs',
258
+ },
259
+ {
260
+ operation: 'GroupBy',
261
+ prismaMethod: 'groupBy',
262
+ source: 'query',
263
+ successStatus: 200,
264
+ resultVar: 'result',
265
+ exportInterface: false,
266
+ hasPassToNext: false,
267
+ importsModel: false,
268
+ argsPartial: true,
269
+ useCapitalizeForArgs: false,
270
+ tsIgnoreCall: true,
271
+ noCastInCall: true,
272
+ wrapCount: false,
273
+ useResLocalsValidator: false,
274
+ importValidatorConfig: false,
275
+ localsDataType: null,
276
+ validatorLocalsType: 'ZodType',
277
+ zodTypeGeneric: null,
278
+ middlewareType: 'emptyParsedQs',
279
+ },
280
+ {
281
+ operation: 'CreateManyAndReturn',
282
+ prismaMethod: 'createManyAndReturn',
283
+ source: 'body',
284
+ successStatus: 201,
285
+ resultVar: 'data',
286
+ exportInterface: false,
287
+ hasPassToNext: false,
288
+ importsModel: false,
289
+ argsPartial: false,
290
+ useCapitalizeForArgs: false,
291
+ tsIgnoreCall: false,
292
+ noCastInCall: false,
293
+ wrapCount: false,
294
+ useResLocalsValidator: false,
295
+ importValidatorConfig: false,
296
+ localsDataType: null,
297
+ validatorLocalsType: 'ZodType',
298
+ zodTypeGeneric: null,
299
+ middlewareType: 'bodyArgs',
300
+ },
301
+ {
302
+ operation: 'UpdateManyAndReturn',
303
+ prismaMethod: 'updateManyAndReturn',
304
+ source: 'body',
305
+ successStatus: 200,
306
+ resultVar: 'data',
307
+ exportInterface: false,
308
+ hasPassToNext: false,
309
+ importsModel: false,
310
+ argsPartial: false,
311
+ useCapitalizeForArgs: false,
312
+ tsIgnoreCall: false,
313
+ noCastInCall: false,
314
+ wrapCount: false,
315
+ useResLocalsValidator: false,
316
+ importValidatorConfig: false,
317
+ localsDataType: null,
318
+ validatorLocalsType: 'ZodType',
319
+ zodTypeGeneric: null,
320
+ middlewareType: 'bodyArgs',
321
+ },
322
+ ];
323
+ function generateOperationFunction(cfg, model, prismaImportStatement) {
324
+ const modelName = model.name;
325
+ const functionName = `${modelName}${cfg.operation}`;
326
+ const interfaceName = `${cfg.operation}Request`;
327
+ const middlewareTypeName = `${cfg.operation}Middleware`;
328
+ const argsTypeName = cfg.useCapitalizeForArgs
329
+ ? `Prisma.${(0, strings_1.capitalize)(modelName)}${cfg.operation}Args`
330
+ : `Prisma.${modelName}${cfg.operation}Args`;
331
+ const zodType = cfg.zodTypeGeneric ? `ZodType<${cfg.zodTypeGeneric}>` : 'ZodType';
332
+ const prismaImport = cfg.importsModel
333
+ ? prismaImportStatement.replace('{ Prisma }', `{ Prisma, ${modelName} }`)
334
+ : prismaImportStatement;
335
+ const importLines = [
336
+ prismaImport,
337
+ `import { Request, Response, NextFunction } from 'express';`,
338
+ `import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';`,
339
+ ];
340
+ if (cfg.source === 'query') {
341
+ importLines.push(`import { ParsedQs } from 'qs';`);
342
+ }
343
+ importLines.push(`import { ZodType } from 'zod';`);
344
+ if (cfg.importValidatorConfig) {
345
+ importLines.push(`import { ValidatorConfig } from '../routeConfig';`);
346
+ }
347
+ const extraTypes = cfg.wrapCount ? `\ntype UpdateManyResult = { count: number };\n` : '';
348
+ const queryType = cfg.argsPartial
349
+ ? `Partial<${argsTypeName}> & ParsedQs`
350
+ : `${argsTypeName} & ParsedQs`;
351
+ let interfaceBody;
352
+ if (cfg.source === 'query') {
353
+ const localsEntries = [];
354
+ if (cfg.localsDataType === 'nullable') {
355
+ localsEntries.push(` data?: ${modelName} | null`);
356
+ }
357
+ else if (cfg.localsDataType === 'array') {
358
+ localsEntries.push(` data?: ${modelName}[]`);
359
+ }
360
+ localsEntries.push(` outputValidator?: ${cfg.validatorLocalsType};`);
361
+ interfaceBody = [
362
+ ` prisma: PrismaClient;`,
363
+ ` query: ${queryType};`,
364
+ ` outputValidation?: ${zodType};`,
365
+ ...(cfg.hasPassToNext ? [` passToNext?: boolean;`] : []),
366
+ ` locals?: {`,
367
+ ...localsEntries,
368
+ ` }`,
369
+ ].join('\n');
370
+ }
371
+ else {
372
+ interfaceBody = [
373
+ ` prisma: PrismaClient;`,
374
+ ` body: ${argsTypeName};`,
375
+ ` outputValidation?: ${zodType};`,
376
+ ` locals?: {`,
377
+ ` outputValidator?: ${zodType};`,
378
+ ` };`,
379
+ ].join('\n');
380
+ }
381
+ const exportKw = cfg.exportInterface ? 'export ' : '';
382
+ const interfaceDecl = `${exportKw}interface ${interfaceName} extends Request {\n${interfaceBody}\n}`;
383
+ let middlewareTypeDecl;
384
+ switch (cfg.middlewareType) {
385
+ case 'queryFull':
386
+ middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, any, ${argsTypeName} & ParsedQs, Record<string, any>>`;
387
+ break;
388
+ case 'bodyArgs':
389
+ middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>`;
390
+ break;
391
+ case 'partialArgs':
392
+ middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, Partial<${argsTypeName}>, Record<string, any>>`;
393
+ break;
394
+ case 'emptyParsedQs':
395
+ middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, {}, ParsedQs>`;
396
+ break;
397
+ }
398
+ const validatorLine = cfg.useResLocalsValidator
399
+ ? `const outputValidator = res.locals.outputValidator?.schema || req.outputValidation;`
400
+ : `const outputValidator = req.locals?.outputValidator || req.outputValidation;`;
401
+ const v = cfg.resultVar;
402
+ const callArg = cfg.source === 'body'
403
+ ? 'req.body'
404
+ : cfg.noCastInCall
405
+ ? 'req.query'
406
+ : `req.query as ${argsTypeName}`;
407
+ const prismaCall = [
408
+ cfg.tsIgnoreCall ? ' // @ts-ignore' : '',
409
+ ` const ${v} = await req.prisma.${(0, strings_1.toPascalCase)(modelName)}.${cfg.prismaMethod}(${callArg});`,
410
+ ].filter(Boolean).join('\n');
411
+ const successJson = cfg.wrapCount
412
+ ? `{ count: validationResult.data.count }`
413
+ : `validationResult.data`;
414
+ const elseJson = cfg.wrapCount
415
+ ? `{ count: ${v}.count }`
416
+ : v;
417
+ let bodyLines;
418
+ if (cfg.hasPassToNext) {
419
+ bodyLines = [
420
+ ` ${validatorLine}`,
421
+ ``,
422
+ prismaCall,
423
+ ` if (req.passToNext) {`,
424
+ ` if (req.locals) req.locals.data = ${v};`,
425
+ ` next();`,
426
+ ` } else if (outputValidator) {`,
427
+ ` const validationResult = outputValidator.safeParse(${v});`,
428
+ ` if (validationResult.success) {`,
429
+ ` return res.status(${cfg.successStatus}).json(${successJson});`,
430
+ ` } else {`,
431
+ ` return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });`,
432
+ ` }`,
433
+ ` } else {`,
434
+ ` return res.status(${cfg.successStatus}).json(${elseJson});`,
435
+ ` }`,
436
+ ];
437
+ }
438
+ else {
439
+ bodyLines = [
440
+ ` ${validatorLine}`,
441
+ ``,
442
+ prismaCall,
443
+ ``,
444
+ ` if (outputValidator) {`,
445
+ ` const validationResult = outputValidator.safeParse(${v});`,
446
+ ` if (validationResult.success) {`,
447
+ ` return res.status(${cfg.successStatus}).json(${successJson});`,
448
+ ` } else {`,
449
+ ` return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });`,
450
+ ` }`,
451
+ ` } else {`,
452
+ ` return res.status(${cfg.successStatus}).json(${elseJson});`,
453
+ ` }`,
454
+ ];
455
+ }
456
+ return `
457
+ ${importLines.join('\n')}
458
+ ${extraTypes}
459
+ ${interfaceDecl}
460
+
461
+ ${middlewareTypeDecl}
462
+
463
+ export async function ${functionName}(req: ${interfaceName}, res: Response, next: NextFunction) {
464
+ try {
465
+ ${bodyLines.join('\n')}
466
+ } catch(error: unknown) {
467
+ next(error)
468
+ }
469
+ }`;
470
+ }
471
+ //# sourceMappingURL=generateOperation.js.map