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