prisma-generator-express 1.16.7 → 1.18.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 +4 -4
- package/dist/generator.js +8 -129
- package/dist/generator.js.map +1 -1
- package/dist/helpers/generateOperation.js +471 -0
- package/dist/helpers/generateOperation.js.map +1 -0
- package/dist/helpers/generateRouteFile.js +18 -0
- package/dist/helpers/generateRouteFile.js.map +1 -1
- package/package.json +16 -16
- package/src/copy/createOutputValidatorMiddleware.ts +1 -1
- package/src/copy/createValidatorMiddleware.ts +1 -1
- package/src/copy/misc.ts +1 -1
- package/src/copy/parseQueryParams.ts +1 -1
- package/src/copy/routeConfig.ts +5 -3
- package/src/copy/transformZod.ts +15 -16
- package/src/generator.ts +9 -143
- package/src/helpers/generateOperation.ts +515 -0
- package/src/helpers/generateRouteFile.ts +19 -1
- package/dist/helpers/generateAggregate.js +0 -51
- package/dist/helpers/generateAggregate.js.map +0 -1
- package/dist/helpers/generateCount.js +0 -50
- package/dist/helpers/generateCount.js.map +0 -1
- package/dist/helpers/generateCreate.js +0 -49
- package/dist/helpers/generateCreate.js.map +0 -1
- package/dist/helpers/generateCreateMany.js +0 -49
- package/dist/helpers/generateCreateMany.js.map +0 -1
- package/dist/helpers/generateDelete.js +0 -49
- package/dist/helpers/generateDelete.js.map +0 -1
- package/dist/helpers/generateDeleteMany.js +0 -49
- package/dist/helpers/generateDeleteMany.js.map +0 -1
- package/dist/helpers/generateFindFirst.js +0 -56
- package/dist/helpers/generateFindFirst.js.map +0 -1
- package/dist/helpers/generateFindMany.js +0 -56
- package/dist/helpers/generateFindMany.js.map +0 -1
- package/dist/helpers/generateFindUnique.js +0 -56
- package/dist/helpers/generateFindUnique.js.map +0 -1
- package/dist/helpers/generateGroupBy.js +0 -51
- package/dist/helpers/generateGroupBy.js.map +0 -1
- package/dist/helpers/generateUpdate.js +0 -49
- package/dist/helpers/generateUpdate.js.map +0 -1
- package/dist/helpers/generateUpdateMany.js +0 -49
- package/dist/helpers/generateUpdateMany.js.map +0 -1
- package/dist/helpers/generateUpsert.js +0 -49
- package/dist/helpers/generateUpsert.js.map +0 -1
- package/src/helpers/generateAggregate.ts +0 -59
- package/src/helpers/generateCount.ts +0 -58
- package/src/helpers/generateCreate.ts +0 -56
- package/src/helpers/generateCreateMany.ts +0 -55
- package/src/helpers/generateDelete.ts +0 -57
- package/src/helpers/generateDeleteMany.ts +0 -57
- package/src/helpers/generateFindFirst.ts +0 -62
- package/src/helpers/generateFindMany.ts +0 -62
- package/src/helpers/generateFindUnique.ts +0 -62
- package/src/helpers/generateGroupBy.ts +0 -60
- package/src/helpers/generateUpdate.ts +0 -56
- package/src/helpers/generateUpdateMany.ts +0 -56
- package/src/helpers/generateUpsert.ts +0 -57
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { toPascalCase } from '../utils/strings'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Generates an Express middleware function that handles updating multiple records 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 generateUpdateManyFunction = (options: {
|
|
11
|
-
model: DMMF.Model
|
|
12
|
-
prismaImportStatement: string
|
|
13
|
-
}): string => {
|
|
14
|
-
const { model, prismaImportStatement } = options
|
|
15
|
-
const modelName = model.name
|
|
16
|
-
const functionName = `${modelName}UpdateMany`
|
|
17
|
-
const argsTypeName = `Prisma.${modelName}UpdateManyArgs`
|
|
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 UpdateManyRequest extends Request {
|
|
26
|
-
prisma: PrismaClient;
|
|
27
|
-
body: ${argsTypeName};
|
|
28
|
-
outputValidation?: ZodTypeAny;
|
|
29
|
-
locals?: {
|
|
30
|
-
outputValidator?: ZodTypeAny;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type UpdateManyMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
35
|
-
|
|
36
|
-
export async function ${functionName}(req: UpdateManyRequest, res: Response, next: NextFunction) {
|
|
37
|
-
try {
|
|
38
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
39
|
-
|
|
40
|
-
const data = await req.prisma.${toPascalCase(modelName)}.updateMany(req.body);
|
|
41
|
-
|
|
42
|
-
if (outputValidator) {
|
|
43
|
-
const validationResult = outputValidator.safeParse(data);
|
|
44
|
-
if (validationResult.success) {
|
|
45
|
-
return res.status(200).json({ count: validationResult.data.count });
|
|
46
|
-
} else {
|
|
47
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
return res.status(200).json({ count: data.count });
|
|
51
|
-
}
|
|
52
|
-
} catch(error: unknown) {
|
|
53
|
-
next(error)
|
|
54
|
-
}
|
|
55
|
-
}`
|
|
56
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { toPascalCase } from '../utils/strings'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Generates an Express middleware function that handles the upsert operation (create or update)
|
|
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 generateUpsertFunction = (options: {
|
|
12
|
-
model: DMMF.Model
|
|
13
|
-
prismaImportStatement: string
|
|
14
|
-
}): string => {
|
|
15
|
-
const { model, prismaImportStatement } = options
|
|
16
|
-
const modelName = model.name
|
|
17
|
-
const functionName = `${modelName}Upsert`
|
|
18
|
-
const argsTypeName = `Prisma.${modelName}UpsertArgs`
|
|
19
|
-
|
|
20
|
-
return `
|
|
21
|
-
${prismaImportStatement}
|
|
22
|
-
import { Request, Response, NextFunction } from 'express';
|
|
23
|
-
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
24
|
-
import { ZodTypeAny } from 'zod';
|
|
25
|
-
|
|
26
|
-
interface UpsertRequest extends Request {
|
|
27
|
-
prisma: PrismaClient;
|
|
28
|
-
body: ${argsTypeName};
|
|
29
|
-
outputValidation?: ZodTypeAny;
|
|
30
|
-
locals?: {
|
|
31
|
-
outputValidator?: ZodTypeAny;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type UpsertMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
36
|
-
|
|
37
|
-
export async function ${functionName}(req: UpsertRequest, res: Response, next: NextFunction) {
|
|
38
|
-
try {
|
|
39
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
40
|
-
|
|
41
|
-
const data = await req.prisma.${toPascalCase(modelName)}.upsert(req.body);
|
|
42
|
-
|
|
43
|
-
if (outputValidator) {
|
|
44
|
-
const validationResult = outputValidator.safeParse(data);
|
|
45
|
-
if (validationResult.success) {
|
|
46
|
-
return res.status(200).json(validationResult.data);
|
|
47
|
-
} else {
|
|
48
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
51
|
-
return res.status(200).json(data);
|
|
52
|
-
}
|
|
53
|
-
} catch(error: unknown) {
|
|
54
|
-
next(error)
|
|
55
|
-
}
|
|
56
|
-
}`
|
|
57
|
-
}
|