prisma-generator-express 1.0.5 → 1.1.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/CHANGELOG.md +14 -0
- package/package.json +4 -12
- package/dist/helpers/genEnum.js +0 -9
- package/dist/helpers/genEnum.js.map +0 -1
- package/dist/helpers/generateFindUnique copy.js +0 -49
- package/dist/helpers/generateFindUnique copy.js.map +0 -1
- package/dist/helpers/generatePrismaImport.js +0 -2
- package/dist/helpers/generatePrismaImport.js.map +0 -1
- package/dist/utils/getGeneratorOptions.js +0 -14
- package/dist/utils/getGeneratorOptions.js.map +0 -1
- package/src/bin.ts +0 -2
- package/src/constants.ts +0 -1
- package/src/generator.ts +0 -174
- package/src/helpers/generateAggregate.ts +0 -58
- package/src/helpers/generateCount.ts +0 -58
- package/src/helpers/generateCreate.ts +0 -58
- package/src/helpers/generateCreateMany.ts +0 -58
- package/src/helpers/generateDelete.ts +0 -60
- package/src/helpers/generateDeleteMany.ts +0 -58
- package/src/helpers/generateFindFirst.ts +0 -68
- package/src/helpers/generateFindMany.ts +0 -68
- package/src/helpers/generateFindUnique.ts +0 -68
- package/src/helpers/generateGroupBy.ts +0 -58
- package/src/helpers/generateImportPrismaStatement.ts +0 -38
- package/src/helpers/generateRouteFile.ts +0 -183
- package/src/helpers/generateUpdate.ts +0 -62
- package/src/helpers/generateUpdateMany.ts +0 -59
- package/src/helpers/generateUpsert.ts +0 -60
- package/src/utils/formatFile.ts +0 -22
- package/src/utils/writeFileSafely.ts +0 -28
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Generates an Express middleware function that handles the upsert operation (create or update)
|
|
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 generateUpsertFunction = (options: {
|
|
11
|
-
model: DMMF.Model
|
|
12
|
-
prismaImportStatement: string
|
|
13
|
-
}): string => {
|
|
14
|
-
const { model, prismaImportStatement } = options
|
|
15
|
-
const modelName = model.name
|
|
16
|
-
const functionName = `${modelName}Upsert`
|
|
17
|
-
const argsTypeName = `Prisma.${modelName}UpsertArgs`
|
|
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 UpsertRequest extends Request {
|
|
26
|
-
prisma: PrismaClient;
|
|
27
|
-
body: ${argsTypeName};
|
|
28
|
-
outputValidation?: ZodTypeAny;
|
|
29
|
-
omitOutputValidation?: boolean;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type UpsertMiddleware = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>
|
|
33
|
-
|
|
34
|
-
export async function ${functionName}(req: UpsertRequest, 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.${modelName.toLowerCase()}.upsert(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 upsert request:', error);
|
|
56
|
-
res.status(500).json({ error: error.message });
|
|
57
|
-
next(error);
|
|
58
|
-
}
|
|
59
|
-
}`
|
|
60
|
-
}
|
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
|
-
}
|
|
@@ -1,28 +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 = operation === 'index' ? 'index' : `${model.name}${operation}`
|
|
18
|
-
const filePath = path.join(
|
|
19
|
-
options.generator.output?.value!,
|
|
20
|
-
`${model.name}/${fileName}.ts`,
|
|
21
|
-
)
|
|
22
|
-
console.log('filePath :>> ', filePath)
|
|
23
|
-
await fs.mkdir(path.dirname(filePath), {
|
|
24
|
-
recursive: true,
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
await fs.writeFile(filePath, await formatFile(content))
|
|
28
|
-
}
|