prisma-generator-express 1.15.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +0 -2
  2. package/dist/helpers/generateAggregate.js +4 -9
  3. package/dist/helpers/generateAggregate.js.map +1 -1
  4. package/dist/helpers/generateCount.js +4 -9
  5. package/dist/helpers/generateCount.js.map +1 -1
  6. package/dist/helpers/generateCreate.js +4 -9
  7. package/dist/helpers/generateCreate.js.map +1 -1
  8. package/dist/helpers/generateCreateMany.js +4 -9
  9. package/dist/helpers/generateCreateMany.js.map +1 -1
  10. package/dist/helpers/generateDelete.js +4 -9
  11. package/dist/helpers/generateDelete.js.map +1 -1
  12. package/dist/helpers/generateDeleteMany.js +4 -9
  13. package/dist/helpers/generateDeleteMany.js.map +1 -1
  14. package/dist/helpers/generateFindFirst.js +4 -9
  15. package/dist/helpers/generateFindFirst.js.map +1 -1
  16. package/dist/helpers/generateFindMany.js +4 -9
  17. package/dist/helpers/generateFindMany.js.map +1 -1
  18. package/dist/helpers/generateFindUnique.js +4 -9
  19. package/dist/helpers/generateFindUnique.js.map +1 -1
  20. package/dist/helpers/generateGroupBy.js +4 -9
  21. package/dist/helpers/generateGroupBy.js.map +1 -1
  22. package/dist/helpers/generateRouteFile.js +28 -28
  23. package/dist/helpers/generateUpdate.js +4 -9
  24. package/dist/helpers/generateUpdate.js.map +1 -1
  25. package/dist/helpers/generateUpdateMany.js +4 -9
  26. package/dist/helpers/generateUpdateMany.js.map +1 -1
  27. package/dist/helpers/generateUpsert.js +4 -9
  28. package/dist/helpers/generateUpsert.js.map +1 -1
  29. package/dist/utils/strings.js +3 -3
  30. package/dist/utils/strings.js.map +1 -1
  31. package/package.json +6 -6
  32. package/src/copy/routeConfig.ts +2 -2
  33. package/src/helpers/generateAggregate.ts +5 -10
  34. package/src/helpers/generateCount.ts +5 -10
  35. package/src/helpers/generateCreate.ts +5 -10
  36. package/src/helpers/generateCreateMany.ts +5 -10
  37. package/src/helpers/generateDelete.ts +5 -10
  38. package/src/helpers/generateDeleteMany.ts +5 -10
  39. package/src/helpers/generateFindFirst.ts +5 -10
  40. package/src/helpers/generateFindMany.ts +5 -10
  41. package/src/helpers/generateFindUnique.ts +5 -10
  42. package/src/helpers/generateGroupBy.ts +5 -10
  43. package/src/helpers/generateRouteFile.ts +28 -28
  44. package/src/helpers/generateUpdate.ts +5 -10
  45. package/src/helpers/generateUpdateMany.ts +5 -10
  46. package/src/helpers/generateUpsert.ts +5 -10
  47. package/src/utils/strings.ts +1 -1
@@ -32,8 +32,8 @@ import { parseQueryParams } from "../parseQueryParams";
32
32
  const defaultBeforeAfter = {
33
33
  before: [] as RequestHandler[],
34
34
  after: [] as RequestHandler[],
35
- input: undefined,
36
- output: undefined,
35
+ inputValidator: undefined,
36
+ outputValidator: undefined,
37
37
  };
38
38
 
39
39
  /**
@@ -85,104 +85,104 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
85
85
 
86
86
 
87
87
  if (config.enableAll || config?.findFirst) {
88
- const { before = [], after = [], input, output } = config.findFirst || defaultBeforeAfter;
89
- setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler, input, output);
88
+ const { before = [], after = [], inputValidator, outputValidator) = config.findFirst || defaultBeforeAfter;
89
+ setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler, inputValidator, outputValidator);
90
90
  if (after.length) {
91
91
  router.use(basePath + '/first', ...after);
92
92
  }
93
93
  }
94
94
 
95
95
  if (config.enableAll || config?.findMany) {
96
- const { before = [], after = [], input, output } = config.findMany || defaultBeforeAfter;
97
- setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler, input, output);
96
+ const { before = [], after = [], inputValidator, outputValidator) = config.findMany || defaultBeforeAfter;
97
+ setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler, inputValidator, outputValidator);
98
98
  if (after.length) {
99
99
  router.use(basePath + '/', ...after);
100
100
  }
101
101
  }
102
102
 
103
103
  if (config.enableAll || config?.findUnique) {
104
- const { before = [], after = [], input, output } = config.findUnique || defaultBeforeAfter;
105
- setupRoute('/:id', 'get', before, ${modelName}FindUnique as any, input, output);
104
+ const { before = [], after = [], inputValidator, outputValidator) = config.findUnique || defaultBeforeAfter;
105
+ setupRoute('/:id', 'get', before, ${modelName}FindUnique as any, inputValidator, outputValidator);
106
106
  if (after.length) {
107
107
  router.use(basePath + '/:id', ...after);
108
108
  }
109
109
  }
110
110
 
111
111
  if (config.enableAll || config?.create) {
112
- const { before = [], after = [], input, output } = config.create || defaultBeforeAfter;
113
- setupRoute('/', 'post', before, ${modelName}Create as RequestHandler, input, output);
112
+ const { before = [], after = [], inputValidator, outputValidator) = config.create || defaultBeforeAfter;
113
+ setupRoute('/', 'post', before, ${modelName}Create as RequestHandler, inputValidator, outputValidator);
114
114
  if (after.length) {
115
115
  router.use(basePath + '/', ...after);
116
116
  }
117
117
  }
118
118
 
119
119
  if (config.enableAll || config?.createMany) {
120
- const { before = [], after = [], input, output } = config.createMany || defaultBeforeAfter;
121
- setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler, input, output);
120
+ const { before = [], after = [], inputValidator, outputValidator) = config.createMany || defaultBeforeAfter;
121
+ setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler, inputValidator, outputValidator);
122
122
  if (after.length) {
123
123
  router.use(basePath + '/many', ...after);
124
124
  }
125
125
  }
126
126
 
127
127
  if (config.enableAll || config?.update) {
128
- const { before = [], after = [], input, output } = config.update || defaultBeforeAfter;
129
- setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, input, output);
128
+ const { before = [], after = [], inputValidator, outputValidator) = config.update || defaultBeforeAfter;
129
+ setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, inputValidator, outputValidator);
130
130
  if (after.length) {
131
131
  router.use(basePath + '/', ...after);
132
132
  }
133
133
  }
134
134
 
135
135
  if (config.enableAll || config?.updateMany) {
136
- const { before = [], after = [], input, output } = config.updateMany || defaultBeforeAfter;
137
- setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler, input, output);
136
+ const { before = [], after = [], inputValidator, outputValidator) = config.updateMany || defaultBeforeAfter;
137
+ setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler, inputValidator, outputValidator);
138
138
  if (after.length) {
139
139
  router.use(basePath + '/many', ...after);
140
140
  }
141
141
  }
142
142
 
143
143
  if (config.enableAll || config?.upsert) {
144
- const { before = [], after = [], input, output } = config.upsert || defaultBeforeAfter;
145
- setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, input, output);
144
+ const { before = [], after = [], inputValidator, outputValidator) = config.upsert || defaultBeforeAfter;
145
+ setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, inputValidator, outputValidator);
146
146
  if (after.length) {
147
147
  router.use(basePath + '/', ...after);
148
148
  }
149
149
  }
150
150
 
151
151
  if (config.enableAll || config?.delete) {
152
- const { before = [], after = [], input, output } = config.delete || defaultBeforeAfter;
153
- setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler, input, output);
152
+ const { before = [], after = [], inputValidator, outputValidator) = config.delete || defaultBeforeAfter;
153
+ setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler, inputValidator, outputValidator);
154
154
  if (after.length) {
155
155
  router.use(basePath + '/', ...after);
156
156
  }
157
157
  }
158
158
 
159
159
  if (config.enableAll || config?.deleteMany) {
160
- const { before = [], after = [], input, output } = config.deleteMany || defaultBeforeAfter;
161
- setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler, input, output);
160
+ const { before = [], after = [], inputValidator, outputValidator) = config.deleteMany || defaultBeforeAfter;
161
+ setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler, inputValidator, outputValidator);
162
162
  if (after.length) {
163
163
  router.use(basePath + '/many', ...after);
164
164
  }
165
165
  }
166
166
 
167
167
  if (config.enableAll || config?.aggregate) {
168
- const { before = [], after = [], input, output } = config.aggregate || defaultBeforeAfter;
169
- setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler, input, output);
168
+ const { before = [], after = [], inputValidator, outputValidator) = config.aggregate || defaultBeforeAfter;
169
+ setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler, inputValidator, outputValidator);
170
170
  if (after.length) {
171
171
  router.use(basePath + '/aggregate', ...after);
172
172
  }
173
173
  }
174
174
 
175
175
  if (config.enableAll || config?.count) {
176
- const { before = [], after = [], input, output } = config.count || defaultBeforeAfter;
177
- setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler, input, output);
176
+ const { before = [], after = [], inputValidator, outputValidator) = config.count || defaultBeforeAfter;
177
+ setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler, inputValidator, outputValidator);
178
178
  if (after.length) {
179
179
  router.use(basePath + '/count', ...after);
180
180
  }
181
181
  }
182
182
 
183
183
  if (config.enableAll || config?.groupBy) {
184
- const { before = [], after = [], input, output } = config.groupBy || defaultBeforeAfter;
185
- setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler, input, output);
184
+ const { before = [], after = [], inputValidator, outputValidator) = config.groupBy || defaultBeforeAfter;
185
+ setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler, inputValidator, outputValidator);
186
186
  if (after.length) {
187
187
  router.use(basePath + '/groupby', ...after);
188
188
  }
@@ -1,5 +1,5 @@
1
1
  import { DMMF } from '@prisma/generator-helper'
2
- import { lowercaseFirstLetter } from '../utils/strings'
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
- if (!outputValidator && !req.omitOutputValidation) {
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 (!req.omitOutputValidation && outputValidator) {
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 (error: unknown) {
58
- return next(error);
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 { lowercaseFirstLetter } from '../utils/strings'
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
- if (!outputValidator && !req.omitOutputValidation) {
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 (!req.omitOutputValidation && outputValidator) {
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 (error: unknown) {
58
- return next(error);
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 { lowercaseFirstLetter } from '../utils/strings'
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
- if (!outputValidator && !req.omitOutputValidation) {
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 (!req.omitOutputValidation && outputValidator) {
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 (error: unknown) {
59
- return next(error);
53
+ } catch(error: unknown) {
54
+ next(error)
60
55
  }
61
56
  }`
62
57
  }
@@ -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 lowercaseFirstLetter(str: string) {
4
+ export function toPascalCase(str: string) {
5
5
  if (!str) return str
6
6
  return str.charAt(0).toLowerCase() + str.slice(1)
7
7
  }