prisma-generator-express 1.33.0 → 1.34.1

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.
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateFastifyRouterFunction = generateFastifyRouterFunction;
4
+ const strings_js_1 = require("../utils/strings.js");
4
5
  function generateFastifyRouterFunction({ model, enums, }) {
5
6
  const modelName = model.name;
7
+ const prefix = (0, strings_js_1.toCamelCase)(modelName);
6
8
  const modelNameLower = modelName.toLowerCase();
7
- const routerFunctionName = `${modelName}Routes`;
9
+ const routerFunctionName = `${prefix}Router`;
8
10
  const fieldsMeta = model.fields.map((f) => ({
9
11
  name: f.name,
10
12
  kind: f.kind,
@@ -23,26 +25,26 @@ function generateFastifyRouterFunction({ model, enums, }) {
23
25
  name: e.name,
24
26
  values: e.values.map((v) => ({ name: v.name })),
25
27
  }));
26
- return `import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
28
+ return `import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify'
27
29
  import {
28
- ${modelName}FindUnique,
29
- ${modelName}FindUniqueOrThrow,
30
- ${modelName}FindFirst,
31
- ${modelName}FindFirstOrThrow,
32
- ${modelName}FindMany,
33
- ${modelName}FindManyPaginated,
34
- ${modelName}Create,
35
- ${modelName}CreateMany,
36
- ${modelName}CreateManyAndReturn,
37
- ${modelName}Update,
38
- ${modelName}UpdateMany,
39
- ${modelName}UpdateManyAndReturn,
40
- ${modelName}Upsert,
41
- ${modelName}Delete,
42
- ${modelName}DeleteMany,
43
- ${modelName}Aggregate,
44
- ${modelName}Count,
45
- ${modelName}GroupBy,
30
+ ${prefix}FindUnique,
31
+ ${prefix}FindUniqueOrThrow,
32
+ ${prefix}FindFirst,
33
+ ${prefix}FindFirstOrThrow,
34
+ ${prefix}FindMany,
35
+ ${prefix}FindManyPaginated,
36
+ ${prefix}Create,
37
+ ${prefix}CreateMany,
38
+ ${prefix}CreateManyAndReturn,
39
+ ${prefix}Update,
40
+ ${prefix}UpdateMany,
41
+ ${prefix}UpdateManyAndReturn,
42
+ ${prefix}Upsert,
43
+ ${prefix}Delete,
44
+ ${prefix}DeleteMany,
45
+ ${prefix}Aggregate,
46
+ ${prefix}Count,
47
+ ${prefix}GroupBy,
46
48
  } from './${modelName}Handlers.js'
47
49
  import type { RouteConfig, FastifyHookHandler } from '../routeConfig.target.js'
48
50
  import { parseQueryParams } from '../parseQueryParams.js'
@@ -134,377 +136,376 @@ function sendError(reply: FastifyReply, error: unknown): void {
134
136
  reply.code(httpError.status).send({ message: httpError.message })
135
137
  }
136
138
 
137
- export async function ${routerFunctionName}(
138
- fastify: FastifyInstance,
139
- config: RouteConfig = {},
140
- ) {
141
- const customPrefix = normalizePrefix(config.customUrlPrefix || '')
142
- const modelPrefix = config.addModelPrefix !== false ? '/${modelNameLower}' : ''
143
- const basePath = customPrefix + modelPrefix
144
-
145
- const openApiDisabled = config.disableOpenApi === true
146
- || (config.disableOpenApi !== false && (
147
- _env.DISABLE_OPENAPI === 'true'
148
- || _env.NODE_ENV === 'production'
149
- ))
150
-
151
- const qbEnabled = isQueryBuilderEnabled(config)
152
-
153
- if (qbEnabled) {
154
- const qbConfig = getQueryBuilderConfig(config)
155
- if (qbConfig) {
156
- try { require('../queryBuilder').startQueryBuilder(qbConfig) } catch (err) { if (_env.NODE_ENV !== 'production') console.warn('[query-builder]', err) }
157
- }
158
- }
139
+ export function ${routerFunctionName}(config: RouteConfig = {}) {
140
+ return async function(fastify: FastifyInstance) {
141
+ const customPrefix = normalizePrefix(config.customUrlPrefix || '')
142
+ const modelPrefix = config.addModelPrefix !== false ? '/${modelNameLower}' : ''
143
+ const basePath = customPrefix + modelPrefix
159
144
 
160
- fastify.setErrorHandler((error, _request, reply) => {
161
- const status = (error as any).status ?? error.statusCode ?? 500
162
- const message = error.message || 'Internal server error'
163
- if (!reply.sent) {
164
- reply.code(status).send({ message })
165
- }
166
- })
167
-
168
- if (!openApiDisabled) {
169
- const openapiJsonPath = basePath ? \`\${basePath}/openapi.json\` : '/openapi.json'
170
- const openapiYamlPath = basePath ? \`\${basePath}/openapi.yaml\` : '/openapi.yaml'
171
-
172
- fastify.get(openapiJsonPath, async (_request, reply) => {
173
- const spec = buildModelOpenApi(
174
- '${modelName}',
175
- MODEL_FIELDS as any,
176
- MODEL_ENUMS as any,
177
- config,
178
- { format: 'json' },
179
- )
180
- return reply.send(spec)
181
- })
145
+ const openApiDisabled = config.disableOpenApi === true
146
+ || (config.disableOpenApi !== false && (
147
+ _env.DISABLE_OPENAPI === 'true'
148
+ || _env.NODE_ENV === 'production'
149
+ ))
182
150
 
183
- fastify.get(openapiYamlPath, async (_request, reply) => {
184
- const spec = buildModelOpenApi(
185
- '${modelName}',
186
- MODEL_FIELDS as any,
187
- MODEL_ENUMS as any,
188
- config,
189
- { format: 'yaml' },
190
- )
191
- return reply.type('application/yaml').send(spec as string)
192
- })
193
- }
151
+ const qbEnabled = isQueryBuilderEnabled(config)
194
152
 
195
- if (config.enableAll || config.findFirst) {
196
- const opConfig = config.findFirst || defaultOpConfig
197
- const { before = [], after = [] } = opConfig
198
- const path = basePath ? \`\${basePath}/first\` : '/first'
199
- fastify.get(path, async (request, reply) => {
200
- try {
201
- parseQueryHook(request)
202
- makeShapeHook(config, opConfig)(request)
203
- if (await runHooks(before, request, reply)) return
204
- await ${modelName}FindFirst(request, reply)
205
- if (await runHooks(after, request, reply)) return
206
- sendResult(request, reply)
207
- } catch (error: unknown) {
208
- sendError(reply, error)
153
+ if (qbEnabled) {
154
+ const qbConfig = getQueryBuilderConfig(config)
155
+ if (qbConfig) {
156
+ try { require('../queryBuilder').startQueryBuilder(qbConfig) } catch (err) { if (_env.NODE_ENV !== 'production') console.warn('[query-builder]', err) }
209
157
  }
210
- })
211
- }
158
+ }
212
159
 
213
- if (config.enableAll || config.findFirstOrThrow) {
214
- const opConfig = config.findFirstOrThrow || defaultOpConfig
215
- const { before = [], after = [] } = opConfig
216
- const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
217
- fastify.get(path, async (request, reply) => {
218
- try {
219
- parseQueryHook(request)
220
- makeShapeHook(config, opConfig)(request)
221
- if (await runHooks(before, request, reply)) return
222
- await ${modelName}FindFirstOrThrow(request, reply)
223
- if (await runHooks(after, request, reply)) return
224
- sendResult(request, reply)
225
- } catch (error: unknown) {
226
- sendError(reply, error)
160
+ fastify.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
161
+ const status = (error as any).status ?? error.statusCode ?? 500
162
+ const message = error.message || 'Internal server error'
163
+ if (!reply.sent) {
164
+ reply.code(status).send({ message })
227
165
  }
228
166
  })
229
- }
230
167
 
231
- if (config.enableAll || config.findManyPaginated) {
232
- const opConfig = config.findManyPaginated || defaultOpConfig
233
- const { before = [], after = [] } = opConfig
234
- const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
235
- fastify.get(path, async (request, reply) => {
236
- try {
237
- parseQueryHook(request)
238
- makeShapeHook(config, opConfig)(request)
239
- if (await runHooks(before, request, reply)) return
240
- await ${modelName}FindManyPaginated(request, reply)
241
- if (await runHooks(after, request, reply)) return
242
- sendResult(request, reply)
243
- } catch (error: unknown) {
244
- sendError(reply, error)
245
- }
246
- })
247
- }
168
+ if (!openApiDisabled) {
169
+ const openapiJsonPath = basePath ? \`\${basePath}/openapi.json\` : '/openapi.json'
170
+ const openapiYamlPath = basePath ? \`\${basePath}/openapi.yaml\` : '/openapi.yaml'
171
+
172
+ fastify.get(openapiJsonPath, async (_request, reply) => {
173
+ const spec = buildModelOpenApi(
174
+ '${modelName}',
175
+ MODEL_FIELDS as any,
176
+ MODEL_ENUMS as any,
177
+ config,
178
+ { format: 'json' },
179
+ )
180
+ return reply.send(spec)
181
+ })
182
+
183
+ fastify.get(openapiYamlPath, async (_request, reply) => {
184
+ const spec = buildModelOpenApi(
185
+ '${modelName}',
186
+ MODEL_FIELDS as any,
187
+ MODEL_ENUMS as any,
188
+ config,
189
+ { format: 'yaml' },
190
+ )
191
+ return reply.type('application/yaml').send(spec as string)
192
+ })
193
+ }
248
194
 
249
- if (config.enableAll || config.aggregate) {
250
- const opConfig = config.aggregate || defaultOpConfig
251
- const { before = [], after = [] } = opConfig
252
- const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
253
- fastify.get(path, async (request, reply) => {
254
- try {
255
- parseQueryHook(request)
256
- makeShapeHook(config, opConfig)(request)
257
- if (await runHooks(before, request, reply)) return
258
- await ${modelName}Aggregate(request, reply)
259
- if (await runHooks(after, request, reply)) return
260
- sendResult(request, reply)
261
- } catch (error: unknown) {
262
- sendError(reply, error)
263
- }
264
- })
265
- }
195
+ if (config.enableAll || config.findFirst) {
196
+ const opConfig = config.findFirst || defaultOpConfig
197
+ const { before = [], after = [] } = opConfig
198
+ const path = basePath ? \`\${basePath}/first\` : '/first'
199
+ fastify.get(path, async (request, reply) => {
200
+ try {
201
+ parseQueryHook(request)
202
+ makeShapeHook(config, opConfig)(request)
203
+ if (await runHooks(before, request, reply)) return
204
+ await ${prefix}FindFirst(request, reply)
205
+ if (await runHooks(after, request, reply)) return
206
+ sendResult(request, reply)
207
+ } catch (error: unknown) {
208
+ sendError(reply, error)
209
+ }
210
+ })
211
+ }
266
212
 
267
- if (config.enableAll || config.count) {
268
- const opConfig = config.count || defaultOpConfig
269
- const { before = [], after = [] } = opConfig
270
- const path = basePath ? \`\${basePath}/count\` : '/count'
271
- fastify.get(path, async (request, reply) => {
272
- try {
273
- parseQueryHook(request)
274
- makeShapeHook(config, opConfig)(request)
275
- if (await runHooks(before, request, reply)) return
276
- await ${modelName}Count(request, reply)
277
- if (await runHooks(after, request, reply)) return
278
- sendResult(request, reply)
279
- } catch (error: unknown) {
280
- sendError(reply, error)
281
- }
282
- })
283
- }
213
+ if (config.enableAll || config.findFirstOrThrow) {
214
+ const opConfig = config.findFirstOrThrow || defaultOpConfig
215
+ const { before = [], after = [] } = opConfig
216
+ const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
217
+ fastify.get(path, async (request, reply) => {
218
+ try {
219
+ parseQueryHook(request)
220
+ makeShapeHook(config, opConfig)(request)
221
+ if (await runHooks(before, request, reply)) return
222
+ await ${prefix}FindFirstOrThrow(request, reply)
223
+ if (await runHooks(after, request, reply)) return
224
+ sendResult(request, reply)
225
+ } catch (error: unknown) {
226
+ sendError(reply, error)
227
+ }
228
+ })
229
+ }
284
230
 
285
- if (config.enableAll || config.groupBy) {
286
- const opConfig = config.groupBy || defaultOpConfig
287
- const { before = [], after = [] } = opConfig
288
- const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
289
- fastify.get(path, async (request, reply) => {
290
- try {
291
- parseQueryHook(request)
292
- makeShapeHook(config, opConfig)(request)
293
- if (await runHooks(before, request, reply)) return
294
- await ${modelName}GroupBy(request, reply)
295
- if (await runHooks(after, request, reply)) return
296
- sendResult(request, reply)
297
- } catch (error: unknown) {
298
- sendError(reply, error)
299
- }
300
- })
301
- }
231
+ if (config.enableAll || config.findManyPaginated) {
232
+ const opConfig = config.findManyPaginated || defaultOpConfig
233
+ const { before = [], after = [] } = opConfig
234
+ const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
235
+ fastify.get(path, async (request, reply) => {
236
+ try {
237
+ parseQueryHook(request)
238
+ makeShapeHook(config, opConfig)(request)
239
+ if (await runHooks(before, request, reply)) return
240
+ await ${prefix}FindManyPaginated(request, reply)
241
+ if (await runHooks(after, request, reply)) return
242
+ sendResult(request, reply)
243
+ } catch (error: unknown) {
244
+ sendError(reply, error)
245
+ }
246
+ })
247
+ }
302
248
 
303
- if (config.enableAll || config.findUniqueOrThrow) {
304
- const opConfig = config.findUniqueOrThrow || defaultOpConfig
305
- const { before = [], after = [] } = opConfig
306
- const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
307
- fastify.get(path, async (request, reply) => {
308
- try {
309
- parseQueryHook(request)
310
- makeShapeHook(config, opConfig)(request)
311
- if (await runHooks(before, request, reply)) return
312
- await ${modelName}FindUniqueOrThrow(request, reply)
313
- if (await runHooks(after, request, reply)) return
314
- sendResult(request, reply)
315
- } catch (error: unknown) {
316
- sendError(reply, error)
317
- }
318
- })
319
- }
249
+ if (config.enableAll || config.aggregate) {
250
+ const opConfig = config.aggregate || defaultOpConfig
251
+ const { before = [], after = [] } = opConfig
252
+ const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
253
+ fastify.get(path, async (request, reply) => {
254
+ try {
255
+ parseQueryHook(request)
256
+ makeShapeHook(config, opConfig)(request)
257
+ if (await runHooks(before, request, reply)) return
258
+ await ${prefix}Aggregate(request, reply)
259
+ if (await runHooks(after, request, reply)) return
260
+ sendResult(request, reply)
261
+ } catch (error: unknown) {
262
+ sendError(reply, error)
263
+ }
264
+ })
265
+ }
320
266
 
321
- if (config.enableAll || config.findUnique) {
322
- const opConfig = config.findUnique || defaultOpConfig
323
- const { before = [], after = [] } = opConfig
324
- const path = basePath ? \`\${basePath}/unique\` : '/unique'
325
- fastify.get(path, async (request, reply) => {
326
- try {
327
- parseQueryHook(request)
328
- makeShapeHook(config, opConfig)(request)
329
- if (await runHooks(before, request, reply)) return
330
- await ${modelName}FindUnique(request, reply)
331
- if (await runHooks(after, request, reply)) return
332
- sendResult(request, reply)
333
- } catch (error: unknown) {
334
- sendError(reply, error)
335
- }
336
- })
337
- }
267
+ if (config.enableAll || config.count) {
268
+ const opConfig = config.count || defaultOpConfig
269
+ const { before = [], after = [] } = opConfig
270
+ const path = basePath ? \`\${basePath}/count\` : '/count'
271
+ fastify.get(path, async (request, reply) => {
272
+ try {
273
+ parseQueryHook(request)
274
+ makeShapeHook(config, opConfig)(request)
275
+ if (await runHooks(before, request, reply)) return
276
+ await ${prefix}Count(request, reply)
277
+ if (await runHooks(after, request, reply)) return
278
+ sendResult(request, reply)
279
+ } catch (error: unknown) {
280
+ sendError(reply, error)
281
+ }
282
+ })
283
+ }
338
284
 
339
- if (config.enableAll || config.findMany) {
340
- const opConfig = config.findMany || defaultOpConfig
341
- const { before = [], after = [] } = opConfig
342
- const path = basePath || '/'
343
- fastify.get(path, async (request, reply) => {
344
- try {
345
- parseQueryHook(request)
346
- makeShapeHook(config, opConfig)(request)
347
- if (await runHooks(before, request, reply)) return
348
- await ${modelName}FindMany(request, reply)
349
- if (await runHooks(after, request, reply)) return
350
- sendResult(request, reply)
351
- } catch (error: unknown) {
352
- sendError(reply, error)
353
- }
354
- })
355
- }
285
+ if (config.enableAll || config.groupBy) {
286
+ const opConfig = config.groupBy || defaultOpConfig
287
+ const { before = [], after = [] } = opConfig
288
+ const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
289
+ fastify.get(path, async (request, reply) => {
290
+ try {
291
+ parseQueryHook(request)
292
+ makeShapeHook(config, opConfig)(request)
293
+ if (await runHooks(before, request, reply)) return
294
+ await ${prefix}GroupBy(request, reply)
295
+ if (await runHooks(after, request, reply)) return
296
+ sendResult(request, reply)
297
+ } catch (error: unknown) {
298
+ sendError(reply, error)
299
+ }
300
+ })
301
+ }
356
302
 
357
- if (config.enableAll || config.createManyAndReturn) {
358
- const opConfig = config.createManyAndReturn || defaultOpConfig
359
- const { before = [], after = [] } = opConfig
360
- const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
361
- fastify.post(path, async (request, reply) => {
362
- try {
363
- makeShapeHook(config, opConfig)(request)
364
- if (await runHooks(before, request, reply)) return
365
- await ${modelName}CreateManyAndReturn(request, reply)
366
- if (await runHooks(after, request, reply)) return
367
- sendResult(request, reply)
368
- } catch (error: unknown) {
369
- sendError(reply, error)
370
- }
371
- })
372
- }
303
+ if (config.enableAll || config.findUniqueOrThrow) {
304
+ const opConfig = config.findUniqueOrThrow || defaultOpConfig
305
+ const { before = [], after = [] } = opConfig
306
+ const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
307
+ fastify.get(path, async (request, reply) => {
308
+ try {
309
+ parseQueryHook(request)
310
+ makeShapeHook(config, opConfig)(request)
311
+ if (await runHooks(before, request, reply)) return
312
+ await ${prefix}FindUniqueOrThrow(request, reply)
313
+ if (await runHooks(after, request, reply)) return
314
+ sendResult(request, reply)
315
+ } catch (error: unknown) {
316
+ sendError(reply, error)
317
+ }
318
+ })
319
+ }
373
320
 
374
- if (config.enableAll || config.createMany) {
375
- const opConfig = config.createMany || defaultOpConfig
376
- const { before = [], after = [] } = opConfig
377
- const path = basePath ? \`\${basePath}/many\` : '/many'
378
- fastify.post(path, async (request, reply) => {
379
- try {
380
- makeShapeHook(config, opConfig)(request)
381
- if (await runHooks(before, request, reply)) return
382
- await ${modelName}CreateMany(request, reply)
383
- if (await runHooks(after, request, reply)) return
384
- sendResult(request, reply)
385
- } catch (error: unknown) {
386
- sendError(reply, error)
387
- }
388
- })
389
- }
321
+ if (config.enableAll || config.findUnique) {
322
+ const opConfig = config.findUnique || defaultOpConfig
323
+ const { before = [], after = [] } = opConfig
324
+ const path = basePath ? \`\${basePath}/unique\` : '/unique'
325
+ fastify.get(path, async (request, reply) => {
326
+ try {
327
+ parseQueryHook(request)
328
+ makeShapeHook(config, opConfig)(request)
329
+ if (await runHooks(before, request, reply)) return
330
+ await ${prefix}FindUnique(request, reply)
331
+ if (await runHooks(after, request, reply)) return
332
+ sendResult(request, reply)
333
+ } catch (error: unknown) {
334
+ sendError(reply, error)
335
+ }
336
+ })
337
+ }
390
338
 
391
- if (config.enableAll || config.create) {
392
- const opConfig = config.create || defaultOpConfig
393
- const { before = [], after = [] } = opConfig
394
- const path = basePath || '/'
395
- fastify.post(path, async (request, reply) => {
396
- try {
397
- makeShapeHook(config, opConfig)(request)
398
- if (await runHooks(before, request, reply)) return
399
- await ${modelName}Create(request, reply)
400
- if (await runHooks(after, request, reply)) return
401
- sendResult(request, reply)
402
- } catch (error: unknown) {
403
- sendError(reply, error)
404
- }
405
- })
406
- }
339
+ if (config.enableAll || config.findMany) {
340
+ const opConfig = config.findMany || defaultOpConfig
341
+ const { before = [], after = [] } = opConfig
342
+ const path = basePath || '/'
343
+ fastify.get(path, async (request, reply) => {
344
+ try {
345
+ parseQueryHook(request)
346
+ makeShapeHook(config, opConfig)(request)
347
+ if (await runHooks(before, request, reply)) return
348
+ await ${prefix}FindMany(request, reply)
349
+ if (await runHooks(after, request, reply)) return
350
+ sendResult(request, reply)
351
+ } catch (error: unknown) {
352
+ sendError(reply, error)
353
+ }
354
+ })
355
+ }
407
356
 
408
- if (config.enableAll || config.updateManyAndReturn) {
409
- const opConfig = config.updateManyAndReturn || defaultOpConfig
410
- const { before = [], after = [] } = opConfig
411
- const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
412
- fastify.put(path, async (request, reply) => {
413
- try {
414
- makeShapeHook(config, opConfig)(request)
415
- if (await runHooks(before, request, reply)) return
416
- await ${modelName}UpdateManyAndReturn(request, reply)
417
- if (await runHooks(after, request, reply)) return
418
- sendResult(request, reply)
419
- } catch (error: unknown) {
420
- sendError(reply, error)
421
- }
422
- })
423
- }
357
+ if (config.enableAll || config.createManyAndReturn) {
358
+ const opConfig = config.createManyAndReturn || defaultOpConfig
359
+ const { before = [], after = [] } = opConfig
360
+ const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
361
+ fastify.post(path, async (request, reply) => {
362
+ try {
363
+ makeShapeHook(config, opConfig)(request)
364
+ if (await runHooks(before, request, reply)) return
365
+ await ${prefix}CreateManyAndReturn(request, reply)
366
+ if (await runHooks(after, request, reply)) return
367
+ sendResult(request, reply)
368
+ } catch (error: unknown) {
369
+ sendError(reply, error)
370
+ }
371
+ })
372
+ }
424
373
 
425
- if (config.enableAll || config.updateMany) {
426
- const opConfig = config.updateMany || defaultOpConfig
427
- const { before = [], after = [] } = opConfig
428
- const path = basePath ? \`\${basePath}/many\` : '/many'
429
- fastify.put(path, async (request, reply) => {
430
- try {
431
- makeShapeHook(config, opConfig)(request)
432
- if (await runHooks(before, request, reply)) return
433
- await ${modelName}UpdateMany(request, reply)
434
- if (await runHooks(after, request, reply)) return
435
- sendResult(request, reply)
436
- } catch (error: unknown) {
437
- sendError(reply, error)
438
- }
439
- })
440
- }
374
+ if (config.enableAll || config.createMany) {
375
+ const opConfig = config.createMany || defaultOpConfig
376
+ const { before = [], after = [] } = opConfig
377
+ const path = basePath ? \`\${basePath}/many\` : '/many'
378
+ fastify.post(path, async (request, reply) => {
379
+ try {
380
+ makeShapeHook(config, opConfig)(request)
381
+ if (await runHooks(before, request, reply)) return
382
+ await ${prefix}CreateMany(request, reply)
383
+ if (await runHooks(after, request, reply)) return
384
+ sendResult(request, reply)
385
+ } catch (error: unknown) {
386
+ sendError(reply, error)
387
+ }
388
+ })
389
+ }
441
390
 
442
- if (config.enableAll || config.update) {
443
- const opConfig = config.update || defaultOpConfig
444
- const { before = [], after = [] } = opConfig
445
- const path = basePath || '/'
446
- fastify.put(path, async (request, reply) => {
447
- try {
448
- makeShapeHook(config, opConfig)(request)
449
- if (await runHooks(before, request, reply)) return
450
- await ${modelName}Update(request, reply)
451
- if (await runHooks(after, request, reply)) return
452
- sendResult(request, reply)
453
- } catch (error: unknown) {
454
- sendError(reply, error)
455
- }
456
- })
457
- }
391
+ if (config.enableAll || config.create) {
392
+ const opConfig = config.create || defaultOpConfig
393
+ const { before = [], after = [] } = opConfig
394
+ const path = basePath || '/'
395
+ fastify.post(path, async (request, reply) => {
396
+ try {
397
+ makeShapeHook(config, opConfig)(request)
398
+ if (await runHooks(before, request, reply)) return
399
+ await ${prefix}Create(request, reply)
400
+ if (await runHooks(after, request, reply)) return
401
+ sendResult(request, reply)
402
+ } catch (error: unknown) {
403
+ sendError(reply, error)
404
+ }
405
+ })
406
+ }
458
407
 
459
- if (config.enableAll || config.upsert) {
460
- const opConfig = config.upsert || defaultOpConfig
461
- const { before = [], after = [] } = opConfig
462
- const path = basePath || '/'
463
- fastify.patch(path, async (request, reply) => {
464
- try {
465
- makeShapeHook(config, opConfig)(request)
466
- if (await runHooks(before, request, reply)) return
467
- await ${modelName}Upsert(request, reply)
468
- if (await runHooks(after, request, reply)) return
469
- sendResult(request, reply)
470
- } catch (error: unknown) {
471
- sendError(reply, error)
472
- }
473
- })
474
- }
408
+ if (config.enableAll || config.updateManyAndReturn) {
409
+ const opConfig = config.updateManyAndReturn || defaultOpConfig
410
+ const { before = [], after = [] } = opConfig
411
+ const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
412
+ fastify.put(path, async (request, reply) => {
413
+ try {
414
+ makeShapeHook(config, opConfig)(request)
415
+ if (await runHooks(before, request, reply)) return
416
+ await ${prefix}UpdateManyAndReturn(request, reply)
417
+ if (await runHooks(after, request, reply)) return
418
+ sendResult(request, reply)
419
+ } catch (error: unknown) {
420
+ sendError(reply, error)
421
+ }
422
+ })
423
+ }
475
424
 
476
- if (config.enableAll || config.deleteMany) {
477
- const opConfig = config.deleteMany || defaultOpConfig
478
- const { before = [], after = [] } = opConfig
479
- const path = basePath ? \`\${basePath}/many\` : '/many'
480
- fastify.delete(path, async (request, reply) => {
481
- try {
482
- makeShapeHook(config, opConfig)(request)
483
- if (await runHooks(before, request, reply)) return
484
- await ${modelName}DeleteMany(request, reply)
485
- if (await runHooks(after, request, reply)) return
486
- sendResult(request, reply)
487
- } catch (error: unknown) {
488
- sendError(reply, error)
489
- }
490
- })
491
- }
425
+ if (config.enableAll || config.updateMany) {
426
+ const opConfig = config.updateMany || defaultOpConfig
427
+ const { before = [], after = [] } = opConfig
428
+ const path = basePath ? \`\${basePath}/many\` : '/many'
429
+ fastify.put(path, async (request, reply) => {
430
+ try {
431
+ makeShapeHook(config, opConfig)(request)
432
+ if (await runHooks(before, request, reply)) return
433
+ await ${prefix}UpdateMany(request, reply)
434
+ if (await runHooks(after, request, reply)) return
435
+ sendResult(request, reply)
436
+ } catch (error: unknown) {
437
+ sendError(reply, error)
438
+ }
439
+ })
440
+ }
492
441
 
493
- if (config.enableAll || config.delete) {
494
- const opConfig = config.delete || defaultOpConfig
495
- const { before = [], after = [] } = opConfig
496
- const path = basePath || '/'
497
- fastify.delete(path, async (request, reply) => {
498
- try {
499
- makeShapeHook(config, opConfig)(request)
500
- if (await runHooks(before, request, reply)) return
501
- await ${modelName}Delete(request, reply)
502
- if (await runHooks(after, request, reply)) return
503
- sendResult(request, reply)
504
- } catch (error: unknown) {
505
- sendError(reply, error)
506
- }
507
- })
442
+ if (config.enableAll || config.update) {
443
+ const opConfig = config.update || defaultOpConfig
444
+ const { before = [], after = [] } = opConfig
445
+ const path = basePath || '/'
446
+ fastify.put(path, async (request, reply) => {
447
+ try {
448
+ makeShapeHook(config, opConfig)(request)
449
+ if (await runHooks(before, request, reply)) return
450
+ await ${prefix}Update(request, reply)
451
+ if (await runHooks(after, request, reply)) return
452
+ sendResult(request, reply)
453
+ } catch (error: unknown) {
454
+ sendError(reply, error)
455
+ }
456
+ })
457
+ }
458
+
459
+ if (config.enableAll || config.upsert) {
460
+ const opConfig = config.upsert || defaultOpConfig
461
+ const { before = [], after = [] } = opConfig
462
+ const path = basePath || '/'
463
+ fastify.patch(path, async (request, reply) => {
464
+ try {
465
+ makeShapeHook(config, opConfig)(request)
466
+ if (await runHooks(before, request, reply)) return
467
+ await ${prefix}Upsert(request, reply)
468
+ if (await runHooks(after, request, reply)) return
469
+ sendResult(request, reply)
470
+ } catch (error: unknown) {
471
+ sendError(reply, error)
472
+ }
473
+ })
474
+ }
475
+
476
+ if (config.enableAll || config.deleteMany) {
477
+ const opConfig = config.deleteMany || defaultOpConfig
478
+ const { before = [], after = [] } = opConfig
479
+ const path = basePath ? \`\${basePath}/many\` : '/many'
480
+ fastify.delete(path, async (request, reply) => {
481
+ try {
482
+ makeShapeHook(config, opConfig)(request)
483
+ if (await runHooks(before, request, reply)) return
484
+ await ${prefix}DeleteMany(request, reply)
485
+ if (await runHooks(after, request, reply)) return
486
+ sendResult(request, reply)
487
+ } catch (error: unknown) {
488
+ sendError(reply, error)
489
+ }
490
+ })
491
+ }
492
+
493
+ if (config.enableAll || config.delete) {
494
+ const opConfig = config.delete || defaultOpConfig
495
+ const { before = [], after = [] } = opConfig
496
+ const path = basePath || '/'
497
+ fastify.delete(path, async (request, reply) => {
498
+ try {
499
+ makeShapeHook(config, opConfig)(request)
500
+ if (await runHooks(before, request, reply)) return
501
+ await ${prefix}Delete(request, reply)
502
+ if (await runHooks(after, request, reply)) return
503
+ sendResult(request, reply)
504
+ } catch (error: unknown) {
505
+ sendError(reply, error)
506
+ }
507
+ })
508
+ }
508
509
  }
509
510
  }
510
511
  `;