prisma-generator-express 1.35.0 → 1.36.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.
@@ -59,8 +59,9 @@ import {
59
59
  } from './${modelName}Handlers'
60
60
  import type { RouteConfig, FastifyHookHandler } from '../routeConfig.target'
61
61
  import { parseQueryParams } from '../parseQueryParams'
62
+ import { sanitizeKeys } from '../misc'
62
63
  import { buildModelOpenApi } from '../buildModelOpenApi'
63
- import { mapError, transformResult } from '../operationRuntime'
64
+ import { mapError, transformResult, HttpError } from '../operationRuntime'
64
65
 
65
66
  const _env = typeof process !== 'undefined' && process.env ? process.env : {} as Record<string, string | undefined>
66
67
 
@@ -102,6 +103,14 @@ function parseQueryHook(request: FastifyRequest): void {
102
103
  }
103
104
  }
104
105
 
106
+ function parseBodyAsQueryHook(request: FastifyRequest): void {
107
+ const body = request.body
108
+ if (!body || typeof body !== 'object' || Array.isArray(body)) {
109
+ throw new HttpError(400, 'Request body must be a JSON object')
110
+ }
111
+ ;(request as any).parsedQuery = sanitizeKeys(body as Record<string, unknown>)
112
+ }
113
+
105
114
  function makeShapeHook(config: RouteConfig, opConfig: any): (request: FastifyRequest) => void {
106
115
  return (request: FastifyRequest) => {
107
116
  ;(request as any).routeConfig = config
@@ -161,6 +170,8 @@ export async function ${routerFunctionName}(
161
170
  || _env.NODE_ENV === 'production'
162
171
  ))
163
172
 
173
+ const postReadsEnabled = !config.disablePostReads
174
+
164
175
  const qbEnabled = isQueryBuilderEnabled(config)
165
176
 
166
177
  if (qbEnabled) {
@@ -221,6 +232,20 @@ export async function ${routerFunctionName}(
221
232
  sendError(reply, error)
222
233
  }
223
234
  })
235
+ if (postReadsEnabled) {
236
+ fastify.post(path, async (request, reply) => {
237
+ try {
238
+ parseBodyAsQueryHook(request)
239
+ makeShapeHook(config, opConfig)(request)
240
+ if (await runHooks(before, request, reply)) return
241
+ await ${prefix}FindFirst(request, reply)
242
+ if (await runHooks(after, request, reply)) return
243
+ sendResult(request, reply)
244
+ } catch (error: unknown) {
245
+ sendError(reply, error)
246
+ }
247
+ })
248
+ }
224
249
  }
225
250
 
226
251
  if (config.enableAll || config.findFirstOrThrow) {
@@ -239,6 +264,20 @@ export async function ${routerFunctionName}(
239
264
  sendError(reply, error)
240
265
  }
241
266
  })
267
+ if (postReadsEnabled) {
268
+ fastify.post(path, async (request, reply) => {
269
+ try {
270
+ parseBodyAsQueryHook(request)
271
+ makeShapeHook(config, opConfig)(request)
272
+ if (await runHooks(before, request, reply)) return
273
+ await ${prefix}FindFirstOrThrow(request, reply)
274
+ if (await runHooks(after, request, reply)) return
275
+ sendResult(request, reply)
276
+ } catch (error: unknown) {
277
+ sendError(reply, error)
278
+ }
279
+ })
280
+ }
242
281
  }
243
282
 
244
283
  if (config.enableAll || config.findManyPaginated) {
@@ -257,6 +296,20 @@ export async function ${routerFunctionName}(
257
296
  sendError(reply, error)
258
297
  }
259
298
  })
299
+ if (postReadsEnabled) {
300
+ fastify.post(path, async (request, reply) => {
301
+ try {
302
+ parseBodyAsQueryHook(request)
303
+ makeShapeHook(config, opConfig)(request)
304
+ if (await runHooks(before, request, reply)) return
305
+ await ${prefix}FindManyPaginated(request, reply)
306
+ if (await runHooks(after, request, reply)) return
307
+ sendResult(request, reply)
308
+ } catch (error: unknown) {
309
+ sendError(reply, error)
310
+ }
311
+ })
312
+ }
260
313
  }
261
314
 
262
315
  if (config.enableAll || config.aggregate) {
@@ -275,6 +328,20 @@ export async function ${routerFunctionName}(
275
328
  sendError(reply, error)
276
329
  }
277
330
  })
331
+ if (postReadsEnabled) {
332
+ fastify.post(path, async (request, reply) => {
333
+ try {
334
+ parseBodyAsQueryHook(request)
335
+ makeShapeHook(config, opConfig)(request)
336
+ if (await runHooks(before, request, reply)) return
337
+ await ${prefix}Aggregate(request, reply)
338
+ if (await runHooks(after, request, reply)) return
339
+ sendResult(request, reply)
340
+ } catch (error: unknown) {
341
+ sendError(reply, error)
342
+ }
343
+ })
344
+ }
278
345
  }
279
346
 
280
347
  if (config.enableAll || config.count) {
@@ -293,6 +360,20 @@ export async function ${routerFunctionName}(
293
360
  sendError(reply, error)
294
361
  }
295
362
  })
363
+ if (postReadsEnabled) {
364
+ fastify.post(path, async (request, reply) => {
365
+ try {
366
+ parseBodyAsQueryHook(request)
367
+ makeShapeHook(config, opConfig)(request)
368
+ if (await runHooks(before, request, reply)) return
369
+ await ${prefix}Count(request, reply)
370
+ if (await runHooks(after, request, reply)) return
371
+ sendResult(request, reply)
372
+ } catch (error: unknown) {
373
+ sendError(reply, error)
374
+ }
375
+ })
376
+ }
296
377
  }
297
378
 
298
379
  if (config.enableAll || config.groupBy) {
@@ -311,6 +392,20 @@ export async function ${routerFunctionName}(
311
392
  sendError(reply, error)
312
393
  }
313
394
  })
395
+ if (postReadsEnabled) {
396
+ fastify.post(path, async (request, reply) => {
397
+ try {
398
+ parseBodyAsQueryHook(request)
399
+ makeShapeHook(config, opConfig)(request)
400
+ if (await runHooks(before, request, reply)) return
401
+ await ${prefix}GroupBy(request, reply)
402
+ if (await runHooks(after, request, reply)) return
403
+ sendResult(request, reply)
404
+ } catch (error: unknown) {
405
+ sendError(reply, error)
406
+ }
407
+ })
408
+ }
314
409
  }
315
410
 
316
411
  if (config.enableAll || config.findUniqueOrThrow) {
@@ -329,6 +424,20 @@ export async function ${routerFunctionName}(
329
424
  sendError(reply, error)
330
425
  }
331
426
  })
427
+ if (postReadsEnabled) {
428
+ fastify.post(path, async (request, reply) => {
429
+ try {
430
+ parseBodyAsQueryHook(request)
431
+ makeShapeHook(config, opConfig)(request)
432
+ if (await runHooks(before, request, reply)) return
433
+ await ${prefix}FindUniqueOrThrow(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
+ }
332
441
  }
333
442
 
334
443
  if (config.enableAll || config.findUnique) {
@@ -347,6 +456,20 @@ export async function ${routerFunctionName}(
347
456
  sendError(reply, error)
348
457
  }
349
458
  })
459
+ if (postReadsEnabled) {
460
+ fastify.post(path, async (request, reply) => {
461
+ try {
462
+ parseBodyAsQueryHook(request)
463
+ makeShapeHook(config, opConfig)(request)
464
+ if (await runHooks(before, request, reply)) return
465
+ await ${prefix}FindUnique(request, reply)
466
+ if (await runHooks(after, request, reply)) return
467
+ sendResult(request, reply)
468
+ } catch (error: unknown) {
469
+ sendError(reply, error)
470
+ }
471
+ })
472
+ }
350
473
  }
351
474
 
352
475
  if (config.enableAll || config.findMany) {
@@ -365,6 +488,21 @@ export async function ${routerFunctionName}(
365
488
  sendError(reply, error)
366
489
  }
367
490
  })
491
+ if (postReadsEnabled) {
492
+ const postPath = basePath ? \`\${basePath}/read\` : '/read'
493
+ fastify.post(postPath, async (request, reply) => {
494
+ try {
495
+ parseBodyAsQueryHook(request)
496
+ makeShapeHook(config, opConfig)(request)
497
+ if (await runHooks(before, request, reply)) return
498
+ await ${prefix}FindMany(request, reply)
499
+ if (await runHooks(after, request, reply)) return
500
+ sendResult(request, reply)
501
+ } catch (error: unknown) {
502
+ sendError(reply, error)
503
+ }
504
+ })
505
+ }
368
506
  }
369
507
 
370
508
  if (config.enableAll || config.createManyAndReturn) {