prisma-generator-express 1.14.3 → 1.15.1
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 +17 -7
- package/dist/generator.js +5 -0
- package/dist/generator.js.map +1 -1
- package/dist/helpers/generateAggregate.js +4 -9
- package/dist/helpers/generateAggregate.js.map +1 -1
- package/dist/helpers/generateCount.js +4 -9
- package/dist/helpers/generateCount.js.map +1 -1
- package/dist/helpers/generateCreate.js +4 -9
- package/dist/helpers/generateCreate.js.map +1 -1
- package/dist/helpers/generateCreateMany.js +4 -9
- package/dist/helpers/generateCreateMany.js.map +1 -1
- package/dist/helpers/generateDelete.js +4 -9
- package/dist/helpers/generateDelete.js.map +1 -1
- package/dist/helpers/generateDeleteMany.js +4 -9
- package/dist/helpers/generateDeleteMany.js.map +1 -1
- package/dist/helpers/generateFindFirst.js +4 -9
- package/dist/helpers/generateFindFirst.js.map +1 -1
- package/dist/helpers/generateFindMany.js +4 -9
- package/dist/helpers/generateFindMany.js.map +1 -1
- package/dist/helpers/generateFindUnique.js +4 -9
- package/dist/helpers/generateFindUnique.js.map +1 -1
- package/dist/helpers/generateGroupBy.js +4 -9
- package/dist/helpers/generateGroupBy.js.map +1 -1
- package/dist/helpers/generateRouteFile.js +30 -34
- package/dist/helpers/generateRouteFile.js.map +1 -1
- package/dist/helpers/generateUpdate.js +4 -9
- package/dist/helpers/generateUpdate.js.map +1 -1
- package/dist/helpers/generateUpdateMany.js +4 -9
- package/dist/helpers/generateUpdateMany.js.map +1 -1
- package/dist/helpers/generateUpsert.js +4 -9
- package/dist/helpers/generateUpsert.js.map +1 -1
- package/dist/utils/strings.js +3 -3
- package/dist/utils/strings.js.map +1 -1
- package/package.json +7 -7
- package/src/generator.ts +10 -0
- package/src/helpers/generateAggregate.ts +5 -10
- package/src/helpers/generateCount.ts +5 -10
- package/src/helpers/generateCreate.ts +5 -10
- package/src/helpers/generateCreateMany.ts +5 -10
- package/src/helpers/generateDelete.ts +5 -10
- package/src/helpers/generateDeleteMany.ts +5 -10
- package/src/helpers/generateFindFirst.ts +5 -10
- package/src/helpers/generateFindMany.ts +5 -10
- package/src/helpers/generateFindUnique.ts +5 -10
- package/src/helpers/generateGroupBy.ts +5 -10
- package/src/helpers/generateRouteFile.ts +30 -34
- package/src/helpers/generateUpdate.ts +5 -10
- package/src/helpers/generateUpdateMany.ts +5 -10
- package/src/helpers/generateUpsert.ts +5 -10
- package/src/utils/strings.ts +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import {
|
|
2
|
+
import { toPascalCase } from '../utils/strings'
|
|
3
3
|
/**
|
|
4
4
|
* Generates an Express middleware function that includes conditional output validation with Zod.
|
|
5
5
|
* This version dynamically includes the correct type for the query parameter based on the Prisma model.
|
|
@@ -29,7 +29,6 @@ export interface FindFirstRequest extends Request {
|
|
|
29
29
|
prisma: PrismaClient;
|
|
30
30
|
query: ${queryTypeName} & ParsedQs;
|
|
31
31
|
outputValidation?: ZodTypeAny;
|
|
32
|
-
omitOutputValidation?: boolean;
|
|
33
32
|
passToNext?: boolean;
|
|
34
33
|
locals?: {
|
|
35
34
|
data?: ${modelName} | null
|
|
@@ -42,15 +41,11 @@ export async function ${functionName}(req: FindFirstRequest, res: Response, next
|
|
|
42
41
|
try {
|
|
43
42
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.findFirst(req.query as ${queryTypeName});
|
|
44
|
+
const data = await req.prisma.${toPascalCase(modelName)}.findFirst(req.query as ${queryTypeName});
|
|
50
45
|
if (req.passToNext) {
|
|
51
46
|
if (req.locals) req.locals.data = data;
|
|
52
47
|
next();
|
|
53
|
-
} else if (
|
|
48
|
+
} else if (outputValidator) {
|
|
54
49
|
const validationResult = outputValidator.safeParse(data);
|
|
55
50
|
if (validationResult.success) {
|
|
56
51
|
return res.status(200).json(validationResult.data);
|
|
@@ -60,8 +55,8 @@ export async function ${functionName}(req: FindFirstRequest, res: Response, next
|
|
|
60
55
|
} else {
|
|
61
56
|
return res.status(200).json(data);
|
|
62
57
|
}
|
|
63
|
-
} catch
|
|
64
|
-
|
|
58
|
+
} catch(error: unknown) {
|
|
59
|
+
next(error)
|
|
65
60
|
}
|
|
66
61
|
}`
|
|
67
62
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import {
|
|
2
|
+
import { toPascalCase } from '../utils/strings'
|
|
3
3
|
/**
|
|
4
4
|
* Generates an Express middleware function that handles multiple record retrieval with optional output validation using Zod.
|
|
5
5
|
* This version dynamically includes the correct type for the query parameter based on the Prisma model.
|
|
@@ -29,7 +29,6 @@ export interface FindManyRequest extends Request {
|
|
|
29
29
|
prisma: PrismaClient;
|
|
30
30
|
query: ${queryTypeName} & ParsedQs;
|
|
31
31
|
outputValidation?: ZodTypeAny;
|
|
32
|
-
omitOutputValidation?: boolean;
|
|
33
32
|
passToNext?: boolean;
|
|
34
33
|
locals?: {
|
|
35
34
|
data?: ${modelName}[]
|
|
@@ -42,15 +41,11 @@ export async function ${functionName}(req: FindManyRequest, res: Response, next:
|
|
|
42
41
|
try {
|
|
43
42
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.findMany(req.query as ${queryTypeName});
|
|
44
|
+
const data = await req.prisma.${toPascalCase(modelName)}.findMany(req.query as ${queryTypeName});
|
|
50
45
|
if (req.passToNext) {
|
|
51
46
|
if (req.locals) req.locals.data = data;
|
|
52
47
|
next();
|
|
53
|
-
} else if (
|
|
48
|
+
} else if (outputValidator) {
|
|
54
49
|
const validationResult = outputValidator.safeParse(data);
|
|
55
50
|
if (validationResult.success) {
|
|
56
51
|
return res.status(200).json(validationResult.data);
|
|
@@ -60,8 +55,8 @@ export async function ${functionName}(req: FindManyRequest, res: Response, next:
|
|
|
60
55
|
} else {
|
|
61
56
|
return res.status(200).json(data);
|
|
62
57
|
}
|
|
63
|
-
} catch
|
|
64
|
-
|
|
58
|
+
} catch(error: unknown) {
|
|
59
|
+
next(error)
|
|
65
60
|
}
|
|
66
61
|
}`
|
|
67
62
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import {
|
|
2
|
+
import { toPascalCase } from '../utils/strings'
|
|
3
3
|
/**
|
|
4
4
|
* Generates an Express middleware function that includes conditional output validation with Zod.
|
|
5
5
|
* This version dynamically includes the correct type for the query parameter based on the Prisma model.
|
|
@@ -29,7 +29,6 @@ export interface FindUniqueRequest extends Request {
|
|
|
29
29
|
prisma: PrismaClient;
|
|
30
30
|
query: ${queryTypeName} & ParsedQs;
|
|
31
31
|
outputValidation?: ZodTypeAny;
|
|
32
|
-
omitOutputValidation?: boolean;
|
|
33
32
|
passToNext?: boolean;
|
|
34
33
|
locals?: {
|
|
35
34
|
data?: ${modelName} | null
|
|
@@ -42,15 +41,11 @@ export async function ${functionName}(req: FindUniqueRequest, res: Response, nex
|
|
|
42
41
|
try {
|
|
43
42
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.findUnique(req.query as ${queryTypeName});
|
|
44
|
+
const data = await req.prisma.${toPascalCase(modelName)}.findUnique(req.query as ${queryTypeName});
|
|
50
45
|
if (req.passToNext) {
|
|
51
46
|
if (req.locals) req.locals.data = data;
|
|
52
47
|
next();
|
|
53
|
-
} else if (
|
|
48
|
+
} else if (outputValidator) {
|
|
54
49
|
const validationResult = outputValidator.safeParse(data);
|
|
55
50
|
if (validationResult.success) {
|
|
56
51
|
return res.status(200).json(validationResult.data);
|
|
@@ -60,8 +55,8 @@ export async function ${functionName}(req: FindUniqueRequest, res: Response, nex
|
|
|
60
55
|
} else {
|
|
61
56
|
return res.status(200).json(data);
|
|
62
57
|
}
|
|
63
|
-
} catch
|
|
64
|
-
|
|
58
|
+
} catch(error: unknown) {
|
|
59
|
+
next(error)
|
|
65
60
|
}
|
|
66
61
|
}`
|
|
67
62
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import {
|
|
2
|
+
import { toPascalCase } from '../utils/strings'
|
|
3
3
|
import { PrismaClient, Prisma } from '@prisma/client'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -29,7 +29,6 @@ interface GroupByRequest extends Request {
|
|
|
29
29
|
prisma: PrismaClient;
|
|
30
30
|
query: Partial<${argsTypeName}> & ParsedQs;
|
|
31
31
|
outputValidation?: ZodTypeAny;
|
|
32
|
-
omitOutputValidation?: boolean;
|
|
33
32
|
locals?: {
|
|
34
33
|
outputValidator?: ZodTypeAny;
|
|
35
34
|
};
|
|
@@ -41,14 +40,10 @@ export async function ${functionName}(req: GroupByRequest, res: Response, next:
|
|
|
41
40
|
try {
|
|
42
41
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
43
42
|
|
|
44
|
-
if (!outputValidator && !req.omitOutputValidation) {
|
|
45
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
43
|
// @ts-ignore
|
|
49
|
-
const result = await req.prisma.${
|
|
44
|
+
const result = await req.prisma.${toPascalCase(modelName)}.groupBy(req.query);
|
|
50
45
|
|
|
51
|
-
if (
|
|
46
|
+
if (outputValidator) {
|
|
52
47
|
const validationResult = outputValidator.safeParse(result);
|
|
53
48
|
if (validationResult.success) {
|
|
54
49
|
return res.status(200).json(validationResult.data);
|
|
@@ -58,8 +53,8 @@ export async function ${functionName}(req: GroupByRequest, res: Response, next:
|
|
|
58
53
|
} else {
|
|
59
54
|
return res.status(200).json(result);
|
|
60
55
|
}
|
|
61
|
-
} catch
|
|
62
|
-
|
|
56
|
+
} catch(error: unknown) {
|
|
57
|
+
next(error)
|
|
63
58
|
}
|
|
64
59
|
}`
|
|
65
60
|
}
|
|
@@ -9,10 +9,7 @@ export function generateRouterFunction({
|
|
|
9
9
|
const routerFunctionName = `${modelName}Router`
|
|
10
10
|
|
|
11
11
|
return `import express, {
|
|
12
|
-
|
|
13
|
-
RequestHandler,
|
|
14
|
-
Request,
|
|
15
|
-
Response,
|
|
12
|
+
RequestHandler
|
|
16
13
|
} from 'express'
|
|
17
14
|
import { ParsedQs } from 'qs'
|
|
18
15
|
import { ${modelName}FindFirst } from './${modelName}FindFirst';
|
|
@@ -28,16 +25,15 @@ import { ${modelName}DeleteMany } from './${modelName}DeleteMany';
|
|
|
28
25
|
import { ${modelName}Aggregate } from './${modelName}Aggregate';
|
|
29
26
|
import { ${modelName}Count } from './${modelName}Count';
|
|
30
27
|
import { ${modelName}GroupBy } from './${modelName}GroupBy';
|
|
31
|
-
import { createValidatorMiddleware
|
|
32
|
-
import { createOutputValidatorMiddleware } from '../createOutputValidatorMiddleware'
|
|
28
|
+
import { createValidatorMiddleware } from '../createValidatorMiddleware'
|
|
33
29
|
import { RouteConfig, ValidatorConfig } from '../routeConfig'
|
|
34
30
|
import { parseQueryParams } from "../parseQueryParams";
|
|
35
31
|
|
|
36
32
|
const defaultBeforeAfter = {
|
|
37
33
|
before: [] as RequestHandler[],
|
|
38
34
|
after: [] as RequestHandler[],
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
inputValidator: undefined,
|
|
36
|
+
outputValidator: undefined,
|
|
41
37
|
};
|
|
42
38
|
|
|
43
39
|
/**
|
|
@@ -89,104 +85,104 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
|
|
|
89
85
|
|
|
90
86
|
|
|
91
87
|
if (config.enableAll || config?.findFirst) {
|
|
92
|
-
const { before = [], after = [],
|
|
93
|
-
setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler,
|
|
88
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.findFirst || defaultBeforeAfter;
|
|
89
|
+
setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler, inputValidator, outputValidator);
|
|
94
90
|
if (after.length) {
|
|
95
91
|
router.use(basePath + '/first', ...after);
|
|
96
92
|
}
|
|
97
93
|
}
|
|
98
94
|
|
|
99
95
|
if (config.enableAll || config?.findMany) {
|
|
100
|
-
const { before = [], after = [],
|
|
101
|
-
setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler,
|
|
96
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.findMany || defaultBeforeAfter;
|
|
97
|
+
setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler, inputValidator, outputValidator);
|
|
102
98
|
if (after.length) {
|
|
103
99
|
router.use(basePath + '/', ...after);
|
|
104
100
|
}
|
|
105
101
|
}
|
|
106
102
|
|
|
107
103
|
if (config.enableAll || config?.findUnique) {
|
|
108
|
-
const { before = [], after = [],
|
|
109
|
-
setupRoute('/:id', 'get', before, ${modelName}FindUnique as any,
|
|
104
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.findUnique || defaultBeforeAfter;
|
|
105
|
+
setupRoute('/:id', 'get', before, ${modelName}FindUnique as any, inputValidator, outputValidator);
|
|
110
106
|
if (after.length) {
|
|
111
107
|
router.use(basePath + '/:id', ...after);
|
|
112
108
|
}
|
|
113
109
|
}
|
|
114
110
|
|
|
115
111
|
if (config.enableAll || config?.create) {
|
|
116
|
-
const { before = [], after = [],
|
|
117
|
-
setupRoute('/', 'post', before, ${modelName}Create as RequestHandler,
|
|
112
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.create || defaultBeforeAfter;
|
|
113
|
+
setupRoute('/', 'post', before, ${modelName}Create as RequestHandler, inputValidator, outputValidator);
|
|
118
114
|
if (after.length) {
|
|
119
115
|
router.use(basePath + '/', ...after);
|
|
120
116
|
}
|
|
121
117
|
}
|
|
122
118
|
|
|
123
119
|
if (config.enableAll || config?.createMany) {
|
|
124
|
-
const { before = [], after = [],
|
|
125
|
-
setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler,
|
|
120
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.createMany || defaultBeforeAfter;
|
|
121
|
+
setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler, inputValidator, outputValidator);
|
|
126
122
|
if (after.length) {
|
|
127
123
|
router.use(basePath + '/many', ...after);
|
|
128
124
|
}
|
|
129
125
|
}
|
|
130
126
|
|
|
131
127
|
if (config.enableAll || config?.update) {
|
|
132
|
-
const { before = [], after = [],
|
|
133
|
-
setupRoute('/', 'put', before, ${modelName}Update as RequestHandler,
|
|
128
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.update || defaultBeforeAfter;
|
|
129
|
+
setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, inputValidator, outputValidator);
|
|
134
130
|
if (after.length) {
|
|
135
131
|
router.use(basePath + '/', ...after);
|
|
136
132
|
}
|
|
137
133
|
}
|
|
138
134
|
|
|
139
135
|
if (config.enableAll || config?.updateMany) {
|
|
140
|
-
const { before = [], after = [],
|
|
141
|
-
setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler,
|
|
136
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.updateMany || defaultBeforeAfter;
|
|
137
|
+
setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler, inputValidator, outputValidator);
|
|
142
138
|
if (after.length) {
|
|
143
139
|
router.use(basePath + '/many', ...after);
|
|
144
140
|
}
|
|
145
141
|
}
|
|
146
142
|
|
|
147
143
|
if (config.enableAll || config?.upsert) {
|
|
148
|
-
const { before = [], after = [],
|
|
149
|
-
setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler,
|
|
144
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.upsert || defaultBeforeAfter;
|
|
145
|
+
setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, inputValidator, outputValidator);
|
|
150
146
|
if (after.length) {
|
|
151
147
|
router.use(basePath + '/', ...after);
|
|
152
148
|
}
|
|
153
149
|
}
|
|
154
150
|
|
|
155
151
|
if (config.enableAll || config?.delete) {
|
|
156
|
-
const { before = [], after = [],
|
|
157
|
-
setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler,
|
|
152
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.delete || defaultBeforeAfter;
|
|
153
|
+
setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler, inputValidator, outputValidator);
|
|
158
154
|
if (after.length) {
|
|
159
155
|
router.use(basePath + '/', ...after);
|
|
160
156
|
}
|
|
161
157
|
}
|
|
162
158
|
|
|
163
159
|
if (config.enableAll || config?.deleteMany) {
|
|
164
|
-
const { before = [], after = [],
|
|
165
|
-
setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler,
|
|
160
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.deleteMany || defaultBeforeAfter;
|
|
161
|
+
setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler, inputValidator, outputValidator);
|
|
166
162
|
if (after.length) {
|
|
167
163
|
router.use(basePath + '/many', ...after);
|
|
168
164
|
}
|
|
169
165
|
}
|
|
170
166
|
|
|
171
167
|
if (config.enableAll || config?.aggregate) {
|
|
172
|
-
const { before = [], after = [],
|
|
173
|
-
setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler,
|
|
168
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.aggregate || defaultBeforeAfter;
|
|
169
|
+
setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler, inputValidator, outputValidator);
|
|
174
170
|
if (after.length) {
|
|
175
171
|
router.use(basePath + '/aggregate', ...after);
|
|
176
172
|
}
|
|
177
173
|
}
|
|
178
174
|
|
|
179
175
|
if (config.enableAll || config?.count) {
|
|
180
|
-
const { before = [], after = [],
|
|
181
|
-
setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler,
|
|
176
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.count || defaultBeforeAfter;
|
|
177
|
+
setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler, inputValidator, outputValidator);
|
|
182
178
|
if (after.length) {
|
|
183
179
|
router.use(basePath + '/count', ...after);
|
|
184
180
|
}
|
|
185
181
|
}
|
|
186
182
|
|
|
187
183
|
if (config.enableAll || config?.groupBy) {
|
|
188
|
-
const { before = [], after = [],
|
|
189
|
-
setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler,
|
|
184
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.groupBy || defaultBeforeAfter;
|
|
185
|
+
setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler, inputValidator, outputValidator);
|
|
190
186
|
if (after.length) {
|
|
191
187
|
router.use(basePath + '/groupby', ...after);
|
|
192
188
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import {
|
|
2
|
+
import { toPascalCase } from '../utils/strings'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Generates an Express middleware function that handles updating records and includes conditional output validation with Zod.
|
|
@@ -26,7 +26,6 @@ interface UpdateRequest extends Request {
|
|
|
26
26
|
prisma: PrismaClient;
|
|
27
27
|
body: ${argsTypeName};
|
|
28
28
|
outputValidation?: ZodTypeAny;
|
|
29
|
-
omitOutputValidation?: boolean;
|
|
30
29
|
locals?: {
|
|
31
30
|
outputValidator?: ZodTypeAny;
|
|
32
31
|
};
|
|
@@ -38,13 +37,9 @@ export async function ${functionName}(req: UpdateRequest, res: Response, next: N
|
|
|
38
37
|
try {
|
|
39
38
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.update(req.body);
|
|
40
|
+
const data = await req.prisma.${toPascalCase(modelName)}.update(req.body);
|
|
46
41
|
|
|
47
|
-
if (
|
|
42
|
+
if (outputValidator) {
|
|
48
43
|
const validationResult = outputValidator.safeParse(data);
|
|
49
44
|
if (validationResult.success) {
|
|
50
45
|
return res.status(200).json(validationResult.data);
|
|
@@ -54,8 +49,8 @@ export async function ${functionName}(req: UpdateRequest, res: Response, next: N
|
|
|
54
49
|
} else {
|
|
55
50
|
return res.status(200).json(data);
|
|
56
51
|
}
|
|
57
|
-
} catch
|
|
58
|
-
|
|
52
|
+
} catch(error: unknown) {
|
|
53
|
+
next(error)
|
|
59
54
|
}
|
|
60
55
|
}`
|
|
61
56
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import {
|
|
2
|
+
import { toPascalCase } from '../utils/strings'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Generates an Express middleware function that handles updating multiple records and includes conditional output validation with Zod.
|
|
@@ -26,7 +26,6 @@ interface UpdateManyRequest extends Request {
|
|
|
26
26
|
prisma: PrismaClient;
|
|
27
27
|
body: ${argsTypeName};
|
|
28
28
|
outputValidation?: ZodTypeAny;
|
|
29
|
-
omitOutputValidation?: boolean;
|
|
30
29
|
locals?: {
|
|
31
30
|
outputValidator?: ZodTypeAny;
|
|
32
31
|
};
|
|
@@ -38,13 +37,9 @@ export async function ${functionName}(req: UpdateManyRequest, res: Response, nex
|
|
|
38
37
|
try {
|
|
39
38
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.updateMany(req.body);
|
|
40
|
+
const data = await req.prisma.${toPascalCase(modelName)}.updateMany(req.body);
|
|
46
41
|
|
|
47
|
-
if (
|
|
42
|
+
if (outputValidator) {
|
|
48
43
|
const validationResult = outputValidator.safeParse(data);
|
|
49
44
|
if (validationResult.success) {
|
|
50
45
|
return res.status(200).json({ count: validationResult.data.count });
|
|
@@ -54,8 +49,8 @@ export async function ${functionName}(req: UpdateManyRequest, res: Response, nex
|
|
|
54
49
|
} else {
|
|
55
50
|
return res.status(200).json({ count: data.count });
|
|
56
51
|
}
|
|
57
|
-
} catch
|
|
58
|
-
|
|
52
|
+
} catch(error: unknown) {
|
|
53
|
+
next(error)
|
|
59
54
|
}
|
|
60
55
|
}`
|
|
61
56
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import {
|
|
2
|
+
import { toPascalCase } from '../utils/strings'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Generates an Express middleware function that handles the upsert operation (create or update)
|
|
@@ -27,7 +27,6 @@ interface UpsertRequest extends Request {
|
|
|
27
27
|
prisma: PrismaClient;
|
|
28
28
|
body: ${argsTypeName};
|
|
29
29
|
outputValidation?: ZodTypeAny;
|
|
30
|
-
omitOutputValidation?: boolean;
|
|
31
30
|
locals?: {
|
|
32
31
|
outputValidator?: ZodTypeAny;
|
|
33
32
|
};
|
|
@@ -39,13 +38,9 @@ export async function ${functionName}(req: UpsertRequest, res: Response, next: N
|
|
|
39
38
|
try {
|
|
40
39
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.upsert(req.body);
|
|
41
|
+
const data = await req.prisma.${toPascalCase(modelName)}.upsert(req.body);
|
|
47
42
|
|
|
48
|
-
if (
|
|
43
|
+
if (outputValidator) {
|
|
49
44
|
const validationResult = outputValidator.safeParse(data);
|
|
50
45
|
if (validationResult.success) {
|
|
51
46
|
return res.status(200).json(validationResult.data);
|
|
@@ -55,8 +50,8 @@ export async function ${functionName}(req: UpsertRequest, res: Response, next: N
|
|
|
55
50
|
} else {
|
|
56
51
|
return res.status(200).json(data);
|
|
57
52
|
}
|
|
58
|
-
} catch
|
|
59
|
-
|
|
53
|
+
} catch(error: unknown) {
|
|
54
|
+
next(error)
|
|
60
55
|
}
|
|
61
56
|
}`
|
|
62
57
|
}
|
package/src/utils/strings.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const capitalize = (str: string) =>
|
|
2
2
|
str.charAt(0).toUpperCase() + str.slice(1)
|
|
3
3
|
|
|
4
|
-
export function
|
|
4
|
+
export function toPascalCase(str: string) {
|
|
5
5
|
if (!str) return str
|
|
6
6
|
return str.charAt(0).toLowerCase() + str.slice(1)
|
|
7
7
|
}
|