prisma-generator-express 1.34.0 → 1.34.2
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/generators/generateFastifyHandler.js +12 -4
- package/dist/generators/generateFastifyHandler.js.map +1 -1
- package/dist/generators/generateOperationCore.js +1 -1
- package/dist/generators/generateOperationCore.js.map +1 -1
- package/dist/generators/generateRouter.js +39 -37
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.js +41 -39
- package/dist/generators/generateRouterFastify.js.map +1 -1
- package/dist/generators/generateUnifiedHandler.js +10 -2
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/utils/strings.js +4 -1
- package/dist/utils/strings.js.map +1 -1
- package/package.json +1 -1
- package/src/generators/generateFastifyHandler.ts +14 -4
- package/src/generators/generateOperationCore.ts +1 -1
- package/src/generators/generateRouter.ts +39 -37
- package/src/generators/generateRouterFastify.ts +41 -39
- package/src/generators/generateUnifiedHandler.ts +12 -2
- package/src/utils/strings.ts +4 -1
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateFastifyHandler = generateFastifyHandler;
|
|
4
|
+
const strings_js_1 = require("../utils/strings.js");
|
|
5
|
+
const CORE_NAME_MAP = {
|
|
6
|
+
delete: 'deleteUnique',
|
|
7
|
+
};
|
|
8
|
+
function coreFnName(op) {
|
|
9
|
+
return CORE_NAME_MAP[op] || op;
|
|
10
|
+
}
|
|
4
11
|
const READ_OPS = [
|
|
5
12
|
'findMany',
|
|
6
13
|
'findFirst',
|
|
@@ -30,26 +37,27 @@ const CREATED_OPS = new Set([
|
|
|
30
37
|
]);
|
|
31
38
|
function generateFastifyHandler(options) {
|
|
32
39
|
const modelName = options.model.name;
|
|
40
|
+
const prefix = (0, strings_js_1.toCamelCase)(modelName);
|
|
33
41
|
const readHandlers = READ_OPS.map((op) => {
|
|
34
|
-
const exportName = `${
|
|
42
|
+
const exportName = `${prefix}${op.charAt(0).toUpperCase() + op.slice(1)}`;
|
|
35
43
|
return `
|
|
36
44
|
export async function ${exportName}(
|
|
37
45
|
request: FastifyRequest,
|
|
38
46
|
_reply: FastifyReply,
|
|
39
47
|
): Promise<void> {
|
|
40
|
-
const data = await core.${op}(buildContext(request))
|
|
48
|
+
const data = await core.${coreFnName(op)}(buildContext(request))
|
|
41
49
|
;(request as any).resultData = data
|
|
42
50
|
}`;
|
|
43
51
|
}).join('\n');
|
|
44
52
|
const writeHandlers = WRITE_OPS.map((op) => {
|
|
45
|
-
const exportName = `${
|
|
53
|
+
const exportName = `${prefix}${op.charAt(0).toUpperCase() + op.slice(1)}`;
|
|
46
54
|
const statusCode = CREATED_OPS.has(op) ? 201 : 200;
|
|
47
55
|
return `
|
|
48
56
|
export async function ${exportName}(
|
|
49
57
|
request: FastifyRequest,
|
|
50
58
|
_reply: FastifyReply,
|
|
51
59
|
): Promise<void> {
|
|
52
|
-
const data = await core.${op}(buildContext(request))
|
|
60
|
+
const data = await core.${coreFnName(op)}(buildContext(request))
|
|
53
61
|
;(request as any).resultData = data
|
|
54
62
|
;(request as any).resultStatus = ${statusCode}
|
|
55
63
|
}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateFastifyHandler.js","sourceRoot":"","sources":["../../src/generators/generateFastifyHandler.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"generateFastifyHandler.js","sourceRoot":"","sources":["../../src/generators/generateFastifyHandler.ts"],"names":[],"mappings":";;AAyCA,wDAsDC;AA9FD,oDAAiD;AAEjD,MAAM,aAAa,GAA2B;IAC5C,MAAM,EAAE,cAAc;CACvB,CAAA;AAED,SAAS,UAAU,CAAC,EAAU;IAC5B,OAAO,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,CAAA;AAChC,CAAC;AAED,MAAM,QAAQ,GAAG;IACf,UAAU;IACV,WAAW;IACX,kBAAkB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;;qCAEL,UAAU;EAC7C,CAAA;IACA,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAEb,OAAO;2BACkB,SAAS;;;;;;;;;;;;;;;;EAgBlC,YAAY;EACZ,aAAa;CACd,CAAA;AACD,CAAC"}
|
|
@@ -407,7 +407,7 @@ export async function ${op}(ctx: OperationContext): Promise<unknown> {
|
|
|
407
407
|
method: 'updateManyAndReturn',
|
|
408
408
|
requiredFields: ['where', 'data'],
|
|
409
409
|
},
|
|
410
|
-
{ name: '
|
|
410
|
+
{ name: 'deleteUnique', method: 'delete', requiredFields: ['where'] },
|
|
411
411
|
{
|
|
412
412
|
name: 'deleteMany',
|
|
413
413
|
method: 'deleteMany',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateOperationCore.js","sourceRoot":"","sources":["../../src/generators/generateOperationCore.ts"],"names":[],"mappings":";;AAEA,4DAqWC;AAMD,8CAmLC;AA9hBD,SAAgB,wBAAwB;IACtC,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmWR,CAAA;AACD,CAAC;AAMD,SAAgB,iBAAiB,CAAC,OAAyB;IACzD,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAA;IACpC,MAAM,cAAc,GAClB,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAExD,MAAM,eAAe,GAAG;QACtB,WAAW;QACX,YAAY;QACZ,mBAAmB;QACnB,kBAAkB;QAClB,OAAO;QACP,WAAW;QACX,SAAS;KACV,CAAA;IAED,MAAM,oBAAoB,GAAG,eAAe;SACzC,GAAG,CACF,CAAC,EAAE,EAAE,EAAE,CAAC;wBACU,EAAE;;;;oCAIU,cAAc;+BACnB,cAAc,2CAA2C,EAAE;;6BAE7D,cAAc,IAAI,EAAE;EAC/C,CACG;SACA,IAAI,CAAC,IAAI,CAAC,CAAA;IAEb,MAAM,QAAQ,GAAG;QACf,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,CAAC,MAAM,CAAC,EAAE;QAC9D,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC,MAAM,CAAC,EAAE;QACtE;YACE,IAAI,EAAE,qBAAqB;YAC3B,MAAM,EAAE,qBAAqB;YAC7B,cAAc,EAAE,CAAC,MAAM,CAAC;SACzB;QACD;YACE,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,QAAQ;YAChB,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC;SAClC;QACD;YACE,IAAI,EAAE,YAAY;YAClB,MAAM,EAAE,YAAY;YACpB,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC;SAClC;QACD;YACE,IAAI,EAAE,qBAAqB;YAC3B,MAAM,EAAE,qBAAqB;YAC7B,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC;SAClC;QACD,EAAE,IAAI,EAAE,
|
|
1
|
+
{"version":3,"file":"generateOperationCore.js","sourceRoot":"","sources":["../../src/generators/generateOperationCore.ts"],"names":[],"mappings":";;AAEA,4DAqWC;AAMD,8CAmLC;AA9hBD,SAAgB,wBAAwB;IACtC,OAAO;;;;;;;;;;;;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cAAc;;EAEzC,oBAAoB;EACpB,aAAa;;;;;;;;;;;;;oCAaqB,cAAc;;;;;;;;;;uBAU3B,cAAc;uBACd,cAAc;gDACW,cAAc;;;;;;;;;;;;;;sCAcxB,cAAc;sCACd,cAAc;;8BAEtB,cAAc;;;;;;;;;;;;kCAYV,cAAc;kCACd,cAAc;;0BAEtB,cAAc;;;;;;;;;;;;;;CAcvC,CAAA;AACD,CAAC"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateRouterFunction = generateRouterFunction;
|
|
4
|
+
const strings_js_1 = require("../utils/strings.js");
|
|
4
5
|
function generateRouterFunction({ model, enums, relativeClientPath, }) {
|
|
5
6
|
const modelName = model.name;
|
|
7
|
+
const prefix = (0, strings_js_1.toCamelCase)(modelName);
|
|
6
8
|
const modelNameLower = modelName.toLowerCase();
|
|
7
|
-
const routerFunctionName = `${
|
|
9
|
+
const routerFunctionName = `${prefix}Router`;
|
|
8
10
|
const fieldsMeta = model.fields.map((f) => ({
|
|
9
11
|
name: f.name,
|
|
10
12
|
kind: f.kind,
|
|
@@ -26,24 +28,24 @@ function generateRouterFunction({ model, enums, relativeClientPath, }) {
|
|
|
26
28
|
return `import express, { Request, Response, NextFunction, RequestHandler } from 'express'
|
|
27
29
|
import type { PrismaClient } from '${relativeClientPath}'
|
|
28
30
|
import {
|
|
29
|
-
${
|
|
30
|
-
${
|
|
31
|
-
${
|
|
32
|
-
${
|
|
33
|
-
${
|
|
34
|
-
${
|
|
35
|
-
${
|
|
36
|
-
${
|
|
37
|
-
${
|
|
38
|
-
${
|
|
39
|
-
${
|
|
40
|
-
${
|
|
41
|
-
${
|
|
42
|
-
${
|
|
43
|
-
${
|
|
44
|
-
${
|
|
45
|
-
${
|
|
46
|
-
${
|
|
31
|
+
${prefix}FindUnique,
|
|
32
|
+
${prefix}FindUniqueOrThrow,
|
|
33
|
+
${prefix}FindFirst,
|
|
34
|
+
${prefix}FindFirstOrThrow,
|
|
35
|
+
${prefix}FindMany,
|
|
36
|
+
${prefix}FindManyPaginated,
|
|
37
|
+
${prefix}Create,
|
|
38
|
+
${prefix}CreateMany,
|
|
39
|
+
${prefix}CreateManyAndReturn,
|
|
40
|
+
${prefix}Update,
|
|
41
|
+
${prefix}UpdateMany,
|
|
42
|
+
${prefix}UpdateManyAndReturn,
|
|
43
|
+
${prefix}Upsert,
|
|
44
|
+
${prefix}Delete,
|
|
45
|
+
${prefix}DeleteMany,
|
|
46
|
+
${prefix}Aggregate,
|
|
47
|
+
${prefix}Count,
|
|
48
|
+
${prefix}GroupBy
|
|
47
49
|
} from './${modelName}Handlers.js'
|
|
48
50
|
import type { RouteConfig } from '../routeConfig.target.js'
|
|
49
51
|
import { parseQueryParams } from '../parseQueryParams.js'
|
|
@@ -178,126 +180,126 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
|
|
|
178
180
|
const opConfig = config.findFirst || defaultOpConfig
|
|
179
181
|
const { before = [], after = [] } = opConfig
|
|
180
182
|
const path = basePath ? \`\${basePath}/first\` : '/first'
|
|
181
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
183
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindFirst as RequestHandler, ...after, respond)
|
|
182
184
|
}
|
|
183
185
|
|
|
184
186
|
if (config.enableAll || config.findFirstOrThrow) {
|
|
185
187
|
const opConfig = config.findFirstOrThrow || defaultOpConfig
|
|
186
188
|
const { before = [], after = [] } = opConfig
|
|
187
189
|
const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
|
|
188
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
190
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindFirstOrThrow as RequestHandler, ...after, respond)
|
|
189
191
|
}
|
|
190
192
|
|
|
191
193
|
if (config.enableAll || config.findManyPaginated) {
|
|
192
194
|
const opConfig = config.findManyPaginated || defaultOpConfig
|
|
193
195
|
const { before = [], after = [] } = opConfig
|
|
194
196
|
const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
|
|
195
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
197
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindManyPaginated as RequestHandler, ...after, respond)
|
|
196
198
|
}
|
|
197
199
|
|
|
198
200
|
if (config.enableAll || config.aggregate) {
|
|
199
201
|
const opConfig = config.aggregate || defaultOpConfig
|
|
200
202
|
const { before = [], after = [] } = opConfig
|
|
201
203
|
const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
|
|
202
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
204
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}Aggregate as RequestHandler, ...after, respond)
|
|
203
205
|
}
|
|
204
206
|
|
|
205
207
|
if (config.enableAll || config.count) {
|
|
206
208
|
const opConfig = config.count || defaultOpConfig
|
|
207
209
|
const { before = [], after = [] } = opConfig
|
|
208
210
|
const path = basePath ? \`\${basePath}/count\` : '/count'
|
|
209
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
211
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}Count as RequestHandler, ...after, respond)
|
|
210
212
|
}
|
|
211
213
|
|
|
212
214
|
if (config.enableAll || config.groupBy) {
|
|
213
215
|
const opConfig = config.groupBy || defaultOpConfig
|
|
214
216
|
const { before = [], after = [] } = opConfig
|
|
215
217
|
const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
|
|
216
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
218
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}GroupBy as RequestHandler, ...after, respond)
|
|
217
219
|
}
|
|
218
220
|
|
|
219
221
|
if (config.enableAll || config.findUniqueOrThrow) {
|
|
220
222
|
const opConfig = config.findUniqueOrThrow || defaultOpConfig
|
|
221
223
|
const { before = [], after = [] } = opConfig
|
|
222
224
|
const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
|
|
223
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
225
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindUniqueOrThrow as RequestHandler, ...after, respond)
|
|
224
226
|
}
|
|
225
227
|
|
|
226
228
|
if (config.enableAll || config.findUnique) {
|
|
227
229
|
const opConfig = config.findUnique || defaultOpConfig
|
|
228
230
|
const { before = [], after = [] } = opConfig
|
|
229
231
|
const path = basePath ? \`\${basePath}/unique\` : '/unique'
|
|
230
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
232
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindUnique as RequestHandler, ...after, respond)
|
|
231
233
|
}
|
|
232
234
|
|
|
233
235
|
if (config.enableAll || config.findMany) {
|
|
234
236
|
const opConfig = config.findMany || defaultOpConfig
|
|
235
237
|
const { before = [], after = [] } = opConfig
|
|
236
238
|
const path = basePath || '/'
|
|
237
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
239
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindMany as RequestHandler, ...after, respond)
|
|
238
240
|
}
|
|
239
241
|
|
|
240
242
|
if (config.enableAll || config.createManyAndReturn) {
|
|
241
243
|
const opConfig = config.createManyAndReturn || defaultOpConfig
|
|
242
244
|
const { before = [], after = [] } = opConfig
|
|
243
245
|
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
244
|
-
router.post(path, setShape(opConfig), ...before, ${
|
|
246
|
+
router.post(path, setShape(opConfig), ...before, ${prefix}CreateManyAndReturn as RequestHandler, ...after, respondCreated)
|
|
245
247
|
}
|
|
246
248
|
|
|
247
249
|
if (config.enableAll || config.createMany) {
|
|
248
250
|
const opConfig = config.createMany || defaultOpConfig
|
|
249
251
|
const { before = [], after = [] } = opConfig
|
|
250
252
|
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
251
|
-
router.post(path, setShape(opConfig), ...before, ${
|
|
253
|
+
router.post(path, setShape(opConfig), ...before, ${prefix}CreateMany as RequestHandler, ...after, respondCreated)
|
|
252
254
|
}
|
|
253
255
|
|
|
254
256
|
if (config.enableAll || config.create) {
|
|
255
257
|
const opConfig = config.create || defaultOpConfig
|
|
256
258
|
const { before = [], after = [] } = opConfig
|
|
257
259
|
const path = basePath || '/'
|
|
258
|
-
router.post(path, setShape(opConfig), ...before, ${
|
|
260
|
+
router.post(path, setShape(opConfig), ...before, ${prefix}Create as RequestHandler, ...after, respondCreated)
|
|
259
261
|
}
|
|
260
262
|
|
|
261
263
|
if (config.enableAll || config.updateManyAndReturn) {
|
|
262
264
|
const opConfig = config.updateManyAndReturn || defaultOpConfig
|
|
263
265
|
const { before = [], after = [] } = opConfig
|
|
264
266
|
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
265
|
-
router.put(path, setShape(opConfig), ...before, ${
|
|
267
|
+
router.put(path, setShape(opConfig), ...before, ${prefix}UpdateManyAndReturn as RequestHandler, ...after, respond)
|
|
266
268
|
}
|
|
267
269
|
|
|
268
270
|
if (config.enableAll || config.updateMany) {
|
|
269
271
|
const opConfig = config.updateMany || defaultOpConfig
|
|
270
272
|
const { before = [], after = [] } = opConfig
|
|
271
273
|
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
272
|
-
router.put(path, setShape(opConfig), ...before, ${
|
|
274
|
+
router.put(path, setShape(opConfig), ...before, ${prefix}UpdateMany as RequestHandler, ...after, respond)
|
|
273
275
|
}
|
|
274
276
|
|
|
275
277
|
if (config.enableAll || config.update) {
|
|
276
278
|
const opConfig = config.update || defaultOpConfig
|
|
277
279
|
const { before = [], after = [] } = opConfig
|
|
278
280
|
const path = basePath || '/'
|
|
279
|
-
router.put(path, setShape(opConfig), ...before, ${
|
|
281
|
+
router.put(path, setShape(opConfig), ...before, ${prefix}Update as RequestHandler, ...after, respond)
|
|
280
282
|
}
|
|
281
283
|
|
|
282
284
|
if (config.enableAll || config.upsert) {
|
|
283
285
|
const opConfig = config.upsert || defaultOpConfig
|
|
284
286
|
const { before = [], after = [] } = opConfig
|
|
285
287
|
const path = basePath || '/'
|
|
286
|
-
router.patch(path, setShape(opConfig), ...before, ${
|
|
288
|
+
router.patch(path, setShape(opConfig), ...before, ${prefix}Upsert as RequestHandler, ...after, respond)
|
|
287
289
|
}
|
|
288
290
|
|
|
289
291
|
if (config.enableAll || config.deleteMany) {
|
|
290
292
|
const opConfig = config.deleteMany || defaultOpConfig
|
|
291
293
|
const { before = [], after = [] } = opConfig
|
|
292
294
|
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
293
|
-
router.delete(path, setShape(opConfig), ...before, ${
|
|
295
|
+
router.delete(path, setShape(opConfig), ...before, ${prefix}DeleteMany as RequestHandler, ...after, respond)
|
|
294
296
|
}
|
|
295
297
|
|
|
296
298
|
if (config.enableAll || config.delete) {
|
|
297
299
|
const opConfig = config.delete || defaultOpConfig
|
|
298
300
|
const { before = [], after = [] } = opConfig
|
|
299
301
|
const path = basePath || '/'
|
|
300
|
-
router.delete(path, setShape(opConfig), ...before, ${
|
|
302
|
+
router.delete(path, setShape(opConfig), ...before, ${prefix}Delete as RequestHandler, ...after, respond)
|
|
301
303
|
}
|
|
302
304
|
|
|
303
305
|
router.use((err: any, _req: Request, res: Response, next: NextFunction) => {
|
|
@@ -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":";;AAGA,wDAsUC;AAxUD,oDAAiD;AAEjD,SAAgB,sBAAsB,CAAC,EACrC,KAAK,EACL,KAAK,EACL,kBAAkB,GAKnB;IACC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,MAAM,GAAG,IAAA,w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OAAO;qCAC4B,kBAAkB;;IAEnD,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;YACE,SAAS;;;;;;;;uBAQE,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;;sBAEpC,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBA6BtC,kBAAkB;;;;;;4DAMwB,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAgE/D,SAAS;;;;;;;;;;;WAWT,SAAS;;;;;;;;;;;;;;kEAc8C,MAAM;;;;;;;kEAON,MAAM;;;;;;;kEAON,MAAM;;;;;;;kEAON,MAAM;;;;;;;kEAON,MAAM;;;;;;;kEAON,MAAM;;;;;;;kEAON,MAAM;;;;;;;kEAON,MAAM;;;;;;;kEAON,MAAM;;;;;;;uDAOjB,MAAM;;;;;;;uDAON,MAAM;;;;;;;uDAON,MAAM;;;;;;;sDAOP,MAAM;;;;;;;sDAON,MAAM;;;;;;;sDAON,MAAM;;;;;;;wDAOJ,MAAM;;;;;;;yDAOL,MAAM;;;;;;;yDAON,MAAM;;;;;;;;;;;;;;CAc9D,CAAA;AACD,CAAC"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateFastifyRouterFunction = generateFastifyRouterFunction;
|
|
4
|
+
const strings_js_1 = require("../utils/strings.js");
|
|
4
5
|
function generateFastifyRouterFunction({ model, enums, }) {
|
|
5
6
|
const modelName = model.name;
|
|
7
|
+
const prefix = (0, strings_js_1.toCamelCase)(modelName);
|
|
6
8
|
const modelNameLower = modelName.toLowerCase();
|
|
7
|
-
const routerFunctionName = `${
|
|
9
|
+
const routerFunctionName = `${prefix}Router`;
|
|
8
10
|
const fieldsMeta = model.fields.map((f) => ({
|
|
9
11
|
name: f.name,
|
|
10
12
|
kind: f.kind,
|
|
@@ -23,26 +25,26 @@ function generateFastifyRouterFunction({ model, enums, }) {
|
|
|
23
25
|
name: e.name,
|
|
24
26
|
values: e.values.map((v) => ({ name: v.name })),
|
|
25
27
|
}));
|
|
26
|
-
return `import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
|
|
28
|
+
return `import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify'
|
|
27
29
|
import {
|
|
28
|
-
${
|
|
29
|
-
${
|
|
30
|
-
${
|
|
31
|
-
${
|
|
32
|
-
${
|
|
33
|
-
${
|
|
34
|
-
${
|
|
35
|
-
${
|
|
36
|
-
${
|
|
37
|
-
${
|
|
38
|
-
${
|
|
39
|
-
${
|
|
40
|
-
${
|
|
41
|
-
${
|
|
42
|
-
${
|
|
43
|
-
${
|
|
44
|
-
${
|
|
45
|
-
${
|
|
30
|
+
${prefix}FindUnique,
|
|
31
|
+
${prefix}FindUniqueOrThrow,
|
|
32
|
+
${prefix}FindFirst,
|
|
33
|
+
${prefix}FindFirstOrThrow,
|
|
34
|
+
${prefix}FindMany,
|
|
35
|
+
${prefix}FindManyPaginated,
|
|
36
|
+
${prefix}Create,
|
|
37
|
+
${prefix}CreateMany,
|
|
38
|
+
${prefix}CreateManyAndReturn,
|
|
39
|
+
${prefix}Update,
|
|
40
|
+
${prefix}UpdateMany,
|
|
41
|
+
${prefix}UpdateManyAndReturn,
|
|
42
|
+
${prefix}Upsert,
|
|
43
|
+
${prefix}Delete,
|
|
44
|
+
${prefix}DeleteMany,
|
|
45
|
+
${prefix}Aggregate,
|
|
46
|
+
${prefix}Count,
|
|
47
|
+
${prefix}GroupBy,
|
|
46
48
|
} from './${modelName}Handlers.js'
|
|
47
49
|
import type { RouteConfig, FastifyHookHandler } from '../routeConfig.target.js'
|
|
48
50
|
import { parseQueryParams } from '../parseQueryParams.js'
|
|
@@ -157,7 +159,7 @@ export async function ${routerFunctionName}(
|
|
|
157
159
|
}
|
|
158
160
|
}
|
|
159
161
|
|
|
160
|
-
fastify.setErrorHandler((error, _request, reply) => {
|
|
162
|
+
fastify.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
|
|
161
163
|
const status = (error as any).status ?? error.statusCode ?? 500
|
|
162
164
|
const message = error.message || 'Internal server error'
|
|
163
165
|
if (!reply.sent) {
|
|
@@ -201,7 +203,7 @@ export async function ${routerFunctionName}(
|
|
|
201
203
|
parseQueryHook(request)
|
|
202
204
|
makeShapeHook(config, opConfig)(request)
|
|
203
205
|
if (await runHooks(before, request, reply)) return
|
|
204
|
-
await ${
|
|
206
|
+
await ${prefix}FindFirst(request, reply)
|
|
205
207
|
if (await runHooks(after, request, reply)) return
|
|
206
208
|
sendResult(request, reply)
|
|
207
209
|
} catch (error: unknown) {
|
|
@@ -219,7 +221,7 @@ export async function ${routerFunctionName}(
|
|
|
219
221
|
parseQueryHook(request)
|
|
220
222
|
makeShapeHook(config, opConfig)(request)
|
|
221
223
|
if (await runHooks(before, request, reply)) return
|
|
222
|
-
await ${
|
|
224
|
+
await ${prefix}FindFirstOrThrow(request, reply)
|
|
223
225
|
if (await runHooks(after, request, reply)) return
|
|
224
226
|
sendResult(request, reply)
|
|
225
227
|
} catch (error: unknown) {
|
|
@@ -237,7 +239,7 @@ export async function ${routerFunctionName}(
|
|
|
237
239
|
parseQueryHook(request)
|
|
238
240
|
makeShapeHook(config, opConfig)(request)
|
|
239
241
|
if (await runHooks(before, request, reply)) return
|
|
240
|
-
await ${
|
|
242
|
+
await ${prefix}FindManyPaginated(request, reply)
|
|
241
243
|
if (await runHooks(after, request, reply)) return
|
|
242
244
|
sendResult(request, reply)
|
|
243
245
|
} catch (error: unknown) {
|
|
@@ -255,7 +257,7 @@ export async function ${routerFunctionName}(
|
|
|
255
257
|
parseQueryHook(request)
|
|
256
258
|
makeShapeHook(config, opConfig)(request)
|
|
257
259
|
if (await runHooks(before, request, reply)) return
|
|
258
|
-
await ${
|
|
260
|
+
await ${prefix}Aggregate(request, reply)
|
|
259
261
|
if (await runHooks(after, request, reply)) return
|
|
260
262
|
sendResult(request, reply)
|
|
261
263
|
} catch (error: unknown) {
|
|
@@ -273,7 +275,7 @@ export async function ${routerFunctionName}(
|
|
|
273
275
|
parseQueryHook(request)
|
|
274
276
|
makeShapeHook(config, opConfig)(request)
|
|
275
277
|
if (await runHooks(before, request, reply)) return
|
|
276
|
-
await ${
|
|
278
|
+
await ${prefix}Count(request, reply)
|
|
277
279
|
if (await runHooks(after, request, reply)) return
|
|
278
280
|
sendResult(request, reply)
|
|
279
281
|
} catch (error: unknown) {
|
|
@@ -291,7 +293,7 @@ export async function ${routerFunctionName}(
|
|
|
291
293
|
parseQueryHook(request)
|
|
292
294
|
makeShapeHook(config, opConfig)(request)
|
|
293
295
|
if (await runHooks(before, request, reply)) return
|
|
294
|
-
await ${
|
|
296
|
+
await ${prefix}GroupBy(request, reply)
|
|
295
297
|
if (await runHooks(after, request, reply)) return
|
|
296
298
|
sendResult(request, reply)
|
|
297
299
|
} catch (error: unknown) {
|
|
@@ -309,7 +311,7 @@ export async function ${routerFunctionName}(
|
|
|
309
311
|
parseQueryHook(request)
|
|
310
312
|
makeShapeHook(config, opConfig)(request)
|
|
311
313
|
if (await runHooks(before, request, reply)) return
|
|
312
|
-
await ${
|
|
314
|
+
await ${prefix}FindUniqueOrThrow(request, reply)
|
|
313
315
|
if (await runHooks(after, request, reply)) return
|
|
314
316
|
sendResult(request, reply)
|
|
315
317
|
} catch (error: unknown) {
|
|
@@ -327,7 +329,7 @@ export async function ${routerFunctionName}(
|
|
|
327
329
|
parseQueryHook(request)
|
|
328
330
|
makeShapeHook(config, opConfig)(request)
|
|
329
331
|
if (await runHooks(before, request, reply)) return
|
|
330
|
-
await ${
|
|
332
|
+
await ${prefix}FindUnique(request, reply)
|
|
331
333
|
if (await runHooks(after, request, reply)) return
|
|
332
334
|
sendResult(request, reply)
|
|
333
335
|
} catch (error: unknown) {
|
|
@@ -345,7 +347,7 @@ export async function ${routerFunctionName}(
|
|
|
345
347
|
parseQueryHook(request)
|
|
346
348
|
makeShapeHook(config, opConfig)(request)
|
|
347
349
|
if (await runHooks(before, request, reply)) return
|
|
348
|
-
await ${
|
|
350
|
+
await ${prefix}FindMany(request, reply)
|
|
349
351
|
if (await runHooks(after, request, reply)) return
|
|
350
352
|
sendResult(request, reply)
|
|
351
353
|
} catch (error: unknown) {
|
|
@@ -362,7 +364,7 @@ export async function ${routerFunctionName}(
|
|
|
362
364
|
try {
|
|
363
365
|
makeShapeHook(config, opConfig)(request)
|
|
364
366
|
if (await runHooks(before, request, reply)) return
|
|
365
|
-
await ${
|
|
367
|
+
await ${prefix}CreateManyAndReturn(request, reply)
|
|
366
368
|
if (await runHooks(after, request, reply)) return
|
|
367
369
|
sendResult(request, reply)
|
|
368
370
|
} catch (error: unknown) {
|
|
@@ -379,7 +381,7 @@ export async function ${routerFunctionName}(
|
|
|
379
381
|
try {
|
|
380
382
|
makeShapeHook(config, opConfig)(request)
|
|
381
383
|
if (await runHooks(before, request, reply)) return
|
|
382
|
-
await ${
|
|
384
|
+
await ${prefix}CreateMany(request, reply)
|
|
383
385
|
if (await runHooks(after, request, reply)) return
|
|
384
386
|
sendResult(request, reply)
|
|
385
387
|
} catch (error: unknown) {
|
|
@@ -396,7 +398,7 @@ export async function ${routerFunctionName}(
|
|
|
396
398
|
try {
|
|
397
399
|
makeShapeHook(config, opConfig)(request)
|
|
398
400
|
if (await runHooks(before, request, reply)) return
|
|
399
|
-
await ${
|
|
401
|
+
await ${prefix}Create(request, reply)
|
|
400
402
|
if (await runHooks(after, request, reply)) return
|
|
401
403
|
sendResult(request, reply)
|
|
402
404
|
} catch (error: unknown) {
|
|
@@ -413,7 +415,7 @@ export async function ${routerFunctionName}(
|
|
|
413
415
|
try {
|
|
414
416
|
makeShapeHook(config, opConfig)(request)
|
|
415
417
|
if (await runHooks(before, request, reply)) return
|
|
416
|
-
await ${
|
|
418
|
+
await ${prefix}UpdateManyAndReturn(request, reply)
|
|
417
419
|
if (await runHooks(after, request, reply)) return
|
|
418
420
|
sendResult(request, reply)
|
|
419
421
|
} catch (error: unknown) {
|
|
@@ -430,7 +432,7 @@ export async function ${routerFunctionName}(
|
|
|
430
432
|
try {
|
|
431
433
|
makeShapeHook(config, opConfig)(request)
|
|
432
434
|
if (await runHooks(before, request, reply)) return
|
|
433
|
-
await ${
|
|
435
|
+
await ${prefix}UpdateMany(request, reply)
|
|
434
436
|
if (await runHooks(after, request, reply)) return
|
|
435
437
|
sendResult(request, reply)
|
|
436
438
|
} catch (error: unknown) {
|
|
@@ -447,7 +449,7 @@ export async function ${routerFunctionName}(
|
|
|
447
449
|
try {
|
|
448
450
|
makeShapeHook(config, opConfig)(request)
|
|
449
451
|
if (await runHooks(before, request, reply)) return
|
|
450
|
-
await ${
|
|
452
|
+
await ${prefix}Update(request, reply)
|
|
451
453
|
if (await runHooks(after, request, reply)) return
|
|
452
454
|
sendResult(request, reply)
|
|
453
455
|
} catch (error: unknown) {
|
|
@@ -464,7 +466,7 @@ export async function ${routerFunctionName}(
|
|
|
464
466
|
try {
|
|
465
467
|
makeShapeHook(config, opConfig)(request)
|
|
466
468
|
if (await runHooks(before, request, reply)) return
|
|
467
|
-
await ${
|
|
469
|
+
await ${prefix}Upsert(request, reply)
|
|
468
470
|
if (await runHooks(after, request, reply)) return
|
|
469
471
|
sendResult(request, reply)
|
|
470
472
|
} catch (error: unknown) {
|
|
@@ -481,7 +483,7 @@ export async function ${routerFunctionName}(
|
|
|
481
483
|
try {
|
|
482
484
|
makeShapeHook(config, opConfig)(request)
|
|
483
485
|
if (await runHooks(before, request, reply)) return
|
|
484
|
-
await ${
|
|
486
|
+
await ${prefix}DeleteMany(request, reply)
|
|
485
487
|
if (await runHooks(after, request, reply)) return
|
|
486
488
|
sendResult(request, reply)
|
|
487
489
|
} catch (error: unknown) {
|
|
@@ -498,7 +500,7 @@ export async function ${routerFunctionName}(
|
|
|
498
500
|
try {
|
|
499
501
|
makeShapeHook(config, opConfig)(request)
|
|
500
502
|
if (await runHooks(before, request, reply)) return
|
|
501
|
-
await ${
|
|
503
|
+
await ${prefix}Delete(request, reply)
|
|
502
504
|
if (await runHooks(after, request, reply)) return
|
|
503
505
|
sendResult(request, reply)
|
|
504
506
|
} catch (error: unknown) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;AAGA,sEAwgBC;AA1gBD,oDAAiD;AAEjD,SAAgB,6BAA6B,CAAC,EAC5C,KAAK,EACL,KAAK,GAIN;IACC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,MAAM,GAAG,IAAA,wBAAW,EAAC,SAAS,CAAC,CAAA;IACrC,MAAM,cAAc,GAAG,SAAS,CAAC,WAAW,EAAE,CAAA;IAC9C,MAAM,kBAAkB,GAAG,GAAG,MAAM,QAAQ,CAAA;IAE5C,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,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAgC/D,SAAS;;;;;;;;;;;WAWT,SAAS;;;;;;;;;;;;;;;;;;;gBAmBJ,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;;gBAkBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;;;;;;;;;gBAiBN,MAAM;;;;;;;;;CASrB,CAAA;AACD,CAAC"}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateUnifiedHandler = generateUnifiedHandler;
|
|
4
|
+
const strings_js_1 = require("../utils/strings.js");
|
|
5
|
+
const CORE_NAME_MAP = {
|
|
6
|
+
delete: 'deleteUnique',
|
|
7
|
+
};
|
|
8
|
+
function coreFnName(op) {
|
|
9
|
+
return CORE_NAME_MAP[op] || op;
|
|
10
|
+
}
|
|
4
11
|
const ALL_OPS = [
|
|
5
12
|
'findMany',
|
|
6
13
|
'findFirst',
|
|
@@ -23,8 +30,9 @@ const ALL_OPS = [
|
|
|
23
30
|
];
|
|
24
31
|
function generateUnifiedHandler(options) {
|
|
25
32
|
const modelName = options.model.name;
|
|
33
|
+
const prefix = (0, strings_js_1.toCamelCase)(modelName);
|
|
26
34
|
const handlers = ALL_OPS.map((op) => {
|
|
27
|
-
const exportName = `${
|
|
35
|
+
const exportName = `${prefix}${op.charAt(0).toUpperCase() + op.slice(1)}`;
|
|
28
36
|
return `
|
|
29
37
|
export async function ${exportName}(
|
|
30
38
|
req: Request,
|
|
@@ -32,7 +40,7 @@ export async function ${exportName}(
|
|
|
32
40
|
next: NextFunction,
|
|
33
41
|
) {
|
|
34
42
|
try {
|
|
35
|
-
res.locals.data = await core.${op}(buildContext(req, res))
|
|
43
|
+
res.locals.data = await core.${coreFnName(op)}(buildContext(req, res))
|
|
36
44
|
next()
|
|
37
45
|
} catch (error: unknown) {
|
|
38
46
|
next(mapError(error))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateUnifiedHandler.js","sourceRoot":"","sources":["../../src/generators/generateUnifiedHandler.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"generateUnifiedHandler.js","sourceRoot":"","sources":["../../src/generators/generateUnifiedHandler.ts"],"names":[],"mappings":";;AAoCA,wDAwCC;AA3ED,oDAAiD;AAMjD,MAAM,aAAa,GAA2B;IAC5C,MAAM,EAAE,cAAc;CACvB,CAAA;AAED,SAAS,UAAU,CAAC,EAAU;IAC5B,OAAO,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,CAAA;AAChC,CAAC;AAED,MAAM,OAAO,GAAG;IACd,UAAU;IACV,WAAW;IACX,kBAAkB;IAClB,YAAY;IACZ,mBAAmB;IACnB,mBAAmB;IACnB,QAAQ;IACR,YAAY;IACZ,qBAAqB;IACrB,QAAQ;IACR,YAAY;IACZ,qBAAqB;IACrB,QAAQ;IACR,QAAQ;IACR,YAAY;IACZ,WAAW;IACX,OAAO;IACP,SAAS;CACV,CAAA;AAED,SAAgB,sBAAsB,CAAC,OAA8B;IACnE,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAA;IACpC,MAAM,MAAM,GAAG,IAAA,wBAAW,EAAC,SAAS,CAAC,CAAA;IAErC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE;QAClC,MAAM,UAAU,GAAG,GAAG,MAAM,GAAG,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAA;QAEzE,OAAO;wBACa,UAAU;;;;;;mCAMC,UAAU,CAAC,EAAE,CAAC;;;;;EAK/C,CAAA;IACA,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAEb,OAAO;2BACkB,SAAS;;;;;;;;;;;;;;;EAelC,QAAQ;CACT,CAAA;AACD,CAAC"}
|
package/dist/utils/strings.js
CHANGED
|
@@ -7,6 +7,9 @@ exports.capitalize = capitalize;
|
|
|
7
7
|
function toCamelCase(str) {
|
|
8
8
|
if (!str)
|
|
9
9
|
return str;
|
|
10
|
-
return str
|
|
10
|
+
return str
|
|
11
|
+
.split('_')
|
|
12
|
+
.map((part, i) => i === 0 ? part.toLowerCase() : (0, exports.capitalize)(part))
|
|
13
|
+
.join('');
|
|
11
14
|
}
|
|
12
15
|
//# sourceMappingURL=strings.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strings.js","sourceRoot":"","sources":["../../src/utils/strings.ts"],"names":[],"mappings":";;;AAGA,
|
|
1
|
+
{"version":3,"file":"strings.js","sourceRoot":"","sources":["../../src/utils/strings.ts"],"names":[],"mappings":";;;AAGA,kCAMC;AATM,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,EAAE,CACxC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AAD/B,QAAA,UAAU,cACqB;AAE5C,SAAgB,WAAW,CAAC,GAAW;IACrC,IAAI,CAAC,GAAG;QAAE,OAAO,GAAG,CAAA;IACpB,OAAO,GAAG;SACP,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,CAAC;SACjE,IAAI,CAAC,EAAE,CAAC,CAAA;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { toCamelCase } from '../utils/strings.js'
|
|
3
|
+
|
|
4
|
+
const CORE_NAME_MAP: Record<string, string> = {
|
|
5
|
+
delete: 'deleteUnique',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function coreFnName(op: string): string {
|
|
9
|
+
return CORE_NAME_MAP[op] || op
|
|
10
|
+
}
|
|
2
11
|
|
|
3
12
|
const READ_OPS = [
|
|
4
13
|
'findMany',
|
|
@@ -34,22 +43,23 @@ export function generateFastifyHandler(options: {
|
|
|
34
43
|
model: DMMF.Model
|
|
35
44
|
}): string {
|
|
36
45
|
const modelName = options.model.name
|
|
46
|
+
const prefix = toCamelCase(modelName)
|
|
37
47
|
|
|
38
48
|
const readHandlers = READ_OPS.map((op) => {
|
|
39
|
-
const exportName = `${
|
|
49
|
+
const exportName = `${prefix}${op.charAt(0).toUpperCase() + op.slice(1)}`
|
|
40
50
|
|
|
41
51
|
return `
|
|
42
52
|
export async function ${exportName}(
|
|
43
53
|
request: FastifyRequest,
|
|
44
54
|
_reply: FastifyReply,
|
|
45
55
|
): Promise<void> {
|
|
46
|
-
const data = await core.${op}(buildContext(request))
|
|
56
|
+
const data = await core.${coreFnName(op)}(buildContext(request))
|
|
47
57
|
;(request as any).resultData = data
|
|
48
58
|
}`
|
|
49
59
|
}).join('\n')
|
|
50
60
|
|
|
51
61
|
const writeHandlers = WRITE_OPS.map((op) => {
|
|
52
|
-
const exportName = `${
|
|
62
|
+
const exportName = `${prefix}${op.charAt(0).toUpperCase() + op.slice(1)}`
|
|
53
63
|
const statusCode = CREATED_OPS.has(op) ? 201 : 200
|
|
54
64
|
|
|
55
65
|
return `
|
|
@@ -57,7 +67,7 @@ export async function ${exportName}(
|
|
|
57
67
|
request: FastifyRequest,
|
|
58
68
|
_reply: FastifyReply,
|
|
59
69
|
): Promise<void> {
|
|
60
|
-
const data = await core.${op}(buildContext(request))
|
|
70
|
+
const data = await core.${coreFnName(op)}(buildContext(request))
|
|
61
71
|
;(request as any).resultData = data
|
|
62
72
|
;(request as any).resultStatus = ${statusCode}
|
|
63
73
|
}`
|
|
@@ -416,7 +416,7 @@ export async function ${op}(ctx: OperationContext): Promise<unknown> {
|
|
|
416
416
|
method: 'updateManyAndReturn',
|
|
417
417
|
requiredFields: ['where', 'data'],
|
|
418
418
|
},
|
|
419
|
-
{ name: '
|
|
419
|
+
{ name: 'deleteUnique', method: 'delete', requiredFields: ['where'] },
|
|
420
420
|
{
|
|
421
421
|
name: 'deleteMany',
|
|
422
422
|
method: 'deleteMany',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { toCamelCase } from '../utils/strings.js'
|
|
2
3
|
|
|
3
4
|
export function generateRouterFunction({
|
|
4
5
|
model,
|
|
@@ -10,8 +11,9 @@ export function generateRouterFunction({
|
|
|
10
11
|
relativeClientPath: string
|
|
11
12
|
}): string {
|
|
12
13
|
const modelName = model.name
|
|
14
|
+
const prefix = toCamelCase(modelName)
|
|
13
15
|
const modelNameLower = modelName.toLowerCase()
|
|
14
|
-
const routerFunctionName = `${
|
|
16
|
+
const routerFunctionName = `${prefix}Router`
|
|
15
17
|
|
|
16
18
|
const fieldsMeta = model.fields.map((f) => ({
|
|
17
19
|
name: f.name,
|
|
@@ -39,24 +41,24 @@ export function generateRouterFunction({
|
|
|
39
41
|
return `import express, { Request, Response, NextFunction, RequestHandler } from 'express'
|
|
40
42
|
import type { PrismaClient } from '${relativeClientPath}'
|
|
41
43
|
import {
|
|
42
|
-
${
|
|
43
|
-
${
|
|
44
|
-
${
|
|
45
|
-
${
|
|
46
|
-
${
|
|
47
|
-
${
|
|
48
|
-
${
|
|
49
|
-
${
|
|
50
|
-
${
|
|
51
|
-
${
|
|
52
|
-
${
|
|
53
|
-
${
|
|
54
|
-
${
|
|
55
|
-
${
|
|
56
|
-
${
|
|
57
|
-
${
|
|
58
|
-
${
|
|
59
|
-
${
|
|
44
|
+
${prefix}FindUnique,
|
|
45
|
+
${prefix}FindUniqueOrThrow,
|
|
46
|
+
${prefix}FindFirst,
|
|
47
|
+
${prefix}FindFirstOrThrow,
|
|
48
|
+
${prefix}FindMany,
|
|
49
|
+
${prefix}FindManyPaginated,
|
|
50
|
+
${prefix}Create,
|
|
51
|
+
${prefix}CreateMany,
|
|
52
|
+
${prefix}CreateManyAndReturn,
|
|
53
|
+
${prefix}Update,
|
|
54
|
+
${prefix}UpdateMany,
|
|
55
|
+
${prefix}UpdateManyAndReturn,
|
|
56
|
+
${prefix}Upsert,
|
|
57
|
+
${prefix}Delete,
|
|
58
|
+
${prefix}DeleteMany,
|
|
59
|
+
${prefix}Aggregate,
|
|
60
|
+
${prefix}Count,
|
|
61
|
+
${prefix}GroupBy
|
|
60
62
|
} from './${modelName}Handlers.js'
|
|
61
63
|
import type { RouteConfig } from '../routeConfig.target.js'
|
|
62
64
|
import { parseQueryParams } from '../parseQueryParams.js'
|
|
@@ -191,126 +193,126 @@ export function ${routerFunctionName}(config: RouteConfig = {}) {
|
|
|
191
193
|
const opConfig = config.findFirst || defaultOpConfig
|
|
192
194
|
const { before = [], after = [] } = opConfig
|
|
193
195
|
const path = basePath ? \`\${basePath}/first\` : '/first'
|
|
194
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
196
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindFirst as RequestHandler, ...after, respond)
|
|
195
197
|
}
|
|
196
198
|
|
|
197
199
|
if (config.enableAll || config.findFirstOrThrow) {
|
|
198
200
|
const opConfig = config.findFirstOrThrow || defaultOpConfig
|
|
199
201
|
const { before = [], after = [] } = opConfig
|
|
200
202
|
const path = basePath ? \`\${basePath}/first/strict\` : '/first/strict'
|
|
201
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
203
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindFirstOrThrow as RequestHandler, ...after, respond)
|
|
202
204
|
}
|
|
203
205
|
|
|
204
206
|
if (config.enableAll || config.findManyPaginated) {
|
|
205
207
|
const opConfig = config.findManyPaginated || defaultOpConfig
|
|
206
208
|
const { before = [], after = [] } = opConfig
|
|
207
209
|
const path = basePath ? \`\${basePath}/paginated\` : '/paginated'
|
|
208
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
210
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindManyPaginated as RequestHandler, ...after, respond)
|
|
209
211
|
}
|
|
210
212
|
|
|
211
213
|
if (config.enableAll || config.aggregate) {
|
|
212
214
|
const opConfig = config.aggregate || defaultOpConfig
|
|
213
215
|
const { before = [], after = [] } = opConfig
|
|
214
216
|
const path = basePath ? \`\${basePath}/aggregate\` : '/aggregate'
|
|
215
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
217
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}Aggregate as RequestHandler, ...after, respond)
|
|
216
218
|
}
|
|
217
219
|
|
|
218
220
|
if (config.enableAll || config.count) {
|
|
219
221
|
const opConfig = config.count || defaultOpConfig
|
|
220
222
|
const { before = [], after = [] } = opConfig
|
|
221
223
|
const path = basePath ? \`\${basePath}/count\` : '/count'
|
|
222
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
224
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}Count as RequestHandler, ...after, respond)
|
|
223
225
|
}
|
|
224
226
|
|
|
225
227
|
if (config.enableAll || config.groupBy) {
|
|
226
228
|
const opConfig = config.groupBy || defaultOpConfig
|
|
227
229
|
const { before = [], after = [] } = opConfig
|
|
228
230
|
const path = basePath ? \`\${basePath}/groupby\` : '/groupby'
|
|
229
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
231
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}GroupBy as RequestHandler, ...after, respond)
|
|
230
232
|
}
|
|
231
233
|
|
|
232
234
|
if (config.enableAll || config.findUniqueOrThrow) {
|
|
233
235
|
const opConfig = config.findUniqueOrThrow || defaultOpConfig
|
|
234
236
|
const { before = [], after = [] } = opConfig
|
|
235
237
|
const path = basePath ? \`\${basePath}/unique/strict\` : '/unique/strict'
|
|
236
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
238
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindUniqueOrThrow as RequestHandler, ...after, respond)
|
|
237
239
|
}
|
|
238
240
|
|
|
239
241
|
if (config.enableAll || config.findUnique) {
|
|
240
242
|
const opConfig = config.findUnique || defaultOpConfig
|
|
241
243
|
const { before = [], after = [] } = opConfig
|
|
242
244
|
const path = basePath ? \`\${basePath}/unique\` : '/unique'
|
|
243
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
245
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindUnique as RequestHandler, ...after, respond)
|
|
244
246
|
}
|
|
245
247
|
|
|
246
248
|
if (config.enableAll || config.findMany) {
|
|
247
249
|
const opConfig = config.findMany || defaultOpConfig
|
|
248
250
|
const { before = [], after = [] } = opConfig
|
|
249
251
|
const path = basePath || '/'
|
|
250
|
-
router.get(path, parseQuery, setShape(opConfig), ...before, ${
|
|
252
|
+
router.get(path, parseQuery, setShape(opConfig), ...before, ${prefix}FindMany as RequestHandler, ...after, respond)
|
|
251
253
|
}
|
|
252
254
|
|
|
253
255
|
if (config.enableAll || config.createManyAndReturn) {
|
|
254
256
|
const opConfig = config.createManyAndReturn || defaultOpConfig
|
|
255
257
|
const { before = [], after = [] } = opConfig
|
|
256
258
|
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
257
|
-
router.post(path, setShape(opConfig), ...before, ${
|
|
259
|
+
router.post(path, setShape(opConfig), ...before, ${prefix}CreateManyAndReturn as RequestHandler, ...after, respondCreated)
|
|
258
260
|
}
|
|
259
261
|
|
|
260
262
|
if (config.enableAll || config.createMany) {
|
|
261
263
|
const opConfig = config.createMany || defaultOpConfig
|
|
262
264
|
const { before = [], after = [] } = opConfig
|
|
263
265
|
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
264
|
-
router.post(path, setShape(opConfig), ...before, ${
|
|
266
|
+
router.post(path, setShape(opConfig), ...before, ${prefix}CreateMany as RequestHandler, ...after, respondCreated)
|
|
265
267
|
}
|
|
266
268
|
|
|
267
269
|
if (config.enableAll || config.create) {
|
|
268
270
|
const opConfig = config.create || defaultOpConfig
|
|
269
271
|
const { before = [], after = [] } = opConfig
|
|
270
272
|
const path = basePath || '/'
|
|
271
|
-
router.post(path, setShape(opConfig), ...before, ${
|
|
273
|
+
router.post(path, setShape(opConfig), ...before, ${prefix}Create as RequestHandler, ...after, respondCreated)
|
|
272
274
|
}
|
|
273
275
|
|
|
274
276
|
if (config.enableAll || config.updateManyAndReturn) {
|
|
275
277
|
const opConfig = config.updateManyAndReturn || defaultOpConfig
|
|
276
278
|
const { before = [], after = [] } = opConfig
|
|
277
279
|
const path = basePath ? \`\${basePath}/many/return\` : '/many/return'
|
|
278
|
-
router.put(path, setShape(opConfig), ...before, ${
|
|
280
|
+
router.put(path, setShape(opConfig), ...before, ${prefix}UpdateManyAndReturn as RequestHandler, ...after, respond)
|
|
279
281
|
}
|
|
280
282
|
|
|
281
283
|
if (config.enableAll || config.updateMany) {
|
|
282
284
|
const opConfig = config.updateMany || defaultOpConfig
|
|
283
285
|
const { before = [], after = [] } = opConfig
|
|
284
286
|
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
285
|
-
router.put(path, setShape(opConfig), ...before, ${
|
|
287
|
+
router.put(path, setShape(opConfig), ...before, ${prefix}UpdateMany as RequestHandler, ...after, respond)
|
|
286
288
|
}
|
|
287
289
|
|
|
288
290
|
if (config.enableAll || config.update) {
|
|
289
291
|
const opConfig = config.update || defaultOpConfig
|
|
290
292
|
const { before = [], after = [] } = opConfig
|
|
291
293
|
const path = basePath || '/'
|
|
292
|
-
router.put(path, setShape(opConfig), ...before, ${
|
|
294
|
+
router.put(path, setShape(opConfig), ...before, ${prefix}Update as RequestHandler, ...after, respond)
|
|
293
295
|
}
|
|
294
296
|
|
|
295
297
|
if (config.enableAll || config.upsert) {
|
|
296
298
|
const opConfig = config.upsert || defaultOpConfig
|
|
297
299
|
const { before = [], after = [] } = opConfig
|
|
298
300
|
const path = basePath || '/'
|
|
299
|
-
router.patch(path, setShape(opConfig), ...before, ${
|
|
301
|
+
router.patch(path, setShape(opConfig), ...before, ${prefix}Upsert as RequestHandler, ...after, respond)
|
|
300
302
|
}
|
|
301
303
|
|
|
302
304
|
if (config.enableAll || config.deleteMany) {
|
|
303
305
|
const opConfig = config.deleteMany || defaultOpConfig
|
|
304
306
|
const { before = [], after = [] } = opConfig
|
|
305
307
|
const path = basePath ? \`\${basePath}/many\` : '/many'
|
|
306
|
-
router.delete(path, setShape(opConfig), ...before, ${
|
|
308
|
+
router.delete(path, setShape(opConfig), ...before, ${prefix}DeleteMany as RequestHandler, ...after, respond)
|
|
307
309
|
}
|
|
308
310
|
|
|
309
311
|
if (config.enableAll || config.delete) {
|
|
310
312
|
const opConfig = config.delete || defaultOpConfig
|
|
311
313
|
const { before = [], after = [] } = opConfig
|
|
312
314
|
const path = basePath || '/'
|
|
313
|
-
router.delete(path, setShape(opConfig), ...before, ${
|
|
315
|
+
router.delete(path, setShape(opConfig), ...before, ${prefix}Delete as RequestHandler, ...after, respond)
|
|
314
316
|
}
|
|
315
317
|
|
|
316
318
|
router.use((err: any, _req: Request, res: Response, next: NextFunction) => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { toCamelCase } from '../utils/strings.js'
|
|
2
3
|
|
|
3
4
|
export function generateFastifyRouterFunction({
|
|
4
5
|
model,
|
|
@@ -8,8 +9,9 @@ export function generateFastifyRouterFunction({
|
|
|
8
9
|
enums: DMMF.DatamodelEnum[]
|
|
9
10
|
}): string {
|
|
10
11
|
const modelName = model.name
|
|
12
|
+
const prefix = toCamelCase(modelName)
|
|
11
13
|
const modelNameLower = modelName.toLowerCase()
|
|
12
|
-
const routerFunctionName = `${
|
|
14
|
+
const routerFunctionName = `${prefix}Router`
|
|
13
15
|
|
|
14
16
|
const fieldsMeta = model.fields.map((f) => ({
|
|
15
17
|
name: f.name,
|
|
@@ -34,26 +36,26 @@ export function generateFastifyRouterFunction({
|
|
|
34
36
|
values: e.values.map((v) => ({ name: v.name })),
|
|
35
37
|
}))
|
|
36
38
|
|
|
37
|
-
return `import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
|
|
39
|
+
return `import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify'
|
|
38
40
|
import {
|
|
39
|
-
${
|
|
40
|
-
${
|
|
41
|
-
${
|
|
42
|
-
${
|
|
43
|
-
${
|
|
44
|
-
${
|
|
45
|
-
${
|
|
46
|
-
${
|
|
47
|
-
${
|
|
48
|
-
${
|
|
49
|
-
${
|
|
50
|
-
${
|
|
51
|
-
${
|
|
52
|
-
${
|
|
53
|
-
${
|
|
54
|
-
${
|
|
55
|
-
${
|
|
56
|
-
${
|
|
41
|
+
${prefix}FindUnique,
|
|
42
|
+
${prefix}FindUniqueOrThrow,
|
|
43
|
+
${prefix}FindFirst,
|
|
44
|
+
${prefix}FindFirstOrThrow,
|
|
45
|
+
${prefix}FindMany,
|
|
46
|
+
${prefix}FindManyPaginated,
|
|
47
|
+
${prefix}Create,
|
|
48
|
+
${prefix}CreateMany,
|
|
49
|
+
${prefix}CreateManyAndReturn,
|
|
50
|
+
${prefix}Update,
|
|
51
|
+
${prefix}UpdateMany,
|
|
52
|
+
${prefix}UpdateManyAndReturn,
|
|
53
|
+
${prefix}Upsert,
|
|
54
|
+
${prefix}Delete,
|
|
55
|
+
${prefix}DeleteMany,
|
|
56
|
+
${prefix}Aggregate,
|
|
57
|
+
${prefix}Count,
|
|
58
|
+
${prefix}GroupBy,
|
|
57
59
|
} from './${modelName}Handlers.js'
|
|
58
60
|
import type { RouteConfig, FastifyHookHandler } from '../routeConfig.target.js'
|
|
59
61
|
import { parseQueryParams } from '../parseQueryParams.js'
|
|
@@ -168,7 +170,7 @@ export async function ${routerFunctionName}(
|
|
|
168
170
|
}
|
|
169
171
|
}
|
|
170
172
|
|
|
171
|
-
fastify.setErrorHandler((error, _request, reply) => {
|
|
173
|
+
fastify.setErrorHandler((error: FastifyError, _request: FastifyRequest, reply: FastifyReply) => {
|
|
172
174
|
const status = (error as any).status ?? error.statusCode ?? 500
|
|
173
175
|
const message = error.message || 'Internal server error'
|
|
174
176
|
if (!reply.sent) {
|
|
@@ -212,7 +214,7 @@ export async function ${routerFunctionName}(
|
|
|
212
214
|
parseQueryHook(request)
|
|
213
215
|
makeShapeHook(config, opConfig)(request)
|
|
214
216
|
if (await runHooks(before, request, reply)) return
|
|
215
|
-
await ${
|
|
217
|
+
await ${prefix}FindFirst(request, reply)
|
|
216
218
|
if (await runHooks(after, request, reply)) return
|
|
217
219
|
sendResult(request, reply)
|
|
218
220
|
} catch (error: unknown) {
|
|
@@ -230,7 +232,7 @@ export async function ${routerFunctionName}(
|
|
|
230
232
|
parseQueryHook(request)
|
|
231
233
|
makeShapeHook(config, opConfig)(request)
|
|
232
234
|
if (await runHooks(before, request, reply)) return
|
|
233
|
-
await ${
|
|
235
|
+
await ${prefix}FindFirstOrThrow(request, reply)
|
|
234
236
|
if (await runHooks(after, request, reply)) return
|
|
235
237
|
sendResult(request, reply)
|
|
236
238
|
} catch (error: unknown) {
|
|
@@ -248,7 +250,7 @@ export async function ${routerFunctionName}(
|
|
|
248
250
|
parseQueryHook(request)
|
|
249
251
|
makeShapeHook(config, opConfig)(request)
|
|
250
252
|
if (await runHooks(before, request, reply)) return
|
|
251
|
-
await ${
|
|
253
|
+
await ${prefix}FindManyPaginated(request, reply)
|
|
252
254
|
if (await runHooks(after, request, reply)) return
|
|
253
255
|
sendResult(request, reply)
|
|
254
256
|
} catch (error: unknown) {
|
|
@@ -266,7 +268,7 @@ export async function ${routerFunctionName}(
|
|
|
266
268
|
parseQueryHook(request)
|
|
267
269
|
makeShapeHook(config, opConfig)(request)
|
|
268
270
|
if (await runHooks(before, request, reply)) return
|
|
269
|
-
await ${
|
|
271
|
+
await ${prefix}Aggregate(request, reply)
|
|
270
272
|
if (await runHooks(after, request, reply)) return
|
|
271
273
|
sendResult(request, reply)
|
|
272
274
|
} catch (error: unknown) {
|
|
@@ -284,7 +286,7 @@ export async function ${routerFunctionName}(
|
|
|
284
286
|
parseQueryHook(request)
|
|
285
287
|
makeShapeHook(config, opConfig)(request)
|
|
286
288
|
if (await runHooks(before, request, reply)) return
|
|
287
|
-
await ${
|
|
289
|
+
await ${prefix}Count(request, reply)
|
|
288
290
|
if (await runHooks(after, request, reply)) return
|
|
289
291
|
sendResult(request, reply)
|
|
290
292
|
} catch (error: unknown) {
|
|
@@ -302,7 +304,7 @@ export async function ${routerFunctionName}(
|
|
|
302
304
|
parseQueryHook(request)
|
|
303
305
|
makeShapeHook(config, opConfig)(request)
|
|
304
306
|
if (await runHooks(before, request, reply)) return
|
|
305
|
-
await ${
|
|
307
|
+
await ${prefix}GroupBy(request, reply)
|
|
306
308
|
if (await runHooks(after, request, reply)) return
|
|
307
309
|
sendResult(request, reply)
|
|
308
310
|
} catch (error: unknown) {
|
|
@@ -320,7 +322,7 @@ export async function ${routerFunctionName}(
|
|
|
320
322
|
parseQueryHook(request)
|
|
321
323
|
makeShapeHook(config, opConfig)(request)
|
|
322
324
|
if (await runHooks(before, request, reply)) return
|
|
323
|
-
await ${
|
|
325
|
+
await ${prefix}FindUniqueOrThrow(request, reply)
|
|
324
326
|
if (await runHooks(after, request, reply)) return
|
|
325
327
|
sendResult(request, reply)
|
|
326
328
|
} catch (error: unknown) {
|
|
@@ -338,7 +340,7 @@ export async function ${routerFunctionName}(
|
|
|
338
340
|
parseQueryHook(request)
|
|
339
341
|
makeShapeHook(config, opConfig)(request)
|
|
340
342
|
if (await runHooks(before, request, reply)) return
|
|
341
|
-
await ${
|
|
343
|
+
await ${prefix}FindUnique(request, reply)
|
|
342
344
|
if (await runHooks(after, request, reply)) return
|
|
343
345
|
sendResult(request, reply)
|
|
344
346
|
} catch (error: unknown) {
|
|
@@ -356,7 +358,7 @@ export async function ${routerFunctionName}(
|
|
|
356
358
|
parseQueryHook(request)
|
|
357
359
|
makeShapeHook(config, opConfig)(request)
|
|
358
360
|
if (await runHooks(before, request, reply)) return
|
|
359
|
-
await ${
|
|
361
|
+
await ${prefix}FindMany(request, reply)
|
|
360
362
|
if (await runHooks(after, request, reply)) return
|
|
361
363
|
sendResult(request, reply)
|
|
362
364
|
} catch (error: unknown) {
|
|
@@ -373,7 +375,7 @@ export async function ${routerFunctionName}(
|
|
|
373
375
|
try {
|
|
374
376
|
makeShapeHook(config, opConfig)(request)
|
|
375
377
|
if (await runHooks(before, request, reply)) return
|
|
376
|
-
await ${
|
|
378
|
+
await ${prefix}CreateManyAndReturn(request, reply)
|
|
377
379
|
if (await runHooks(after, request, reply)) return
|
|
378
380
|
sendResult(request, reply)
|
|
379
381
|
} catch (error: unknown) {
|
|
@@ -390,7 +392,7 @@ export async function ${routerFunctionName}(
|
|
|
390
392
|
try {
|
|
391
393
|
makeShapeHook(config, opConfig)(request)
|
|
392
394
|
if (await runHooks(before, request, reply)) return
|
|
393
|
-
await ${
|
|
395
|
+
await ${prefix}CreateMany(request, reply)
|
|
394
396
|
if (await runHooks(after, request, reply)) return
|
|
395
397
|
sendResult(request, reply)
|
|
396
398
|
} catch (error: unknown) {
|
|
@@ -407,7 +409,7 @@ export async function ${routerFunctionName}(
|
|
|
407
409
|
try {
|
|
408
410
|
makeShapeHook(config, opConfig)(request)
|
|
409
411
|
if (await runHooks(before, request, reply)) return
|
|
410
|
-
await ${
|
|
412
|
+
await ${prefix}Create(request, reply)
|
|
411
413
|
if (await runHooks(after, request, reply)) return
|
|
412
414
|
sendResult(request, reply)
|
|
413
415
|
} catch (error: unknown) {
|
|
@@ -424,7 +426,7 @@ export async function ${routerFunctionName}(
|
|
|
424
426
|
try {
|
|
425
427
|
makeShapeHook(config, opConfig)(request)
|
|
426
428
|
if (await runHooks(before, request, reply)) return
|
|
427
|
-
await ${
|
|
429
|
+
await ${prefix}UpdateManyAndReturn(request, reply)
|
|
428
430
|
if (await runHooks(after, request, reply)) return
|
|
429
431
|
sendResult(request, reply)
|
|
430
432
|
} catch (error: unknown) {
|
|
@@ -441,7 +443,7 @@ export async function ${routerFunctionName}(
|
|
|
441
443
|
try {
|
|
442
444
|
makeShapeHook(config, opConfig)(request)
|
|
443
445
|
if (await runHooks(before, request, reply)) return
|
|
444
|
-
await ${
|
|
446
|
+
await ${prefix}UpdateMany(request, reply)
|
|
445
447
|
if (await runHooks(after, request, reply)) return
|
|
446
448
|
sendResult(request, reply)
|
|
447
449
|
} catch (error: unknown) {
|
|
@@ -458,7 +460,7 @@ export async function ${routerFunctionName}(
|
|
|
458
460
|
try {
|
|
459
461
|
makeShapeHook(config, opConfig)(request)
|
|
460
462
|
if (await runHooks(before, request, reply)) return
|
|
461
|
-
await ${
|
|
463
|
+
await ${prefix}Update(request, reply)
|
|
462
464
|
if (await runHooks(after, request, reply)) return
|
|
463
465
|
sendResult(request, reply)
|
|
464
466
|
} catch (error: unknown) {
|
|
@@ -475,7 +477,7 @@ export async function ${routerFunctionName}(
|
|
|
475
477
|
try {
|
|
476
478
|
makeShapeHook(config, opConfig)(request)
|
|
477
479
|
if (await runHooks(before, request, reply)) return
|
|
478
|
-
await ${
|
|
480
|
+
await ${prefix}Upsert(request, reply)
|
|
479
481
|
if (await runHooks(after, request, reply)) return
|
|
480
482
|
sendResult(request, reply)
|
|
481
483
|
} catch (error: unknown) {
|
|
@@ -492,7 +494,7 @@ export async function ${routerFunctionName}(
|
|
|
492
494
|
try {
|
|
493
495
|
makeShapeHook(config, opConfig)(request)
|
|
494
496
|
if (await runHooks(before, request, reply)) return
|
|
495
|
-
await ${
|
|
497
|
+
await ${prefix}DeleteMany(request, reply)
|
|
496
498
|
if (await runHooks(after, request, reply)) return
|
|
497
499
|
sendResult(request, reply)
|
|
498
500
|
} catch (error: unknown) {
|
|
@@ -509,7 +511,7 @@ export async function ${routerFunctionName}(
|
|
|
509
511
|
try {
|
|
510
512
|
makeShapeHook(config, opConfig)(request)
|
|
511
513
|
if (await runHooks(before, request, reply)) return
|
|
512
|
-
await ${
|
|
514
|
+
await ${prefix}Delete(request, reply)
|
|
513
515
|
if (await runHooks(after, request, reply)) return
|
|
514
516
|
sendResult(request, reply)
|
|
515
517
|
} catch (error: unknown) {
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { toCamelCase } from '../utils/strings.js'
|
|
2
3
|
|
|
3
4
|
export interface UnifiedHandlerOptions {
|
|
4
5
|
model: DMMF.Model
|
|
5
6
|
}
|
|
6
7
|
|
|
8
|
+
const CORE_NAME_MAP: Record<string, string> = {
|
|
9
|
+
delete: 'deleteUnique',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function coreFnName(op: string): string {
|
|
13
|
+
return CORE_NAME_MAP[op] || op
|
|
14
|
+
}
|
|
15
|
+
|
|
7
16
|
const ALL_OPS = [
|
|
8
17
|
'findMany',
|
|
9
18
|
'findFirst',
|
|
@@ -27,9 +36,10 @@ const ALL_OPS = [
|
|
|
27
36
|
|
|
28
37
|
export function generateUnifiedHandler(options: UnifiedHandlerOptions): string {
|
|
29
38
|
const modelName = options.model.name
|
|
39
|
+
const prefix = toCamelCase(modelName)
|
|
30
40
|
|
|
31
41
|
const handlers = ALL_OPS.map((op) => {
|
|
32
|
-
const exportName = `${
|
|
42
|
+
const exportName = `${prefix}${op.charAt(0).toUpperCase() + op.slice(1)}`
|
|
33
43
|
|
|
34
44
|
return `
|
|
35
45
|
export async function ${exportName}(
|
|
@@ -38,7 +48,7 @@ export async function ${exportName}(
|
|
|
38
48
|
next: NextFunction,
|
|
39
49
|
) {
|
|
40
50
|
try {
|
|
41
|
-
res.locals.data = await core.${op}(buildContext(req, res))
|
|
51
|
+
res.locals.data = await core.${coreFnName(op)}(buildContext(req, res))
|
|
42
52
|
next()
|
|
43
53
|
} catch (error: unknown) {
|
|
44
54
|
next(mapError(error))
|
package/src/utils/strings.ts
CHANGED
|
@@ -3,5 +3,8 @@ export const capitalize = (str: string) =>
|
|
|
3
3
|
|
|
4
4
|
export function toCamelCase(str: string) {
|
|
5
5
|
if (!str) return str
|
|
6
|
-
return str
|
|
6
|
+
return str
|
|
7
|
+
.split('_')
|
|
8
|
+
.map((part, i) => i === 0 ? part.toLowerCase() : capitalize(part))
|
|
9
|
+
.join('')
|
|
7
10
|
}
|