prisma-generator-express 1.33.0 → 1.34.1
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/dist/generators/generateFastifyHandler.js +12 -4
- package/dist/generators/generateFastifyHandler.js.map +1 -1
- package/dist/generators/generateOperationCore.js +1 -1
- package/dist/generators/generateOperationCore.js.map +1 -1
- package/dist/generators/generateRouter.js +39 -37
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.js +369 -368
- package/dist/generators/generateRouterFastify.js.map +1 -1
- package/dist/generators/generateUnifiedHandler.js +10 -2
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/index.js +9 -12
- package/dist/index.js.map +1 -1
- package/dist/utils/strings.js +4 -1
- package/dist/utils/strings.js.map +1 -1
- package/package.json +1 -1
- package/src/generators/generateFastifyHandler.ts +14 -4
- package/src/generators/generateOperationCore.ts +1 -1
- package/src/generators/generateRouter.ts +39 -37
- package/src/generators/generateRouterFastify.ts +369 -368
- package/src/generators/generateUnifiedHandler.ts +12 -2
- package/src/index.ts +10 -15
- package/src/utils/strings.ts +4 -1
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { DMMF } from '@prisma/generator-helper'
|
|
2
|
+
import { toCamelCase } from '../utils/strings.js'
|
|
2
3
|
|
|
3
4
|
export interface UnifiedHandlerOptions {
|
|
4
5
|
model: DMMF.Model
|
|
5
6
|
}
|
|
6
7
|
|
|
8
|
+
const CORE_NAME_MAP: Record<string, string> = {
|
|
9
|
+
delete: 'deleteUnique',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function coreFnName(op: string): string {
|
|
13
|
+
return CORE_NAME_MAP[op] || op
|
|
14
|
+
}
|
|
15
|
+
|
|
7
16
|
const ALL_OPS = [
|
|
8
17
|
'findMany',
|
|
9
18
|
'findFirst',
|
|
@@ -27,9 +36,10 @@ const ALL_OPS = [
|
|
|
27
36
|
|
|
28
37
|
export function generateUnifiedHandler(options: UnifiedHandlerOptions): string {
|
|
29
38
|
const modelName = options.model.name
|
|
39
|
+
const prefix = toCamelCase(modelName)
|
|
30
40
|
|
|
31
41
|
const handlers = ALL_OPS.map((op) => {
|
|
32
|
-
const exportName = `${
|
|
42
|
+
const exportName = `${prefix}${op.charAt(0).toUpperCase() + op.slice(1)}`
|
|
33
43
|
|
|
34
44
|
return `
|
|
35
45
|
export async function ${exportName}(
|
|
@@ -38,7 +48,7 @@ export async function ${exportName}(
|
|
|
38
48
|
next: NextFunction,
|
|
39
49
|
) {
|
|
40
50
|
try {
|
|
41
|
-
res.locals.data = await core.${op}(buildContext(req, res))
|
|
51
|
+
res.locals.data = await core.${coreFnName(op)}(buildContext(req, res))
|
|
42
52
|
next()
|
|
43
53
|
} catch (error: unknown) {
|
|
44
54
|
next(mapError(error))
|
package/src/index.ts
CHANGED
|
@@ -31,35 +31,30 @@ function getTarget(options: GeneratorOptions): Target {
|
|
|
31
31
|
throw new Error(`Invalid target "${raw}". Expected "express" or "fastify".`)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
function resolveOutputPath(options: GeneratorOptions, target: Target): string {
|
|
35
|
-
const explicit = options.generator.output?.value
|
|
36
|
-
|
|
37
|
-
if (explicit && explicit.length > 0) {
|
|
38
|
-
return explicit
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const schemaDir = path.dirname(options.schemaPath)
|
|
42
|
-
return path.join(schemaDir, 'generated', target)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
34
|
generatorHandler({
|
|
46
35
|
onManifest() {
|
|
47
36
|
return {
|
|
48
37
|
version: require('../package.json').version,
|
|
49
|
-
defaultOutput: '',
|
|
38
|
+
defaultOutput: '../generated/express',
|
|
50
39
|
prettyName: GENERATOR_NAME,
|
|
51
40
|
}
|
|
52
41
|
},
|
|
53
42
|
|
|
54
43
|
async onGenerate(options: GeneratorOptions) {
|
|
55
44
|
const target = getTarget(options)
|
|
56
|
-
const outputPath = resolveOutputPath(options, target)
|
|
57
45
|
|
|
58
|
-
options.generator.output
|
|
46
|
+
const hasExplicitOutput = !!options.generator.output?.fromEnvVar
|
|
47
|
+
|| (options.generator.config as Record<string, unknown>).output !== undefined
|
|
48
|
+
|
|
49
|
+
if (!hasExplicitOutput) {
|
|
50
|
+
const schemaDir = path.dirname(options.schemaPath)
|
|
51
|
+
const outputPath = path.join(schemaDir, 'generated', target)
|
|
52
|
+
options.generator.output = { value: outputPath, fromEnvVar: null }
|
|
53
|
+
}
|
|
59
54
|
|
|
60
55
|
console.log(`\n═══ Prisma Generator (${target.toUpperCase()}) ═══`)
|
|
61
56
|
console.log(` Target: ${target}`)
|
|
62
|
-
console.log(` Output: ${
|
|
57
|
+
console.log(` Output: ${options.generator.output?.value}`)
|
|
63
58
|
|
|
64
59
|
await copyFiles(options, target)
|
|
65
60
|
|
package/src/utils/strings.ts
CHANGED
|
@@ -3,5 +3,8 @@ export const capitalize = (str: string) =>
|
|
|
3
3
|
|
|
4
4
|
export function toCamelCase(str: string) {
|
|
5
5
|
if (!str) return str
|
|
6
|
-
return str
|
|
6
|
+
return str
|
|
7
|
+
.split('_')
|
|
8
|
+
.map((part, i) => i === 0 ? part.toLowerCase() : capitalize(part))
|
|
9
|
+
.join('')
|
|
7
10
|
}
|