prisma-generator-express 1.57.0 → 1.58.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 (41) hide show
  1. package/README.md +111 -29
  2. package/dist/copy/misc.js +25 -6
  3. package/dist/copy/misc.js.map +1 -1
  4. package/dist/generators/generateFastifyHandler.js +11 -0
  5. package/dist/generators/generateFastifyHandler.js.map +1 -1
  6. package/dist/generators/generateHonoHandler.js +14 -20
  7. package/dist/generators/generateHonoHandler.js.map +1 -1
  8. package/dist/generators/generateImportPrismaStatement.js +7 -1
  9. package/dist/generators/generateImportPrismaStatement.js.map +1 -1
  10. package/dist/generators/generateOperationCore.js +58 -17
  11. package/dist/generators/generateOperationCore.js.map +1 -1
  12. package/dist/generators/generateRouteConfigType.js +12 -7
  13. package/dist/generators/generateRouteConfigType.js.map +1 -1
  14. package/dist/generators/generateRouter.js +57 -32
  15. package/dist/generators/generateRouter.js.map +1 -1
  16. package/dist/generators/generateRouterFastify.js +235 -191
  17. package/dist/generators/generateRouterFastify.js.map +1 -1
  18. package/dist/generators/generateRouterHono.js +121 -87
  19. package/dist/generators/generateRouterHono.js.map +1 -1
  20. package/dist/index.js +4 -4
  21. package/dist/index.js.map +1 -1
  22. package/package.json +1 -1
  23. package/src/copy/autoIncludeRuntime.ts +9 -5
  24. package/src/copy/buildModelOpenApi.ts +96 -0
  25. package/src/copy/docsRenderer.ts +577 -174
  26. package/src/copy/materializedRouter.ts +40 -1
  27. package/src/copy/misc.ts +23 -6
  28. package/src/copy/operationDefinitions.ts +10 -0
  29. package/src/copy/operationRuntime.ts +28 -9
  30. package/src/copy/routeConfig.express.ts +9 -9
  31. package/src/copy/routeConfig.hono.ts +63 -5
  32. package/src/copy/routeConfig.ts +44 -22
  33. package/src/generators/generateFastifyHandler.ts +12 -0
  34. package/src/generators/generateHonoHandler.ts +15 -20
  35. package/src/generators/generateImportPrismaStatement.ts +10 -1
  36. package/src/generators/generateOperationCore.ts +58 -17
  37. package/src/generators/generateRouteConfigType.ts +13 -8
  38. package/src/generators/generateRouter.ts +57 -32
  39. package/src/generators/generateRouterFastify.ts +235 -191
  40. package/src/generators/generateRouterHono.ts +121 -87
  41. package/src/index.ts +6 -4
@@ -47,6 +47,7 @@ import {
47
47
  ${modelName}Aggregate,
48
48
  ${modelName}Count,
49
49
  ${modelName}GroupBy,
50
+ ${modelName}UpdateEach,
50
51
  } from './${modelName}Handlers${ext}'
51
52
  import type {
52
53
  RouteConfig,
@@ -58,6 +59,7 @@ import type {
58
59
  import { parseQueryParams } from '../parseQueryParams${ext}'
59
60
  import { sanitizeKeys, normalizePrefix, getEnv } from '../misc${ext}'
60
61
  import { buildModelOpenApi } from '../buildModelOpenApi${ext}'
62
+ import { validateCountSourceWhere } from '../routeConfig${ext}'
61
63
  import { mapError, transformResult, mergePaginationConfig, HttpError, type OperationContext } from '../operationRuntime${ext}'
62
64
 
63
65
  ${(0, generateRouteConfigType_1.generateRouteConfigType)(modelName, 'FastifyHookHandler', guardShapesImport, importStyle, 'fastify')}
@@ -171,6 +173,7 @@ function sendResult(request: FastifyRequest, reply: FastifyReply): void {
171
173
  }
172
174
 
173
175
  function sendError(reply: FastifyReply, error: unknown): void {
176
+ if (reply.sent) return
174
177
  const httpError = mapError(error)
175
178
  reply.code(httpError.status).send({ message: httpError.message })
176
179
  }
@@ -179,229 +182,270 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
179
182
  fastify: FastifyInstance,
180
183
  config: ${modelName}RouteConfig<TCtx, TPrisma> = {},
181
184
  ) {
182
- const customPrefix = normalizePrefix(config.customUrlPrefix || '')
183
- const modelPrefix = config.addModelPrefix !== false ? '/${modelNameLower}' : ''
184
- const basePath = customPrefix + modelPrefix
185
-
186
- const openApiDisabled = config.disableOpenApi === true
187
- || (config.disableOpenApi !== false && (
188
- _env.DISABLE_OPENAPI === 'true'
189
- || _env.NODE_ENV === 'production'
190
- ))
191
-
192
- const postReadsEnabled = !config.disablePostReads
193
-
194
- const openApiJsonSpec = openApiDisabled
195
- ? null
196
- : buildModelOpenApi(
197
- '${modelName}',
198
- MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
199
- MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
200
- config,
201
- { format: 'json', writeStrategy: WRITE_STRATEGY },
202
- )
203
- const openApiYamlSpec = openApiDisabled
204
- ? null
205
- : buildModelOpenApi(
206
- '${modelName}',
207
- MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
208
- MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
209
- config,
210
- { format: 'yaml', writeStrategy: WRITE_STRATEGY },
211
- )
212
-
213
- const qbEnabled = isQueryBuilderEnabled(config)
214
-
215
- if (qbEnabled) {
216
- const qbConfig = getQueryBuilderConfig(config)
217
- if (qbConfig) {
218
- try {
219
- startQueryBuilder(qbConfig)
220
- } catch (err) {
221
- if (_env.NODE_ENV !== 'production') console.warn('[query-builder]', err)
185
+ validateCountSourceWhere(config.pagination?.countSource, '${modelName} pagination')
186
+ validateCountSourceWhere(
187
+ (config.findManyPaginated && typeof config.findManyPaginated === 'object' ? config.findManyPaginated : undefined)?.pagination?.countSource,
188
+ '${modelName} findManyPaginated pagination',
189
+ )
190
+
191
+ await fastify.register(async (instance) => {
192
+ const isEnabled = (value: unknown): boolean => value !== false && !!(config.enableAll || value)
193
+
194
+ const customPrefix = normalizePrefix(config.customUrlPrefix || '')
195
+ const modelPrefix = config.addModelPrefix !== false ? '/${modelNameLower}' : ''
196
+ const basePath = customPrefix + modelPrefix
197
+
198
+ const openApiDisabled = config.disableOpenApi === true
199
+ || (config.disableOpenApi !== false && (
200
+ _env.NODE_ENV === 'production'
201
+ || _env.DISABLE_OPENAPI === 'true'
202
+ ))
203
+
204
+ const postReadsEnabled = !config.disablePostReads
205
+
206
+ let _openApiJsonCache: unknown = undefined
207
+ const getOpenApiJson = (): unknown => {
208
+ if (_openApiJsonCache === undefined) {
209
+ _openApiJsonCache = buildModelOpenApi(
210
+ '${modelName}',
211
+ MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
212
+ MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
213
+ config,
214
+ { format: 'json', writeStrategy: WRITE_STRATEGY },
215
+ )
222
216
  }
217
+ return _openApiJsonCache
218
+ }
219
+ let _openApiYamlCache: string | undefined = undefined
220
+ const getOpenApiYaml = (): string => {
221
+ if (_openApiYamlCache === undefined) {
222
+ _openApiYamlCache = buildModelOpenApi(
223
+ '${modelName}',
224
+ MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
225
+ MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
226
+ config,
227
+ { format: 'yaml', writeStrategy: WRITE_STRATEGY },
228
+ ) as string
229
+ }
230
+ return _openApiYamlCache
223
231
  }
224
- }
225
232
 
226
- fastify.addHook('onRequest', async (request: FastifyRequest) => {
227
- (request as FastifyExtended & { findManyPaginatedMode?: FindManyPaginatedMode }).findManyPaginatedMode = FIND_MANY_PAGINATED_MODE
228
- })
233
+ const qbEnabled = isQueryBuilderEnabled(config)
229
234
 
230
- fastify.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
231
- const e = error as { status?: number; statusCode?: number; message?: string }
232
- const status = e.status ?? e.statusCode ?? 500
233
- const message = error.message || 'Internal server error'
234
- if (!reply.sent) {
235
- reply.code(status).send({ message })
235
+ if (qbEnabled) {
236
+ const qbConfig = getQueryBuilderConfig(config)
237
+ if (qbConfig) {
238
+ try {
239
+ startQueryBuilder(qbConfig)
240
+ } catch (err) {
241
+ if (_env.NODE_ENV !== 'production') console.warn('[query-builder]', err)
242
+ }
243
+ }
236
244
  }
237
- })
238
-
239
- if (!openApiDisabled) {
240
- const openapiJsonPath = basePath ? \`\${basePath}/openapi.json\` : '/openapi.json'
241
- const openapiYamlPath = basePath ? \`\${basePath}/openapi.yaml\` : '/openapi.yaml'
242
245
 
243
- fastify.get(openapiJsonPath, async (_request, reply) => {
244
- return reply.send(openApiJsonSpec)
246
+ instance.addHook('onRequest', async (request: FastifyRequest) => {
247
+ (request as FastifyExtended & { findManyPaginatedMode?: FindManyPaginatedMode }).findManyPaginatedMode = FIND_MANY_PAGINATED_MODE
245
248
  })
246
249
 
247
- fastify.get(openapiYamlPath, async (_request, reply) => {
248
- return reply.type('application/yaml').send(openApiYamlSpec as string)
250
+ instance.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
251
+ const e = error as { status?: number; statusCode?: number; message?: string }
252
+ const status = e.status ?? e.statusCode ?? 500
253
+ const message = error.message || 'Internal server error'
254
+ if (!reply.sent) {
255
+ reply.code(status).send({ message })
256
+ }
249
257
  })
250
- }
251
258
 
252
- const handleGet = (
253
- opConfig: OperationConfigLike,
254
- handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
255
- parseFn: (req: FastifyRequest) => void,
256
- ) => async (request: FastifyRequest, reply: FastifyReply) => {
257
- try {
258
- parseFn(request)
259
- makeShapeHook(config, opConfig)(request)
260
- const { before = [], after = [] } = opConfig
261
- if (await runHooks(before, request, reply)) return
262
- await handlerFn(request, reply)
263
- if (await runHooks(after, request, reply)) return
264
- sendResult(request, reply)
265
- } catch (error: unknown) {
266
- sendError(reply, error)
259
+ if (!openApiDisabled) {
260
+ const openapiJsonPath = basePath ? \`\${basePath}/openapi.json\` : '/openapi.json'
261
+ const openapiYamlPath = basePath ? \`\${basePath}/openapi.yaml\` : '/openapi.yaml'
262
+
263
+ instance.get(openapiJsonPath, async (_request, reply) => {
264
+ return reply.send(getOpenApiJson())
265
+ })
266
+
267
+ instance.get(openapiYamlPath, async (_request, reply) => {
268
+ return reply.type('application/yaml').send(getOpenApiYaml())
269
+ })
267
270
  }
268
- }
269
271
 
270
- const handleWrite = (
271
- opConfig: OperationConfigLike,
272
- handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
273
- ) => async (request: FastifyRequest, reply: FastifyReply) => {
274
- try {
275
- makeShapeHook(config, opConfig)(request)
276
- const { before = [], after = [] } = opConfig
277
- if (await runHooks(before, request, reply)) return
278
- await handlerFn(request, reply)
279
- if (await runHooks(after, request, reply)) return
280
- sendResult(request, reply)
281
- } catch (error: unknown) {
282
- sendError(reply, error)
272
+ const handleGet = (
273
+ opConfig: OperationConfigLike,
274
+ handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
275
+ parseFn: (req: FastifyRequest) => void,
276
+ ) => async (request: FastifyRequest, reply: FastifyReply) => {
277
+ try {
278
+ parseFn(request)
279
+ makeShapeHook(config, opConfig)(request)
280
+ const { before = [], after = [] } = opConfig
281
+ if (await runHooks(before, request, reply)) return
282
+ await handlerFn(request, reply)
283
+ if (await runHooks(after, request, reply)) return
284
+ sendResult(request, reply)
285
+ } catch (error: unknown) {
286
+ sendError(reply, error)
287
+ }
283
288
  }
284
- }
285
289
 
286
- if (config.enableAll || config.findFirst) {
287
- const opConfig: OperationConfigLike = (config.findFirst as OperationConfigLike | undefined) ?? defaultOpConfig
288
- const path = basePath ? \`\${basePath}/first\` : '/first'
289
- fastify.get(path, handleGet(opConfig, ${modelName}FindFirst, parseQueryHook))
290
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindFirst, parseBodyAsQueryHook))
291
- }
290
+ const handleWrite = (
291
+ opConfig: OperationConfigLike,
292
+ handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
293
+ ) => async (request: FastifyRequest, reply: FastifyReply) => {
294
+ try {
295
+ makeShapeHook(config, opConfig)(request)
296
+ const { before = [], after = [] } = opConfig
297
+ if (await runHooks(before, request, reply)) return
298
+ await handlerFn(request, reply)
299
+ if (await runHooks(after, request, reply)) return
300
+ sendResult(request, reply)
301
+ } catch (error: unknown) {
302
+ sendError(reply, error)
303
+ }
304
+ }
292
305
 
293
- if (config.enableAll || config.findFirstOrThrow) {
294
- const opConfig: OperationConfigLike = (config.findFirstOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
295
- const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
296
- fastify.get(path, handleGet(opConfig, ${modelName}FindFirstOrThrow, parseQueryHook))
297
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindFirstOrThrow, parseBodyAsQueryHook))
298
- }
306
+ if (isEnabled(config.findFirst)) {
307
+ const opConfig: OperationConfigLike = (config.findFirst as OperationConfigLike | undefined) ?? defaultOpConfig
308
+ const path = basePath ? \`\${basePath}/first\` : '/first'
309
+ instance.get(path, handleGet(opConfig, ${modelName}FindFirst, parseQueryHook))
310
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}FindFirst, parseBodyAsQueryHook))
311
+ }
299
312
 
300
- if (config.enableAll || config.findManyPaginated) {
301
- const opConfig: OperationConfigLike = (config.findManyPaginated as OperationConfigLike | undefined) ?? defaultOpConfig
302
- const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
303
- fastify.get(path, handleGet(opConfig, ${modelName}FindManyPaginated, parseQueryHook))
304
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindManyPaginated, parseBodyAsQueryHook))
305
- }
313
+ if (isEnabled(config.findFirstOrThrow)) {
314
+ const opConfig: OperationConfigLike = (config.findFirstOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
315
+ const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
316
+ instance.get(path, handleGet(opConfig, ${modelName}FindFirstOrThrow, parseQueryHook))
317
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}FindFirstOrThrow, parseBodyAsQueryHook))
318
+ }
306
319
 
307
- if (config.enableAll || config.aggregate) {
308
- const opConfig: OperationConfigLike = (config.aggregate as OperationConfigLike | undefined) ?? defaultOpConfig
309
- const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
310
- fastify.get(path, handleGet(opConfig, ${modelName}Aggregate, parseQueryHook))
311
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}Aggregate, parseBodyAsQueryHook))
312
- }
320
+ if (isEnabled(config.findManyPaginated)) {
321
+ const opConfig: OperationConfigLike = (config.findManyPaginated as OperationConfigLike | undefined) ?? defaultOpConfig
322
+ const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
323
+ instance.get(path, handleGet(opConfig, ${modelName}FindManyPaginated, parseQueryHook))
324
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}FindManyPaginated, parseBodyAsQueryHook))
325
+ }
313
326
 
314
- if (config.enableAll || config.count) {
315
- const opConfig: OperationConfigLike = (config.count as OperationConfigLike | undefined) ?? defaultOpConfig
316
- const path = basePath ? \`\${basePath}/count\` : '/count'
317
- fastify.get(path, handleGet(opConfig, ${modelName}Count, parseQueryHook))
318
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}Count, parseBodyAsQueryHook))
319
- }
327
+ if (isEnabled(config.aggregate)) {
328
+ const opConfig: OperationConfigLike = (config.aggregate as OperationConfigLike | undefined) ?? defaultOpConfig
329
+ const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
330
+ instance.get(path, handleGet(opConfig, ${modelName}Aggregate, parseQueryHook))
331
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}Aggregate, parseBodyAsQueryHook))
332
+ }
320
333
 
321
- if (config.enableAll || config.groupBy) {
322
- const opConfig: OperationConfigLike = (config.groupBy as OperationConfigLike | undefined) ?? defaultOpConfig
323
- const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
324
- fastify.get(path, handleGet(opConfig, ${modelName}GroupBy, parseQueryHook))
325
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}GroupBy, parseBodyAsQueryHook))
326
- }
334
+ if (isEnabled(config.count)) {
335
+ const opConfig: OperationConfigLike = (config.count as OperationConfigLike | undefined) ?? defaultOpConfig
336
+ const path = basePath ? \`\${basePath}/count\` : '/count'
337
+ instance.get(path, handleGet(opConfig, ${modelName}Count, parseQueryHook))
338
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}Count, parseBodyAsQueryHook))
339
+ }
327
340
 
328
- if (config.enableAll || config.findUniqueOrThrow) {
329
- const opConfig: OperationConfigLike = (config.findUniqueOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
330
- const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
331
- fastify.get(path, handleGet(opConfig, ${modelName}FindUniqueOrThrow, parseQueryHook))
332
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindUniqueOrThrow, parseBodyAsQueryHook))
333
- }
341
+ if (isEnabled(config.groupBy)) {
342
+ const opConfig: OperationConfigLike = (config.groupBy as OperationConfigLike | undefined) ?? defaultOpConfig
343
+ const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
344
+ instance.get(path, handleGet(opConfig, ${modelName}GroupBy, parseQueryHook))
345
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}GroupBy, parseBodyAsQueryHook))
346
+ }
334
347
 
335
- if (config.enableAll || config.findUnique) {
336
- const opConfig: OperationConfigLike = (config.findUnique as OperationConfigLike | undefined) ?? defaultOpConfig
337
- const path = basePath ? \`\${basePath}/unique\` : '/unique'
338
- fastify.get(path, handleGet(opConfig, ${modelName}FindUnique, parseQueryHook))
339
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindUnique, parseBodyAsQueryHook))
340
- }
348
+ if (isEnabled(config.findUniqueOrThrow)) {
349
+ const opConfig: OperationConfigLike = (config.findUniqueOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
350
+ const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
351
+ instance.get(path, handleGet(opConfig, ${modelName}FindUniqueOrThrow, parseQueryHook))
352
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}FindUniqueOrThrow, parseBodyAsQueryHook))
353
+ }
341
354
 
342
- if (config.enableAll || config.findMany) {
343
- const opConfig: OperationConfigLike = (config.findMany as OperationConfigLike | undefined) ?? defaultOpConfig
344
- const path = basePath || '/'
345
- fastify.get(path, handleGet(opConfig, ${modelName}FindMany, parseQueryHook))
346
- if (postReadsEnabled) {
347
- const postPath = basePath ? \`\${basePath}/read\` : '/read'
348
- fastify.post(postPath, handleGet(opConfig, ${modelName}FindMany, parseBodyAsQueryHook))
355
+ if (isEnabled(config.findUnique)) {
356
+ const opConfig: OperationConfigLike = (config.findUnique as OperationConfigLike | undefined) ?? defaultOpConfig
357
+ const path = basePath ? \`\${basePath}/unique\` : '/unique'
358
+ instance.get(path, handleGet(opConfig, ${modelName}FindUnique, parseQueryHook))
359
+ if (postReadsEnabled) instance.post(path, handleGet(opConfig, ${modelName}FindUnique, parseBodyAsQueryHook))
349
360
  }
350
- }
351
361
 
352
- if (config.enableAll || config.createManyAndReturn) {
353
- const opConfig: OperationConfigLike = (config.createManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
354
- const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
355
- fastify.post(path, handleWrite(opConfig, ${modelName}CreateManyAndReturn))
356
- }
362
+ if (isEnabled(config.findMany)) {
363
+ const opConfig: OperationConfigLike = (config.findMany as OperationConfigLike | undefined) ?? defaultOpConfig
364
+ const path = basePath || '/'
365
+ instance.get(path, handleGet(opConfig, ${modelName}FindMany, parseQueryHook))
366
+ if (postReadsEnabled) {
367
+ const postPath = basePath ? \`\${basePath}/read\` : '/read'
368
+ instance.post(postPath, handleGet(opConfig, ${modelName}FindMany, parseBodyAsQueryHook))
369
+ }
370
+ }
357
371
 
358
- if (config.enableAll || config.createMany) {
359
- const opConfig: OperationConfigLike = (config.createMany as OperationConfigLike | undefined) ?? defaultOpConfig
360
- const path = basePath ? \`\${basePath}/many\` : '/many'
361
- fastify.post(path, handleWrite(opConfig, ${modelName}CreateMany))
362
- }
372
+ if (isEnabled(config.createManyAndReturn)) {
373
+ const opConfig: OperationConfigLike = (config.createManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
374
+ const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
375
+ instance.post(path, handleWrite(opConfig, ${modelName}CreateManyAndReturn))
376
+ }
363
377
 
364
- if (config.enableAll || config.create) {
365
- const opConfig: OperationConfigLike = (config.create as OperationConfigLike | undefined) ?? defaultOpConfig
366
- const path = basePath || '/'
367
- fastify.post(path, handleWrite(opConfig, ${modelName}Create))
368
- }
378
+ if (isEnabled(config.createMany)) {
379
+ const opConfig: OperationConfigLike = (config.createMany as OperationConfigLike | undefined) ?? defaultOpConfig
380
+ const path = basePath ? \`\${basePath}/many\` : '/many'
381
+ instance.post(path, handleWrite(opConfig, ${modelName}CreateMany))
382
+ }
369
383
 
370
- if (config.enableAll || config.updateManyAndReturn) {
371
- const opConfig: OperationConfigLike = (config.updateManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
372
- const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
373
- fastify.put(path, handleWrite(opConfig, ${modelName}UpdateManyAndReturn))
374
- }
384
+ if (isEnabled(config.create)) {
385
+ const opConfig: OperationConfigLike = (config.create as OperationConfigLike | undefined) ?? defaultOpConfig
386
+ const path = basePath || '/'
387
+ instance.post(path, handleWrite(opConfig, ${modelName}Create))
388
+ }
375
389
 
376
- if (config.enableAll || config.updateMany) {
377
- const opConfig: OperationConfigLike = (config.updateMany as OperationConfigLike | undefined) ?? defaultOpConfig
378
- const path = basePath ? \`\${basePath}/many\` : '/many'
379
- fastify.put(path, handleWrite(opConfig, ${modelName}UpdateMany))
380
- }
390
+ if (isEnabled(config.updateManyAndReturn)) {
391
+ const opConfig: OperationConfigLike = (config.updateManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
392
+ const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
393
+ instance.put(path, handleWrite(opConfig, ${modelName}UpdateManyAndReturn))
394
+ }
381
395
 
382
- if (config.enableAll || config.update) {
383
- const opConfig: OperationConfigLike = (config.update as OperationConfigLike | undefined) ?? defaultOpConfig
384
- const path = basePath || '/'
385
- fastify.put(path, handleWrite(opConfig, ${modelName}Update))
386
- }
396
+ if (isEnabled(config.updateMany)) {
397
+ const opConfig: OperationConfigLike = (config.updateMany as OperationConfigLike | undefined) ?? defaultOpConfig
398
+ const path = basePath ? \`\${basePath}/many\` : '/many'
399
+ instance.put(path, handleWrite(opConfig, ${modelName}UpdateMany))
400
+ }
387
401
 
388
- if (config.enableAll || config.upsert) {
389
- const opConfig: OperationConfigLike = (config.upsert as OperationConfigLike | undefined) ?? defaultOpConfig
390
- const path = basePath || '/'
391
- fastify.patch(path, handleWrite(opConfig, ${modelName}Upsert))
392
- }
402
+ if (isEnabled(config.update)) {
403
+ const opConfig: OperationConfigLike = (config.update as OperationConfigLike | undefined) ?? defaultOpConfig
404
+ const path = basePath || '/'
405
+ instance.put(path, handleWrite(opConfig, ${modelName}Update))
406
+ }
393
407
 
394
- if (config.enableAll || config.deleteMany) {
395
- const opConfig: OperationConfigLike = (config.deleteMany as OperationConfigLike | undefined) ?? defaultOpConfig
396
- const path = basePath ? \`\${basePath}/many\` : '/many'
397
- fastify.delete(path, handleWrite(opConfig, ${modelName}DeleteMany))
398
- }
408
+ if (isEnabled(config.upsert)) {
409
+ const opConfig: OperationConfigLike = (config.upsert as OperationConfigLike | undefined) ?? defaultOpConfig
410
+ const path = basePath || '/'
411
+ instance.patch(path, handleWrite(opConfig, ${modelName}Upsert))
412
+ }
399
413
 
400
- if (config.enableAll || config.delete) {
401
- const opConfig: OperationConfigLike = (config.delete as OperationConfigLike | undefined) ?? defaultOpConfig
402
- const path = basePath || '/'
403
- fastify.delete(path, handleWrite(opConfig, ${modelName}Delete))
404
- }
414
+ if (isEnabled(config.deleteMany)) {
415
+ const opConfig: OperationConfigLike = (config.deleteMany as OperationConfigLike | undefined) ?? defaultOpConfig
416
+ const path = basePath ? \`\${basePath}/many\` : '/many'
417
+ instance.delete(path, handleWrite(opConfig, ${modelName}DeleteMany))
418
+ }
419
+
420
+ if (isEnabled(config.delete)) {
421
+ const opConfig: OperationConfigLike = (config.delete as OperationConfigLike | undefined) ?? defaultOpConfig
422
+ const path = basePath || '/'
423
+ instance.delete(path, handleWrite(opConfig, ${modelName}Delete))
424
+ }
425
+
426
+ if (config.updateEach) {
427
+ const opConfig: OperationConfigLike = (config.updateEach as OperationConfigLike | undefined) ?? defaultOpConfig
428
+ if ((!opConfig.before || opConfig.before.length === 0) && _env.NODE_ENV !== 'production') {
429
+ console.warn(
430
+ '[${modelName}Router] updateEach is enabled without a before hook. ' +
431
+ 'This endpoint bypasses guard shapes and should be protected by authentication middleware.',
432
+ )
433
+ }
434
+ const path = basePath ? \`\${basePath}/each\` : '/each'
435
+ instance.post(path, async (request: FastifyRequest, reply: FastifyReply) => {
436
+ try {
437
+ makeShapeHook(config, opConfig)(request)
438
+ const { before = [], after = [] } = opConfig
439
+ if (await runHooks(before, request, reply)) return
440
+ await ${modelName}UpdateEach(request, reply)
441
+ if (await runHooks(after, request, reply)) return
442
+ sendResult(request, reply)
443
+ } catch (error: unknown) {
444
+ sendError(reply, error)
445
+ }
446
+ })
447
+ }
448
+ })
405
449
  }
406
450
  `;
407
451
  }
@@ -1 +1 @@
1
- {"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;AAMA,sEAuaC;AA5aD,uEAAmE;AAEnE,kDAA8C;AAG9C,SAAgB,6BAA6B,CAAC,EAC5C,KAAK,EACL,KAAK,EACL,iBAAiB,EACjB,WAAW,EACX,aAAa,EACb,qBAAqB,EACrB,SAAS,GASV;IACC,MAAM,GAAG,GAAG,IAAA,qBAAS,EAAC,WAAW,CAAC,CAAA;IAClC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,cAAc,GAAG,SAAS,CAAC,WAAW,EAAE,CAAA;IAC9C,MAAM,kBAAkB,GAAG,GAAG,SAAS,QAAQ,CAAA;IAE/C,MAAM,UAAU,GAAG,KAAK,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