prisma-generator-express 1.41.0 → 1.43.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 (45) hide show
  1. package/dist/generators/generateFastifyHandler.js +17 -4
  2. package/dist/generators/generateFastifyHandler.js.map +1 -1
  3. package/dist/generators/generateHonoHandler.js +4 -4
  4. package/dist/generators/generateOperationCore.d.ts +0 -1
  5. package/dist/generators/generateOperationCore.js +34 -546
  6. package/dist/generators/generateOperationCore.js.map +1 -1
  7. package/dist/generators/generateRelationMeta.d.ts +13 -0
  8. package/dist/generators/generateRelationMeta.js +106 -0
  9. package/dist/generators/generateRelationMeta.js.map +1 -0
  10. package/dist/generators/generateRouteConfigType.js +6 -6
  11. package/dist/generators/generateRouteConfigType.js.map +1 -1
  12. package/dist/generators/generateRouter.js +141 -60
  13. package/dist/generators/generateRouter.js.map +1 -1
  14. package/dist/generators/generateRouterFastify.js +127 -384
  15. package/dist/generators/generateRouterFastify.js.map +1 -1
  16. package/dist/generators/generateRouterHono.js +48 -36
  17. package/dist/generators/generateRouterHono.js.map +1 -1
  18. package/dist/generators/generateUnifiedHandler.js +24 -8
  19. package/dist/generators/generateUnifiedHandler.js.map +1 -1
  20. package/dist/index.js +21 -5
  21. package/dist/index.js.map +1 -1
  22. package/dist/utils/copyFiles.js +12 -0
  23. package/dist/utils/copyFiles.js.map +1 -1
  24. package/dist/utils/writeFileSafely.js +3 -0
  25. package/dist/utils/writeFileSafely.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/copy/autoIncludePlanner.ts +299 -0
  28. package/src/copy/autoIncludeRuntime.ts +307 -0
  29. package/src/copy/operationRuntime.ts +603 -0
  30. package/src/copy/routeConfig.express.ts +5 -3
  31. package/src/copy/routeConfig.fastify.ts +2 -2
  32. package/src/copy/routeConfig.hono.ts +3 -3
  33. package/src/copy/routeConfig.ts +20 -9
  34. package/src/generators/generateFastifyHandler.ts +17 -4
  35. package/src/generators/generateHonoHandler.ts +4 -4
  36. package/src/generators/generateOperationCore.ts +34 -546
  37. package/src/generators/generateRelationMeta.ts +154 -0
  38. package/src/generators/generateRouteConfigType.ts +7 -7
  39. package/src/generators/generateRouter.ts +141 -60
  40. package/src/generators/generateRouterFastify.ts +127 -384
  41. package/src/generators/generateRouterHono.ts +48 -36
  42. package/src/generators/generateUnifiedHandler.ts +24 -8
  43. package/src/index.ts +25 -7
  44. package/src/utils/copyFiles.ts +13 -0
  45. package/src/utils/writeFileSafely.ts +3 -0
@@ -45,6 +45,7 @@ export function generateFastifyRouterFunction({
45
45
  }))
46
46
 
47
47
  return `import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify'
48
+ import { startQueryBuilder } from '../queryBuilder${ext}'
48
49
  import {
49
50
  ${prefix}FindUnique,
50
51
  ${prefix}FindUniqueOrThrow,
@@ -69,7 +70,7 @@ import type { RouteConfig, FastifyHookHandler } from '../routeConfig.target${ext
69
70
  import { parseQueryParams } from '../parseQueryParams${ext}'
70
71
  import { sanitizeKeys } from '../misc${ext}'
71
72
  import { buildModelOpenApi } from '../buildModelOpenApi${ext}'
72
- import { mapError, transformResult, HttpError } from '../operationRuntime${ext}'
73
+ import { mapError, transformResult, HttpError, type OperationContext } from '../operationRuntime${ext}'
73
74
 
74
75
  ${generateRouteConfigType(modelName, 'FastifyHookHandler', guardShapesImport, importStyle, 'fastify')}
75
76
  const _env = typeof process !== 'undefined' && process.env ? process.env : {} as Record<string, string | undefined>
@@ -78,9 +79,27 @@ const MODEL_FIELDS = ${JSON.stringify(fieldsMeta, null, 2)} as const
78
79
 
79
80
  const MODEL_ENUMS = ${JSON.stringify(enumsMeta, null, 2)} as const
80
81
 
81
- const defaultOpConfig = {
82
- before: [] as FastifyHookHandler[],
83
- after: [] as FastifyHookHandler[],
82
+ type OperationConfigLike = {
83
+ before?: FastifyHookHandler[]
84
+ after?: FastifyHookHandler[]
85
+ shape?: Record<string, unknown>
86
+ }
87
+
88
+ type FastifyExtended = FastifyRequest & {
89
+ prisma?: unknown
90
+ postgres?: unknown
91
+ sqlite?: unknown
92
+ parsedQuery?: Record<string, unknown>
93
+ routeConfig?: { pagination?: OperationContext['paginationConfig'] }
94
+ guardShape?: Record<string, unknown>
95
+ guardCaller?: string
96
+ resultData?: unknown
97
+ resultStatus?: number
98
+ }
99
+
100
+ const defaultOpConfig: OperationConfigLike = {
101
+ before: [],
102
+ after: [],
84
103
  }
85
104
 
86
105
  function normalizePrefix(p: string): string {
@@ -108,7 +127,7 @@ function getQueryBuilderConfig(config: RouteConfig) {
108
127
  function parseQueryHook(request: FastifyRequest): void {
109
128
  const raw = request.query as Record<string, unknown>
110
129
  if (raw && Object.keys(raw).length > 0) {
111
- ;(request as any).parsedQuery = parseQueryParams(raw)
130
+ (request as FastifyExtended).parsedQuery = parseQueryParams(raw) as Record<string, unknown>
112
131
  }
113
132
  }
114
133
 
@@ -117,21 +136,28 @@ function parseBodyAsQueryHook(request: FastifyRequest): void {
117
136
  if (!body || typeof body !== 'object' || Array.isArray(body)) {
118
137
  throw new HttpError(400, 'Request body must be a JSON object')
119
138
  }
120
- ;(request as any).parsedQuery = sanitizeKeys(body as Record<string, unknown>)
139
+ (request as FastifyExtended).parsedQuery = sanitizeKeys(body as Record<string, unknown>)
121
140
  }
122
141
 
123
- function makeShapeHook(config: RouteConfig, opConfig: any): (request: FastifyRequest) => void {
142
+ function makeShapeHook(
143
+ config: ${modelName}RouteConfig,
144
+ opConfig: OperationConfigLike,
145
+ ): (request: FastifyRequest) => void {
124
146
  return (request: FastifyRequest) => {
125
- ;(request as any).routeConfig = config
147
+ const fx = request as FastifyExtended
148
+ const paginationConfig = (config as { pagination?: OperationContext['paginationConfig'] }).pagination
149
+ if (paginationConfig) {
150
+ fx.routeConfig = { pagination: paginationConfig }
151
+ }
126
152
  if (opConfig.shape) {
127
- ;(request as any).guardShape = opConfig.shape
153
+ fx.guardShape = opConfig.shape
128
154
  const headerName = config.guard?.variantHeader || 'x-api-variant'
129
155
  const headerValue = request.headers[headerName]
130
156
  const caller = config.guard?.resolveVariant?.(request)
131
157
  ?? (Array.isArray(headerValue) ? headerValue[0] : headerValue)
132
158
  ?? undefined
133
159
  if (caller) {
134
- ;(request as any).guardCaller = caller
160
+ fx.guardCaller = caller
135
161
  }
136
162
  }
137
163
  }
@@ -150,7 +176,7 @@ async function runHooks(
150
176
  }
151
177
 
152
178
  function sendResult(request: FastifyRequest, reply: FastifyReply): void {
153
- const req = request as any
179
+ const req = request as FastifyExtended
154
180
  const data = req.resultData
155
181
  const status = req.resultStatus ?? 200
156
182
  if (data === undefined) {
@@ -186,12 +212,17 @@ export async function ${routerFunctionName}<TCtx = unknown>(
186
212
  if (qbEnabled) {
187
213
  const qbConfig = getQueryBuilderConfig(config)
188
214
  if (qbConfig) {
189
- try { require('../queryBuilder${ext}').startQueryBuilder(qbConfig) } catch (err) { if (_env.NODE_ENV !== 'production') console.warn('[query-builder]', err) }
215
+ try {
216
+ startQueryBuilder(qbConfig)
217
+ } catch (err) {
218
+ if (_env.NODE_ENV !== 'production') console.warn('[query-builder]', err)
219
+ }
190
220
  }
191
221
  }
192
222
 
193
223
  fastify.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
194
- const status = (error as any).status ?? error.statusCode ?? 500
224
+ const e = error as { status?: number; statusCode?: number; message?: string }
225
+ const status = e.status ?? e.statusCode ?? 500
195
226
  const message = error.message || 'Internal server error'
196
227
  if (!reply.sent) {
197
228
  reply.code(status).send({ message })
@@ -205,8 +236,8 @@ export async function ${routerFunctionName}<TCtx = unknown>(
205
236
  fastify.get(openapiJsonPath, async (_request, reply) => {
206
237
  const spec = buildModelOpenApi(
207
238
  '${modelName}',
208
- MODEL_FIELDS as any,
209
- MODEL_ENUMS as any,
239
+ MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
240
+ MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
210
241
  config,
211
242
  { format: 'json' },
212
243
  )
@@ -216,8 +247,8 @@ export async function ${routerFunctionName}<TCtx = unknown>(
216
247
  fastify.get(openapiYamlPath, async (_request, reply) => {
217
248
  const spec = buildModelOpenApi(
218
249
  '${modelName}',
219
- MODEL_FIELDS as any,
220
- MODEL_ENUMS as any,
250
+ MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
251
+ MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
221
252
  config,
222
253
  { format: 'yaml' },
223
254
  )
@@ -225,446 +256,158 @@ export async function ${routerFunctionName}<TCtx = unknown>(
225
256
  })
226
257
  }
227
258
 
259
+ const handleGet = (
260
+ opConfig: OperationConfigLike,
261
+ handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
262
+ parseFn: (req: FastifyRequest) => void,
263
+ ) => async (request: FastifyRequest, reply: FastifyReply) => {
264
+ try {
265
+ parseFn(request)
266
+ makeShapeHook(config, opConfig)(request)
267
+ const { before = [], after = [] } = opConfig
268
+ if (await runHooks(before, request, reply)) return
269
+ await handlerFn(request, reply)
270
+ if (await runHooks(after, request, reply)) return
271
+ sendResult(request, reply)
272
+ } catch (error: unknown) {
273
+ sendError(reply, error)
274
+ }
275
+ }
276
+
277
+ const handleWrite = (
278
+ opConfig: OperationConfigLike,
279
+ handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
280
+ ) => async (request: FastifyRequest, reply: FastifyReply) => {
281
+ try {
282
+ makeShapeHook(config, opConfig)(request)
283
+ const { before = [], after = [] } = opConfig
284
+ if (await runHooks(before, request, reply)) return
285
+ await handlerFn(request, reply)
286
+ if (await runHooks(after, request, reply)) return
287
+ sendResult(request, reply)
288
+ } catch (error: unknown) {
289
+ sendError(reply, error)
290
+ }
291
+ }
292
+
228
293
  if (config.enableAll || config.findFirst) {
229
- const opConfig = config.findFirst || defaultOpConfig
230
- const { before = [], after = [] } = opConfig
294
+ const opConfig: OperationConfigLike = (config.findFirst as OperationConfigLike | undefined) ?? defaultOpConfig
231
295
  const path = basePath ? \`\${basePath}/first\` : '/first'
232
- fastify.get(path, async (request, reply) => {
233
- try {
234
- parseQueryHook(request)
235
- makeShapeHook(config, opConfig)(request)
236
- if (await runHooks(before, request, reply)) return
237
- await ${prefix}FindFirst(request, reply)
238
- if (await runHooks(after, request, reply)) return
239
- sendResult(request, reply)
240
- } catch (error: unknown) {
241
- sendError(reply, error)
242
- }
243
- })
244
- if (postReadsEnabled) {
245
- fastify.post(path, async (request, reply) => {
246
- try {
247
- parseBodyAsQueryHook(request)
248
- makeShapeHook(config, opConfig)(request)
249
- if (await runHooks(before, request, reply)) return
250
- await ${prefix}FindFirst(request, reply)
251
- if (await runHooks(after, request, reply)) return
252
- sendResult(request, reply)
253
- } catch (error: unknown) {
254
- sendError(reply, error)
255
- }
256
- })
257
- }
296
+ fastify.get(path, handleGet(opConfig, ${prefix}FindFirst, parseQueryHook))
297
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}FindFirst, parseBodyAsQueryHook))
258
298
  }
259
299
 
260
300
  if (config.enableAll || config.findFirstOrThrow) {
261
- const opConfig = config.findFirstOrThrow || defaultOpConfig
262
- const { before = [], after = [] } = opConfig
301
+ const opConfig: OperationConfigLike = (config.findFirstOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
263
302
  const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
264
- fastify.get(path, async (request, reply) => {
265
- try {
266
- parseQueryHook(request)
267
- makeShapeHook(config, opConfig)(request)
268
- if (await runHooks(before, request, reply)) return
269
- await ${prefix}FindFirstOrThrow(request, reply)
270
- if (await runHooks(after, request, reply)) return
271
- sendResult(request, reply)
272
- } catch (error: unknown) {
273
- sendError(reply, error)
274
- }
275
- })
276
- if (postReadsEnabled) {
277
- fastify.post(path, async (request, reply) => {
278
- try {
279
- parseBodyAsQueryHook(request)
280
- makeShapeHook(config, opConfig)(request)
281
- if (await runHooks(before, request, reply)) return
282
- await ${prefix}FindFirstOrThrow(request, reply)
283
- if (await runHooks(after, request, reply)) return
284
- sendResult(request, reply)
285
- } catch (error: unknown) {
286
- sendError(reply, error)
287
- }
288
- })
289
- }
303
+ fastify.get(path, handleGet(opConfig, ${prefix}FindFirstOrThrow, parseQueryHook))
304
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}FindFirstOrThrow, parseBodyAsQueryHook))
290
305
  }
291
306
 
292
307
  if (config.enableAll || config.findManyPaginated) {
293
- const opConfig = config.findManyPaginated || defaultOpConfig
294
- const { before = [], after = [] } = opConfig
308
+ const opConfig: OperationConfigLike = (config.findManyPaginated as OperationConfigLike | undefined) ?? defaultOpConfig
295
309
  const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
296
- fastify.get(path, async (request, reply) => {
297
- try {
298
- parseQueryHook(request)
299
- makeShapeHook(config, opConfig)(request)
300
- if (await runHooks(before, request, reply)) return
301
- await ${prefix}FindManyPaginated(request, reply)
302
- if (await runHooks(after, request, reply)) return
303
- sendResult(request, reply)
304
- } catch (error: unknown) {
305
- sendError(reply, error)
306
- }
307
- })
308
- if (postReadsEnabled) {
309
- fastify.post(path, async (request, reply) => {
310
- try {
311
- parseBodyAsQueryHook(request)
312
- makeShapeHook(config, opConfig)(request)
313
- if (await runHooks(before, request, reply)) return
314
- await ${prefix}FindManyPaginated(request, reply)
315
- if (await runHooks(after, request, reply)) return
316
- sendResult(request, reply)
317
- } catch (error: unknown) {
318
- sendError(reply, error)
319
- }
320
- })
321
- }
310
+ fastify.get(path, handleGet(opConfig, ${prefix}FindManyPaginated, parseQueryHook))
311
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}FindManyPaginated, parseBodyAsQueryHook))
322
312
  }
323
313
 
324
314
  if (config.enableAll || config.aggregate) {
325
- const opConfig = config.aggregate || defaultOpConfig
326
- const { before = [], after = [] } = opConfig
315
+ const opConfig: OperationConfigLike = (config.aggregate as OperationConfigLike | undefined) ?? defaultOpConfig
327
316
  const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
328
- fastify.get(path, async (request, reply) => {
329
- try {
330
- parseQueryHook(request)
331
- makeShapeHook(config, opConfig)(request)
332
- if (await runHooks(before, request, reply)) return
333
- await ${prefix}Aggregate(request, reply)
334
- if (await runHooks(after, request, reply)) return
335
- sendResult(request, reply)
336
- } catch (error: unknown) {
337
- sendError(reply, error)
338
- }
339
- })
340
- if (postReadsEnabled) {
341
- fastify.post(path, async (request, reply) => {
342
- try {
343
- parseBodyAsQueryHook(request)
344
- makeShapeHook(config, opConfig)(request)
345
- if (await runHooks(before, request, reply)) return
346
- await ${prefix}Aggregate(request, reply)
347
- if (await runHooks(after, request, reply)) return
348
- sendResult(request, reply)
349
- } catch (error: unknown) {
350
- sendError(reply, error)
351
- }
352
- })
353
- }
317
+ fastify.get(path, handleGet(opConfig, ${prefix}Aggregate, parseQueryHook))
318
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}Aggregate, parseBodyAsQueryHook))
354
319
  }
355
320
 
356
321
  if (config.enableAll || config.count) {
357
- const opConfig = config.count || defaultOpConfig
358
- const { before = [], after = [] } = opConfig
322
+ const opConfig: OperationConfigLike = (config.count as OperationConfigLike | undefined) ?? defaultOpConfig
359
323
  const path = basePath ? \`\${basePath}/count\` : '/count'
360
- fastify.get(path, async (request, reply) => {
361
- try {
362
- parseQueryHook(request)
363
- makeShapeHook(config, opConfig)(request)
364
- if (await runHooks(before, request, reply)) return
365
- await ${prefix}Count(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
- if (postReadsEnabled) {
373
- fastify.post(path, async (request, reply) => {
374
- try {
375
- parseBodyAsQueryHook(request)
376
- makeShapeHook(config, opConfig)(request)
377
- if (await runHooks(before, request, reply)) return
378
- await ${prefix}Count(request, reply)
379
- if (await runHooks(after, request, reply)) return
380
- sendResult(request, reply)
381
- } catch (error: unknown) {
382
- sendError(reply, error)
383
- }
384
- })
385
- }
324
+ fastify.get(path, handleGet(opConfig, ${prefix}Count, parseQueryHook))
325
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}Count, parseBodyAsQueryHook))
386
326
  }
387
327
 
388
328
  if (config.enableAll || config.groupBy) {
389
- const opConfig = config.groupBy || defaultOpConfig
390
- const { before = [], after = [] } = opConfig
329
+ const opConfig: OperationConfigLike = (config.groupBy as OperationConfigLike | undefined) ?? defaultOpConfig
391
330
  const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
392
- fastify.get(path, async (request, reply) => {
393
- try {
394
- parseQueryHook(request)
395
- makeShapeHook(config, opConfig)(request)
396
- if (await runHooks(before, request, reply)) return
397
- await ${prefix}GroupBy(request, reply)
398
- if (await runHooks(after, request, reply)) return
399
- sendResult(request, reply)
400
- } catch (error: unknown) {
401
- sendError(reply, error)
402
- }
403
- })
404
- if (postReadsEnabled) {
405
- fastify.post(path, async (request, reply) => {
406
- try {
407
- parseBodyAsQueryHook(request)
408
- makeShapeHook(config, opConfig)(request)
409
- if (await runHooks(before, request, reply)) return
410
- await ${prefix}GroupBy(request, reply)
411
- if (await runHooks(after, request, reply)) return
412
- sendResult(request, reply)
413
- } catch (error: unknown) {
414
- sendError(reply, error)
415
- }
416
- })
417
- }
331
+ fastify.get(path, handleGet(opConfig, ${prefix}GroupBy, parseQueryHook))
332
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}GroupBy, parseBodyAsQueryHook))
418
333
  }
419
334
 
420
335
  if (config.enableAll || config.findUniqueOrThrow) {
421
- const opConfig = config.findUniqueOrThrow || defaultOpConfig
422
- const { before = [], after = [] } = opConfig
336
+ const opConfig: OperationConfigLike = (config.findUniqueOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
423
337
  const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
424
- fastify.get(path, async (request, reply) => {
425
- try {
426
- parseQueryHook(request)
427
- makeShapeHook(config, opConfig)(request)
428
- if (await runHooks(before, request, reply)) return
429
- await ${prefix}FindUniqueOrThrow(request, reply)
430
- if (await runHooks(after, request, reply)) return
431
- sendResult(request, reply)
432
- } catch (error: unknown) {
433
- sendError(reply, error)
434
- }
435
- })
436
- if (postReadsEnabled) {
437
- fastify.post(path, async (request, reply) => {
438
- try {
439
- parseBodyAsQueryHook(request)
440
- makeShapeHook(config, opConfig)(request)
441
- if (await runHooks(before, request, reply)) return
442
- await ${prefix}FindUniqueOrThrow(request, reply)
443
- if (await runHooks(after, request, reply)) return
444
- sendResult(request, reply)
445
- } catch (error: unknown) {
446
- sendError(reply, error)
447
- }
448
- })
449
- }
338
+ fastify.get(path, handleGet(opConfig, ${prefix}FindUniqueOrThrow, parseQueryHook))
339
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}FindUniqueOrThrow, parseBodyAsQueryHook))
450
340
  }
451
341
 
452
342
  if (config.enableAll || config.findUnique) {
453
- const opConfig = config.findUnique || defaultOpConfig
454
- const { before = [], after = [] } = opConfig
343
+ const opConfig: OperationConfigLike = (config.findUnique as OperationConfigLike | undefined) ?? defaultOpConfig
455
344
  const path = basePath ? \`\${basePath}/unique\` : '/unique'
456
- fastify.get(path, async (request, reply) => {
457
- try {
458
- parseQueryHook(request)
459
- makeShapeHook(config, opConfig)(request)
460
- if (await runHooks(before, request, reply)) return
461
- await ${prefix}FindUnique(request, reply)
462
- if (await runHooks(after, request, reply)) return
463
- sendResult(request, reply)
464
- } catch (error: unknown) {
465
- sendError(reply, error)
466
- }
467
- })
468
- if (postReadsEnabled) {
469
- fastify.post(path, async (request, reply) => {
470
- try {
471
- parseBodyAsQueryHook(request)
472
- makeShapeHook(config, opConfig)(request)
473
- if (await runHooks(before, request, reply)) return
474
- await ${prefix}FindUnique(request, reply)
475
- if (await runHooks(after, request, reply)) return
476
- sendResult(request, reply)
477
- } catch (error: unknown) {
478
- sendError(reply, error)
479
- }
480
- })
481
- }
345
+ fastify.get(path, handleGet(opConfig, ${prefix}FindUnique, parseQueryHook))
346
+ if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${prefix}FindUnique, parseBodyAsQueryHook))
482
347
  }
483
348
 
484
349
  if (config.enableAll || config.findMany) {
485
- const opConfig = config.findMany || defaultOpConfig
486
- const { before = [], after = [] } = opConfig
350
+ const opConfig: OperationConfigLike = (config.findMany as OperationConfigLike | undefined) ?? defaultOpConfig
487
351
  const path = basePath || '/'
488
- fastify.get(path, async (request, reply) => {
489
- try {
490
- parseQueryHook(request)
491
- makeShapeHook(config, opConfig)(request)
492
- if (await runHooks(before, request, reply)) return
493
- await ${prefix}FindMany(request, reply)
494
- if (await runHooks(after, request, reply)) return
495
- sendResult(request, reply)
496
- } catch (error: unknown) {
497
- sendError(reply, error)
498
- }
499
- })
352
+ fastify.get(path, handleGet(opConfig, ${prefix}FindMany, parseQueryHook))
500
353
  if (postReadsEnabled) {
501
354
  const postPath = basePath ? \`\${basePath}/read\` : '/read'
502
- fastify.post(postPath, async (request, reply) => {
503
- try {
504
- parseBodyAsQueryHook(request)
505
- makeShapeHook(config, opConfig)(request)
506
- if (await runHooks(before, request, reply)) return
507
- await ${prefix}FindMany(request, reply)
508
- if (await runHooks(after, request, reply)) return
509
- sendResult(request, reply)
510
- } catch (error: unknown) {
511
- sendError(reply, error)
512
- }
513
- })
355
+ fastify.post(postPath, handleGet(opConfig, ${prefix}FindMany, parseBodyAsQueryHook))
514
356
  }
515
357
  }
516
358
 
517
359
  if (config.enableAll || config.createManyAndReturn) {
518
- const opConfig = config.createManyAndReturn || defaultOpConfig
519
- const { before = [], after = [] } = opConfig
360
+ const opConfig: OperationConfigLike = (config.createManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
520
361
  const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
521
- fastify.post(path, async (request, reply) => {
522
- try {
523
- makeShapeHook(config, opConfig)(request)
524
- if (await runHooks(before, request, reply)) return
525
- await ${prefix}CreateManyAndReturn(request, reply)
526
- if (await runHooks(after, request, reply)) return
527
- sendResult(request, reply)
528
- } catch (error: unknown) {
529
- sendError(reply, error)
530
- }
531
- })
362
+ fastify.post(path, handleWrite(opConfig, ${prefix}CreateManyAndReturn))
532
363
  }
533
364
 
534
365
  if (config.enableAll || config.createMany) {
535
- const opConfig = config.createMany || defaultOpConfig
536
- const { before = [], after = [] } = opConfig
366
+ const opConfig: OperationConfigLike = (config.createMany as OperationConfigLike | undefined) ?? defaultOpConfig
537
367
  const path = basePath ? \`\${basePath}/many\` : '/many'
538
- fastify.post(path, async (request, reply) => {
539
- try {
540
- makeShapeHook(config, opConfig)(request)
541
- if (await runHooks(before, request, reply)) return
542
- await ${prefix}CreateMany(request, reply)
543
- if (await runHooks(after, request, reply)) return
544
- sendResult(request, reply)
545
- } catch (error: unknown) {
546
- sendError(reply, error)
547
- }
548
- })
368
+ fastify.post(path, handleWrite(opConfig, ${prefix}CreateMany))
549
369
  }
550
370
 
551
371
  if (config.enableAll || config.create) {
552
- const opConfig = config.create || defaultOpConfig
553
- const { before = [], after = [] } = opConfig
372
+ const opConfig: OperationConfigLike = (config.create as OperationConfigLike | undefined) ?? defaultOpConfig
554
373
  const path = basePath || '/'
555
- fastify.post(path, async (request, reply) => {
556
- try {
557
- makeShapeHook(config, opConfig)(request)
558
- if (await runHooks(before, request, reply)) return
559
- await ${prefix}Create(request, reply)
560
- if (await runHooks(after, request, reply)) return
561
- sendResult(request, reply)
562
- } catch (error: unknown) {
563
- sendError(reply, error)
564
- }
565
- })
374
+ fastify.post(path, handleWrite(opConfig, ${prefix}Create))
566
375
  }
567
376
 
568
377
  if (config.enableAll || config.updateManyAndReturn) {
569
- const opConfig = config.updateManyAndReturn || defaultOpConfig
570
- const { before = [], after = [] } = opConfig
378
+ const opConfig: OperationConfigLike = (config.updateManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
571
379
  const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
572
- fastify.put(path, async (request, reply) => {
573
- try {
574
- makeShapeHook(config, opConfig)(request)
575
- if (await runHooks(before, request, reply)) return
576
- await ${prefix}UpdateManyAndReturn(request, reply)
577
- if (await runHooks(after, request, reply)) return
578
- sendResult(request, reply)
579
- } catch (error: unknown) {
580
- sendError(reply, error)
581
- }
582
- })
380
+ fastify.put(path, handleWrite(opConfig, ${prefix}UpdateManyAndReturn))
583
381
  }
584
382
 
585
383
  if (config.enableAll || config.updateMany) {
586
- const opConfig = config.updateMany || defaultOpConfig
587
- const { before = [], after = [] } = opConfig
384
+ const opConfig: OperationConfigLike = (config.updateMany as OperationConfigLike | undefined) ?? defaultOpConfig
588
385
  const path = basePath ? \`\${basePath}/many\` : '/many'
589
- fastify.put(path, async (request, reply) => {
590
- try {
591
- makeShapeHook(config, opConfig)(request)
592
- if (await runHooks(before, request, reply)) return
593
- await ${prefix}UpdateMany(request, reply)
594
- if (await runHooks(after, request, reply)) return
595
- sendResult(request, reply)
596
- } catch (error: unknown) {
597
- sendError(reply, error)
598
- }
599
- })
386
+ fastify.put(path, handleWrite(opConfig, ${prefix}UpdateMany))
600
387
  }
601
388
 
602
389
  if (config.enableAll || config.update) {
603
- const opConfig = config.update || defaultOpConfig
604
- const { before = [], after = [] } = opConfig
390
+ const opConfig: OperationConfigLike = (config.update as OperationConfigLike | undefined) ?? defaultOpConfig
605
391
  const path = basePath || '/'
606
- fastify.put(path, async (request, reply) => {
607
- try {
608
- makeShapeHook(config, opConfig)(request)
609
- if (await runHooks(before, request, reply)) return
610
- await ${prefix}Update(request, reply)
611
- if (await runHooks(after, request, reply)) return
612
- sendResult(request, reply)
613
- } catch (error: unknown) {
614
- sendError(reply, error)
615
- }
616
- })
392
+ fastify.put(path, handleWrite(opConfig, ${prefix}Update))
617
393
  }
618
394
 
619
395
  if (config.enableAll || config.upsert) {
620
- const opConfig = config.upsert || defaultOpConfig
621
- const { before = [], after = [] } = opConfig
396
+ const opConfig: OperationConfigLike = (config.upsert as OperationConfigLike | undefined) ?? defaultOpConfig
622
397
  const path = basePath || '/'
623
- fastify.patch(path, async (request, reply) => {
624
- try {
625
- makeShapeHook(config, opConfig)(request)
626
- if (await runHooks(before, request, reply)) return
627
- await ${prefix}Upsert(request, reply)
628
- if (await runHooks(after, request, reply)) return
629
- sendResult(request, reply)
630
- } catch (error: unknown) {
631
- sendError(reply, error)
632
- }
633
- })
398
+ fastify.patch(path, handleWrite(opConfig, ${prefix}Upsert))
634
399
  }
635
400
 
636
401
  if (config.enableAll || config.deleteMany) {
637
- const opConfig = config.deleteMany || defaultOpConfig
638
- const { before = [], after = [] } = opConfig
402
+ const opConfig: OperationConfigLike = (config.deleteMany as OperationConfigLike | undefined) ?? defaultOpConfig
639
403
  const path = basePath ? \`\${basePath}/many\` : '/many'
640
- fastify.delete(path, async (request, reply) => {
641
- try {
642
- makeShapeHook(config, opConfig)(request)
643
- if (await runHooks(before, request, reply)) return
644
- await ${prefix}DeleteMany(request, reply)
645
- if (await runHooks(after, request, reply)) return
646
- sendResult(request, reply)
647
- } catch (error: unknown) {
648
- sendError(reply, error)
649
- }
650
- })
404
+ fastify.delete(path, handleWrite(opConfig, ${prefix}DeleteMany))
651
405
  }
652
406
 
653
407
  if (config.enableAll || config.delete) {
654
- const opConfig = config.delete || defaultOpConfig
655
- const { before = [], after = [] } = opConfig
408
+ const opConfig: OperationConfigLike = (config.delete as OperationConfigLike | undefined) ?? defaultOpConfig
656
409
  const path = basePath || '/'
657
- fastify.delete(path, async (request, reply) => {
658
- try {
659
- makeShapeHook(config, opConfig)(request)
660
- if (await runHooks(before, request, reply)) return
661
- await ${prefix}Delete(request, reply)
662
- if (await runHooks(after, request, reply)) return
663
- sendResult(request, reply)
664
- } catch (error: unknown) {
665
- sendError(reply, error)
666
- }
667
- })
410
+ fastify.delete(path, handleWrite(opConfig, ${prefix}Delete))
668
411
  }
669
412
  }
670
413
  `