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
|
@@ -5,10 +5,7 @@ function generateRouterFunction({ model, }) {
|
|
|
5
5
|
const modelName = model.name;
|
|
6
6
|
const routerFunctionName = `${modelName}Router`;
|
|
7
7
|
return `import express, {
|
|
8
|
-
|
|
9
|
-
RequestHandler,
|
|
10
|
-
Request,
|
|
11
|
-
Response,
|
|
8
|
+
RequestHandler
|
|
12
9
|
} from 'express'
|
|
13
10
|
import { ParsedQs } from 'qs'
|
|
14
11
|
import { ${modelName}FindFirst } from './${modelName}FindFirst';
|
|
@@ -24,16 +21,15 @@ import { ${modelName}DeleteMany } from './${modelName}DeleteMany';
|
|
|
24
21
|
import { ${modelName}Aggregate } from './${modelName}Aggregate';
|
|
25
22
|
import { ${modelName}Count } from './${modelName}Count';
|
|
26
23
|
import { ${modelName}GroupBy } from './${modelName}GroupBy';
|
|
27
|
-
import { createValidatorMiddleware
|
|
28
|
-
import { createOutputValidatorMiddleware } from '../createOutputValidatorMiddleware'
|
|
24
|
+
import { createValidatorMiddleware } from '../createValidatorMiddleware'
|
|
29
25
|
import { RouteConfig, ValidatorConfig } from '../routeConfig'
|
|
30
26
|
import { parseQueryParams } from "../parseQueryParams";
|
|
31
27
|
|
|
32
28
|
const defaultBeforeAfter = {
|
|
33
29
|
before: [] as RequestHandler[],
|
|
34
30
|
after: [] as RequestHandler[],
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
inputValidator: undefined,
|
|
32
|
+
outputValidator: undefined,
|
|
37
33
|
};
|
|
38
34
|
|
|
39
35
|
/**
|
|
@@ -85,104 +81,104 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
|
|
|
85
81
|
|
|
86
82
|
|
|
87
83
|
if (config.enableAll || config?.findFirst) {
|
|
88
|
-
const { before = [], after = [],
|
|
89
|
-
setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler,
|
|
84
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.findFirst || defaultBeforeAfter;
|
|
85
|
+
setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler, inputValidator, outputValidator);
|
|
90
86
|
if (after.length) {
|
|
91
87
|
router.use(basePath + '/first', ...after);
|
|
92
88
|
}
|
|
93
89
|
}
|
|
94
90
|
|
|
95
91
|
if (config.enableAll || config?.findMany) {
|
|
96
|
-
const { before = [], after = [],
|
|
97
|
-
setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler,
|
|
92
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.findMany || defaultBeforeAfter;
|
|
93
|
+
setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler, inputValidator, outputValidator);
|
|
98
94
|
if (after.length) {
|
|
99
95
|
router.use(basePath + '/', ...after);
|
|
100
96
|
}
|
|
101
97
|
}
|
|
102
98
|
|
|
103
99
|
if (config.enableAll || config?.findUnique) {
|
|
104
|
-
const { before = [], after = [],
|
|
105
|
-
setupRoute('/:id', 'get', before, ${modelName}FindUnique as any,
|
|
100
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.findUnique || defaultBeforeAfter;
|
|
101
|
+
setupRoute('/:id', 'get', before, ${modelName}FindUnique as any, inputValidator, outputValidator);
|
|
106
102
|
if (after.length) {
|
|
107
103
|
router.use(basePath + '/:id', ...after);
|
|
108
104
|
}
|
|
109
105
|
}
|
|
110
106
|
|
|
111
107
|
if (config.enableAll || config?.create) {
|
|
112
|
-
const { before = [], after = [],
|
|
113
|
-
setupRoute('/', 'post', before, ${modelName}Create as RequestHandler,
|
|
108
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.create || defaultBeforeAfter;
|
|
109
|
+
setupRoute('/', 'post', before, ${modelName}Create as RequestHandler, inputValidator, outputValidator);
|
|
114
110
|
if (after.length) {
|
|
115
111
|
router.use(basePath + '/', ...after);
|
|
116
112
|
}
|
|
117
113
|
}
|
|
118
114
|
|
|
119
115
|
if (config.enableAll || config?.createMany) {
|
|
120
|
-
const { before = [], after = [],
|
|
121
|
-
setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler,
|
|
116
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.createMany || defaultBeforeAfter;
|
|
117
|
+
setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler, inputValidator, outputValidator);
|
|
122
118
|
if (after.length) {
|
|
123
119
|
router.use(basePath + '/many', ...after);
|
|
124
120
|
}
|
|
125
121
|
}
|
|
126
122
|
|
|
127
123
|
if (config.enableAll || config?.update) {
|
|
128
|
-
const { before = [], after = [],
|
|
129
|
-
setupRoute('/', 'put', before, ${modelName}Update as RequestHandler,
|
|
124
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.update || defaultBeforeAfter;
|
|
125
|
+
setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, inputValidator, outputValidator);
|
|
130
126
|
if (after.length) {
|
|
131
127
|
router.use(basePath + '/', ...after);
|
|
132
128
|
}
|
|
133
129
|
}
|
|
134
130
|
|
|
135
131
|
if (config.enableAll || config?.updateMany) {
|
|
136
|
-
const { before = [], after = [],
|
|
137
|
-
setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler,
|
|
132
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.updateMany || defaultBeforeAfter;
|
|
133
|
+
setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler, inputValidator, outputValidator);
|
|
138
134
|
if (after.length) {
|
|
139
135
|
router.use(basePath + '/many', ...after);
|
|
140
136
|
}
|
|
141
137
|
}
|
|
142
138
|
|
|
143
139
|
if (config.enableAll || config?.upsert) {
|
|
144
|
-
const { before = [], after = [],
|
|
145
|
-
setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler,
|
|
140
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.upsert || defaultBeforeAfter;
|
|
141
|
+
setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, inputValidator, outputValidator);
|
|
146
142
|
if (after.length) {
|
|
147
143
|
router.use(basePath + '/', ...after);
|
|
148
144
|
}
|
|
149
145
|
}
|
|
150
146
|
|
|
151
147
|
if (config.enableAll || config?.delete) {
|
|
152
|
-
const { before = [], after = [],
|
|
153
|
-
setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler,
|
|
148
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.delete || defaultBeforeAfter;
|
|
149
|
+
setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler, inputValidator, outputValidator);
|
|
154
150
|
if (after.length) {
|
|
155
151
|
router.use(basePath + '/', ...after);
|
|
156
152
|
}
|
|
157
153
|
}
|
|
158
154
|
|
|
159
155
|
if (config.enableAll || config?.deleteMany) {
|
|
160
|
-
const { before = [], after = [],
|
|
161
|
-
setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler,
|
|
156
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.deleteMany || defaultBeforeAfter;
|
|
157
|
+
setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler, inputValidator, outputValidator);
|
|
162
158
|
if (after.length) {
|
|
163
159
|
router.use(basePath + '/many', ...after);
|
|
164
160
|
}
|
|
165
161
|
}
|
|
166
162
|
|
|
167
163
|
if (config.enableAll || config?.aggregate) {
|
|
168
|
-
const { before = [], after = [],
|
|
169
|
-
setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler,
|
|
164
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.aggregate || defaultBeforeAfter;
|
|
165
|
+
setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler, inputValidator, outputValidator);
|
|
170
166
|
if (after.length) {
|
|
171
167
|
router.use(basePath + '/aggregate', ...after);
|
|
172
168
|
}
|
|
173
169
|
}
|
|
174
170
|
|
|
175
171
|
if (config.enableAll || config?.count) {
|
|
176
|
-
const { before = [], after = [],
|
|
177
|
-
setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler,
|
|
172
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.count || defaultBeforeAfter;
|
|
173
|
+
setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler, inputValidator, outputValidator);
|
|
178
174
|
if (after.length) {
|
|
179
175
|
router.use(basePath + '/count', ...after);
|
|
180
176
|
}
|
|
181
177
|
}
|
|
182
178
|
|
|
183
179
|
if (config.enableAll || config?.groupBy) {
|
|
184
|
-
const { before = [], after = [],
|
|
185
|
-
setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler,
|
|
180
|
+
const { before = [], after = [], inputValidator, outputValidator) = config.groupBy || defaultBeforeAfter;
|
|
181
|
+
setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler, inputValidator, outputValidator);
|
|
186
182
|
if (after.length) {
|
|
187
183
|
router.use(basePath + '/groupby', ...after);
|
|
188
184
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateRouteFile.js","sourceRoot":"","sources":["../../src/helpers/generateRouteFile.ts"],"names":[],"mappings":";;;AAEA,SAAgB,sBAAsB,CAAC,EACrC,KAAK,GAGN;IACC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,kBAAkB,GAAG,GAAG,SAAS,QAAQ,CAAA;IAE/C,OAAO
|
|
1
|
+
{"version":3,"file":"generateRouteFile.js","sourceRoot":"","sources":["../../src/helpers/generateRouteFile.ts"],"names":[],"mappings":";;;AAEA,SAAgB,sBAAsB,CAAC,EACrC,KAAK,GAGN;IACC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,kBAAkB,GAAG,GAAG,SAAS,QAAQ,CAAA;IAE/C,OAAO;;;;WAIE,SAAS,uBAAuB,SAAS;WACzC,SAAS,sBAAsB,SAAS;WACxC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,oBAAoB,SAAS;WACtC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,oBAAoB,SAAS;WACtC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,oBAAoB,SAAS;WACtC,SAAS,oBAAoB,SAAS;WACtC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,uBAAuB,SAAS;WACzC,SAAS,mBAAmB,SAAS;WACrC,SAAS,qBAAqB,SAAS;;;;;;;;;;;;;qCAab,SAAS;;;;;kBAK5B,kBAAkB;;iFAE6C,SAAS,CAAC,WAAW,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0CA0C9D,SAAS;;;;;;;;qCAQd,SAAS;;;;;;;;wCAQN,SAAS;;;;;;;;sCAQX,SAAS;;;;;;;;0CAQL,SAAS;;;;;;;;qCAQd,SAAS;;;;;;;;yCAQL,SAAS;;;;;;;;uCAQX,SAAS;;;;;;;;wCAQR,SAAS;;;;;;;;4CAQL,SAAS;;;;;;;;8CAQP,SAAS;;;;;;;;0CAQb,SAAS;;;;;;;;4CAQP,SAAS;;;;;;;;CAQpD,CAAA;AACD,CAAC;AA/LD,wDA+LC"}
|
|
@@ -17,7 +17,6 @@ interface UpdateRequest extends Request {
|
|
|
17
17
|
prisma: PrismaClient;
|
|
18
18
|
body: ${argsTypeName};
|
|
19
19
|
outputValidation?: ZodTypeAny;
|
|
20
|
-
omitOutputValidation?: boolean;
|
|
21
20
|
locals?: {
|
|
22
21
|
outputValidator?: ZodTypeAny;
|
|
23
22
|
};
|
|
@@ -29,13 +28,9 @@ export async function ${functionName}(req: UpdateRequest, res: Response, next: N
|
|
|
29
28
|
try {
|
|
30
29
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const data = await req.prisma.${(0, strings_1.lowercaseFirstLetter)(modelName)}.update(req.body);
|
|
31
|
+
const data = await req.prisma.${(0, strings_1.toPascalCase)(modelName)}.update(req.body);
|
|
37
32
|
|
|
38
|
-
if (
|
|
33
|
+
if (outputValidator) {
|
|
39
34
|
const validationResult = outputValidator.safeParse(data);
|
|
40
35
|
if (validationResult.success) {
|
|
41
36
|
return res.status(200).json(validationResult.data);
|
|
@@ -45,8 +40,8 @@ export async function ${functionName}(req: UpdateRequest, res: Response, next: N
|
|
|
45
40
|
} else {
|
|
46
41
|
return res.status(200).json(data);
|
|
47
42
|
}
|
|
48
|
-
} catch
|
|
49
|
-
|
|
43
|
+
} catch(error: unknown) {
|
|
44
|
+
next(error)
|
|
50
45
|
}
|
|
51
46
|
}`;
|
|
52
47
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateUpdate.js","sourceRoot":"","sources":["../../src/helpers/generateUpdate.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"generateUpdate.js","sourceRoot":"","sources":["../../src/helpers/generateUpdate.ts"],"names":[],"mappings":";;;AACA,8CAA+C;AAQxC,MAAM,sBAAsB,GAAG,CAAC,OAGtC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,QAAQ,CAAA;IACzC,MAAM,YAAY,GAAG,UAAU,SAAS,YAAY,CAAA;IAEpD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;;;uEAOiD,YAAY;;wBAE3D,YAAY;;;;oCAIA,IAAA,sBAAY,EAAC,SAAS,CAAC;;;;;;;;;;;;;;;EAezD,CAAA;AACF,CAAC,CAAA;AA9CY,QAAA,sBAAsB,0BA8ClC"}
|
|
@@ -17,7 +17,6 @@ interface UpdateManyRequest extends Request {
|
|
|
17
17
|
prisma: PrismaClient;
|
|
18
18
|
body: ${argsTypeName};
|
|
19
19
|
outputValidation?: ZodTypeAny;
|
|
20
|
-
omitOutputValidation?: boolean;
|
|
21
20
|
locals?: {
|
|
22
21
|
outputValidator?: ZodTypeAny;
|
|
23
22
|
};
|
|
@@ -29,13 +28,9 @@ export async function ${functionName}(req: UpdateManyRequest, res: Response, nex
|
|
|
29
28
|
try {
|
|
30
29
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const data = await req.prisma.${(0, strings_1.lowercaseFirstLetter)(modelName)}.updateMany(req.body);
|
|
31
|
+
const data = await req.prisma.${(0, strings_1.toPascalCase)(modelName)}.updateMany(req.body);
|
|
37
32
|
|
|
38
|
-
if (
|
|
33
|
+
if (outputValidator) {
|
|
39
34
|
const validationResult = outputValidator.safeParse(data);
|
|
40
35
|
if (validationResult.success) {
|
|
41
36
|
return res.status(200).json({ count: validationResult.data.count });
|
|
@@ -45,8 +40,8 @@ export async function ${functionName}(req: UpdateManyRequest, res: Response, nex
|
|
|
45
40
|
} else {
|
|
46
41
|
return res.status(200).json({ count: data.count });
|
|
47
42
|
}
|
|
48
|
-
} catch
|
|
49
|
-
|
|
43
|
+
} catch(error: unknown) {
|
|
44
|
+
next(error)
|
|
50
45
|
}
|
|
51
46
|
}`;
|
|
52
47
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateUpdateMany.js","sourceRoot":"","sources":["../../src/helpers/generateUpdateMany.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"generateUpdateMany.js","sourceRoot":"","sources":["../../src/helpers/generateUpdateMany.ts"],"names":[],"mappings":";;;AACA,8CAA+C;AAQxC,MAAM,0BAA0B,GAAG,CAAC,OAG1C,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,YAAY,CAAA;IAC7C,MAAM,YAAY,GAAG,UAAU,SAAS,gBAAgB,CAAA;IAExD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;;;2EAOqD,YAAY;;wBAE/D,YAAY;;;;oCAIA,IAAA,sBAAY,EAAC,SAAS,CAAC;;;;;;;;;;;;;;;EAezD,CAAA;AACF,CAAC,CAAA;AA9CY,QAAA,0BAA0B,8BA8CtC"}
|
|
@@ -17,7 +17,6 @@ interface UpsertRequest extends Request {
|
|
|
17
17
|
prisma: PrismaClient;
|
|
18
18
|
body: ${argsTypeName};
|
|
19
19
|
outputValidation?: ZodTypeAny;
|
|
20
|
-
omitOutputValidation?: boolean;
|
|
21
20
|
locals?: {
|
|
22
21
|
outputValidator?: ZodTypeAny;
|
|
23
22
|
};
|
|
@@ -29,13 +28,9 @@ export async function ${functionName}(req: UpsertRequest, res: Response, next: N
|
|
|
29
28
|
try {
|
|
30
29
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const data = await req.prisma.${(0, strings_1.lowercaseFirstLetter)(modelName)}.upsert(req.body);
|
|
31
|
+
const data = await req.prisma.${(0, strings_1.toPascalCase)(modelName)}.upsert(req.body);
|
|
37
32
|
|
|
38
|
-
if (
|
|
33
|
+
if (outputValidator) {
|
|
39
34
|
const validationResult = outputValidator.safeParse(data);
|
|
40
35
|
if (validationResult.success) {
|
|
41
36
|
return res.status(200).json(validationResult.data);
|
|
@@ -45,8 +40,8 @@ export async function ${functionName}(req: UpsertRequest, res: Response, next: N
|
|
|
45
40
|
} else {
|
|
46
41
|
return res.status(200).json(data);
|
|
47
42
|
}
|
|
48
|
-
} catch
|
|
49
|
-
|
|
43
|
+
} catch(error: unknown) {
|
|
44
|
+
next(error)
|
|
50
45
|
}
|
|
51
46
|
}`;
|
|
52
47
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateUpsert.js","sourceRoot":"","sources":["../../src/helpers/generateUpsert.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"generateUpsert.js","sourceRoot":"","sources":["../../src/helpers/generateUpsert.ts"],"names":[],"mappings":";;;AACA,8CAA+C;AASxC,MAAM,sBAAsB,GAAG,CAAC,OAGtC,EAAU,EAAE;IACX,MAAM,EAAE,KAAK,EAAE,qBAAqB,EAAE,GAAG,OAAO,CAAA;IAChD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,YAAY,GAAG,GAAG,SAAS,QAAQ,CAAA;IACzC,MAAM,YAAY,GAAG,UAAU,SAAS,YAAY,CAAA;IAEpD,OAAO;EACP,qBAAqB;;;;;;;UAOb,YAAY;;;;;;;uEAOiD,YAAY;;wBAE3D,YAAY;;;;oCAIA,IAAA,sBAAY,EAAC,SAAS,CAAC;;;;;;;;;;;;;;;EAezD,CAAA;AACF,CAAC,CAAA;AA9CY,QAAA,sBAAsB,0BA8ClC"}
|
package/dist/utils/strings.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.toPascalCase = exports.capitalize = void 0;
|
|
4
4
|
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
|
|
5
5
|
exports.capitalize = capitalize;
|
|
6
|
-
function
|
|
6
|
+
function toPascalCase(str) {
|
|
7
7
|
if (!str)
|
|
8
8
|
return str;
|
|
9
9
|
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
10
10
|
}
|
|
11
|
-
exports.
|
|
11
|
+
exports.toPascalCase = toPascalCase;
|
|
12
12
|
//# sourceMappingURL=strings.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strings.js","sourceRoot":"","sources":["../../src/utils/strings.ts"],"names":[],"mappings":";;;AAAO,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,
|
|
1
|
+
{"version":3,"file":"strings.js","sourceRoot":"","sources":["../../src/utils/strings.ts"],"names":[],"mappings":";;;AAAO,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,YAAY,CAAC,GAAW;IACtC,IAAI,CAAC,GAAG;QAAE,OAAO,GAAG,CAAA;IACpB,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AACnD,CAAC;AAHD,oCAGC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-generator-express",
|
|
3
3
|
"description": "Prisma generator of Express CRUD API",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.15.1",
|
|
5
5
|
"main": "dist/generator.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"bin": {
|
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
"prepublishOnly": "node copy.js "
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@prisma/client": "5.
|
|
24
|
-
"@prisma/generator-helper": "5.
|
|
23
|
+
"@prisma/client": "5.15.0",
|
|
24
|
+
"@prisma/generator-helper": "5.15.0",
|
|
25
25
|
"@prisma/sdk": "4.0.0",
|
|
26
26
|
"express": "^4.19.2",
|
|
27
27
|
"lodash": "^4.17.21",
|
|
28
|
-
"prettier": "3.2
|
|
28
|
+
"prettier": "3.3.2",
|
|
29
29
|
"zod": "^3.23.8"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"@types/express": "^4.17.21",
|
|
36
36
|
"@types/jest": "29.5.12",
|
|
37
37
|
"@types/lodash": "^4.17.4",
|
|
38
|
-
"@types/node": "20.
|
|
38
|
+
"@types/node": "20.14.2",
|
|
39
39
|
"@types/prettier": "3.0.0",
|
|
40
40
|
"jest": "29.7.0",
|
|
41
|
-
"prisma": "5.
|
|
42
|
-
"semantic-release": "^
|
|
41
|
+
"prisma": "5.15.0",
|
|
42
|
+
"semantic-release": "^24.0.0",
|
|
43
43
|
"ts-jest": "29.1.4",
|
|
44
44
|
"typescript": "5.4.5"
|
|
45
45
|
},
|
package/src/generator.ts
CHANGED
|
@@ -35,6 +35,16 @@ generatorHandler({
|
|
|
35
35
|
const prismaImportStatement = generateImportPrismaStatement(options)
|
|
36
36
|
|
|
37
37
|
for await (const model of options.dmmf.datamodel.models) {
|
|
38
|
+
if (
|
|
39
|
+
model.documentation &&
|
|
40
|
+
model.documentation.includes('generator off')
|
|
41
|
+
) {
|
|
42
|
+
logger.info(
|
|
43
|
+
`Skipping generation for model ${model.name} as it is marked with 'generator off'.`,
|
|
44
|
+
)
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
|
|
38
48
|
await writeFileSafely({
|
|
39
49
|
content: generateFindUniqueFunction({
|
|
40
50
|
model,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { capitalize,
|
|
2
|
+
import { capitalize, toPascalCase } from '../utils/strings'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Generates an Express middleware function that handles aggregation queries
|
|
@@ -29,7 +29,6 @@ interface AggregateRequest 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?: ValidatorConfig;
|
|
35
34
|
};
|
|
@@ -41,13 +40,9 @@ export async function ${functionName}(req: AggregateRequest, res: Response, next
|
|
|
41
40
|
try {
|
|
42
41
|
const outputValidator = res.locals.outputValidator?.schema || req.outputValidation;
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const result = await req.prisma.${lowercaseFirstLetter(modelName)}.aggregate(req.query as ${argsTypeName});
|
|
43
|
+
const result = await req.prisma.${toPascalCase(modelName)}.aggregate(req.query as ${argsTypeName});
|
|
49
44
|
|
|
50
|
-
if (
|
|
45
|
+
if (outputValidator) {
|
|
51
46
|
const validationResult = outputValidator.safeParse(result);
|
|
52
47
|
if (validationResult.success) {
|
|
53
48
|
return res.status(200).json(validationResult.data);
|
|
@@ -57,8 +52,8 @@ export async function ${functionName}(req: AggregateRequest, res: Response, next
|
|
|
57
52
|
} else {
|
|
58
53
|
return res.status(200).json(result);
|
|
59
54
|
}
|
|
60
|
-
} catch
|
|
61
|
-
|
|
55
|
+
} catch(error: unknown) {
|
|
56
|
+
next(error)
|
|
62
57
|
}
|
|
63
58
|
}`
|
|
64
59
|
}
|
|
@@ -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 count queries
|
|
@@ -28,7 +28,6 @@ interface CountRequest extends Request {
|
|
|
28
28
|
prisma: PrismaClient;
|
|
29
29
|
query: Partial<${argsTypeName}> & ParsedQs;
|
|
30
30
|
outputValidation?: ZodTypeAny;
|
|
31
|
-
omitOutputValidation?: boolean;
|
|
32
31
|
locals?: {
|
|
33
32
|
outputValidator?: ZodTypeAny;
|
|
34
33
|
};
|
|
@@ -40,13 +39,9 @@ export async function ${functionName}(req: CountRequest, res: Response, next: Ne
|
|
|
40
39
|
try {
|
|
41
40
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const result = await req.prisma.${lowercaseFirstLetter(modelName)}.count(req.query as ${argsTypeName});
|
|
42
|
+
const result = await req.prisma.${toPascalCase(modelName)}.count(req.query as ${argsTypeName});
|
|
48
43
|
|
|
49
|
-
if (
|
|
44
|
+
if (outputValidator) {
|
|
50
45
|
const validationResult = outputValidator.safeParse(result);
|
|
51
46
|
if (validationResult.success) {
|
|
52
47
|
return res.status(200).json(validationResult.data);
|
|
@@ -56,8 +51,8 @@ export async function ${functionName}(req: CountRequest, res: Response, next: Ne
|
|
|
56
51
|
} else {
|
|
57
52
|
return res.status(200).json(result);
|
|
58
53
|
}
|
|
59
|
-
} catch
|
|
60
|
-
|
|
54
|
+
} catch(error: unknown) {
|
|
55
|
+
next(error)
|
|
61
56
|
}
|
|
62
57
|
}`
|
|
63
58
|
}
|
|
@@ -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 creation of records and includes conditional output validation with Zod.
|
|
@@ -26,7 +26,6 @@ interface CreateRequest 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: CreateRequest, 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)}.create(req.body);
|
|
40
|
+
const data = await req.prisma.${toPascalCase(modelName)}.create(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(201).json(validationResult.data);
|
|
@@ -54,8 +49,8 @@ export async function ${functionName}(req: CreateRequest, res: Response, next: N
|
|
|
54
49
|
} else {
|
|
55
50
|
return res.status(201).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
|
* Generates an Express middleware function that handles the creation of multiple records and includes conditional output validation with Zod.
|
|
5
5
|
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
@@ -25,7 +25,6 @@ interface CreateManyRequest extends Request {
|
|
|
25
25
|
prisma: PrismaClient;
|
|
26
26
|
body: ${argsTypeName};
|
|
27
27
|
outputValidation?: ZodTypeAny;
|
|
28
|
-
omitOutputValidation?: boolean;
|
|
29
28
|
locals?: {
|
|
30
29
|
outputValidator?: ZodTypeAny;
|
|
31
30
|
};
|
|
@@ -37,13 +36,9 @@ export async function ${functionName}(req: CreateManyRequest, res: Response, nex
|
|
|
37
36
|
try {
|
|
38
37
|
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
39
38
|
|
|
40
|
-
|
|
41
|
-
throw new Error('Output validation schema or omission flag must be provided.');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.createMany(req.body);
|
|
39
|
+
const data = await req.prisma.${toPascalCase(modelName)}.createMany(req.body);
|
|
45
40
|
|
|
46
|
-
if (
|
|
41
|
+
if (outputValidator) {
|
|
47
42
|
const validationResult = outputValidator.safeParse(data);
|
|
48
43
|
if (validationResult.success) {
|
|
49
44
|
return res.status(201).json(validationResult.data);
|
|
@@ -53,8 +48,8 @@ export async function ${functionName}(req: CreateManyRequest, res: Response, nex
|
|
|
53
48
|
} else {
|
|
54
49
|
return res.status(201).json(data);
|
|
55
50
|
}
|
|
56
|
-
} catch
|
|
57
|
-
|
|
51
|
+
} catch(error: unknown) {
|
|
52
|
+
next(error)
|
|
58
53
|
}
|
|
59
54
|
}`
|
|
60
55
|
}
|
|
@@ -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 deleting records
|
|
@@ -27,7 +27,6 @@ interface DeleteRequest 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: DeleteRequest, 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)}.delete(req.body);
|
|
41
|
+
const data = await req.prisma.${toPascalCase(modelName)}.delete(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: DeleteRequest, 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
|
}
|
|
@@ -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 batch deleting records
|
|
@@ -27,7 +27,6 @@ interface DeleteManyRequest 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: DeleteManyRequest, res: Response, nex
|
|
|
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 result = await req.prisma.${lowercaseFirstLetter(modelName)}.deleteMany(req.body);
|
|
41
|
+
const result = await req.prisma.${toPascalCase(modelName)}.deleteMany(req.body);
|
|
47
42
|
|
|
48
|
-
if (
|
|
43
|
+
if (outputValidator) {
|
|
49
44
|
const validationResult = outputValidator.safeParse(result);
|
|
50
45
|
if (validationResult.success) {
|
|
51
46
|
return res.status(200).json(validationResult.data);
|
|
@@ -55,8 +50,8 @@ export async function ${functionName}(req: DeleteManyRequest, res: Response, nex
|
|
|
55
50
|
} else {
|
|
56
51
|
return res.status(200).json(result);
|
|
57
52
|
}
|
|
58
|
-
} catch
|
|
59
|
-
|
|
53
|
+
} catch(error: unknown) {
|
|
54
|
+
next(error)
|
|
60
55
|
}
|
|
61
56
|
}`
|
|
62
57
|
}
|