prisma-generator-express 1.28.0 → 1.30.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/README.md +244 -14
- package/dist/constants.d.ts +1 -0
- package/dist/generators/generateFastifyHandler.d.ts +4 -0
- package/dist/generators/generateFastifyHandler.js +78 -0
- package/dist/generators/generateFastifyHandler.js.map +1 -0
- package/dist/generators/generateOperationCore.d.ts +6 -0
- package/dist/generators/generateOperationCore.js +534 -0
- package/dist/generators/generateOperationCore.js.map +1 -0
- package/dist/generators/generateQueryBuilderHelper.js +85 -69
- package/dist/generators/generateQueryBuilderHelper.js.map +1 -1
- package/dist/generators/generateRouter.js +1 -25
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.d.ts +5 -0
- package/dist/generators/generateRouterFastify.js +512 -0
- package/dist/generators/generateRouterFastify.js.map +1 -0
- package/dist/generators/generateUnifiedDocs.d.ts +2 -1
- package/dist/generators/generateUnifiedDocs.js +147 -82
- package/dist/generators/generateUnifiedDocs.js.map +1 -1
- package/dist/generators/generateUnifiedHandler.d.ts +0 -1
- package/dist/generators/generateUnifiedHandler.js +47 -516
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/generators/generateUnifiedScalarUI.d.ts +2 -0
- package/dist/generators/generateUnifiedScalarUI.js +127 -1324
- package/dist/generators/generateUnifiedScalarUI.js.map +1 -1
- package/dist/index.js +33 -8
- package/dist/index.js.map +1 -1
- package/dist/utils/copyFiles.d.ts +2 -1
- package/dist/utils/copyFiles.js +73 -39
- package/dist/utils/copyFiles.js.map +1 -1
- package/dist/utils/writeFileSafely.js +3 -0
- package/dist/utils/writeFileSafely.js.map +1 -1
- package/package.json +4 -1
- package/src/client/encodeQueryParams.ts +1 -1
- package/src/constants.ts +2 -0
- package/src/copy/createOutputValidatorMiddleware.ts +9 -12
- package/src/copy/docsRenderer.ts +1285 -0
- package/src/copy/parseQueryParams.ts +4 -8
- package/src/copy/routeConfig.ts +10 -4
- package/src/generators/generateFastifyHandler.ts +86 -0
- package/src/generators/generateOperationCore.ts +545 -0
- package/src/generators/generateQueryBuilderHelper.ts +86 -70
- package/src/generators/generateRouter.ts +1 -25
- package/src/generators/generateRouterFastify.ts +522 -0
- package/src/generators/generateUnifiedDocs.ts +164 -81
- package/src/generators/generateUnifiedHandler.ts +45 -533
- package/src/generators/generateUnifiedScalarUI.ts +134 -1323
- package/src/index.ts +45 -9
- package/src/utils/copyFiles.ts +88 -44
- package/src/utils/writeFileSafely.ts +4 -0
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateFastifyRouterFunction = generateFastifyRouterFunction;
|
|
4
|
+
function generateFastifyRouterFunction({ model, enums, }) {
|
|
5
|
+
const modelName = model.name;
|
|
6
|
+
const modelNameLower = modelName.toLowerCase();
|
|
7
|
+
const routerFunctionName = `${modelName}Routes`;
|
|
8
|
+
const fieldsMeta = model.fields.map((f) => ({
|
|
9
|
+
name: f.name,
|
|
10
|
+
kind: f.kind,
|
|
11
|
+
type: f.type,
|
|
12
|
+
isList: f.isList,
|
|
13
|
+
isRequired: f.isRequired,
|
|
14
|
+
hasDefaultValue: f.hasDefaultValue,
|
|
15
|
+
isUpdatedAt: f.isUpdatedAt ?? false,
|
|
16
|
+
documentation: f.documentation,
|
|
17
|
+
relationFromFields: f.relationFromFields,
|
|
18
|
+
}));
|
|
19
|
+
const referencedEnumTypes = new Set(model.fields.filter((f) => f.kind === 'enum').map((f) => f.type));
|
|
20
|
+
const enumsMeta = enums
|
|
21
|
+
.filter((e) => referencedEnumTypes.has(e.name))
|
|
22
|
+
.map((e) => ({
|
|
23
|
+
name: e.name,
|
|
24
|
+
values: e.values.map((v) => ({ name: v.name })),
|
|
25
|
+
}));
|
|
26
|
+
return `import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
|
|
27
|
+
import {
|
|
28
|
+
${modelName}FindUnique,
|
|
29
|
+
${modelName}FindUniqueOrThrow,
|
|
30
|
+
${modelName}FindFirst,
|
|
31
|
+
${modelName}FindFirstOrThrow,
|
|
32
|
+
${modelName}FindMany,
|
|
33
|
+
${modelName}FindManyPaginated,
|
|
34
|
+
${modelName}Create,
|
|
35
|
+
${modelName}CreateMany,
|
|
36
|
+
${modelName}CreateManyAndReturn,
|
|
37
|
+
${modelName}Update,
|
|
38
|
+
${modelName}UpdateMany,
|
|
39
|
+
${modelName}UpdateManyAndReturn,
|
|
40
|
+
${modelName}Upsert,
|
|
41
|
+
${modelName}Delete,
|
|
42
|
+
${modelName}DeleteMany,
|
|
43
|
+
${modelName}Aggregate,
|
|
44
|
+
${modelName}Count,
|
|
45
|
+
${modelName}GroupBy,
|
|
46
|
+
} from './${modelName}Handlers.js'
|
|
47
|
+
import type { RouteConfig, FastifyHookHandler } from '../routeConfig.js'
|
|
48
|
+
import { parseQueryParams } from '../parseQueryParams.js'
|
|
49
|
+
import { buildModelOpenApi } from '../buildModelOpenApi.js'
|
|
50
|
+
import { mapError, transformResult } from '../operationRuntime.js'
|
|
51
|
+
|
|
52
|
+
const _env = typeof process !== 'undefined' && process.env ? process.env : {} as Record<string, string | undefined>
|
|
53
|
+
|
|
54
|
+
const MODEL_FIELDS = ${JSON.stringify(fieldsMeta, null, 2)} as const
|
|
55
|
+
|
|
56
|
+
const MODEL_ENUMS = ${JSON.stringify(enumsMeta, null, 2)} as const
|
|
57
|
+
|
|
58
|
+
const defaultOpConfig = {
|
|
59
|
+
before: [] as FastifyHookHandler[],
|
|
60
|
+
after: [] as FastifyHookHandler[],
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function normalizePrefix(p: string): string {
|
|
64
|
+
if (!p) return ''
|
|
65
|
+
let result = p
|
|
66
|
+
if (!result.startsWith('/')) result = '/' + result
|
|
67
|
+
while (result.length > 1 && result.endsWith('/')) result = result.slice(0, -1)
|
|
68
|
+
if (result === '/') return ''
|
|
69
|
+
return result
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isQueryBuilderEnabled(config: RouteConfig): boolean {
|
|
73
|
+
if (config.queryBuilder === false) return false
|
|
74
|
+
if (typeof config.queryBuilder === 'object' && config.queryBuilder.enabled === false) return false
|
|
75
|
+
if (_env.NODE_ENV === 'production') return false
|
|
76
|
+
return true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getQueryBuilderConfig(config: RouteConfig) {
|
|
80
|
+
if (config.queryBuilder === false) return null
|
|
81
|
+
if (typeof config.queryBuilder === 'object') return config.queryBuilder
|
|
82
|
+
return {}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function parseQueryHook(request: FastifyRequest): void {
|
|
86
|
+
const raw = request.query as Record<string, unknown>
|
|
87
|
+
if (raw && Object.keys(raw).length > 0) {
|
|
88
|
+
;(request as any).parsedQuery = parseQueryParams(raw)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function makeShapeHook(config: RouteConfig, opConfig: any): (request: FastifyRequest) => void {
|
|
93
|
+
return (request: FastifyRequest) => {
|
|
94
|
+
;(request as any).routeConfig = config
|
|
95
|
+
if (opConfig.shape) {
|
|
96
|
+
;(request as any).guardShape = opConfig.shape
|
|
97
|
+
const headerName = config.guard?.variantHeader || 'x-api-variant'
|
|
98
|
+
const headerValue = request.headers[headerName]
|
|
99
|
+
const caller = config.guard?.resolveVariant?.(request)
|
|
100
|
+
?? (Array.isArray(headerValue) ? headerValue[0] : headerValue)
|
|
101
|
+
?? undefined
|
|
102
|
+
if (caller) {
|
|
103
|
+
;(request as any).guardCaller = caller
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function runHooks(
|
|
110
|
+
hooks: FastifyHookHandler[],
|
|
111
|
+
request: FastifyRequest,
|
|
112
|
+
reply: FastifyReply,
|
|
113
|
+
): Promise<boolean> {
|
|
114
|
+
for (const hook of hooks) {
|
|
115
|
+
if (reply.sent) return true
|
|
116
|
+
await hook(request, reply)
|
|
117
|
+
}
|
|
118
|
+
return reply.sent
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function sendResult(request: FastifyRequest, reply: FastifyReply): void {
|
|
122
|
+
const req = request as any
|
|
123
|
+
const data = req.resultData
|
|
124
|
+
const status = req.resultStatus ?? 200
|
|
125
|
+
if (data === undefined) {
|
|
126
|
+
reply.code(500).send({ message: 'No data set by handler' })
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
reply.code(status).send(transformResult(data))
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function sendError(reply: FastifyReply, error: unknown): void {
|
|
133
|
+
const httpError = mapError(error)
|
|
134
|
+
reply.code(httpError.status).send({ message: httpError.message })
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function ${routerFunctionName}(
|
|
138
|
+
fastify: FastifyInstance,
|
|
139
|
+
config: RouteConfig = {},
|
|
140
|
+
) {
|
|
141
|
+
const customPrefix = normalizePrefix(config.customUrlPrefix || '')
|
|
142
|
+
const modelPrefix = config.addModelPrefix !== false ? '/${modelNameLower}' : ''
|
|
143
|
+
const basePath = customPrefix + modelPrefix
|
|
144
|
+
|
|
145
|
+
const openApiDisabled = config.disableOpenApi === true
|
|
146
|
+
|| (config.disableOpenApi !== false && (
|
|
147
|
+
_env.DISABLE_OPENAPI === 'true'
|
|
148
|
+
|| _env.NODE_ENV === 'production'
|
|
149
|
+
))
|
|
150
|
+
|
|
151
|
+
const qbEnabled = isQueryBuilderEnabled(config)
|
|
152
|
+
|
|
153
|
+
if (qbEnabled) {
|
|
154
|
+
const qbConfig = getQueryBuilderConfig(config)
|
|
155
|
+
if (qbConfig) {
|
|
156
|
+
try { require('../queryBuilder').startQueryBuilder(qbConfig) } catch (err) { if (_env.NODE_ENV !== 'production') console.warn('[query-builder]', err) }
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
fastify.setErrorHandler((error, _request, reply) => {
|
|
161
|
+
const status = (error as any).status ?? error.statusCode ?? 500
|
|
162
|
+
const message = error.message || 'Internal server error'
|
|
163
|
+
if (!reply.sent) {
|
|
164
|
+
reply.code(status).send({ message })
|
|
165
|
+
}
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
if (!openApiDisabled) {
|
|
169
|
+
const openapiJsonPath = basePath ? \`\${basePath}/openapi.json\` : '/openapi.json'
|
|
170
|
+
const openapiYamlPath = basePath ? \`\${basePath}/openapi.yaml\` : '/openapi.yaml'
|
|
171
|
+
|
|
172
|
+
fastify.get(openapiJsonPath, async (_request, reply) => {
|
|
173
|
+
const spec = buildModelOpenApi(
|
|
174
|
+
'${modelName}',
|
|
175
|
+
MODEL_FIELDS as any,
|
|
176
|
+
MODEL_ENUMS as any,
|
|
177
|
+
config,
|
|
178
|
+
{ format: 'json' },
|
|
179
|
+
)
|
|
180
|
+
return reply.send(spec)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
fastify.get(openapiYamlPath, async (_request, reply) => {
|
|
184
|
+
const spec = buildModelOpenApi(
|
|
185
|
+
'${modelName}',
|
|
186
|
+
MODEL_FIELDS as any,
|
|
187
|
+
MODEL_ENUMS as any,
|
|
188
|
+
config,
|
|
189
|
+
{ format: 'yaml' },
|
|
190
|
+
)
|
|
191
|
+
return reply.type('application/yaml').send(spec as string)
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (config.enableAll || config.findFirst) {
|
|
196
|
+
const opConfig = config.findFirst || defaultOpConfig
|
|
197
|
+
const { before = [], after = [] } = opConfig
|
|
198
|
+
const path = basePath ? \`\${basePath}/first\` : '/first'
|
|
199
|
+
fastify.get(path, async (request, reply) => {
|
|
200
|
+
try {
|
|
201
|
+
parseQueryHook(request)
|
|
202
|
+
makeShapeHook(config, opConfig)(request)
|
|
203
|
+
if (await runHooks(before, request, reply)) return
|
|
204
|
+
await ${modelName}FindFirst(request, reply)
|
|
205
|
+
if (await runHooks(after, request, reply)) return
|
|
206
|
+
sendResult(request, reply)
|
|
207
|
+
} catch (error: unknown) {
|
|
208
|
+
sendError(reply, error)
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (config.enableAll || config.findFirstOrThrow) {
|
|
214
|
+
const opConfig = config.findFirstOrThrow || defaultOpConfig
|
|
215
|
+
const { before = [], after = [] } = opConfig
|
|
216
|
+
const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
|
|
217
|
+
fastify.get(path, async (request, reply) => {
|
|
218
|
+
try {
|
|
219
|
+
parseQueryHook(request)
|
|
220
|
+
makeShapeHook(config, opConfig)(request)
|
|
221
|
+
if (await runHooks(before, request, reply)) return
|
|
222
|
+
await ${modelName}FindFirstOrThrow(request, reply)
|
|
223
|
+
if (await runHooks(after, request, reply)) return
|
|
224
|
+
sendResult(request, reply)
|
|
225
|
+
} catch (error: unknown) {
|
|
226
|
+
sendError(reply, error)
|
|
227
|
+
}
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (config.enableAll || config.findManyPaginated) {
|
|
232
|
+
const opConfig = config.findManyPaginated || defaultOpConfig
|
|
233
|
+
const { before = [], after = [] } = opConfig
|
|
234
|
+
const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
|
|
235
|
+
fastify.get(path, async (request, reply) => {
|
|
236
|
+
try {
|
|
237
|
+
parseQueryHook(request)
|
|
238
|
+
makeShapeHook(config, opConfig)(request)
|
|
239
|
+
if (await runHooks(before, request, reply)) return
|
|
240
|
+
await ${modelName}FindManyPaginated(request, reply)
|
|
241
|
+
if (await runHooks(after, request, reply)) return
|
|
242
|
+
sendResult(request, reply)
|
|
243
|
+
} catch (error: unknown) {
|
|
244
|
+
sendError(reply, error)
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (config.enableAll || config.aggregate) {
|
|
250
|
+
const opConfig = config.aggregate || defaultOpConfig
|
|
251
|
+
const { before = [], after = [] } = opConfig
|
|
252
|
+
const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
|
|
253
|
+
fastify.get(path, async (request, reply) => {
|
|
254
|
+
try {
|
|
255
|
+
parseQueryHook(request)
|
|
256
|
+
makeShapeHook(config, opConfig)(request)
|
|
257
|
+
if (await runHooks(before, request, reply)) return
|
|
258
|
+
await ${modelName}Aggregate(request, reply)
|
|
259
|
+
if (await runHooks(after, request, reply)) return
|
|
260
|
+
sendResult(request, reply)
|
|
261
|
+
} catch (error: unknown) {
|
|
262
|
+
sendError(reply, error)
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (config.enableAll || config.count) {
|
|
268
|
+
const opConfig = config.count || defaultOpConfig
|
|
269
|
+
const { before = [], after = [] } = opConfig
|
|
270
|
+
const path = basePath ? \`\${basePath}/count\` : '/count'
|
|
271
|
+
fastify.get(path, async (request, reply) => {
|
|
272
|
+
try {
|
|
273
|
+
parseQueryHook(request)
|
|
274
|
+
makeShapeHook(config, opConfig)(request)
|
|
275
|
+
if (await runHooks(before, request, reply)) return
|
|
276
|
+
await ${modelName}Count(request, reply)
|
|
277
|
+
if (await runHooks(after, request, reply)) return
|
|
278
|
+
sendResult(request, reply)
|
|
279
|
+
} catch (error: unknown) {
|
|
280
|
+
sendError(reply, error)
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (config.enableAll || config.groupBy) {
|
|
286
|
+
const opConfig = config.groupBy || defaultOpConfig
|
|
287
|
+
const { before = [], after = [] } = opConfig
|
|
288
|
+
const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
|
|
289
|
+
fastify.get(path, async (request, reply) => {
|
|
290
|
+
try {
|
|
291
|
+
parseQueryHook(request)
|
|
292
|
+
makeShapeHook(config, opConfig)(request)
|
|
293
|
+
if (await runHooks(before, request, reply)) return
|
|
294
|
+
await ${modelName}GroupBy(request, reply)
|
|
295
|
+
if (await runHooks(after, request, reply)) return
|
|
296
|
+
sendResult(request, reply)
|
|
297
|
+
} catch (error: unknown) {
|
|
298
|
+
sendError(reply, error)
|
|
299
|
+
}
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (config.enableAll || config.findUniqueOrThrow) {
|
|
304
|
+
const opConfig = config.findUniqueOrThrow || defaultOpConfig
|
|
305
|
+
const { before = [], after = [] } = opConfig
|
|
306
|
+
const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
|
|
307
|
+
fastify.get(path, async (request, reply) => {
|
|
308
|
+
try {
|
|
309
|
+
parseQueryHook(request)
|
|
310
|
+
makeShapeHook(config, opConfig)(request)
|
|
311
|
+
if (await runHooks(before, request, reply)) return
|
|
312
|
+
await ${modelName}FindUniqueOrThrow(request, reply)
|
|
313
|
+
if (await runHooks(after, request, reply)) return
|
|
314
|
+
sendResult(request, reply)
|
|
315
|
+
} catch (error: unknown) {
|
|
316
|
+
sendError(reply, error)
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (config.enableAll || config.findUnique) {
|
|
322
|
+
const opConfig = config.findUnique || defaultOpConfig
|
|
323
|
+
const { before = [], after = [] } = opConfig
|
|
324
|
+
const path = basePath ? \`\${basePath}/unique\` : '/unique'
|
|
325
|
+
fastify.get(path, async (request, reply) => {
|
|
326
|
+
try {
|
|
327
|
+
parseQueryHook(request)
|
|
328
|
+
makeShapeHook(config, opConfig)(request)
|
|
329
|
+
if (await runHooks(before, request, reply)) return
|
|
330
|
+
await ${modelName}FindUnique(request, reply)
|
|
331
|
+
if (await runHooks(after, request, reply)) return
|
|
332
|
+
sendResult(request, reply)
|
|
333
|
+
} catch (error: unknown) {
|
|
334
|
+
sendError(reply, error)
|
|
335
|
+
}
|
|
336
|
+
})
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (config.enableAll || config.findMany) {
|
|
340
|
+
const opConfig = config.findMany || defaultOpConfig
|
|
341
|
+
const { before = [], after = [] } = opConfig
|
|
342
|
+
const path = basePath || '/'
|
|
343
|
+
fastify.get(path, async (request, reply) => {
|
|
344
|
+
try {
|
|
345
|
+
parseQueryHook(request)
|
|
346
|
+
makeShapeHook(config, opConfig)(request)
|
|
347
|
+
if (await runHooks(before, request, reply)) return
|
|
348
|
+
await ${modelName}FindMany(request, reply)
|
|
349
|
+
if (await runHooks(after, request, reply)) return
|
|
350
|
+
sendResult(request, reply)
|
|
351
|
+
} catch (error: unknown) {
|
|
352
|
+
sendError(reply, error)
|
|
353
|
+
}
|
|
354
|
+
})
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (config.enableAll || config.createManyAndReturn) {
|
|
358
|
+
const opConfig = config.createManyAndReturn || defaultOpConfig
|
|
359
|
+
const { before = [], after = [] } = opConfig
|
|
360
|
+
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
361
|
+
fastify.post(path, async (request, reply) => {
|
|
362
|
+
try {
|
|
363
|
+
makeShapeHook(config, opConfig)(request)
|
|
364
|
+
if (await runHooks(before, request, reply)) return
|
|
365
|
+
await ${modelName}CreateManyAndReturn(request, reply)
|
|
366
|
+
if (await runHooks(after, request, reply)) return
|
|
367
|
+
sendResult(request, reply)
|
|
368
|
+
} catch (error: unknown) {
|
|
369
|
+
sendError(reply, error)
|
|
370
|
+
}
|
|
371
|
+
})
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (config.enableAll || config.createMany) {
|
|
375
|
+
const opConfig = config.createMany || defaultOpConfig
|
|
376
|
+
const { before = [], after = [] } = opConfig
|
|
377
|
+
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
378
|
+
fastify.post(path, async (request, reply) => {
|
|
379
|
+
try {
|
|
380
|
+
makeShapeHook(config, opConfig)(request)
|
|
381
|
+
if (await runHooks(before, request, reply)) return
|
|
382
|
+
await ${modelName}CreateMany(request, reply)
|
|
383
|
+
if (await runHooks(after, request, reply)) return
|
|
384
|
+
sendResult(request, reply)
|
|
385
|
+
} catch (error: unknown) {
|
|
386
|
+
sendError(reply, error)
|
|
387
|
+
}
|
|
388
|
+
})
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (config.enableAll || config.create) {
|
|
392
|
+
const opConfig = config.create || defaultOpConfig
|
|
393
|
+
const { before = [], after = [] } = opConfig
|
|
394
|
+
const path = basePath || '/'
|
|
395
|
+
fastify.post(path, async (request, reply) => {
|
|
396
|
+
try {
|
|
397
|
+
makeShapeHook(config, opConfig)(request)
|
|
398
|
+
if (await runHooks(before, request, reply)) return
|
|
399
|
+
await ${modelName}Create(request, reply)
|
|
400
|
+
if (await runHooks(after, request, reply)) return
|
|
401
|
+
sendResult(request, reply)
|
|
402
|
+
} catch (error: unknown) {
|
|
403
|
+
sendError(reply, error)
|
|
404
|
+
}
|
|
405
|
+
})
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (config.enableAll || config.updateManyAndReturn) {
|
|
409
|
+
const opConfig = config.updateManyAndReturn || defaultOpConfig
|
|
410
|
+
const { before = [], after = [] } = opConfig
|
|
411
|
+
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
412
|
+
fastify.put(path, async (request, reply) => {
|
|
413
|
+
try {
|
|
414
|
+
makeShapeHook(config, opConfig)(request)
|
|
415
|
+
if (await runHooks(before, request, reply)) return
|
|
416
|
+
await ${modelName}UpdateManyAndReturn(request, reply)
|
|
417
|
+
if (await runHooks(after, request, reply)) return
|
|
418
|
+
sendResult(request, reply)
|
|
419
|
+
} catch (error: unknown) {
|
|
420
|
+
sendError(reply, error)
|
|
421
|
+
}
|
|
422
|
+
})
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (config.enableAll || config.updateMany) {
|
|
426
|
+
const opConfig = config.updateMany || defaultOpConfig
|
|
427
|
+
const { before = [], after = [] } = opConfig
|
|
428
|
+
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
429
|
+
fastify.put(path, async (request, reply) => {
|
|
430
|
+
try {
|
|
431
|
+
makeShapeHook(config, opConfig)(request)
|
|
432
|
+
if (await runHooks(before, request, reply)) return
|
|
433
|
+
await ${modelName}UpdateMany(request, reply)
|
|
434
|
+
if (await runHooks(after, request, reply)) return
|
|
435
|
+
sendResult(request, reply)
|
|
436
|
+
} catch (error: unknown) {
|
|
437
|
+
sendError(reply, error)
|
|
438
|
+
}
|
|
439
|
+
})
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (config.enableAll || config.update) {
|
|
443
|
+
const opConfig = config.update || defaultOpConfig
|
|
444
|
+
const { before = [], after = [] } = opConfig
|
|
445
|
+
const path = basePath || '/'
|
|
446
|
+
fastify.put(path, async (request, reply) => {
|
|
447
|
+
try {
|
|
448
|
+
makeShapeHook(config, opConfig)(request)
|
|
449
|
+
if (await runHooks(before, request, reply)) return
|
|
450
|
+
await ${modelName}Update(request, reply)
|
|
451
|
+
if (await runHooks(after, request, reply)) return
|
|
452
|
+
sendResult(request, reply)
|
|
453
|
+
} catch (error: unknown) {
|
|
454
|
+
sendError(reply, error)
|
|
455
|
+
}
|
|
456
|
+
})
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (config.enableAll || config.upsert) {
|
|
460
|
+
const opConfig = config.upsert || defaultOpConfig
|
|
461
|
+
const { before = [], after = [] } = opConfig
|
|
462
|
+
const path = basePath || '/'
|
|
463
|
+
fastify.patch(path, async (request, reply) => {
|
|
464
|
+
try {
|
|
465
|
+
makeShapeHook(config, opConfig)(request)
|
|
466
|
+
if (await runHooks(before, request, reply)) return
|
|
467
|
+
await ${modelName}Upsert(request, reply)
|
|
468
|
+
if (await runHooks(after, request, reply)) return
|
|
469
|
+
sendResult(request, reply)
|
|
470
|
+
} catch (error: unknown) {
|
|
471
|
+
sendError(reply, error)
|
|
472
|
+
}
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (config.enableAll || config.deleteMany) {
|
|
477
|
+
const opConfig = config.deleteMany || defaultOpConfig
|
|
478
|
+
const { before = [], after = [] } = opConfig
|
|
479
|
+
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
480
|
+
fastify.delete(path, async (request, reply) => {
|
|
481
|
+
try {
|
|
482
|
+
makeShapeHook(config, opConfig)(request)
|
|
483
|
+
if (await runHooks(before, request, reply)) return
|
|
484
|
+
await ${modelName}DeleteMany(request, reply)
|
|
485
|
+
if (await runHooks(after, request, reply)) return
|
|
486
|
+
sendResult(request, reply)
|
|
487
|
+
} catch (error: unknown) {
|
|
488
|
+
sendError(reply, error)
|
|
489
|
+
}
|
|
490
|
+
})
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (config.enableAll || config.delete) {
|
|
494
|
+
const opConfig = config.delete || defaultOpConfig
|
|
495
|
+
const { before = [], after = [] } = opConfig
|
|
496
|
+
const path = basePath || '/'
|
|
497
|
+
fastify.delete(path, async (request, reply) => {
|
|
498
|
+
try {
|
|
499
|
+
makeShapeHook(config, opConfig)(request)
|
|
500
|
+
if (await runHooks(before, request, reply)) return
|
|
501
|
+
await ${modelName}Delete(request, reply)
|
|
502
|
+
if (await runHooks(after, request, reply)) return
|
|
503
|
+
sendResult(request, reply)
|
|
504
|
+
} catch (error: unknown) {
|
|
505
|
+
sendError(reply, error)
|
|
506
|
+
}
|
|
507
|
+
})
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
`;
|
|
511
|
+
}
|
|
512
|
+
//# sourceMappingURL=generateRouterFastify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;AAEA,sEAugBC;AAvgBD,SAAgB,6BAA6B,CAAC,EAC5C,KAAK,EACL,KAAK,GAIN;IACC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,cAAc,GAAG,SAAS,CAAC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