prisma-generator-express 1.34.4 → 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.
@@ -62,6 +62,7 @@ import {
62
62
  } from './${modelName}Handlers'
63
63
  import type { RouteConfig } from '../routeConfig.target'
64
64
  import { parseQueryParams } from '../parseQueryParams'
65
+ import { sanitizeKeys } from '../misc'
65
66
  import { buildModelOpenApi } from '../buildModelOpenApi'
66
67
  import { transformResult } from '../operationRuntime'
67
68
 
@@ -113,6 +114,8 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
113
114
  || _env.NODE_ENV === 'production'
114
115
  ))
115
116
 
117
+ const postReadsEnabled = !config.disablePostReads
118
+
116
119
  const qbEnabled = isQueryBuilderEnabled(config)
117
120
 
118
121
  if (qbEnabled) {
@@ -130,6 +133,14 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
130
133
  next()
131
134
  }
132
135
 
136
+ const parseBodyAsQuery: RequestHandler = (req, res, next) => {
137
+ if (!req.body || typeof req.body !== 'object' || Array.isArray(req.body)) {
138
+ return next({ status: 400, message: 'Request body must be a JSON object' })
139
+ }
140
+ res.locals.parsedQuery = sanitizeKeys(req.body)
141
+ next()
142
+ }
143
+
133
144
  const setShape = (opConfig: any): RequestHandler => {
134
145
  return (req, res, next) => {
135
146
  res.locals.routeConfig = config
@@ -194,6 +205,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
194
205
  const { before = [], after = [] } = opConfig
195
206
  const path = basePath ? \`\${basePath}/first\` : '/first'
196
207
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindFirst as RequestHandler, ...after, respond)
208
+ if (postReadsEnabled) {
209
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}FindFirst as RequestHandler, ...after, respond)
210
+ }
197
211
  }
198
212
 
199
213
  if (config.enableAll || config.findFirstOrThrow) {
@@ -201,6 +215,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
201
215
  const { before = [], after = [] } = opConfig
202
216
  const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
203
217
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindFirstOrThrow as RequestHandler, ...after, respond)
218
+ if (postReadsEnabled) {
219
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}FindFirstOrThrow as RequestHandler, ...after, respond)
220
+ }
204
221
  }
205
222
 
206
223
  if (config.enableAll || config.findManyPaginated) {
@@ -208,6 +225,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
208
225
  const { before = [], after = [] } = opConfig
209
226
  const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
210
227
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindManyPaginated as RequestHandler, ...after, respond)
228
+ if (postReadsEnabled) {
229
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}FindManyPaginated as RequestHandler, ...after, respond)
230
+ }
211
231
  }
212
232
 
213
233
  if (config.enableAll || config.aggregate) {
@@ -215,6 +235,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
215
235
  const { before = [], after = [] } = opConfig
216
236
  const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
217
237
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}Aggregate as RequestHandler, ...after, respond)
238
+ if (postReadsEnabled) {
239
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}Aggregate as RequestHandler, ...after, respond)
240
+ }
218
241
  }
219
242
 
220
243
  if (config.enableAll || config.count) {
@@ -222,6 +245,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
222
245
  const { before = [], after = [] } = opConfig
223
246
  const path = basePath ? \`\${basePath}/count\` : '/count'
224
247
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}Count as RequestHandler, ...after, respond)
248
+ if (postReadsEnabled) {
249
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}Count as RequestHandler, ...after, respond)
250
+ }
225
251
  }
226
252
 
227
253
  if (config.enableAll || config.groupBy) {
@@ -229,6 +255,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
229
255
  const { before = [], after = [] } = opConfig
230
256
  const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
231
257
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}GroupBy as RequestHandler, ...after, respond)
258
+ if (postReadsEnabled) {
259
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}GroupBy as RequestHandler, ...after, respond)
260
+ }
232
261
  }
233
262
 
234
263
  if (config.enableAll || config.findUniqueOrThrow) {
@@ -236,6 +265,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
236
265
  const { before = [], after = [] } = opConfig
237
266
  const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
238
267
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindUniqueOrThrow as RequestHandler, ...after, respond)
268
+ if (postReadsEnabled) {
269
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}FindUniqueOrThrow as RequestHandler, ...after, respond)
270
+ }
239
271
  }
240
272
 
241
273
  if (config.enableAll || config.findUnique) {
@@ -243,6 +275,9 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
243
275
  const { before = [], after = [] } = opConfig
244
276
  const path = basePath ? \`\${basePath}/unique\` : '/unique'
245
277
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindUnique as RequestHandler, ...after, respond)
278
+ if (postReadsEnabled) {
279
+ router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}FindUnique as RequestHandler, ...after, respond)
280
+ }
246
281
  }
247
282
 
248
283
  if (config.enableAll || config.findMany) {
@@ -250,6 +285,10 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
250
285
  const { before = [], after = [] } = opConfig
251
286
  const path = basePath || '/'
252
287
  router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindMany as RequestHandler, ...after, respond)
288
+ if (postReadsEnabled) {
289
+ const postPath = basePath ? \`\${basePath}/read\` : '/read'
290
+ router.post(postPath, parseBodyAsQuery, setShape(opConfig), ...before, ${prefix}FindMany as RequestHandler, ...after, respond)
291
+ }
253
292
  }
254
293
 
255
294
  if (config.enableAll || config.createManyAndReturn) {
@@ -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) {