prisma-generator-express 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/helpers/generateAggregate.js +6 -4
- package/dist/helpers/generateAggregate.js.map +1 -1
- package/dist/helpers/generateCount.js +6 -4
- package/dist/helpers/generateCount.js.map +1 -1
- package/dist/helpers/generateCreate.js +2 -1
- package/dist/helpers/generateCreate.js.map +1 -1
- package/dist/helpers/generateCreateMany.js +2 -1
- package/dist/helpers/generateCreateMany.js.map +1 -1
- package/dist/helpers/generateDelete.js +2 -1
- package/dist/helpers/generateDelete.js.map +1 -1
- package/dist/helpers/generateDeleteMany.js +2 -1
- package/dist/helpers/generateDeleteMany.js.map +1 -1
- package/dist/helpers/generateFindFirst.js +2 -1
- package/dist/helpers/generateFindFirst.js.map +1 -1
- package/dist/helpers/generateFindMany.js +2 -1
- package/dist/helpers/generateFindMany.js.map +1 -1
- package/dist/helpers/generateFindUnique.js +2 -1
- package/dist/helpers/generateFindUnique.js.map +1 -1
- package/dist/helpers/generateGroupBy.js +7 -4
- package/dist/helpers/generateGroupBy.js.map +1 -1
- package/dist/helpers/generateRouteFile.js +10 -10
- package/dist/helpers/generateUpdate.js +2 -4
- package/dist/helpers/generateUpdate.js.map +1 -1
- package/dist/helpers/generateUpdateMany.js +2 -1
- package/dist/helpers/generateUpdateMany.js.map +1 -1
- package/dist/helpers/generateUpsert.js +2 -1
- package/dist/helpers/generateUpsert.js.map +1 -1
- package/dist/utils/strings.js +12 -0
- package/dist/utils/strings.js.map +1 -0
- package/package.json +11 -3
- package/src/bin.ts +2 -0
- package/src/constants.ts +1 -0
- package/src/generator.ts +174 -0
- package/src/helpers/generateAggregate.ts +60 -0
- package/src/helpers/generateCount.ts +60 -0
- package/src/helpers/generateCreate.ts +58 -0
- package/src/helpers/generateCreateMany.ts +58 -0
- package/src/helpers/generateDelete.ts +60 -0
- package/src/helpers/generateDeleteMany.ts +58 -0
- package/src/helpers/generateFindFirst.ts +68 -0
- package/src/helpers/generateFindMany.ts +68 -0
- package/src/helpers/generateFindUnique.ts +68 -0
- package/src/helpers/generateGroupBy.ts +62 -0
- package/src/helpers/generateImportPrismaStatement.ts +38 -0
- package/src/helpers/generateRouteFile.ts +183 -0
- package/src/helpers/generateUpdate.ts +59 -0
- package/src/helpers/generateUpdateMany.ts +59 -0
- package/src/helpers/generateUpsert.ts +60 -0
- package/src/utils/formatFile.ts +22 -0
- package/src/utils/strings.ts +7 -0
- package/src/utils/writeFileSafely.ts +28 -0
- package/CHANGELOG.md +0 -14
package/src/generator.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { generatorHandler, GeneratorOptions } from '@prisma/generator-helper'
|
|
2
|
+
import { logger } from '@prisma/sdk'
|
|
3
|
+
import { GENERATOR_NAME } from './constants'
|
|
4
|
+
import { writeFileSafely } from './utils/writeFileSafely'
|
|
5
|
+
import { generateFindUniqueFunction } from './helpers/generateFindUnique'
|
|
6
|
+
import { generateImportPrismaStatement } from './helpers/generateImportPrismaStatement'
|
|
7
|
+
import { generateFindManyFunction } from './helpers/generateFindMany'
|
|
8
|
+
import { generateFindFirstFunction } from './helpers/generateFindFirst'
|
|
9
|
+
import { generateCreateFunction } from './helpers/generateCreate'
|
|
10
|
+
import { generateRouterFunction } from './helpers/generateRouteFile'
|
|
11
|
+
import { generateCreateManyFunction } from './helpers/generateCreateMany'
|
|
12
|
+
import { generateUpdateFunction } from './helpers/generateUpdate'
|
|
13
|
+
import { generateUpdateManyFunction } from './helpers/generateUpdateMany'
|
|
14
|
+
import { generateUpsertFunction } from './helpers/generateUpsert'
|
|
15
|
+
import { generateDeleteFunction } from './helpers/generateDelete'
|
|
16
|
+
import { generateDeleteManyFunction } from './helpers/generateDeleteMany'
|
|
17
|
+
import { generateAggregateFunction } from './helpers/generateAggregate'
|
|
18
|
+
import { generateCountFunction } from './helpers/generateCount'
|
|
19
|
+
import { generateGroupByFunction } from './helpers/generateGroupBy'
|
|
20
|
+
|
|
21
|
+
const { version } = require('../package.json')
|
|
22
|
+
|
|
23
|
+
generatorHandler({
|
|
24
|
+
onManifest() {
|
|
25
|
+
logger.info(`${GENERATOR_NAME}:Registered`)
|
|
26
|
+
return {
|
|
27
|
+
version,
|
|
28
|
+
defaultOutput: '../generated',
|
|
29
|
+
prettyName: GENERATOR_NAME,
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
onGenerate: async (options: GeneratorOptions) => {
|
|
33
|
+
const prismaImportStatement = generateImportPrismaStatement(options)
|
|
34
|
+
|
|
35
|
+
options.dmmf.datamodel.models.forEach(async (model) => {
|
|
36
|
+
await writeFileSafely({
|
|
37
|
+
content: generateFindUniqueFunction({
|
|
38
|
+
model,
|
|
39
|
+
prismaImportStatement,
|
|
40
|
+
}),
|
|
41
|
+
options,
|
|
42
|
+
model,
|
|
43
|
+
operation: 'FindUnique',
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
await writeFileSafely({
|
|
47
|
+
content: generateFindFirstFunction({
|
|
48
|
+
model,
|
|
49
|
+
prismaImportStatement,
|
|
50
|
+
}),
|
|
51
|
+
options,
|
|
52
|
+
model,
|
|
53
|
+
operation: 'FindFirst',
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
await writeFileSafely({
|
|
57
|
+
content: generateFindManyFunction({
|
|
58
|
+
model,
|
|
59
|
+
prismaImportStatement,
|
|
60
|
+
}),
|
|
61
|
+
options,
|
|
62
|
+
model,
|
|
63
|
+
operation: 'FindMany',
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
await writeFileSafely({
|
|
67
|
+
content: generateCreateFunction({
|
|
68
|
+
model,
|
|
69
|
+
prismaImportStatement,
|
|
70
|
+
}),
|
|
71
|
+
options,
|
|
72
|
+
model,
|
|
73
|
+
operation: 'Create',
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
await writeFileSafely({
|
|
77
|
+
content: generateCreateManyFunction({
|
|
78
|
+
model,
|
|
79
|
+
prismaImportStatement,
|
|
80
|
+
}),
|
|
81
|
+
options,
|
|
82
|
+
model,
|
|
83
|
+
operation: 'CreateMany',
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
await writeFileSafely({
|
|
87
|
+
content: generateUpdateFunction({
|
|
88
|
+
model,
|
|
89
|
+
prismaImportStatement,
|
|
90
|
+
}),
|
|
91
|
+
options,
|
|
92
|
+
model,
|
|
93
|
+
operation: 'Update',
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
await writeFileSafely({
|
|
97
|
+
content: generateUpdateManyFunction({
|
|
98
|
+
model,
|
|
99
|
+
prismaImportStatement,
|
|
100
|
+
}),
|
|
101
|
+
options,
|
|
102
|
+
model,
|
|
103
|
+
operation: 'UpdateMany',
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
await writeFileSafely({
|
|
107
|
+
content: generateUpsertFunction({
|
|
108
|
+
model,
|
|
109
|
+
prismaImportStatement,
|
|
110
|
+
}),
|
|
111
|
+
options,
|
|
112
|
+
model,
|
|
113
|
+
operation: 'Upsert',
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
await writeFileSafely({
|
|
117
|
+
content: generateDeleteFunction({
|
|
118
|
+
model,
|
|
119
|
+
prismaImportStatement,
|
|
120
|
+
}),
|
|
121
|
+
options,
|
|
122
|
+
model,
|
|
123
|
+
operation: 'Delete',
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
await writeFileSafely({
|
|
127
|
+
content: generateDeleteManyFunction({
|
|
128
|
+
model,
|
|
129
|
+
prismaImportStatement,
|
|
130
|
+
}),
|
|
131
|
+
options,
|
|
132
|
+
model,
|
|
133
|
+
operation: 'DeleteMany',
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
await writeFileSafely({
|
|
137
|
+
content: generateAggregateFunction({
|
|
138
|
+
model,
|
|
139
|
+
prismaImportStatement,
|
|
140
|
+
}),
|
|
141
|
+
options,
|
|
142
|
+
model,
|
|
143
|
+
operation: 'Aggregate',
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
await writeFileSafely({
|
|
147
|
+
content: generateCountFunction({
|
|
148
|
+
model,
|
|
149
|
+
prismaImportStatement,
|
|
150
|
+
}),
|
|
151
|
+
options,
|
|
152
|
+
model,
|
|
153
|
+
operation: 'Count',
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
await writeFileSafely({
|
|
157
|
+
content: generateGroupByFunction({
|
|
158
|
+
model,
|
|
159
|
+
prismaImportStatement,
|
|
160
|
+
}),
|
|
161
|
+
options,
|
|
162
|
+
model,
|
|
163
|
+
operation: 'GroupBy',
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
await writeFileSafely({
|
|
167
|
+
content: generateRouterFunction({ model }),
|
|
168
|
+
options,
|
|
169
|
+
model,
|
|
170
|
+
operation: 'index',
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
},
|
|
174
|
+
})
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { capitalize, lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generates an Express middleware function that handles aggregation queries
|
|
6
|
+
* and includes conditional output validation with Zod.
|
|
7
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
8
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
9
|
+
* @returns {string} - The generated middleware function as a string.
|
|
10
|
+
*/
|
|
11
|
+
export const generateAggregateFunction = (options: {
|
|
12
|
+
model: DMMF.Model
|
|
13
|
+
prismaImportStatement: string
|
|
14
|
+
}): string => {
|
|
15
|
+
const { model, prismaImportStatement } = options
|
|
16
|
+
const modelName = model.name
|
|
17
|
+
const functionName = `${modelName}Aggregate`
|
|
18
|
+
const argsTypeName = `Prisma.${capitalize(modelName)}AggregateArgs`
|
|
19
|
+
|
|
20
|
+
return `
|
|
21
|
+
${prismaImportStatement}
|
|
22
|
+
import { Request, Response, NextFunction } from 'express';
|
|
23
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core'
|
|
24
|
+
import { ParsedQs } from 'qs'
|
|
25
|
+
import { ZodTypeAny } from 'zod';
|
|
26
|
+
|
|
27
|
+
interface AggregateRequest extends Request {
|
|
28
|
+
prisma: PrismaClient;
|
|
29
|
+
query: Partial<${argsTypeName}> & ParsedQs;
|
|
30
|
+
outputValidation?: ZodTypeAny;
|
|
31
|
+
omitOutputValidation?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type AggregateMiddleware = RequestHandler<ParamsDictionary, any, Partial<${argsTypeName}>, Record<string, any>>;
|
|
35
|
+
|
|
36
|
+
export async function ${functionName}(req: AggregateRequest, res: Response, next: NextFunction) {
|
|
37
|
+
try {
|
|
38
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
39
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const result = await req.prisma.${lowercaseFirstLetter(modelName)}.aggregate(req.query as ${argsTypeName});
|
|
43
|
+
|
|
44
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
45
|
+
const validationResult = req.outputValidation.safeParse(result);
|
|
46
|
+
if (validationResult.success) {
|
|
47
|
+
res.status(200).json(validationResult.data);
|
|
48
|
+
} else {
|
|
49
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
res.status(200).json(result);
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error('Error in handling aggregation request:', error);
|
|
56
|
+
res.status(500).json({ error: error.message });
|
|
57
|
+
next(error);
|
|
58
|
+
}
|
|
59
|
+
}`
|
|
60
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generates an Express middleware function that handles count queries
|
|
6
|
+
* and includes conditional output validation with Zod.
|
|
7
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
8
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
9
|
+
* @returns {string} - The generated middleware function as a string.
|
|
10
|
+
*/
|
|
11
|
+
export const generateCountFunction = (options: {
|
|
12
|
+
model: DMMF.Model
|
|
13
|
+
prismaImportStatement: string
|
|
14
|
+
}): string => {
|
|
15
|
+
const { model, prismaImportStatement } = options
|
|
16
|
+
const modelName = model.name
|
|
17
|
+
const functionName = `${modelName}Count`
|
|
18
|
+
const argsTypeName = `Prisma.${modelName}CountArgs`
|
|
19
|
+
|
|
20
|
+
return `
|
|
21
|
+
${prismaImportStatement}
|
|
22
|
+
import { Request, Response, NextFunction } from 'express';
|
|
23
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
24
|
+
import { ParsedQs } from 'qs';
|
|
25
|
+
import { ZodTypeAny } from 'zod';
|
|
26
|
+
|
|
27
|
+
interface CountRequest extends Request {
|
|
28
|
+
prisma: PrismaClient;
|
|
29
|
+
query: Partial<${argsTypeName}> & ParsedQs;
|
|
30
|
+
outputValidation?: ZodTypeAny;
|
|
31
|
+
omitOutputValidation?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type CountMiddleware = RequestHandler<ParamsDictionary, any, {}, ParsedQs>;
|
|
35
|
+
|
|
36
|
+
export async function ${functionName}(req: CountRequest, res: Response, next: NextFunction) {
|
|
37
|
+
try {
|
|
38
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
39
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const result = await req.prisma.${lowercaseFirstLetter(modelName)}.count(req.query as ${argsTypeName});
|
|
43
|
+
|
|
44
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
45
|
+
const validationResult = req.outputValidation.safeParse(result);
|
|
46
|
+
if (validationResult.success) {
|
|
47
|
+
res.status(200).json(validationResult.data);
|
|
48
|
+
} else {
|
|
49
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
res.status(200).json(result);
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error('Error in handling count request:', error);
|
|
56
|
+
res.status(500).json({ error: error.message });
|
|
57
|
+
next(error);
|
|
58
|
+
}
|
|
59
|
+
}`
|
|
60
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
/**
|
|
4
|
+
* Generates an Express middleware function that handles creation of records and includes conditional output validation with Zod.
|
|
5
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
6
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
7
|
+
* @returns {string} - The generated middleware function as a string.
|
|
8
|
+
*/
|
|
9
|
+
export const generateCreateFunction = (options: {
|
|
10
|
+
model: DMMF.Model
|
|
11
|
+
prismaImportStatement: string
|
|
12
|
+
}): string => {
|
|
13
|
+
const { model, prismaImportStatement } = options
|
|
14
|
+
const modelName = model.name
|
|
15
|
+
const functionName = `${modelName}Create`
|
|
16
|
+
const argsTypeName = `Prisma.${modelName}CreateArgs`
|
|
17
|
+
|
|
18
|
+
return `
|
|
19
|
+
${prismaImportStatement}
|
|
20
|
+
import { Request, Response, NextFunction } from 'express';
|
|
21
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core'
|
|
22
|
+
import { ZodTypeAny } from 'zod';
|
|
23
|
+
|
|
24
|
+
interface CreateRequest extends Request {
|
|
25
|
+
prisma: PrismaClient;
|
|
26
|
+
body: ${argsTypeName};
|
|
27
|
+
outputValidation?: ZodTypeAny;
|
|
28
|
+
omitOutputValidation?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type CreateMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
32
|
+
|
|
33
|
+
export async function ${functionName}(req: CreateRequest, res: Response, next: NextFunction) {
|
|
34
|
+
try {
|
|
35
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
36
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.create(req.body);
|
|
40
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
41
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
42
|
+
if (validationResult.success) {
|
|
43
|
+
res.status(201).json(validationResult.data);
|
|
44
|
+
} else {
|
|
45
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
46
|
+
}
|
|
47
|
+
} else if (!req.omitOutputValidation) {
|
|
48
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted.');
|
|
49
|
+
} else {
|
|
50
|
+
res.status(201).json(data);
|
|
51
|
+
}
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Error in handling create request:', error);
|
|
54
|
+
res.status(500).json({ error: error.message });
|
|
55
|
+
next(error);
|
|
56
|
+
}
|
|
57
|
+
}`
|
|
58
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
/**
|
|
4
|
+
* Generates an Express middleware function that handles the creation of multiple records and includes conditional output validation with Zod.
|
|
5
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
6
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
7
|
+
* @returns {string} - The generated middleware function as a string.
|
|
8
|
+
*/
|
|
9
|
+
export const generateCreateManyFunction = (options: {
|
|
10
|
+
model: DMMF.Model
|
|
11
|
+
prismaImportStatement: string
|
|
12
|
+
}): string => {
|
|
13
|
+
const { model, prismaImportStatement } = options
|
|
14
|
+
const modelName = model.name
|
|
15
|
+
const functionName = `${modelName}CreateMany`
|
|
16
|
+
const argsTypeName = `Prisma.${modelName}CreateManyArgs`
|
|
17
|
+
|
|
18
|
+
return `
|
|
19
|
+
${prismaImportStatement}
|
|
20
|
+
import { Request, Response, NextFunction } from 'express';
|
|
21
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
22
|
+
import { ZodTypeAny } from 'zod';
|
|
23
|
+
|
|
24
|
+
interface CreateManyRequest extends Request {
|
|
25
|
+
prisma: PrismaClient;
|
|
26
|
+
body: ${argsTypeName};
|
|
27
|
+
outputValidation?: ZodTypeAny;
|
|
28
|
+
omitOutputValidation?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type CreateManyMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
32
|
+
|
|
33
|
+
export async function ${functionName}(req: CreateManyRequest, res: Response, next: NextFunction) {
|
|
34
|
+
try {
|
|
35
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
36
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.createMany(req.body);
|
|
40
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
41
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
42
|
+
if (validationResult.success) {
|
|
43
|
+
res.status(201).json(validationResult.data);
|
|
44
|
+
} else {
|
|
45
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
46
|
+
}
|
|
47
|
+
} else if (!req.omitOutputValidation) {
|
|
48
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted.');
|
|
49
|
+
} else {
|
|
50
|
+
res.status(201).json(data);
|
|
51
|
+
}
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Error in handling createMany request:', error);
|
|
54
|
+
res.status(500).json({ error: error.message });
|
|
55
|
+
next(error);
|
|
56
|
+
}
|
|
57
|
+
}`
|
|
58
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
/**
|
|
4
|
+
* Generates an Express middleware function that handles deleting records
|
|
5
|
+
* and includes conditional output validation with Zod.
|
|
6
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
7
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
8
|
+
* @returns {string} - The generated middleware function as a string.
|
|
9
|
+
*/
|
|
10
|
+
export const generateDeleteFunction = (options: {
|
|
11
|
+
model: DMMF.Model
|
|
12
|
+
prismaImportStatement: string
|
|
13
|
+
}): string => {
|
|
14
|
+
const { model, prismaImportStatement } = options
|
|
15
|
+
const modelName = model.name
|
|
16
|
+
const functionName = `${modelName}Delete`
|
|
17
|
+
const argsTypeName = `Prisma.${modelName}DeleteArgs`
|
|
18
|
+
|
|
19
|
+
return `
|
|
20
|
+
${prismaImportStatement}
|
|
21
|
+
import { Request, Response, NextFunction } from 'express';
|
|
22
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
23
|
+
import { ZodTypeAny } from 'zod';
|
|
24
|
+
|
|
25
|
+
interface DeleteRequest extends Request {
|
|
26
|
+
prisma: PrismaClient;
|
|
27
|
+
body: ${argsTypeName};
|
|
28
|
+
outputValidation?: ZodTypeAny;
|
|
29
|
+
omitOutputValidation?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type DeleteMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
33
|
+
|
|
34
|
+
export async function ${functionName}(req: DeleteRequest, res: Response, next: NextFunction) {
|
|
35
|
+
try {
|
|
36
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
37
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const data = await req.prisma.${lowercaseFirstLetter(modelName)}.delete(req.body);
|
|
41
|
+
|
|
42
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
43
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
44
|
+
if (validationResult.success) {
|
|
45
|
+
res.status(200).json(validationResult.data);
|
|
46
|
+
} else {
|
|
47
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
48
|
+
}
|
|
49
|
+
} else if (!req.omitOutputValidation) {
|
|
50
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted.');
|
|
51
|
+
} else {
|
|
52
|
+
res.status(200).json(data);
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error('Error in handling delete request:', error);
|
|
56
|
+
res.status(500).json({ error: error.message });
|
|
57
|
+
next(error);
|
|
58
|
+
}
|
|
59
|
+
}`
|
|
60
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
/**
|
|
4
|
+
* Generates an Express middleware function that handles batch deleting records
|
|
5
|
+
* and includes conditional output validation with Zod.
|
|
6
|
+
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
7
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
8
|
+
* @returns {string} - The generated middleware function as a string.
|
|
9
|
+
*/
|
|
10
|
+
export const generateDeleteManyFunction = (options: {
|
|
11
|
+
model: DMMF.Model
|
|
12
|
+
prismaImportStatement: string
|
|
13
|
+
}): string => {
|
|
14
|
+
const { model, prismaImportStatement } = options
|
|
15
|
+
const modelName = model.name
|
|
16
|
+
const functionName = `${modelName}DeleteMany`
|
|
17
|
+
const argsTypeName = `Prisma.${modelName}DeleteManyArgs`
|
|
18
|
+
|
|
19
|
+
return `
|
|
20
|
+
${prismaImportStatement}
|
|
21
|
+
import { Request, Response, NextFunction } from 'express';
|
|
22
|
+
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
23
|
+
import { ZodTypeAny } from 'zod';
|
|
24
|
+
|
|
25
|
+
interface DeleteManyRequest extends Request {
|
|
26
|
+
prisma: PrismaClient;
|
|
27
|
+
body: ${argsTypeName};
|
|
28
|
+
outputValidation?: ZodTypeAny;
|
|
29
|
+
omitOutputValidation?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type DeleteManyMiddleware = RequestHandler<ParamsDictionary, any, Prisma.${modelName}DeleteManyArgs, Record<string, any>>;
|
|
33
|
+
|
|
34
|
+
export async function ${functionName}(req: DeleteManyRequest, res: Response, next: NextFunction) {
|
|
35
|
+
try {
|
|
36
|
+
if (!req.outputValidation && !req.omitOutputValidation) {
|
|
37
|
+
throw new Error('Output validation schema or omission flag must be provided.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const result = await req.prisma.${lowercaseFirstLetter(modelName)}.deleteMany(req.body);
|
|
41
|
+
|
|
42
|
+
if (!req.omitOutputValidation && req.outputValidation) {
|
|
43
|
+
const validationResult = req.outputValidation.safeParse(result);
|
|
44
|
+
if (validationResult.success) {
|
|
45
|
+
res.status(200).json(validationResult.data);
|
|
46
|
+
} else {
|
|
47
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
res.status(200).json(result);
|
|
51
|
+
}
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Error in handling batch delete request:', error);
|
|
54
|
+
res.status(500).json({ error: error.message });
|
|
55
|
+
next(error);
|
|
56
|
+
}
|
|
57
|
+
}`
|
|
58
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
/**
|
|
4
|
+
* Generates an Express middleware function that includes conditional output validation with Zod.
|
|
5
|
+
* This version dynamically includes the correct type for the query parameter based on the Prisma model.
|
|
6
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
7
|
+
* @returns {string} - The generated middleware function as a string.
|
|
8
|
+
*/
|
|
9
|
+
export const generateFindFirstFunction = (options: {
|
|
10
|
+
model: DMMF.Model
|
|
11
|
+
prismaImportStatement: string
|
|
12
|
+
}): string => {
|
|
13
|
+
const { model, prismaImportStatement } = options
|
|
14
|
+
const modelName = model.name
|
|
15
|
+
const functionName = `${modelName}FindFirst`
|
|
16
|
+
const queryTypeName = `Prisma.${modelName}FindFirstArgs`
|
|
17
|
+
|
|
18
|
+
return `
|
|
19
|
+
${prismaImportStatement.replace('{ Prisma }', `{ Prisma, ${modelName} }`)}
|
|
20
|
+
import { Request, Response, NextFunction } from 'express'
|
|
21
|
+
import {
|
|
22
|
+
RequestHandler,
|
|
23
|
+
ParamsDictionary,
|
|
24
|
+
} from 'express-serve-static-core'
|
|
25
|
+
import { ParsedQs } from 'qs';
|
|
26
|
+
import { ZodTypeAny } from 'zod';
|
|
27
|
+
|
|
28
|
+
export interface FindFirstRequest extends Request {
|
|
29
|
+
prisma: PrismaClient;
|
|
30
|
+
query: ${queryTypeName} & ParsedQs;
|
|
31
|
+
outputValidation?: ZodTypeAny;
|
|
32
|
+
omitOutputValidation?: boolean;
|
|
33
|
+
passToNext?: boolean;
|
|
34
|
+
locals: {
|
|
35
|
+
data?: ${modelName} | null
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export type FindFirstMiddleware = RequestHandler<ParamsDictionary, any, any, ${queryTypeName} & ParsedQs, Record<string, any>>
|
|
39
|
+
|
|
40
|
+
export async function ${functionName}(req: FindFirstRequest, res: Response, next: NextFunction) {
|
|
41
|
+
try {
|
|
42
|
+
if (!req.outputValidation && !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)}.findFirst(req.query as ${queryTypeName});
|
|
47
|
+
if (req.passToNext) {
|
|
48
|
+
req.locals.data = data;
|
|
49
|
+
next();
|
|
50
|
+
} else if (!req.omitOutputValidation && req.outputValidation) {
|
|
51
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
52
|
+
if (validationResult.success) {
|
|
53
|
+
res.status(200).json(validationResult.data);
|
|
54
|
+
} else {
|
|
55
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
56
|
+
}
|
|
57
|
+
} else if (!req.omitOutputValidation) {
|
|
58
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted. Attach omitOutputValidation = true to request to suppress this error.');
|
|
59
|
+
} else {
|
|
60
|
+
res.status(200).json(data);
|
|
61
|
+
}
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error('Error in handling request:', error);
|
|
64
|
+
res.status(500).json({ error: error.message });
|
|
65
|
+
next(error);
|
|
66
|
+
}
|
|
67
|
+
}`
|
|
68
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { lowercaseFirstLetter } from '../utils/strings'
|
|
3
|
+
/**
|
|
4
|
+
* Generates an Express middleware function that handles multiple record retrieval with optional output validation using Zod.
|
|
5
|
+
* This version dynamically includes the correct type for the query parameter based on the Prisma model.
|
|
6
|
+
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
7
|
+
* @returns {string} - The generated middleware function as a string.
|
|
8
|
+
*/
|
|
9
|
+
export const generateFindManyFunction = (options: {
|
|
10
|
+
model: DMMF.Model
|
|
11
|
+
prismaImportStatement: string
|
|
12
|
+
}): string => {
|
|
13
|
+
const { model, prismaImportStatement } = options
|
|
14
|
+
const modelName = model.name
|
|
15
|
+
const functionName = `${modelName}FindMany`
|
|
16
|
+
const queryTypeName = `Prisma.${modelName}FindManyArgs`
|
|
17
|
+
|
|
18
|
+
return `
|
|
19
|
+
${prismaImportStatement.replace('{ Prisma }', `{ Prisma, ${modelName} }`)}
|
|
20
|
+
import { Request, Response, NextFunction } from 'express'
|
|
21
|
+
import {
|
|
22
|
+
RequestHandler,
|
|
23
|
+
ParamsDictionary,
|
|
24
|
+
} from 'express-serve-static-core'
|
|
25
|
+
import { ParsedQs } from 'qs';
|
|
26
|
+
import { ZodTypeAny } from 'zod';
|
|
27
|
+
|
|
28
|
+
export interface FindManyRequest extends Request {
|
|
29
|
+
prisma: PrismaClient;
|
|
30
|
+
query: ${queryTypeName} & ParsedQs;
|
|
31
|
+
outputValidation?: ZodTypeAny;
|
|
32
|
+
omitOutputValidation?: boolean;
|
|
33
|
+
passToNext?: boolean;
|
|
34
|
+
locals: {
|
|
35
|
+
data?: ${modelName}[]
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export type FindManyMiddleware = RequestHandler<ParamsDictionary, any, any, ${queryTypeName} & ParsedQs, Record<string, any>>
|
|
39
|
+
|
|
40
|
+
export async function ${functionName}(req: FindManyRequest, res: Response, next: NextFunction) {
|
|
41
|
+
try {
|
|
42
|
+
if (!req.outputValidation && !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)}.findMany(req.query as ${queryTypeName});
|
|
47
|
+
if (req.passToNext) {
|
|
48
|
+
req.locals.data = data;
|
|
49
|
+
next();
|
|
50
|
+
} else if (!req.omitOutputValidation && req.outputValidation) {
|
|
51
|
+
const validationResult = req.outputValidation.safeParse(data);
|
|
52
|
+
if (validationResult.success) {
|
|
53
|
+
res.status(200).json(validationResult.data);
|
|
54
|
+
} else {
|
|
55
|
+
res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
56
|
+
}
|
|
57
|
+
} else if (!req.omitOutputValidation) {
|
|
58
|
+
throw new Error('Output validation schema must be provided unless explicitly omitted. Attach omitOutputValidation = true to request to suppress this error.');
|
|
59
|
+
} else {
|
|
60
|
+
res.status(200).json(data);
|
|
61
|
+
}
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error('Error in handling request:', error);
|
|
64
|
+
res.status(500).json({ error: error.message });
|
|
65
|
+
next(error);
|
|
66
|
+
}
|
|
67
|
+
}`
|
|
68
|
+
}
|