prisma-generator-express 1.59.0 → 1.60.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.
- package/dist/client/encodeQueryParams.js +19 -23
- package/dist/client/encodeQueryParams.js.map +1 -1
- package/dist/copy/operationDefinitions.d.ts +37 -0
- package/dist/copy/operationDefinitions.js +412 -0
- package/dist/copy/operationDefinitions.js.map +1 -0
- package/dist/generators/generateFastifyHandler.js +10 -41
- package/dist/generators/generateFastifyHandler.js.map +1 -1
- package/dist/generators/generateHonoHandler.js +10 -41
- package/dist/generators/generateHonoHandler.js.map +1 -1
- package/dist/generators/generateModelMetadata.d.ts +8 -0
- package/dist/generators/generateModelMetadata.js +77 -0
- package/dist/generators/generateModelMetadata.js.map +1 -0
- package/dist/generators/generateOperationCore.js +20 -30
- package/dist/generators/generateOperationCore.js.map +1 -1
- package/dist/generators/generateQueryBuilderHelper.js +1 -1
- package/dist/generators/generateQueryBuilderHelper.js.map +1 -1
- package/dist/generators/generateRouteConfigType.js +11 -57
- package/dist/generators/generateRouteConfigType.js.map +1 -1
- package/dist/generators/generateRouter.js +64 -177
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.js +59 -169
- package/dist/generators/generateRouterFastify.js.map +1 -1
- package/dist/generators/generateRouterHono.js +52 -152
- package/dist/generators/generateRouterHono.js.map +1 -1
- package/dist/generators/generateUnifiedHandler.js +7 -30
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/generators/generateUnifiedScalarUI.js +9 -74
- package/dist/generators/generateUnifiedScalarUI.js.map +1 -1
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/utils/copyFiles.js +7 -0
- package/dist/utils/copyFiles.js.map +1 -1
- package/dist/utils/writeFileSafely.js +5 -12
- package/dist/utils/writeFileSafely.js.map +1 -1
- package/package.json +12 -2
- package/src/client/encodeQueryParams.ts +23 -36
- package/src/copy/autoIncludePlanner.ts +14 -23
- package/src/copy/autoIncludeRuntime.ts +117 -228
- package/src/copy/buildModelOpenApi.ts +248 -628
- package/src/copy/concurrency.ts +20 -0
- package/src/copy/docsRenderer.ts +63 -333
- package/src/copy/errorMapper.ts +126 -0
- package/src/copy/guardHelpers.ts +56 -0
- package/src/copy/materializedCount.ts +68 -0
- package/src/copy/materializedRouter.ts +33 -29
- package/src/copy/operationDefinitions.ts +359 -35
- package/src/copy/operationRuntime.ts +11 -605
- package/src/copy/pagination.ts +151 -0
- package/src/copy/scalarTypes.ts +2 -0
- package/src/copy/sse.ts +296 -0
- package/src/generators/generateFastifyHandler.ts +13 -47
- package/src/generators/generateHonoHandler.ts +13 -47
- package/src/generators/generateModelMetadata.ts +92 -0
- package/src/generators/generateOperationCore.ts +19 -32
- package/src/generators/generateQueryBuilderHelper.ts +1 -1
- package/src/generators/generateRouteConfigType.ts +9 -60
- package/src/generators/generateRouter.ts +88 -180
- package/src/generators/generateRouterFastify.ts +65 -172
- package/src/generators/generateRouterHono.ts +58 -155
- package/src/generators/generateUnifiedHandler.ts +8 -33
- package/src/generators/generateUnifiedScalarUI.ts +9 -91
- package/src/index.ts +13 -1
- package/src/utils/copyFiles.ts +7 -0
- package/src/utils/writeFileSafely.ts +5 -11
|
@@ -3,58 +3,69 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.generateRouterFunction = generateRouterFunction;
|
|
4
4
|
const generateRouteConfigType_1 = require("./generateRouteConfigType");
|
|
5
5
|
const importExt_1 = require("../utils/importExt");
|
|
6
|
+
const operationDefinitions_1 = require("../copy/operationDefinitions");
|
|
7
|
+
function pathExpr(basePath, suffix) {
|
|
8
|
+
if (!suffix)
|
|
9
|
+
return basePath || '/';
|
|
10
|
+
if (!basePath)
|
|
11
|
+
return `'${suffix}'`;
|
|
12
|
+
return `\`\${basePath}${suffix}\``;
|
|
13
|
+
}
|
|
14
|
+
function emitReadOp(meta, modelName) {
|
|
15
|
+
const c = meta.name.charAt(0).toUpperCase() + meta.name.slice(1);
|
|
16
|
+
const handlerName = `${modelName}${c}`;
|
|
17
|
+
const pathValue = pathExpr('basePath', meta.pathSuffix);
|
|
18
|
+
const postReadBlock = meta.supportsPostRead
|
|
19
|
+
? ` if (postReadsEnabled) {
|
|
20
|
+
const postPath = ${meta.name === 'findMany' ? "basePath ? `${basePath}/read` : '/read'" : `path`}
|
|
21
|
+
router.post(postPath, parseBodyAsQuery, setShape(opConfig), ...before, ${handlerName} as RequestHandler, ...after, respond)
|
|
22
|
+
}`
|
|
23
|
+
: '';
|
|
24
|
+
return ` if (isEnabled(config.${meta.configKey})) {
|
|
25
|
+
const opConfig: OperationConfigLike = (config.${meta.configKey} as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
26
|
+
const { before = [], after = [] } = opConfig
|
|
27
|
+
const path = ${pathValue}
|
|
28
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.${meta.coreName}, '${meta.name}'), ${handlerName} as RequestHandler, ...after, respond)
|
|
29
|
+
${postReadBlock}
|
|
30
|
+
}`;
|
|
31
|
+
}
|
|
32
|
+
function emitWriteOp(meta, modelName) {
|
|
33
|
+
const c = meta.name.charAt(0).toUpperCase() + meta.name.slice(1);
|
|
34
|
+
const handlerName = `${modelName}${c}`;
|
|
35
|
+
const pathValue = pathExpr('basePath', meta.pathSuffix);
|
|
36
|
+
const respondFn = meta.successStatus === 201 ? 'respondCreated' : 'respond';
|
|
37
|
+
return ` if (isEnabled(config.${meta.configKey})) {
|
|
38
|
+
const opConfig: OperationConfigLike = (config.${meta.configKey} as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
39
|
+
const { before = [], after = [] } = opConfig
|
|
40
|
+
const path = ${pathValue}
|
|
41
|
+
router.${meta.method}(path, setShape(opConfig), ...before, ${handlerName} as RequestHandler, ...after, ${respondFn})
|
|
42
|
+
}`;
|
|
43
|
+
}
|
|
6
44
|
function generateRouterFunction({ model, enums, guardShapesImport, importStyle, writeStrategy, findManyPaginatedMode, dropGuard, }) {
|
|
7
45
|
const ext = (0, importExt_1.importExt)(importStyle);
|
|
8
46
|
const modelName = model.name;
|
|
9
47
|
const modelNameLower = modelName.toLowerCase();
|
|
10
48
|
const delegateKey = modelName.charAt(0).toLowerCase() + modelName.slice(1);
|
|
11
49
|
const routerFunctionName = `${modelName}Router`;
|
|
12
|
-
const
|
|
13
|
-
name
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
relationFromFields: f.relationFromFields,
|
|
22
|
-
}));
|
|
23
|
-
const referencedEnumTypes = new Set(model.fields.filter((f) => f.kind === 'enum').map((f) => f.type));
|
|
24
|
-
const enumsMeta = enums
|
|
25
|
-
.filter((e) => referencedEnumTypes.has(e.name))
|
|
26
|
-
.map((e) => ({
|
|
27
|
-
name: e.name,
|
|
28
|
-
values: e.values.map((v) => ({ name: v.name })),
|
|
29
|
-
}));
|
|
50
|
+
const handlerImports = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.name !== 'updateEach')
|
|
51
|
+
.map((m) => ` ${modelName}${m.name.charAt(0).toUpperCase() + m.name.slice(1)},`)
|
|
52
|
+
.join('\n');
|
|
53
|
+
const readOps = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.kind === 'read');
|
|
54
|
+
const writeOps = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.kind === 'write' || m.kind === 'batch').filter((m) => m.name !== 'updateEach');
|
|
55
|
+
const readOpBlocks = readOps.map((m) => emitReadOp(m, modelName)).join('\n\n');
|
|
56
|
+
const writeOpBlocks = writeOps
|
|
57
|
+
.map((m) => emitWriteOp(m, modelName))
|
|
58
|
+
.join('\n\n');
|
|
30
59
|
return `import express from 'express'
|
|
31
60
|
import type { Request, Response, NextFunction, RequestHandler } from 'express'
|
|
32
61
|
import { startQueryBuilder } from '../queryBuilder${ext}'
|
|
33
62
|
import {
|
|
34
|
-
|
|
35
|
-
${modelName}FindUniqueOrThrow,
|
|
36
|
-
${modelName}FindFirst,
|
|
37
|
-
${modelName}FindFirstOrThrow,
|
|
38
|
-
${modelName}FindMany,
|
|
39
|
-
${modelName}FindManyPaginated,
|
|
40
|
-
${modelName}Create,
|
|
41
|
-
${modelName}CreateMany,
|
|
42
|
-
${modelName}CreateManyAndReturn,
|
|
43
|
-
${modelName}Update,
|
|
44
|
-
${modelName}UpdateMany,
|
|
45
|
-
${modelName}UpdateManyAndReturn,
|
|
46
|
-
${modelName}Upsert,
|
|
47
|
-
${modelName}Delete,
|
|
48
|
-
${modelName}DeleteMany,
|
|
49
|
-
${modelName}Aggregate,
|
|
50
|
-
${modelName}Count,
|
|
51
|
-
${modelName}GroupBy,
|
|
63
|
+
${handlerImports}
|
|
52
64
|
} from './${modelName}Handlers${ext}'
|
|
53
65
|
import * as core from './${modelName}Core${ext}'
|
|
54
66
|
import type {
|
|
55
67
|
RouteConfig,
|
|
56
68
|
QueryBuilderConfig,
|
|
57
|
-
WriteStrategy,
|
|
58
69
|
FindManyPaginatedMode,
|
|
59
70
|
PaginationConfig,
|
|
60
71
|
} from '../routeConfig.target${ext}'
|
|
@@ -63,30 +74,26 @@ import { sanitizeKeys, normalizePrefix, getEnv } from '../misc${ext}'
|
|
|
63
74
|
import { buildModelOpenApi } from '../buildModelOpenApi${ext}'
|
|
64
75
|
import { validateCountSourceWhere } from '../routeConfig${ext}'
|
|
65
76
|
import type { OperationContext } from '../operationRuntime${ext}'
|
|
77
|
+
import { transformResult } from '../operationRuntime${ext}'
|
|
78
|
+
import { HttpError, mapError } from '../errorMapper${ext}'
|
|
79
|
+
import { mergePaginationConfig } from '../pagination${ext}'
|
|
66
80
|
import {
|
|
67
|
-
transformResult,
|
|
68
81
|
acceptsEventStream,
|
|
69
82
|
runProgressiveEndpoint,
|
|
70
83
|
runSingleResultSSE,
|
|
71
84
|
emitTerminalSSEError,
|
|
72
85
|
removeReqCloseListener,
|
|
73
|
-
|
|
74
|
-
mapError,
|
|
75
|
-
HttpError,
|
|
76
|
-
} from '../operationRuntime${ext}'
|
|
86
|
+
} from '../sse${ext}'
|
|
77
87
|
import { relationModels } from '../relationModels${ext}'
|
|
78
88
|
import { runAutoIncludeProgressive } from '../autoIncludeRuntime${ext}'
|
|
89
|
+
import { MODEL_FIELDS, MODEL_ENUMS } from './${modelName}Metadata${ext}'
|
|
79
90
|
|
|
80
91
|
${(0, generateRouteConfigType_1.generateRouteConfigType)(modelName, 'RequestHandler', guardShapesImport, importStyle, 'express')}
|
|
81
92
|
const _env = getEnv()
|
|
82
93
|
|
|
83
|
-
const WRITE_STRATEGY: WriteStrategy = '${writeStrategy}'
|
|
84
94
|
const FIND_MANY_PAGINATED_MODE: FindManyPaginatedMode = '${findManyPaginatedMode}'
|
|
85
95
|
const DROP_GUARD = ${dropGuard} || _env.E2E === 'true'
|
|
86
96
|
|
|
87
|
-
const MODEL_FIELDS = ${JSON.stringify(fieldsMeta, null, 2)} as const
|
|
88
|
-
const MODEL_ENUMS = ${JSON.stringify(enumsMeta, null, 2)} as const
|
|
89
|
-
|
|
90
97
|
type OperationConfigLike = {
|
|
91
98
|
before?: RequestHandler[]
|
|
92
99
|
after?: RequestHandler[]
|
|
@@ -160,7 +167,7 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(config: ${m
|
|
|
160
167
|
MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
|
|
161
168
|
MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
|
|
162
169
|
config as unknown as Parameters<typeof buildModelOpenApi>[3],
|
|
163
|
-
{ format: 'json', writeStrategy:
|
|
170
|
+
{ format: 'json', writeStrategy: '${writeStrategy}' },
|
|
164
171
|
)
|
|
165
172
|
}
|
|
166
173
|
return _openApiJsonCache
|
|
@@ -173,7 +180,7 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(config: ${m
|
|
|
173
180
|
MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
|
|
174
181
|
MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
|
|
175
182
|
config as unknown as Parameters<typeof buildModelOpenApi>[3],
|
|
176
|
-
{ format: 'yaml', writeStrategy:
|
|
183
|
+
{ format: 'yaml', writeStrategy: '${writeStrategy}' },
|
|
177
184
|
) as string
|
|
178
185
|
}
|
|
179
186
|
return _openApiYamlCache
|
|
@@ -351,12 +358,14 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(config: ${m
|
|
|
351
358
|
}
|
|
352
359
|
|
|
353
360
|
const respond: RequestHandler = (_req, res) => {
|
|
361
|
+
if (res.headersSent || res.writableEnded) return
|
|
354
362
|
const data = readLocals(res).data
|
|
355
363
|
if (data === undefined) return res.status(500).json({ message: 'No data set by handler' })
|
|
356
364
|
return res.json(transformResult(data))
|
|
357
365
|
}
|
|
358
366
|
|
|
359
367
|
const respondCreated: RequestHandler = (_req, res) => {
|
|
368
|
+
if (res.headersSent || res.writableEnded) return
|
|
360
369
|
const data = readLocals(res).data
|
|
361
370
|
if (data === undefined) return res.status(500).json({ message: 'No data set by handler' })
|
|
362
371
|
return res.status(201).json(transformResult(data))
|
|
@@ -373,127 +382,10 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(config: ${m
|
|
|
373
382
|
})
|
|
374
383
|
}
|
|
375
384
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
const path = basePath ? \`\${basePath}/first\` : '/first'
|
|
380
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.findFirst, 'findFirst'), ${modelName}FindFirst as RequestHandler, ...after, respond)
|
|
381
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}FindFirst as RequestHandler, ...after, respond)
|
|
382
|
-
}
|
|
383
|
-
if (isEnabled(config.findFirstOrThrow)) {
|
|
384
|
-
const opConfig: OperationConfigLike = (config.findFirstOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
385
|
-
const { before = [], after = [] } = opConfig
|
|
386
|
-
const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
|
|
387
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.findFirstOrThrow, 'findFirstOrThrow'), ${modelName}FindFirstOrThrow as RequestHandler, ...after, respond)
|
|
388
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}FindFirstOrThrow as RequestHandler, ...after, respond)
|
|
389
|
-
}
|
|
390
|
-
if (isEnabled(config.findManyPaginated)) {
|
|
391
|
-
const opConfig: OperationConfigLike = (config.findManyPaginated as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
392
|
-
const { before = [], after = [] } = opConfig
|
|
393
|
-
const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
|
|
394
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.findManyPaginated, 'findManyPaginated'), ${modelName}FindManyPaginated as RequestHandler, ...after, respond)
|
|
395
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}FindManyPaginated as RequestHandler, ...after, respond)
|
|
396
|
-
}
|
|
397
|
-
if (isEnabled(config.aggregate)) {
|
|
398
|
-
const opConfig: OperationConfigLike = (config.aggregate as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
399
|
-
const { before = [], after = [] } = opConfig
|
|
400
|
-
const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
|
|
401
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.aggregate, 'aggregate'), ${modelName}Aggregate as RequestHandler, ...after, respond)
|
|
402
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}Aggregate as RequestHandler, ...after, respond)
|
|
403
|
-
}
|
|
404
|
-
if (isEnabled(config.count)) {
|
|
405
|
-
const opConfig: OperationConfigLike = (config.count as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
406
|
-
const { before = [], after = [] } = opConfig
|
|
407
|
-
const path = basePath ? \`\${basePath}/count\` : '/count'
|
|
408
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.count, 'count'), ${modelName}Count as RequestHandler, ...after, respond)
|
|
409
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}Count as RequestHandler, ...after, respond)
|
|
410
|
-
}
|
|
411
|
-
if (isEnabled(config.groupBy)) {
|
|
412
|
-
const opConfig: OperationConfigLike = (config.groupBy as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
413
|
-
const { before = [], after = [] } = opConfig
|
|
414
|
-
const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
|
|
415
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.groupBy, 'groupBy'), ${modelName}GroupBy as RequestHandler, ...after, respond)
|
|
416
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}GroupBy as RequestHandler, ...after, respond)
|
|
417
|
-
}
|
|
418
|
-
if (isEnabled(config.findUniqueOrThrow)) {
|
|
419
|
-
const opConfig: OperationConfigLike = (config.findUniqueOrThrow as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
420
|
-
const { before = [], after = [] } = opConfig
|
|
421
|
-
const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
|
|
422
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.findUniqueOrThrow, 'findUniqueOrThrow'), ${modelName}FindUniqueOrThrow as RequestHandler, ...after, respond)
|
|
423
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}FindUniqueOrThrow as RequestHandler, ...after, respond)
|
|
424
|
-
}
|
|
425
|
-
if (isEnabled(config.findUnique)) {
|
|
426
|
-
const opConfig: OperationConfigLike = (config.findUnique as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
427
|
-
const { before = [], after = [] } = opConfig
|
|
428
|
-
const path = basePath ? \`\${basePath}/unique\` : '/unique'
|
|
429
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.findUnique, 'findUnique'), ${modelName}FindUnique as RequestHandler, ...after, respond)
|
|
430
|
-
if (postReadsEnabled) router.post(path, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}FindUnique as RequestHandler, ...after, respond)
|
|
431
|
-
}
|
|
432
|
-
if (isEnabled(config.findMany)) {
|
|
433
|
-
const opConfig: OperationConfigLike = (config.findMany as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
434
|
-
const { before = [], after = [] } = opConfig
|
|
435
|
-
const path = basePath || '/'
|
|
436
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, maybeProgressiveSSE(opConfig, core.findMany, 'findMany'), ${modelName}FindMany as RequestHandler, ...after, respond)
|
|
437
|
-
if (postReadsEnabled) {
|
|
438
|
-
const postPath = basePath ? \`\${basePath}/read\` : '/read'
|
|
439
|
-
router.post(postPath, parseBodyAsQuery, setShape(opConfig), ...before, ${modelName}FindMany as RequestHandler, ...after, respond)
|
|
440
|
-
}
|
|
441
|
-
}
|
|
385
|
+
${readOpBlocks}
|
|
386
|
+
|
|
387
|
+
${writeOpBlocks}
|
|
442
388
|
|
|
443
|
-
if (isEnabled(config.createManyAndReturn)) {
|
|
444
|
-
const opConfig: OperationConfigLike = (config.createManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
445
|
-
const { before = [], after = [] } = opConfig
|
|
446
|
-
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
447
|
-
router.post(path, setShape(opConfig), ...before, ${modelName}CreateManyAndReturn as RequestHandler, ...after, respondCreated)
|
|
448
|
-
}
|
|
449
|
-
if (isEnabled(config.createMany)) {
|
|
450
|
-
const opConfig: OperationConfigLike = (config.createMany as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
451
|
-
const { before = [], after = [] } = opConfig
|
|
452
|
-
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
453
|
-
router.post(path, setShape(opConfig), ...before, ${modelName}CreateMany as RequestHandler, ...after, respondCreated)
|
|
454
|
-
}
|
|
455
|
-
if (isEnabled(config.create)) {
|
|
456
|
-
const opConfig: OperationConfigLike = (config.create as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
457
|
-
const { before = [], after = [] } = opConfig
|
|
458
|
-
const path = basePath || '/'
|
|
459
|
-
router.post(path, setShape(opConfig), ...before, ${modelName}Create as RequestHandler, ...after, respondCreated)
|
|
460
|
-
}
|
|
461
|
-
if (isEnabled(config.updateManyAndReturn)) {
|
|
462
|
-
const opConfig: OperationConfigLike = (config.updateManyAndReturn as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
463
|
-
const { before = [], after = [] } = opConfig
|
|
464
|
-
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
465
|
-
router.put(path, setShape(opConfig), ...before, ${modelName}UpdateManyAndReturn as RequestHandler, ...after, respond)
|
|
466
|
-
}
|
|
467
|
-
if (isEnabled(config.updateMany)) {
|
|
468
|
-
const opConfig: OperationConfigLike = (config.updateMany as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
469
|
-
const { before = [], after = [] } = opConfig
|
|
470
|
-
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
471
|
-
router.put(path, setShape(opConfig), ...before, ${modelName}UpdateMany as RequestHandler, ...after, respond)
|
|
472
|
-
}
|
|
473
|
-
if (isEnabled(config.update)) {
|
|
474
|
-
const opConfig: OperationConfigLike = (config.update as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
475
|
-
const { before = [], after = [] } = opConfig
|
|
476
|
-
const path = basePath || '/'
|
|
477
|
-
router.put(path, setShape(opConfig), ...before, ${modelName}Update as RequestHandler, ...after, respond)
|
|
478
|
-
}
|
|
479
|
-
if (isEnabled(config.upsert)) {
|
|
480
|
-
const opConfig: OperationConfigLike = (config.upsert as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
481
|
-
const { before = [], after = [] } = opConfig
|
|
482
|
-
const path = basePath || '/'
|
|
483
|
-
router.patch(path, setShape(opConfig), ...before, ${modelName}Upsert as RequestHandler, ...after, respond)
|
|
484
|
-
}
|
|
485
|
-
if (isEnabled(config.deleteMany)) {
|
|
486
|
-
const opConfig: OperationConfigLike = (config.deleteMany as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
487
|
-
const { before = [], after = [] } = opConfig
|
|
488
|
-
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
489
|
-
router.delete(path, setShape(opConfig), ...before, ${modelName}DeleteMany as RequestHandler, ...after, respond)
|
|
490
|
-
}
|
|
491
|
-
if (isEnabled(config.delete)) {
|
|
492
|
-
const opConfig: OperationConfigLike = (config.delete as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
493
|
-
const { before = [], after = [] } = opConfig
|
|
494
|
-
const path = basePath || '/'
|
|
495
|
-
router.delete(path, setShape(opConfig), ...before, ${modelName}Delete as RequestHandler, ...after, respond)
|
|
496
|
-
}
|
|
497
389
|
if (config.updateEach) {
|
|
498
390
|
const opConfig: OperationConfigLike = (config.updateEach as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
499
391
|
if ((!opConfig.before || opConfig.before.length === 0) && _env.NODE_ENV !== 'production') {
|
|
@@ -510,6 +402,9 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(config: ${m
|
|
|
510
402
|
...before,
|
|
511
403
|
async (req: Request, res: Response, next: NextFunction) => {
|
|
512
404
|
try {
|
|
405
|
+
if (!Array.isArray(req.body)) {
|
|
406
|
+
throw new HttpError(400, 'updateEach body must be an array of { where, data } items')
|
|
407
|
+
}
|
|
513
408
|
const atomic = req.get('x-batch-atomic') === 'true'
|
|
514
409
|
readLocals(res).data = await core.updateEach(buildContext(req, res), atomic)
|
|
515
410
|
next()
|
|
@@ -523,15 +418,7 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(config: ${m
|
|
|
523
418
|
}
|
|
524
419
|
|
|
525
420
|
router.use((err: unknown, _req: Request, res: Response, next: NextFunction) => {
|
|
526
|
-
|
|
527
|
-
if (err instanceof HttpError) {
|
|
528
|
-
httpError = err
|
|
529
|
-
} else if (err && typeof err === 'object' && typeof (err as { status?: number }).status === 'number') {
|
|
530
|
-
const e = err as { status: number; message?: string }
|
|
531
|
-
httpError = new HttpError(e.status, e.message || 'Internal server error')
|
|
532
|
-
} else {
|
|
533
|
-
httpError = mapError(err)
|
|
534
|
-
}
|
|
421
|
+
const httpError = mapError(err)
|
|
535
422
|
if (!res.headersSent) return res.status(httpError.status).json({ message: httpError.message })
|
|
536
423
|
next(err)
|
|
537
424
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateRouter.js","sourceRoot":"","sources":["../../src/generators/generateRouter.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"generateRouter.js","sourceRoot":"","sources":["../../src/generators/generateRouter.ts"],"names":[],"mappings":"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"}
|
|
@@ -3,56 +3,62 @@ 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
|
+
const operationDefinitions_1 = require("../copy/operationDefinitions");
|
|
7
|
+
function pathExpr(suffix) {
|
|
8
|
+
if (!suffix)
|
|
9
|
+
return `basePath || '/'`;
|
|
10
|
+
return `\`\${basePath}${suffix}\``;
|
|
11
|
+
}
|
|
12
|
+
function emitReadOp(meta, modelName) {
|
|
13
|
+
const c = meta.name.charAt(0).toUpperCase() + meta.name.slice(1);
|
|
14
|
+
const handlerName = `${modelName}${c}`;
|
|
15
|
+
const pathValue = pathExpr(meta.pathSuffix);
|
|
16
|
+
const postReadLine = meta.supportsPostRead
|
|
17
|
+
? meta.name === 'findMany'
|
|
18
|
+
? ` if (postReadsEnabled) {
|
|
19
|
+
const postPath = basePath ? \`\${basePath}/read\` : '/read'
|
|
20
|
+
instance.post(postPath, handleRead(opConfig, ${handlerName}, parseBodyAsQueryHook))
|
|
21
|
+
}`
|
|
22
|
+
: ` if (postReadsEnabled) instance.post(path, handleRead(opConfig, ${handlerName}, parseBodyAsQueryHook))`
|
|
23
|
+
: '';
|
|
24
|
+
return ` if (isEnabled(config.${meta.configKey})) {
|
|
25
|
+
const opConfig: OperationConfigLike = (config.${meta.configKey} as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
26
|
+
const path = ${pathValue}
|
|
27
|
+
instance.get(path, handleRead(opConfig, ${handlerName}, parseQueryHook))
|
|
28
|
+
${postReadLine}
|
|
29
|
+
}`;
|
|
30
|
+
}
|
|
31
|
+
function emitWriteOp(meta, modelName) {
|
|
32
|
+
const c = meta.name.charAt(0).toUpperCase() + meta.name.slice(1);
|
|
33
|
+
const handlerName = `${modelName}${c}`;
|
|
34
|
+
const pathValue = pathExpr(meta.pathSuffix);
|
|
35
|
+
return ` if (isEnabled(config.${meta.configKey})) {
|
|
36
|
+
const opConfig: OperationConfigLike = (config.${meta.configKey} as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
37
|
+
const path = ${pathValue}
|
|
38
|
+
instance.${meta.method}(path, handleWrite(opConfig, ${handlerName}))
|
|
39
|
+
}`;
|
|
40
|
+
}
|
|
6
41
|
function generateFastifyRouterFunction({ model, enums, guardShapesImport, importStyle, writeStrategy, findManyPaginatedMode, dropGuard, }) {
|
|
7
42
|
const ext = (0, importExt_1.importExt)(importStyle);
|
|
8
43
|
const modelName = model.name;
|
|
9
44
|
const modelNameLower = modelName.toLowerCase();
|
|
10
45
|
const routerFunctionName = `${modelName}Router`;
|
|
11
|
-
const
|
|
12
|
-
name
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
documentation: f.documentation,
|
|
20
|
-
relationFromFields: f.relationFromFields,
|
|
21
|
-
}));
|
|
22
|
-
const referencedEnumTypes = new Set(model.fields.filter((f) => f.kind === 'enum').map((f) => f.type));
|
|
23
|
-
const enumsMeta = enums
|
|
24
|
-
.filter((e) => referencedEnumTypes.has(e.name))
|
|
25
|
-
.map((e) => ({
|
|
26
|
-
name: e.name,
|
|
27
|
-
values: e.values.map((v) => ({ name: v.name })),
|
|
28
|
-
}));
|
|
46
|
+
const handlerImports = operationDefinitions_1.OPERATION_METADATA
|
|
47
|
+
.map((m) => ` ${modelName}${m.name.charAt(0).toUpperCase() + m.name.slice(1)},`)
|
|
48
|
+
.join('\n');
|
|
49
|
+
const readOps = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.kind === 'read');
|
|
50
|
+
const writeOps = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.kind === 'write' || m.kind === 'batch')
|
|
51
|
+
.filter((m) => m.name !== 'updateEach');
|
|
52
|
+
const readOpBlocks = readOps.map((m) => emitReadOp(m, modelName)).join('\n\n');
|
|
53
|
+
const writeOpBlocks = writeOps.map((m) => emitWriteOp(m, modelName)).join('\n\n');
|
|
29
54
|
return `import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify'
|
|
30
55
|
import { startQueryBuilder } from '../queryBuilder${ext}'
|
|
31
56
|
import {
|
|
32
|
-
|
|
33
|
-
${modelName}FindUniqueOrThrow,
|
|
34
|
-
${modelName}FindFirst,
|
|
35
|
-
${modelName}FindFirstOrThrow,
|
|
36
|
-
${modelName}FindMany,
|
|
37
|
-
${modelName}FindManyPaginated,
|
|
38
|
-
${modelName}Create,
|
|
39
|
-
${modelName}CreateMany,
|
|
40
|
-
${modelName}CreateManyAndReturn,
|
|
41
|
-
${modelName}Update,
|
|
42
|
-
${modelName}UpdateMany,
|
|
43
|
-
${modelName}UpdateManyAndReturn,
|
|
44
|
-
${modelName}Upsert,
|
|
45
|
-
${modelName}Delete,
|
|
46
|
-
${modelName}DeleteMany,
|
|
47
|
-
${modelName}Aggregate,
|
|
48
|
-
${modelName}Count,
|
|
49
|
-
${modelName}GroupBy,
|
|
50
|
-
${modelName}UpdateEach,
|
|
57
|
+
${handlerImports}
|
|
51
58
|
} from './${modelName}Handlers${ext}'
|
|
52
59
|
import type {
|
|
53
60
|
RouteConfig,
|
|
54
61
|
FastifyHookHandler,
|
|
55
|
-
WriteStrategy,
|
|
56
62
|
FindManyPaginatedMode,
|
|
57
63
|
PaginationConfig,
|
|
58
64
|
} from '../routeConfig.target${ext}'
|
|
@@ -60,19 +66,18 @@ import { parseQueryParams } from '../parseQueryParams${ext}'
|
|
|
60
66
|
import { sanitizeKeys, normalizePrefix, getEnv } from '../misc${ext}'
|
|
61
67
|
import { buildModelOpenApi } from '../buildModelOpenApi${ext}'
|
|
62
68
|
import { validateCountSourceWhere } from '../routeConfig${ext}'
|
|
63
|
-
import {
|
|
69
|
+
import type { OperationContext } from '../operationRuntime${ext}'
|
|
70
|
+
import { transformResult } from '../operationRuntime${ext}'
|
|
71
|
+
import { HttpError, mapError } from '../errorMapper${ext}'
|
|
72
|
+
import { mergePaginationConfig } from '../pagination${ext}'
|
|
73
|
+
import { MODEL_FIELDS, MODEL_ENUMS } from './${modelName}Metadata${ext}'
|
|
64
74
|
|
|
65
75
|
${(0, generateRouteConfigType_1.generateRouteConfigType)(modelName, 'FastifyHookHandler', guardShapesImport, importStyle, 'fastify')}
|
|
66
76
|
const _env = getEnv()
|
|
67
77
|
|
|
68
|
-
const WRITE_STRATEGY: WriteStrategy = '${writeStrategy}'
|
|
69
78
|
const FIND_MANY_PAGINATED_MODE: FindManyPaginatedMode = '${findManyPaginatedMode}'
|
|
70
79
|
const DROP_GUARD = ${dropGuard} || _env.E2E === 'true'
|
|
71
80
|
|
|
72
|
-
const MODEL_FIELDS = ${JSON.stringify(fieldsMeta, null, 2)} as const
|
|
73
|
-
|
|
74
|
-
const MODEL_ENUMS = ${JSON.stringify(enumsMeta, null, 2)} as const
|
|
75
|
-
|
|
76
81
|
type OperationConfigLike = {
|
|
77
82
|
before?: FastifyHookHandler[]
|
|
78
83
|
after?: FastifyHookHandler[]
|
|
@@ -211,7 +216,7 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
211
216
|
MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
|
|
212
217
|
MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
|
|
213
218
|
config,
|
|
214
|
-
{ format: 'json', writeStrategy:
|
|
219
|
+
{ format: 'json', writeStrategy: '${writeStrategy}' },
|
|
215
220
|
)
|
|
216
221
|
}
|
|
217
222
|
return _openApiJsonCache
|
|
@@ -224,7 +229,7 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
224
229
|
MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
|
|
225
230
|
MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
|
|
226
231
|
config,
|
|
227
|
-
{ format: 'yaml', writeStrategy:
|
|
232
|
+
{ format: 'yaml', writeStrategy: '${writeStrategy}' },
|
|
228
233
|
) as string
|
|
229
234
|
}
|
|
230
235
|
return _openApiYamlCache
|
|
@@ -248,11 +253,9 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
248
253
|
})
|
|
249
254
|
|
|
250
255
|
instance.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
|
|
251
|
-
const
|
|
252
|
-
const status = e.status ?? e.statusCode ?? 500
|
|
253
|
-
const message = error.message || 'Internal server error'
|
|
256
|
+
const httpError = mapError(error)
|
|
254
257
|
if (!reply.sent) {
|
|
255
|
-
reply.code(status).send({ message })
|
|
258
|
+
reply.code(httpError.status).send({ message: httpError.message })
|
|
256
259
|
}
|
|
257
260
|
})
|
|
258
261
|
|
|
@@ -269,7 +272,7 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
269
272
|
})
|
|
270
273
|
}
|
|
271
274
|
|
|
272
|
-
const
|
|
275
|
+
const handleRead = (
|
|
273
276
|
opConfig: OperationConfigLike,
|
|
274
277
|
handlerFn: (req: FastifyRequest, reply: FastifyReply) => Promise<void>,
|
|
275
278
|
parseFn: (req: FastifyRequest) => void,
|
|
@@ -303,125 +306,9 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
303
306
|
}
|
|
304
307
|
}
|
|
305
308
|
|
|
306
|
-
|
|
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
|
-
}
|
|
312
|
-
|
|
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
|
-
}
|
|
319
|
-
|
|
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
|
-
}
|
|
326
|
-
|
|
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
|
-
}
|
|
333
|
-
|
|
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
|
-
}
|
|
340
|
-
|
|
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
|
-
}
|
|
347
|
-
|
|
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
|
-
}
|
|
354
|
-
|
|
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))
|
|
360
|
-
}
|
|
361
|
-
|
|
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
|
-
}
|
|
371
|
-
|
|
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
|
-
}
|
|
309
|
+
${readOpBlocks}
|
|
377
310
|
|
|
378
|
-
|
|
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
|
-
}
|
|
383
|
-
|
|
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
|
-
}
|
|
389
|
-
|
|
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
|
-
}
|
|
395
|
-
|
|
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
|
-
}
|
|
401
|
-
|
|
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
|
-
}
|
|
407
|
-
|
|
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
|
-
}
|
|
413
|
-
|
|
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
|
-
}
|
|
311
|
+
${writeOpBlocks}
|
|
425
312
|
|
|
426
313
|
if (config.updateEach) {
|
|
427
314
|
const opConfig: OperationConfigLike = (config.updateEach as OperationConfigLike | undefined) ?? defaultOpConfig
|
|
@@ -434,6 +321,9 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
434
321
|
const path = basePath ? \`\${basePath}/each\` : '/each'
|
|
435
322
|
instance.post(path, async (request: FastifyRequest, reply: FastifyReply) => {
|
|
436
323
|
try {
|
|
324
|
+
if (!Array.isArray(request.body)) {
|
|
325
|
+
throw new HttpError(400, 'updateEach body must be an array of { where, data } items')
|
|
326
|
+
}
|
|
437
327
|
makeShapeHook(config, opConfig)(request)
|
|
438
328
|
const { before = [], after = [] } = opConfig
|
|
439
329
|
if (await runHooks(before, request, reply)) return
|