prisma-generator-express 1.18.0 → 1.19.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 +399 -194
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +1 -1
- package/dist/bin.js.map +1 -1
- package/dist/client/encodeQueryParams.d.ts +1 -0
- package/dist/client/encodeQueryParams.js +33 -0
- package/dist/client/encodeQueryParams.js.map +1 -0
- package/dist/constants.d.ts +1 -0
- package/dist/copy/misc.d.ts +5 -0
- package/dist/copy/misc.js +52 -0
- package/dist/copy/misc.js.map +1 -0
- package/dist/generators/generateImportPrismaStatement.d.ts +3 -0
- package/dist/generators/generateImportPrismaStatement.js +55 -0
- package/dist/generators/generateImportPrismaStatement.js.map +1 -0
- package/dist/generators/generateQueryBuilderHelper.d.ts +2 -0
- package/dist/generators/generateQueryBuilderHelper.js +139 -0
- package/dist/generators/generateQueryBuilderHelper.js.map +1 -0
- package/dist/generators/generateRouter.d.ts +6 -0
- package/dist/generators/generateRouter.js +340 -0
- package/dist/generators/generateRouter.js.map +1 -0
- package/dist/generators/generateUnifiedDocs.d.ts +1 -0
- package/dist/generators/generateUnifiedDocs.js +171 -0
- package/dist/generators/generateUnifiedDocs.js.map +1 -0
- package/dist/generators/generateUnifiedHandler.d.ts +6 -0
- package/dist/generators/generateUnifiedHandler.js +444 -0
- package/dist/generators/generateUnifiedHandler.js.map +1 -0
- package/dist/generators/generateUnifiedScalarUI.d.ts +5 -0
- package/dist/generators/generateUnifiedScalarUI.js +1390 -0
- package/dist/generators/generateUnifiedScalarUI.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/copyFiles.d.ts +6 -0
- package/dist/utils/copyFiles.js +123 -21
- package/dist/utils/copyFiles.js.map +1 -1
- package/dist/utils/strings.d.ts +2 -0
- package/dist/utils/writeFileSafely.d.ts +10 -0
- package/dist/utils/writeFileSafely.js +86 -14
- package/dist/utils/writeFileSafely.js.map +1 -1
- package/package.json +64 -31
- package/src/client/encodeQueryParams.ts +56 -0
- package/src/copy/buildModelOpenApi.ts +1569 -0
- package/src/copy/misc.ts +21 -0
- package/src/copy/operationDefinitions.ts +96 -0
- package/src/copy/parseQueryParams.ts +36 -21
- package/src/copy/routeConfig.ts +68 -28
- package/dist/generator.js +0 -47
- package/dist/generator.js.map +0 -1
- package/dist/helpers/generateImportPrismaStatement.js +0 -25
- package/dist/helpers/generateImportPrismaStatement.js.map +0 -1
- package/dist/helpers/generateOperation.js +0 -471
- package/dist/helpers/generateOperation.js.map +0 -1
- package/dist/helpers/generateRouteFile.js +0 -210
- package/dist/helpers/generateRouteFile.js.map +0 -1
- package/dist/utils/formatFile.js +0 -26
- package/dist/utils/formatFile.js.map +0 -1
- package/src/bin.ts +0 -2
- package/src/constants.ts +0 -1
- package/src/copy/encodeQueryParams.spec.ts +0 -303
- package/src/copy/encodeQueryParams.ts +0 -44
- package/src/copy/misc.spec.ts +0 -62
- package/src/copy/parseQueryParams.spec.ts +0 -187
- package/src/copy/transformZod.spec.ts +0 -763
- package/src/generator.ts +0 -54
- package/src/helpers/generateImportPrismaStatement.ts +0 -38
- package/src/helpers/generateOperation.ts +0 -515
- package/src/helpers/generateRouteFile.ts +0 -213
- package/src/utils/copyFiles.ts +0 -27
- package/src/utils/formatFile.ts +0 -22
- package/src/utils/strings.ts +0 -7
- package/src/utils/writeFileSafely.ts +0 -29
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
|
|
3
|
-
export function generateRouterFunction({
|
|
4
|
-
model,
|
|
5
|
-
}: {
|
|
6
|
-
model: DMMF.Model
|
|
7
|
-
}): string {
|
|
8
|
-
const modelName = model.name
|
|
9
|
-
const routerFunctionName = `${modelName}Router`
|
|
10
|
-
|
|
11
|
-
return `import express, {
|
|
12
|
-
RequestHandler
|
|
13
|
-
} from 'express'
|
|
14
|
-
import { ParsedQs } from 'qs'
|
|
15
|
-
import { ${modelName}FindFirst } from './${modelName}FindFirst';
|
|
16
|
-
import { ${modelName}FindMany } from './${modelName}FindMany';
|
|
17
|
-
import { ${modelName}FindUnique } from './${modelName}FindUnique';
|
|
18
|
-
import { ${modelName}Create } from './${modelName}Create';
|
|
19
|
-
import { ${modelName}CreateMany } from './${modelName}CreateMany';
|
|
20
|
-
import { ${modelName}CreateManyAndReturn } from './${modelName}CreateManyAndReturn';
|
|
21
|
-
import { ${modelName}Update } from './${modelName}Update';
|
|
22
|
-
import { ${modelName}UpdateMany } from './${modelName}UpdateMany';
|
|
23
|
-
import { ${modelName}UpdateManyAndReturn } from './${modelName}UpdateManyAndReturn';
|
|
24
|
-
import { ${modelName}Upsert } from './${modelName}Upsert';
|
|
25
|
-
import { ${modelName}Delete } from './${modelName}Delete';
|
|
26
|
-
import { ${modelName}DeleteMany } from './${modelName}DeleteMany';
|
|
27
|
-
import { ${modelName}Aggregate } from './${modelName}Aggregate';
|
|
28
|
-
import { ${modelName}Count } from './${modelName}Count';
|
|
29
|
-
import { ${modelName}GroupBy } from './${modelName}GroupBy';
|
|
30
|
-
import { createValidatorMiddleware, removeTrailingSlash } from '../createValidatorMiddleware'
|
|
31
|
-
import { RouteConfig, ValidatorConfig } from '../routeConfig'
|
|
32
|
-
import { parseQueryParams } from "../parseQueryParams";
|
|
33
|
-
|
|
34
|
-
const defaultBeforeAfter = {
|
|
35
|
-
before: [] as RequestHandler[],
|
|
36
|
-
after: [] as RequestHandler[],
|
|
37
|
-
inputValidator: undefined,
|
|
38
|
-
outputValidator: undefined,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Generates an Express router for ${modelName} model.
|
|
43
|
-
* @param config Contains optional middleware to enable routes.
|
|
44
|
-
* @param customUrlPrefix Optional custom URL prefix for the routes.
|
|
45
|
-
* @returns {express.Router}
|
|
46
|
-
*/
|
|
47
|
-
export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
|
|
48
|
-
const router = express.Router();
|
|
49
|
-
const basePath = removeTrailingSlash(config.customUrlPrefix || '') +
|
|
50
|
-
removeTrailingSlash(config.addModelPrefix !== false ? '/${modelName.toLowerCase()}' : '');
|
|
51
|
-
|
|
52
|
-
const setupRoute = (
|
|
53
|
-
path: string,
|
|
54
|
-
method: "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head",
|
|
55
|
-
middlewares: RequestHandler[],
|
|
56
|
-
handler: RequestHandler,
|
|
57
|
-
inputValidator?: ValidatorConfig,
|
|
58
|
-
outputValidator?: ValidatorConfig
|
|
59
|
-
) => {
|
|
60
|
-
const middlewaresWithValidators: RequestHandler[] = [
|
|
61
|
-
(req, res, next) => {
|
|
62
|
-
if (req.query) {
|
|
63
|
-
req.query = parseQueryParams(req.query as Record<string, string>) as ParsedQs;
|
|
64
|
-
}
|
|
65
|
-
next();
|
|
66
|
-
},
|
|
67
|
-
...middlewares
|
|
68
|
-
];
|
|
69
|
-
|
|
70
|
-
if (inputValidator) {
|
|
71
|
-
middlewaresWithValidators.push(createValidatorMiddleware({
|
|
72
|
-
schema: inputValidator.schema,
|
|
73
|
-
allowedPaths: inputValidator.allow,
|
|
74
|
-
forbiddenPaths: inputValidator.forbid,
|
|
75
|
-
target: method === 'get' ? 'query' : 'body',
|
|
76
|
-
}));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
middlewaresWithValidators.push((req, res, next) => {
|
|
80
|
-
res.locals.outputValidator = outputValidator;
|
|
81
|
-
next();
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
middlewaresWithValidators.push(handler);
|
|
85
|
-
|
|
86
|
-
router[method](removeTrailingSlash(basePath + path), ...middlewaresWithValidators);
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (config.enableAll || config?.findFirst) {
|
|
91
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.findFirst || defaultBeforeAfter;
|
|
92
|
-
setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler, inputValidator, outputValidator);
|
|
93
|
-
if (after.length) {
|
|
94
|
-
router.use(removeTrailingSlash(basePath) + '/first', ...after);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (config.enableAll || config?.findMany) {
|
|
99
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.findMany || defaultBeforeAfter;
|
|
100
|
-
setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler, inputValidator, outputValidator);
|
|
101
|
-
if (after.length) {
|
|
102
|
-
router.use(removeTrailingSlash(basePath + '/'), ...after);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (config.enableAll || config?.findUnique) {
|
|
107
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.findUnique || defaultBeforeAfter;
|
|
108
|
-
setupRoute('/:id', 'get', before, ${modelName}FindUnique as any, inputValidator, outputValidator);
|
|
109
|
-
if (after.length) {
|
|
110
|
-
router.use(removeTrailingSlash(basePath) + '/:id', ...after);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (config.enableAll || config?.create) {
|
|
115
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.create || defaultBeforeAfter;
|
|
116
|
-
setupRoute('/', 'post', before, ${modelName}Create as RequestHandler, inputValidator, outputValidator);
|
|
117
|
-
if (after.length) {
|
|
118
|
-
router.use(removeTrailingSlash(basePath + '/'), ...after);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (config.enableAll || config?.createMany) {
|
|
123
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.createMany || defaultBeforeAfter;
|
|
124
|
-
setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler, inputValidator, outputValidator);
|
|
125
|
-
if (after.length) {
|
|
126
|
-
router.use(removeTrailingSlash(basePath) + '/many', ...after);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (config.enableAll || config?.createManyAndReturn) {
|
|
131
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.createManyAndReturn || defaultBeforeAfter;
|
|
132
|
-
setupRoute('/many/return', 'post', before, ${modelName}CreateManyAndReturn as RequestHandler, inputValidator, outputValidator);
|
|
133
|
-
if (after.length) {
|
|
134
|
-
router.use(removeTrailingSlash(basePath) + '/many/return', ...after);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (config.enableAll || config?.update) {
|
|
139
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.update || defaultBeforeAfter;
|
|
140
|
-
setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, inputValidator, outputValidator);
|
|
141
|
-
if (after.length) {
|
|
142
|
-
router.use(removeTrailingSlash(basePath + '/'), ...after);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (config.enableAll || config?.updateMany) {
|
|
147
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.updateMany || defaultBeforeAfter;
|
|
148
|
-
setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler, inputValidator, outputValidator);
|
|
149
|
-
if (after.length) {
|
|
150
|
-
router.use(removeTrailingSlash(basePath) + '/many', ...after);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (config.enableAll || config?.updateManyAndReturn) {
|
|
155
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.updateManyAndReturn || defaultBeforeAfter;
|
|
156
|
-
setupRoute('/many/return', 'put', before, ${modelName}UpdateManyAndReturn as RequestHandler, inputValidator, outputValidator);
|
|
157
|
-
if (after.length) {
|
|
158
|
-
router.use(removeTrailingSlash(basePath) + '/many/return', ...after);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (config.enableAll || config?.upsert) {
|
|
163
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.upsert || defaultBeforeAfter;
|
|
164
|
-
setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, inputValidator, outputValidator);
|
|
165
|
-
if (after.length) {
|
|
166
|
-
router.use(removeTrailingSlash(basePath + '/'), ...after);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (config.enableAll || config?.delete) {
|
|
171
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.delete || defaultBeforeAfter;
|
|
172
|
-
setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler, inputValidator, outputValidator);
|
|
173
|
-
if (after.length) {
|
|
174
|
-
router.use(removeTrailingSlash(basePath + '/'), ...after);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (config.enableAll || config?.deleteMany) {
|
|
179
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.deleteMany || defaultBeforeAfter;
|
|
180
|
-
setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler, inputValidator, outputValidator);
|
|
181
|
-
if (after.length) {
|
|
182
|
-
router.use(removeTrailingSlash(basePath) + '/many', ...after);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (config.enableAll || config?.aggregate) {
|
|
187
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.aggregate || defaultBeforeAfter;
|
|
188
|
-
setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler, inputValidator, outputValidator);
|
|
189
|
-
if (after.length) {
|
|
190
|
-
router.use(removeTrailingSlash(basePath) + '/aggregate', ...after);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if (config.enableAll || config?.count) {
|
|
195
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.count || defaultBeforeAfter;
|
|
196
|
-
setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler, inputValidator, outputValidator);
|
|
197
|
-
if (after.length) {
|
|
198
|
-
router.use(removeTrailingSlash(basePath) + '/count', ...after);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (config.enableAll || config?.groupBy) {
|
|
203
|
-
const { before = [], after = [], inputValidator, outputValidator } = config.groupBy || defaultBeforeAfter;
|
|
204
|
-
setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler, inputValidator, outputValidator);
|
|
205
|
-
if (after.length) {
|
|
206
|
-
router.use(removeTrailingSlash(basePath) + '/groupby', ...after);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return router;
|
|
211
|
-
}
|
|
212
|
-
`
|
|
213
|
-
}
|
package/src/utils/copyFiles.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { GeneratorOptions } from '@prisma/generator-helper'
|
|
2
|
-
import fs from 'fs/promises'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
|
|
5
|
-
export const copyFiles = async (
|
|
6
|
-
options: GeneratorOptions,
|
|
7
|
-
destPath?: string,
|
|
8
|
-
) => {
|
|
9
|
-
const sourceDir = path.join(__dirname.replace('dist', 'src'), '..', 'copy')
|
|
10
|
-
const destinationDir = destPath || options.generator.output?.value!
|
|
11
|
-
|
|
12
|
-
const files = await fs.readdir(sourceDir)
|
|
13
|
-
|
|
14
|
-
for (const file of files) {
|
|
15
|
-
if (file.endsWith('.spec.ts')) {
|
|
16
|
-
continue
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const sourceFile = path.join(sourceDir, file)
|
|
20
|
-
const destinationFile = path.join(destinationDir, file)
|
|
21
|
-
|
|
22
|
-
await fs.mkdir(path.dirname(destinationFile), { recursive: true })
|
|
23
|
-
await fs.copyFile(sourceFile, destinationFile)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
console.log(`Files copied from ${sourceDir} to ${destinationDir}`)
|
|
27
|
-
}
|
package/src/utils/formatFile.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import prettier from 'prettier'
|
|
2
|
-
|
|
3
|
-
export const formatFile = (content: string): Promise<string> => {
|
|
4
|
-
return new Promise((res, rej) =>
|
|
5
|
-
prettier.resolveConfig(process.cwd()).then((options) => {
|
|
6
|
-
if (!options) {
|
|
7
|
-
res(content) // no prettier config was found, no need to format
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
const formatted = prettier.format(content, {
|
|
12
|
-
...options,
|
|
13
|
-
parser: 'typescript',
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
res(formatted)
|
|
17
|
-
} catch (error) {
|
|
18
|
-
rej(error)
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
)
|
|
22
|
-
}
|
package/src/utils/strings.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import fs from 'fs/promises'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { formatFile } from './formatFile'
|
|
4
|
-
import { DMMF, GeneratorOptions } from '@prisma/generator-helper'
|
|
5
|
-
|
|
6
|
-
export const writeFileSafely = async ({
|
|
7
|
-
model,
|
|
8
|
-
operation,
|
|
9
|
-
options,
|
|
10
|
-
content,
|
|
11
|
-
}: {
|
|
12
|
-
model?: DMMF.Model
|
|
13
|
-
operation: string
|
|
14
|
-
options: GeneratorOptions
|
|
15
|
-
content: string
|
|
16
|
-
}) => {
|
|
17
|
-
const fileName =
|
|
18
|
-
operation === 'index' ? 'index' : `${model?.name || ''}${operation}`
|
|
19
|
-
const filePath = path.join(
|
|
20
|
-
options.generator.output?.value!,
|
|
21
|
-
model ? `${model?.name}/${fileName}.ts` : `/${fileName}.ts`,
|
|
22
|
-
)
|
|
23
|
-
console.log('filePath :>> ', filePath)
|
|
24
|
-
await fs.mkdir(path.dirname(filePath), {
|
|
25
|
-
recursive: true,
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
await fs.writeFile(filePath, await formatFile(content))
|
|
29
|
-
}
|