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,59 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { capitalize, toPascalCase } 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
|
-
import { ValidatorConfig } from '../routeConfig'
|
|
27
|
-
|
|
28
|
-
interface AggregateRequest extends Request {
|
|
29
|
-
prisma: PrismaClient;
|
|
30
|
-
query: Partial<${argsTypeName}> & ParsedQs;
|
|
31
|
-
outputValidation?: ZodTypeAny;
|
|
32
|
-
locals?: {
|
|
33
|
-
outputValidator?: ValidatorConfig;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export type AggregateMiddleware = RequestHandler<ParamsDictionary, any, Partial<${argsTypeName}>, Record<string, any>>;
|
|
38
|
-
|
|
39
|
-
export async function ${functionName}(req: AggregateRequest, res: Response, next: NextFunction) {
|
|
40
|
-
try {
|
|
41
|
-
const outputValidator = res.locals.outputValidator?.schema || req.outputValidation;
|
|
42
|
-
|
|
43
|
-
const result = await req.prisma.${toPascalCase(modelName)}.aggregate(req.query as ${argsTypeName});
|
|
44
|
-
|
|
45
|
-
if (outputValidator) {
|
|
46
|
-
const validationResult = outputValidator.safeParse(result);
|
|
47
|
-
if (validationResult.success) {
|
|
48
|
-
return res.status(200).json(validationResult.data);
|
|
49
|
-
} else {
|
|
50
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
51
|
-
}
|
|
52
|
-
} else {
|
|
53
|
-
return res.status(200).json(result);
|
|
54
|
-
}
|
|
55
|
-
} catch(error: unknown) {
|
|
56
|
-
next(error)
|
|
57
|
-
}
|
|
58
|
-
}`
|
|
59
|
-
}
|
|
@@ -1,58 +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 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
|
-
locals?: {
|
|
32
|
-
outputValidator?: ZodTypeAny;
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type CountMiddleware = RequestHandler<ParamsDictionary, any, {}, ParsedQs>;
|
|
37
|
-
|
|
38
|
-
export async function ${functionName}(req: CountRequest, res: Response, next: NextFunction) {
|
|
39
|
-
try {
|
|
40
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
41
|
-
|
|
42
|
-
const result = await req.prisma.${toPascalCase(modelName)}.count(req.query as ${argsTypeName});
|
|
43
|
-
|
|
44
|
-
if (outputValidator) {
|
|
45
|
-
const validationResult = outputValidator.safeParse(result);
|
|
46
|
-
if (validationResult.success) {
|
|
47
|
-
return res.status(200).json(validationResult.data);
|
|
48
|
-
} else {
|
|
49
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
return res.status(200).json(result);
|
|
53
|
-
}
|
|
54
|
-
} catch(error: unknown) {
|
|
55
|
-
next(error)
|
|
56
|
-
}
|
|
57
|
-
}`
|
|
58
|
-
}
|
|
@@ -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 creation of 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 generateCreateFunction = (options: {
|
|
11
|
-
model: DMMF.Model
|
|
12
|
-
prismaImportStatement: string
|
|
13
|
-
}): string => {
|
|
14
|
-
const { model, prismaImportStatement } = options
|
|
15
|
-
const modelName = model.name
|
|
16
|
-
const functionName = `${modelName}Create`
|
|
17
|
-
const argsTypeName = `Prisma.${modelName}CreateArgs`
|
|
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 CreateRequest extends Request {
|
|
26
|
-
prisma: PrismaClient;
|
|
27
|
-
body: ${argsTypeName};
|
|
28
|
-
outputValidation?: ZodTypeAny;
|
|
29
|
-
locals?: {
|
|
30
|
-
outputValidator?: ZodTypeAny;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type CreateMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
35
|
-
|
|
36
|
-
export async function ${functionName}(req: CreateRequest, res: Response, next: NextFunction) {
|
|
37
|
-
try {
|
|
38
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
39
|
-
|
|
40
|
-
const data = await req.prisma.${toPascalCase(modelName)}.create(req.body);
|
|
41
|
-
|
|
42
|
-
if (outputValidator) {
|
|
43
|
-
const validationResult = outputValidator.safeParse(data);
|
|
44
|
-
if (validationResult.success) {
|
|
45
|
-
return res.status(201).json(validationResult.data);
|
|
46
|
-
} else {
|
|
47
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
return res.status(201).json(data);
|
|
51
|
-
}
|
|
52
|
-
} catch(error: unknown) {
|
|
53
|
-
next(error)
|
|
54
|
-
}
|
|
55
|
-
}`
|
|
56
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { toPascalCase } 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
|
-
locals?: {
|
|
29
|
-
outputValidator?: ZodTypeAny;
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type CreateManyMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
34
|
-
|
|
35
|
-
export async function ${functionName}(req: CreateManyRequest, res: Response, next: NextFunction) {
|
|
36
|
-
try {
|
|
37
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
38
|
-
|
|
39
|
-
const data = await req.prisma.${toPascalCase(modelName)}.createMany(req.body);
|
|
40
|
-
|
|
41
|
-
if (outputValidator) {
|
|
42
|
-
const validationResult = outputValidator.safeParse(data);
|
|
43
|
-
if (validationResult.success) {
|
|
44
|
-
return res.status(201).json(validationResult.data);
|
|
45
|
-
} else {
|
|
46
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
47
|
-
}
|
|
48
|
-
} else {
|
|
49
|
-
return res.status(201).json(data);
|
|
50
|
-
}
|
|
51
|
-
} catch(error: unknown) {
|
|
52
|
-
next(error)
|
|
53
|
-
}
|
|
54
|
-
}`
|
|
55
|
-
}
|
|
@@ -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 deleting records
|
|
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 generateDeleteFunction = (options: {
|
|
12
|
-
model: DMMF.Model
|
|
13
|
-
prismaImportStatement: string
|
|
14
|
-
}): string => {
|
|
15
|
-
const { model, prismaImportStatement } = options
|
|
16
|
-
const modelName = model.name
|
|
17
|
-
const functionName = `${modelName}Delete`
|
|
18
|
-
const argsTypeName = `Prisma.${modelName}DeleteArgs`
|
|
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 DeleteRequest extends Request {
|
|
27
|
-
prisma: PrismaClient;
|
|
28
|
-
body: ${argsTypeName};
|
|
29
|
-
outputValidation?: ZodTypeAny;
|
|
30
|
-
locals?: {
|
|
31
|
-
outputValidator?: ZodTypeAny;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type DeleteMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
36
|
-
|
|
37
|
-
export async function ${functionName}(req: DeleteRequest, res: Response, next: NextFunction) {
|
|
38
|
-
try {
|
|
39
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
40
|
-
|
|
41
|
-
const data = await req.prisma.${toPascalCase(modelName)}.delete(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
|
-
}
|
|
@@ -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 batch deleting records
|
|
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 generateDeleteManyFunction = (options: {
|
|
12
|
-
model: DMMF.Model
|
|
13
|
-
prismaImportStatement: string
|
|
14
|
-
}): string => {
|
|
15
|
-
const { model, prismaImportStatement } = options
|
|
16
|
-
const modelName = model.name
|
|
17
|
-
const functionName = `${modelName}DeleteMany`
|
|
18
|
-
const argsTypeName = `Prisma.${modelName}DeleteManyArgs`
|
|
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 DeleteManyRequest extends Request {
|
|
27
|
-
prisma: PrismaClient;
|
|
28
|
-
body: ${argsTypeName};
|
|
29
|
-
outputValidation?: ZodTypeAny;
|
|
30
|
-
locals?: {
|
|
31
|
-
outputValidator?: ZodTypeAny;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type DeleteManyMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>;
|
|
36
|
-
|
|
37
|
-
export async function ${functionName}(req: DeleteManyRequest, res: Response, next: NextFunction) {
|
|
38
|
-
try {
|
|
39
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
40
|
-
|
|
41
|
-
const result = await req.prisma.${toPascalCase(modelName)}.deleteMany(req.body);
|
|
42
|
-
|
|
43
|
-
if (outputValidator) {
|
|
44
|
-
const validationResult = outputValidator.safeParse(result);
|
|
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(result);
|
|
52
|
-
}
|
|
53
|
-
} catch(error: unknown) {
|
|
54
|
-
next(error)
|
|
55
|
-
}
|
|
56
|
-
}`
|
|
57
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { toPascalCase } 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
|
-
passToNext?: boolean;
|
|
33
|
-
locals?: {
|
|
34
|
-
data?: ${modelName} | null
|
|
35
|
-
outputValidator?: ZodTypeAny;
|
|
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
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
43
|
-
|
|
44
|
-
const data = await req.prisma.${toPascalCase(modelName)}.findFirst(req.query as ${queryTypeName});
|
|
45
|
-
if (req.passToNext) {
|
|
46
|
-
if (req.locals) req.locals.data = data;
|
|
47
|
-
next();
|
|
48
|
-
} else if (outputValidator) {
|
|
49
|
-
const validationResult = outputValidator.safeParse(data);
|
|
50
|
-
if (validationResult.success) {
|
|
51
|
-
return res.status(200).json(validationResult.data);
|
|
52
|
-
} else {
|
|
53
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
return res.status(200).json(data);
|
|
57
|
-
}
|
|
58
|
-
} catch(error: unknown) {
|
|
59
|
-
next(error)
|
|
60
|
-
}
|
|
61
|
-
}`
|
|
62
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { toPascalCase } 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
|
-
passToNext?: boolean;
|
|
33
|
-
locals?: {
|
|
34
|
-
data?: ${modelName}[]
|
|
35
|
-
outputValidator?: ZodTypeAny;
|
|
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
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
43
|
-
|
|
44
|
-
const data = await req.prisma.${toPascalCase(modelName)}.findMany(req.query as ${queryTypeName});
|
|
45
|
-
if (req.passToNext) {
|
|
46
|
-
if (req.locals) req.locals.data = data;
|
|
47
|
-
next();
|
|
48
|
-
} else if (outputValidator) {
|
|
49
|
-
const validationResult = outputValidator.safeParse(data);
|
|
50
|
-
if (validationResult.success) {
|
|
51
|
-
return res.status(200).json(validationResult.data);
|
|
52
|
-
} else {
|
|
53
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
return res.status(200).json(data);
|
|
57
|
-
}
|
|
58
|
-
} catch(error: unknown) {
|
|
59
|
-
next(error)
|
|
60
|
-
}
|
|
61
|
-
}`
|
|
62
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { toPascalCase } 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 generateFindUniqueFunction = (options: {
|
|
10
|
-
model: DMMF.Model
|
|
11
|
-
prismaImportStatement: string
|
|
12
|
-
}): string => {
|
|
13
|
-
const { model, prismaImportStatement } = options
|
|
14
|
-
const modelName = model.name
|
|
15
|
-
const functionName = `${modelName}FindUnique`
|
|
16
|
-
const queryTypeName = `Prisma.${modelName}FindUniqueArgs`
|
|
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 FindUniqueRequest extends Request {
|
|
29
|
-
prisma: PrismaClient;
|
|
30
|
-
query: ${queryTypeName} & ParsedQs;
|
|
31
|
-
outputValidation?: ZodTypeAny;
|
|
32
|
-
passToNext?: boolean;
|
|
33
|
-
locals?: {
|
|
34
|
-
data?: ${modelName} | null
|
|
35
|
-
outputValidator?: ZodTypeAny;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export type FindUniqueMiddleware = RequestHandler<ParamsDictionary, any, any, ${queryTypeName} & ParsedQs, Record<string, any>>
|
|
39
|
-
|
|
40
|
-
export async function ${functionName}(req: FindUniqueRequest, res: Response, next: NextFunction) {
|
|
41
|
-
try {
|
|
42
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
43
|
-
|
|
44
|
-
const data = await req.prisma.${toPascalCase(modelName)}.findUnique(req.query as ${queryTypeName});
|
|
45
|
-
if (req.passToNext) {
|
|
46
|
-
if (req.locals) req.locals.data = data;
|
|
47
|
-
next();
|
|
48
|
-
} else if (outputValidator) {
|
|
49
|
-
const validationResult = outputValidator.safeParse(data);
|
|
50
|
-
if (validationResult.success) {
|
|
51
|
-
return res.status(200).json(validationResult.data);
|
|
52
|
-
} else {
|
|
53
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
return res.status(200).json(data);
|
|
57
|
-
}
|
|
58
|
-
} catch(error: unknown) {
|
|
59
|
-
next(error)
|
|
60
|
-
}
|
|
61
|
-
}`
|
|
62
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
import { toPascalCase } from '../utils/strings'
|
|
3
|
-
import { PrismaClient, Prisma } from '@prisma/client'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generates an Express middleware function that handles groupBy queries
|
|
7
|
-
* and includes conditional output validation with Zod.
|
|
8
|
-
* This version dynamically includes the correct type for the arguments based on the Prisma model.
|
|
9
|
-
* @param options - The options containing the model name and the import statement for Prisma types.
|
|
10
|
-
* @returns {string} - The generated middleware function as a string.
|
|
11
|
-
*/
|
|
12
|
-
export const generateGroupByFunction = (options: {
|
|
13
|
-
model: DMMF.Model
|
|
14
|
-
prismaImportStatement: string
|
|
15
|
-
}): string => {
|
|
16
|
-
const { model, prismaImportStatement } = options
|
|
17
|
-
const modelName = model.name
|
|
18
|
-
const functionName = `${modelName}GroupBy`
|
|
19
|
-
const argsTypeName = `Prisma.${modelName}GroupByArgs`
|
|
20
|
-
|
|
21
|
-
return `
|
|
22
|
-
${prismaImportStatement}
|
|
23
|
-
import { Request, Response, NextFunction } from 'express';
|
|
24
|
-
import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';
|
|
25
|
-
import { ParsedQs } from 'qs';
|
|
26
|
-
import { ZodTypeAny } from 'zod';
|
|
27
|
-
|
|
28
|
-
interface GroupByRequest extends Request {
|
|
29
|
-
prisma: PrismaClient;
|
|
30
|
-
query: Partial<${argsTypeName}> & ParsedQs;
|
|
31
|
-
outputValidation?: ZodTypeAny;
|
|
32
|
-
locals?: {
|
|
33
|
-
outputValidator?: ZodTypeAny;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export type GroupByMiddleware = RequestHandler<ParamsDictionary, any, {}, ParsedQs>;
|
|
38
|
-
|
|
39
|
-
export async function ${functionName}(req: GroupByRequest, res: Response, next: NextFunction) {
|
|
40
|
-
try {
|
|
41
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
42
|
-
|
|
43
|
-
// @ts-ignore
|
|
44
|
-
const result = await req.prisma.${toPascalCase(modelName)}.groupBy(req.query);
|
|
45
|
-
|
|
46
|
-
if (outputValidator) {
|
|
47
|
-
const validationResult = outputValidator.safeParse(result);
|
|
48
|
-
if (validationResult.success) {
|
|
49
|
-
return res.status(200).json(validationResult.data);
|
|
50
|
-
} else {
|
|
51
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
return res.status(200).json(result);
|
|
55
|
-
}
|
|
56
|
-
} catch(error: unknown) {
|
|
57
|
-
next(error)
|
|
58
|
-
}
|
|
59
|
-
}`
|
|
60
|
-
}
|
|
@@ -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 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 generateUpdateFunction = (options: {
|
|
11
|
-
model: DMMF.Model
|
|
12
|
-
prismaImportStatement: string
|
|
13
|
-
}): string => {
|
|
14
|
-
const { model, prismaImportStatement } = options
|
|
15
|
-
const modelName = model.name
|
|
16
|
-
const functionName = `${modelName}Update`
|
|
17
|
-
const argsTypeName = `Prisma.${modelName}UpdateArgs`
|
|
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 UpdateRequest extends Request {
|
|
26
|
-
prisma: PrismaClient;
|
|
27
|
-
body: ${argsTypeName};
|
|
28
|
-
outputValidation?: ZodTypeAny;
|
|
29
|
-
locals?: {
|
|
30
|
-
outputValidator?: ZodTypeAny;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type UpdateMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
35
|
-
|
|
36
|
-
export async function ${functionName}(req: UpdateRequest, res: Response, next: NextFunction) {
|
|
37
|
-
try {
|
|
38
|
-
const outputValidator = req.locals?.outputValidator || req.outputValidation;
|
|
39
|
-
|
|
40
|
-
const data = await req.prisma.${toPascalCase(modelName)}.update(req.body);
|
|
41
|
-
|
|
42
|
-
if (outputValidator) {
|
|
43
|
-
const validationResult = outputValidator.safeParse(data);
|
|
44
|
-
if (validationResult.success) {
|
|
45
|
-
return res.status(200).json(validationResult.data);
|
|
46
|
-
} else {
|
|
47
|
-
return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
return res.status(200).json(data);
|
|
51
|
-
}
|
|
52
|
-
} catch(error: unknown) {
|
|
53
|
-
next(error)
|
|
54
|
-
}
|
|
55
|
-
}`
|
|
56
|
-
}
|