prisma-generator-express 1.56.4 → 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 (44) hide show
  1. package/README.md +286 -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 +43 -0
  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 +44 -15
  13. package/dist/generators/generateRouteConfigType.js.map +1 -1
  14. package/dist/generators/generateRouter.d.ts +2 -1
  15. package/dist/generators/generateRouter.js +60 -34
  16. package/dist/generators/generateRouter.js.map +1 -1
  17. package/dist/generators/generateRouterFastify.d.ts +2 -1
  18. package/dist/generators/generateRouterFastify.js +238 -193
  19. package/dist/generators/generateRouterFastify.js.map +1 -1
  20. package/dist/generators/generateRouterHono.d.ts +2 -1
  21. package/dist/generators/generateRouterHono.js +124 -89
  22. package/dist/generators/generateRouterHono.js.map +1 -1
  23. package/dist/index.js +22 -4
  24. package/dist/index.js.map +1 -1
  25. package/package.json +1 -1
  26. package/src/copy/autoIncludeRuntime.ts +9 -5
  27. package/src/copy/buildModelOpenApi.ts +96 -0
  28. package/src/copy/docsRenderer.ts +577 -174
  29. package/src/copy/materializedRouter.ts +40 -1
  30. package/src/copy/misc.ts +23 -6
  31. package/src/copy/operationDefinitions.ts +10 -0
  32. package/src/copy/operationRuntime.ts +28 -9
  33. package/src/copy/routeConfig.express.ts +9 -9
  34. package/src/copy/routeConfig.hono.ts +63 -5
  35. package/src/copy/routeConfig.ts +44 -20
  36. package/src/generators/generateFastifyHandler.ts +12 -0
  37. package/src/generators/generateHonoHandler.ts +15 -20
  38. package/src/generators/generateImportPrismaStatement.ts +13 -0
  39. package/src/generators/generateOperationCore.ts +58 -17
  40. package/src/generators/generateRouteConfigType.ts +52 -17
  41. package/src/generators/generateRouter.ts +61 -33
  42. package/src/generators/generateRouterFastify.ts +239 -192
  43. package/src/generators/generateRouterHono.ts +125 -88
  44. package/src/index.ts +25 -5
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateFastifyRouterFunction = generateFastifyRouterFunction;
4
4
  const generateRouteConfigType_1 = require("./generateRouteConfigType");
5
5
  const importExt_1 = require("../utils/importExt");
6
- function generateFastifyRouterFunction({ model, enums, guardShapesImport, importStyle, writeStrategy, findManyPaginatedMode, }) {
6
+ function generateFastifyRouterFunction({ model, enums, guardShapesImport, importStyle, writeStrategy, findManyPaginatedMode, dropGuard, }) {
7
7
  const ext = (0, importExt_1.importExt)(importStyle);
8
8
  const modelName = model.name;
9
9
  const modelNameLower = modelName.toLowerCase();
@@ -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')}
@@ -65,6 +67,7 @@ const _env = getEnv()
65
67
 
66
68
  const WRITE_STRATEGY: WriteStrategy = '${writeStrategy}'
67
69
  const FIND_MANY_PAGINATED_MODE: FindManyPaginatedMode = '${findManyPaginatedMode}'
70
+ const DROP_GUARD = ${dropGuard} || _env.E2E === 'true'
68
71
 
69
72
  const MODEL_FIELDS = ${JSON.stringify(fieldsMeta, null, 2)} as const
70
73
 
@@ -140,7 +143,7 @@ function makeShapeHook(
140
143
  if (caller) {
141
144
  fx.guardCaller = caller
142
145
  }
143
- if (opConfig.shape) {
146
+ if (opConfig.shape && !DROP_GUARD) {
144
147
  fx.guardShape = opConfig.shape
145
148
  }
146
149
  }
@@ -170,6 +173,7 @@ function sendResult(request: FastifyRequest, reply: FastifyReply): void {
170
173
  }
171
174
 
172
175
  function sendError(reply: FastifyReply, error: unknown): void {
176
+ if (reply.sent) return
173
177
  const httpError = mapError(error)
174
178
  reply.code(httpError.status).send({ message: httpError.message })
175
179
  }
@@ -178,229 +182,270 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
178
182
  fastify: FastifyInstance,
179
183
  config: ${modelName}RouteConfig<TCtx, TPrisma> = {},
180
184
  ) {
181
- const customPrefix = normalizePrefix(config.customUrlPrefix || '')
182
- const modelPrefix = config.addModelPrefix !== false ? '/${modelNameLower}' : ''
183
- const basePath = customPrefix + modelPrefix
184
-
185
- const openApiDisabled = config.disableOpenApi === true
186
- || (config.disableOpenApi !== false && (
187
- _env.DISABLE_OPENAPI === 'true'
188
- || _env.NODE_ENV === 'production'
189
- ))
190
-
191
- const postReadsEnabled = !config.disablePostReads
192
-
193
- const openApiJsonSpec = openApiDisabled
194
- ? null
195
- : buildModelOpenApi(
196
- '${modelName}',
197
- MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
198
- MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
199
- config,
200
- { format: 'json', writeStrategy: WRITE_STRATEGY },
201
- )
202
- const openApiYamlSpec = openApiDisabled
203
- ? null
204
- : buildModelOpenApi(
205
- '${modelName}',
206
- MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
207
- MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
208
- config,
209
- { format: 'yaml', writeStrategy: WRITE_STRATEGY },
210
- )
211
-
212
- const qbEnabled = isQueryBuilderEnabled(config)
213
-
214
- if (qbEnabled) {
215
- const qbConfig = getQueryBuilderConfig(config)
216
- if (qbConfig) {
217
- try {
218
- startQueryBuilder(qbConfig)
219
- } catch (err) {
220
- 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
+ )
221
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
222
231
  }
223
- }
224
232
 
225
- fastify.addHook('onRequest', async (request: FastifyRequest) => {
226
- (request as FastifyExtended & { findManyPaginatedMode?: FindManyPaginatedMode }).findManyPaginatedMode = FIND_MANY_PAGINATED_MODE
227
- })
233
+ const qbEnabled = isQueryBuilderEnabled(config)
228
234
 
229
- fastify.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
230
- const e = error as { status?: number; statusCode?: number; message?: string }
231
- const status = e.status ?? e.statusCode ?? 500
232
- const message = error.message || 'Internal server error'
233
- if (!reply.sent) {
234
- 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
+ }
235
244
  }
236
- })
237
-
238
- if (!openApiDisabled) {
239
- const openapiJsonPath = basePath ? \`\${basePath}/openapi.json\` : '/openapi.json'
240
- const openapiYamlPath = basePath ? \`\${basePath}/openapi.yaml\` : '/openapi.yaml'
241
245
 
242
- fastify.get(openapiJsonPath, async (_request, reply) => {
243
- return reply.send(openApiJsonSpec)
246
+ instance.addHook('onRequest', async (request: FastifyRequest) => {
247
+ (request as FastifyExtended & { findManyPaginatedMode?: FindManyPaginatedMode }).findManyPaginatedMode = FIND_MANY_PAGINATED_MODE
244
248
  })
245
249
 
246
- fastify.get(openapiYamlPath, async (_request, reply) => {
247
- 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
+ }
248
257
  })
249
- }
250
258
 
251
- const handleGet = (
252
- opConfig: OperationConfigLike,
253
- handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
254
- parseFn: (req: FastifyRequest) => void,
255
- ) => async (request: FastifyRequest, reply: FastifyReply) => {
256
- try {
257
- parseFn(request)
258
- makeShapeHook(config, opConfig)(request)
259
- const { before = [], after = [] } = opConfig
260
- if (await runHooks(before, request, reply)) return
261
- await handlerFn(request, reply)
262
- if (await runHooks(after, request, reply)) return
263
- sendResult(request, reply)
264
- } catch (error: unknown) {
265
- 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
+ })
266
270
  }
267
- }
268
271
 
269
- const handleWrite = (
270
- opConfig: OperationConfigLike,
271
- handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
272
- ) => async (request: FastifyRequest, reply: FastifyReply) => {
273
- try {
274
- makeShapeHook(config, opConfig)(request)
275
- const { before = [], after = [] } = opConfig
276
- if (await runHooks(before, request, reply)) return
277
- await handlerFn(request, reply)
278
- if (await runHooks(after, request, reply)) return
279
- sendResult(request, reply)
280
- } catch (error: unknown) {
281
- 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
+ }
282
288
  }
283
- }
284
289
 
285
- if (config.enableAll || config.findFirst) {
286
- const opConfig: OperationConfigLike = (config.findFirst as OperationConfigLike | undefined) ?? defaultOpConfig
287
- const path = basePath ? \`\${basePath}/first\` : '/first'
288
- fastify.get(path, handleGet(opConfig, ${modelName}FindFirst, parseQueryHook))
289
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindFirst, parseBodyAsQueryHook))
290
- }
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
+ }
291
305
 
292
- if (config.enableAll || config.findFirstOrThrow) {
293
- const opConfig: OperationConfigLike = (config.findFirstOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
294
- const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
295
- fastify.get(path, handleGet(opConfig, ${modelName}FindFirstOrThrow, parseQueryHook))
296
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindFirstOrThrow, parseBodyAsQueryHook))
297
- }
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
+ }
298
312
 
299
- if (config.enableAll || config.findManyPaginated) {
300
- const opConfig: OperationConfigLike = (config.findManyPaginated as OperationConfigLike | undefined) ?? defaultOpConfig
301
- const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
302
- fastify.get(path, handleGet(opConfig, ${modelName}FindManyPaginated, parseQueryHook))
303
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindManyPaginated, parseBodyAsQueryHook))
304
- }
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
+ }
305
319
 
306
- if (config.enableAll || config.aggregate) {
307
- const opConfig: OperationConfigLike = (config.aggregate as OperationConfigLike | undefined) ?? defaultOpConfig
308
- const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
309
- fastify.get(path, handleGet(opConfig, ${modelName}Aggregate, parseQueryHook))
310
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}Aggregate, parseBodyAsQueryHook))
311
- }
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
+ }
312
326
 
313
- if (config.enableAll || config.count) {
314
- const opConfig: OperationConfigLike = (config.count as OperationConfigLike | undefined) ?? defaultOpConfig
315
- const path = basePath ? \`\${basePath}/count\` : '/count'
316
- fastify.get(path, handleGet(opConfig, ${modelName}Count, parseQueryHook))
317
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}Count, parseBodyAsQueryHook))
318
- }
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
+ }
319
333
 
320
- if (config.enableAll || config.groupBy) {
321
- const opConfig: OperationConfigLike = (config.groupBy as OperationConfigLike | undefined) ?? defaultOpConfig
322
- const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
323
- fastify.get(path, handleGet(opConfig, ${modelName}GroupBy, parseQueryHook))
324
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}GroupBy, parseBodyAsQueryHook))
325
- }
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
+ }
326
340
 
327
- if (config.enableAll || config.findUniqueOrThrow) {
328
- const opConfig: OperationConfigLike = (config.findUniqueOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
329
- const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
330
- fastify.get(path, handleGet(opConfig, ${modelName}FindUniqueOrThrow, parseQueryHook))
331
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindUniqueOrThrow, parseBodyAsQueryHook))
332
- }
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
+ }
333
347
 
334
- if (config.enableAll || config.findUnique) {
335
- const opConfig: OperationConfigLike = (config.findUnique as OperationConfigLike | undefined) ?? defaultOpConfig
336
- const path = basePath ? \`\${basePath}/unique\` : '/unique'
337
- fastify.get(path, handleGet(opConfig, ${modelName}FindUnique, parseQueryHook))
338
- if (postReadsEnabled) fastify.post(path, handleGet(opConfig, ${modelName}FindUnique, parseBodyAsQueryHook))
339
- }
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
+ }
340
354
 
341
- if (config.enableAll || config.findMany) {
342
- const opConfig: OperationConfigLike = (config.findMany as OperationConfigLike | undefined) ?? defaultOpConfig
343
- const path = basePath || '/'
344
- fastify.get(path, handleGet(opConfig, ${modelName}FindMany, parseQueryHook))
345
- if (postReadsEnabled) {
346
- const postPath = basePath ? \`\${basePath}/read\` : '/read'
347
- 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))
348
360
  }
349
- }
350
361
 
351
- if (config.enableAll || config.createManyAndReturn) {
352
- const opConfig: OperationConfigLike = (config.createManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
353
- const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
354
- fastify.post(path, handleWrite(opConfig, ${modelName}CreateManyAndReturn))
355
- }
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
+ }
356
371
 
357
- if (config.enableAll || config.createMany) {
358
- const opConfig: OperationConfigLike = (config.createMany as OperationConfigLike | undefined) ?? defaultOpConfig
359
- const path = basePath ? \`\${basePath}/many\` : '/many'
360
- fastify.post(path, handleWrite(opConfig, ${modelName}CreateMany))
361
- }
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
+ }
362
377
 
363
- if (config.enableAll || config.create) {
364
- const opConfig: OperationConfigLike = (config.create as OperationConfigLike | undefined) ?? defaultOpConfig
365
- const path = basePath || '/'
366
- fastify.post(path, handleWrite(opConfig, ${modelName}Create))
367
- }
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
+ }
368
383
 
369
- if (config.enableAll || config.updateManyAndReturn) {
370
- const opConfig: OperationConfigLike = (config.updateManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
371
- const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
372
- fastify.put(path, handleWrite(opConfig, ${modelName}UpdateManyAndReturn))
373
- }
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
+ }
374
389
 
375
- if (config.enableAll || config.updateMany) {
376
- const opConfig: OperationConfigLike = (config.updateMany as OperationConfigLike | undefined) ?? defaultOpConfig
377
- const path = basePath ? \`\${basePath}/many\` : '/many'
378
- fastify.put(path, handleWrite(opConfig, ${modelName}UpdateMany))
379
- }
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
+ }
380
395
 
381
- if (config.enableAll || config.update) {
382
- const opConfig: OperationConfigLike = (config.update as OperationConfigLike | undefined) ?? defaultOpConfig
383
- const path = basePath || '/'
384
- fastify.put(path, handleWrite(opConfig, ${modelName}Update))
385
- }
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
+ }
386
401
 
387
- if (config.enableAll || config.upsert) {
388
- const opConfig: OperationConfigLike = (config.upsert as OperationConfigLike | undefined) ?? defaultOpConfig
389
- const path = basePath || '/'
390
- fastify.patch(path, handleWrite(opConfig, ${modelName}Upsert))
391
- }
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
+ }
392
407
 
393
- if (config.enableAll || config.deleteMany) {
394
- const opConfig: OperationConfigLike = (config.deleteMany as OperationConfigLike | undefined) ?? defaultOpConfig
395
- const path = basePath ? \`\${basePath}/many\` : '/many'
396
- fastify.delete(path, handleWrite(opConfig, ${modelName}DeleteMany))
397
- }
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
+ }
398
413
 
399
- if (config.enableAll || config.delete) {
400
- const opConfig: OperationConfigLike = (config.delete as OperationConfigLike | undefined) ?? defaultOpConfig
401
- const path = basePath || '/'
402
- fastify.delete(path, handleWrite(opConfig, ${modelName}Delete))
403
- }
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
+ })
404
449
  }
405
450
  `;
406
451
  }
@@ -1 +1 @@
1
- {"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;AAMA,sEAoaC;AAzaD,uEAAmE;AAEnE,kDAA8C;AAG9C,SAAgB,6BAA6B,CAAC,EAC5C,KAAK,EACL,KAAK,EACL,iBAAiB,EACjB,WAAW,EACX,aAAa,EACb,qBAAqB,GAQtB;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,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC1C,IAAI,EAAE,CAAC,CAAC,IAAI;QACZ,IAAI,EAAE,CAAC,CAAC,IAAI;QACZ,IAAI,EAAE,CAAC,CAAC,IAAI;QACZ,MAAM,EAAE,CAAC,CAAC,MAAM;QAChB,UAAU,EAAE,CAAC,CAAC,UAAU;QACxB,eAAe,EAAE,CAAC,CAAC,eAAe;QAClC,WAAW,EAAE,CAAC,CAAC,WAAW,IAAI,K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;;;;;;+CAMhC,SAAS;sEACc,SAAS;;;;;;+CAMhC,SAAS;;;sDAGF,SAAS;;;;;;;kDAOb,SAAS;;;;;;kDAMT,SAAS;;;;;;kDAMT,SAAS;;;;;;iDAMV,SAAS;;;;;;iDAMT,SAAS;;;;;;iDAMT,SAAS;;;;;;mDAMP,SAAS;;;;;;oDAMR,SAAS;;;;;;oDAMT,SAAS;;;;;;;cAO/C,SAAS;;;;;;;;;;kBAUL,SAAS;;;;;;;;;;CAU1B,CAAA;AACD,CAAC"}
@@ -1,11 +1,12 @@
1
1
  import { DMMF } from '@prisma/generator-helper';
2
2
  import { ImportStyle } from '../utils/resolveImportStyle';
3
3
  import { WriteStrategy, FindManyPaginatedMode } from '../constants';
4
- export declare function generateHonoRouterFunction({ model, enums, guardShapesImport, importStyle, writeStrategy, findManyPaginatedMode, }: {
4
+ export declare function generateHonoRouterFunction({ model, enums, guardShapesImport, importStyle, writeStrategy, findManyPaginatedMode, dropGuard, }: {
5
5
  model: DMMF.Model;
6
6
  enums: DMMF.DatamodelEnum[];
7
7
  guardShapesImport: string | null;
8
8
  importStyle: ImportStyle;
9
9
  writeStrategy: WriteStrategy;
10
10
  findManyPaginatedMode: FindManyPaginatedMode;
11
+ dropGuard: boolean;
11
12
  }): string;