@stryke/prisma-trpc-generator 0.2.0 → 0.2.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/chunk-6UJ53OQ6.js +19 -0
- package/dist/chunk-GQ7ABBSV.js +160 -0
- package/dist/chunk-LVOPI6NH.js +18 -0
- package/dist/chunk-RYVIOFDO.js +333 -0
- package/dist/chunk-SHUYVCID.js +6 -0
- package/dist/chunk-V6DNCMCB.js +31 -0
- package/dist/config.cjs +52 -18
- package/dist/config.js +7 -0
- package/dist/generator.cjs +1 -3
- package/dist/generator.js +7 -0
- package/dist/helpers.cjs +360 -206
- package/dist/helpers.js +29 -0
- package/dist/index.cjs +520 -8
- package/dist/index.js +6 -0
- package/dist/prisma-generator.cjs +502 -77
- package/dist/prisma-generator.js +10 -0
- package/dist/project.cjs +36 -13
- package/dist/project.js +7 -0
- package/package.json +59 -151
- package/dist/config.d.ts +0 -28
- package/dist/config.mjs +0 -1
- package/dist/generator.d.ts +0 -2
- package/dist/generator.mjs +0 -2
- package/dist/helpers.d.ts +0 -18
- package/dist/helpers.mjs +0 -26
- package/dist/index.d.ts +0 -1
- package/dist/index.mjs +0 -1
- package/dist/prisma-generator.d.ts +0 -2
- package/dist/prisma-generator.mjs +0 -6
- package/dist/project.d.ts +0 -2
- package/dist/project.mjs +0 -1
- package/dist/utils/getRelativePath.cjs +0 -18
- package/dist/utils/getRelativePath.d.ts +0 -1
- package/dist/utils/getRelativePath.mjs +0 -1
- package/dist/utils/removeDir.cjs +0 -18
- package/dist/utils/removeDir.d.ts +0 -1
- package/dist/utils/removeDir.mjs +0 -1
- package/dist/utils/uncapitalizeFirstLetter.cjs +0 -8
- package/dist/utils/uncapitalizeFirstLetter.d.ts +0 -1
- package/dist/utils/uncapitalizeFirstLetter.mjs +0 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generate
|
|
3
|
+
} from "./chunk-GQ7ABBSV.js";
|
|
4
|
+
import {
|
|
5
|
+
__name
|
|
6
|
+
} from "./chunk-SHUYVCID.js";
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import { generatorHandler } from "@prisma/generator-helper";
|
|
10
|
+
generatorHandler({
|
|
11
|
+
onManifest: /* @__PURE__ */ __name(() => ({
|
|
12
|
+
defaultOutput: "./generated",
|
|
13
|
+
prettyName: "Prisma tRPC Generator",
|
|
14
|
+
requiresGenerators: [
|
|
15
|
+
"prisma-client-js"
|
|
16
|
+
]
|
|
17
|
+
}), "onManifest"),
|
|
18
|
+
onGenerate: generate
|
|
19
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
configSchema
|
|
3
|
+
} from "./chunk-V6DNCMCB.js";
|
|
4
|
+
import {
|
|
5
|
+
generateBaseRouter,
|
|
6
|
+
generateCreateRouterImport,
|
|
7
|
+
generateProcedure,
|
|
8
|
+
generateRPCImport,
|
|
9
|
+
generateRouterImport,
|
|
10
|
+
generateRouterSchemaImports,
|
|
11
|
+
generateShieldImport,
|
|
12
|
+
getInputTypeByOpName,
|
|
13
|
+
resolveModelsComments
|
|
14
|
+
} from "./chunk-RYVIOFDO.js";
|
|
15
|
+
import {
|
|
16
|
+
project
|
|
17
|
+
} from "./chunk-LVOPI6NH.js";
|
|
18
|
+
import {
|
|
19
|
+
__name
|
|
20
|
+
} from "./chunk-SHUYVCID.js";
|
|
21
|
+
|
|
22
|
+
// src/prisma-generator.ts
|
|
23
|
+
import { getDMMF, parseEnvValue } from "@prisma/internals";
|
|
24
|
+
import { promises as fs2 } from "node:fs";
|
|
25
|
+
import path2 from "node:path";
|
|
26
|
+
import pluralize from "pluralize";
|
|
27
|
+
import { generate as PrismaTrpcShieldGenerator } from "prisma-trpc-shield-generator/lib/prisma-generator";
|
|
28
|
+
import { generate as PrismaZodGenerator } from "prisma-zod-generator/lib/prisma-generator";
|
|
29
|
+
|
|
30
|
+
// src/utils/removeDir.ts
|
|
31
|
+
import { promises as fs } from "node:fs";
|
|
32
|
+
import path from "node:path";
|
|
33
|
+
async function removeDir(dirPath, onlyContent) {
|
|
34
|
+
const dirEntries = await fs.readdir(dirPath, {
|
|
35
|
+
withFileTypes: true
|
|
36
|
+
});
|
|
37
|
+
await Promise.all(dirEntries.map(async (dirEntry) => {
|
|
38
|
+
const fullPath = path.join(dirPath, dirEntry.name);
|
|
39
|
+
return dirEntry.isDirectory() ? removeDir(fullPath, false) : fs.unlink(fullPath);
|
|
40
|
+
}));
|
|
41
|
+
if (!onlyContent) {
|
|
42
|
+
await fs.rmdir(dirPath);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
__name(removeDir, "removeDir");
|
|
46
|
+
|
|
47
|
+
// src/prisma-generator.ts
|
|
48
|
+
async function generate(options) {
|
|
49
|
+
const outputDir = parseEnvValue(options.generator.output);
|
|
50
|
+
const results = configSchema.safeParse(options.generator.config);
|
|
51
|
+
if (!results.success) throw new Error("Invalid options passed");
|
|
52
|
+
const config = results.data;
|
|
53
|
+
await fs2.mkdir(outputDir, {
|
|
54
|
+
recursive: true
|
|
55
|
+
});
|
|
56
|
+
await removeDir(outputDir, true);
|
|
57
|
+
if (config.withZod) {
|
|
58
|
+
await PrismaZodGenerator(options);
|
|
59
|
+
}
|
|
60
|
+
if (config.withShield === true) {
|
|
61
|
+
const shieldOutputPath = path2.join(outputDir, "./shield");
|
|
62
|
+
await PrismaTrpcShieldGenerator({
|
|
63
|
+
...options,
|
|
64
|
+
generator: {
|
|
65
|
+
...options.generator,
|
|
66
|
+
output: {
|
|
67
|
+
...options.generator.output,
|
|
68
|
+
value: shieldOutputPath
|
|
69
|
+
},
|
|
70
|
+
config: {
|
|
71
|
+
...options.generator.config,
|
|
72
|
+
contextPath: config.contextPath
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const prismaClientProvider = options.otherGenerators.find((it) => parseEnvValue(it.provider) === "prisma-client-js");
|
|
78
|
+
const prismaClientDmmf = await getDMMF({
|
|
79
|
+
datamodel: options.datamodel,
|
|
80
|
+
previewFeatures: prismaClientProvider.previewFeatures
|
|
81
|
+
});
|
|
82
|
+
const modelOperations = prismaClientDmmf.mappings.modelOperations;
|
|
83
|
+
const models = prismaClientDmmf.datamodel.models;
|
|
84
|
+
const hiddenModels = [];
|
|
85
|
+
resolveModelsComments(models, hiddenModels);
|
|
86
|
+
const createRouter = project.createSourceFile(path2.resolve(outputDir, "routers", "helpers", "createRouter.ts"), void 0, {
|
|
87
|
+
overwrite: true
|
|
88
|
+
});
|
|
89
|
+
generateRPCImport(createRouter);
|
|
90
|
+
if (config.withShield) {
|
|
91
|
+
generateShieldImport(createRouter, options, config.withShield);
|
|
92
|
+
}
|
|
93
|
+
generateBaseRouter(createRouter, config, options);
|
|
94
|
+
createRouter.formatText({
|
|
95
|
+
indentSize: 2
|
|
96
|
+
});
|
|
97
|
+
const appRouter = project.createSourceFile(path2.resolve(outputDir, "routers", `index.ts`), void 0, {
|
|
98
|
+
overwrite: true
|
|
99
|
+
});
|
|
100
|
+
generateCreateRouterImport({
|
|
101
|
+
sourceFile: appRouter
|
|
102
|
+
});
|
|
103
|
+
const routerStatements = [];
|
|
104
|
+
for (const modelOperation of modelOperations) {
|
|
105
|
+
const { model, ...operations } = modelOperation;
|
|
106
|
+
if (hiddenModels.includes(model)) continue;
|
|
107
|
+
const modelActions = Object.keys(operations).filter((opType) => config.generateModelActions.includes(opType.replace("One", "")));
|
|
108
|
+
if (!modelActions.length) continue;
|
|
109
|
+
const plural = pluralize(model.toLowerCase());
|
|
110
|
+
generateRouterImport(appRouter, plural, model);
|
|
111
|
+
const modelRouter = project.createSourceFile(path2.resolve(outputDir, "routers", `${model}.router.ts`), void 0, {
|
|
112
|
+
overwrite: true
|
|
113
|
+
});
|
|
114
|
+
generateCreateRouterImport({
|
|
115
|
+
sourceFile: modelRouter,
|
|
116
|
+
config
|
|
117
|
+
});
|
|
118
|
+
if (config.withZod) {
|
|
119
|
+
generateRouterSchemaImports(modelRouter, model, modelActions);
|
|
120
|
+
}
|
|
121
|
+
modelRouter.addStatements(
|
|
122
|
+
/* ts */
|
|
123
|
+
`
|
|
124
|
+
export const ${plural}Router = t.router({`
|
|
125
|
+
);
|
|
126
|
+
for (const opType of modelActions) {
|
|
127
|
+
const opNameWithModel = operations[opType];
|
|
128
|
+
const baseOpType = opType.replace("OrThrow", "");
|
|
129
|
+
generateProcedure(modelRouter, opNameWithModel, getInputTypeByOpName(baseOpType, model), model, opType, baseOpType, config);
|
|
130
|
+
}
|
|
131
|
+
modelRouter.addStatements(
|
|
132
|
+
/* ts */
|
|
133
|
+
`
|
|
134
|
+
})`
|
|
135
|
+
);
|
|
136
|
+
modelRouter.formatText({
|
|
137
|
+
indentSize: 2
|
|
138
|
+
});
|
|
139
|
+
routerStatements.push(
|
|
140
|
+
/* ts */
|
|
141
|
+
`
|
|
142
|
+
${model.toLowerCase()}: ${plural}Router`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
appRouter.addStatements(
|
|
146
|
+
/* ts */
|
|
147
|
+
`
|
|
148
|
+
export const appRouter = t.router({${routerStatements}})
|
|
149
|
+
`
|
|
150
|
+
);
|
|
151
|
+
appRouter.formatText({
|
|
152
|
+
indentSize: 2
|
|
153
|
+
});
|
|
154
|
+
await project.save();
|
|
155
|
+
}
|
|
156
|
+
__name(generate, "generate");
|
|
157
|
+
|
|
158
|
+
export {
|
|
159
|
+
generate
|
|
160
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/project.ts
|
|
2
|
+
import { ModuleKind, Project, ScriptTarget } from "ts-morph";
|
|
3
|
+
var compilerOptions = {
|
|
4
|
+
target: ScriptTarget.ES2019,
|
|
5
|
+
module: ModuleKind.CommonJS,
|
|
6
|
+
emitDecoratorMetadata: true,
|
|
7
|
+
experimentalDecorators: true,
|
|
8
|
+
esModuleInterop: true
|
|
9
|
+
};
|
|
10
|
+
var project = new Project({
|
|
11
|
+
compilerOptions: {
|
|
12
|
+
...compilerOptions
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
project
|
|
18
|
+
};
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__name
|
|
3
|
+
} from "./chunk-SHUYVCID.js";
|
|
4
|
+
|
|
5
|
+
// src/helpers.ts
|
|
6
|
+
import { parseEnvValue } from "@prisma/internals";
|
|
7
|
+
|
|
8
|
+
// src/utils/getRelativePath.ts
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
function getRelativePath(outputPath, filePath, isOutsideOutputPath, schemaPath) {
|
|
11
|
+
const fromPath = path.join(outputPath, "routers", "helpers");
|
|
12
|
+
let toPath = path.join(outputPath, filePath);
|
|
13
|
+
if (isOutsideOutputPath) {
|
|
14
|
+
const schemaPathSplit = schemaPath?.split(path.sep);
|
|
15
|
+
const schemaPathWithoutFileAndExtension = schemaPathSplit.slice(0, schemaPathSplit.length - 1).join(path.posix.sep);
|
|
16
|
+
toPath = path.join(schemaPathWithoutFileAndExtension, filePath);
|
|
17
|
+
}
|
|
18
|
+
const newPath = path.relative(fromPath, toPath).split(path.sep).join(path.posix.sep);
|
|
19
|
+
return newPath;
|
|
20
|
+
}
|
|
21
|
+
__name(getRelativePath, "getRelativePath");
|
|
22
|
+
|
|
23
|
+
// src/utils/uncapitalizeFirstLetter.ts
|
|
24
|
+
var uncapitalizeFirstLetter = /* @__PURE__ */ __name((str) => {
|
|
25
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
26
|
+
}, "uncapitalizeFirstLetter");
|
|
27
|
+
|
|
28
|
+
// src/helpers.ts
|
|
29
|
+
var getProcedureName = /* @__PURE__ */ __name((config) => {
|
|
30
|
+
return config.withShield ? "shieldedProcedure" : config.withMiddleware ? "protectedProcedure" : "publicProcedure";
|
|
31
|
+
}, "getProcedureName");
|
|
32
|
+
var generateCreateRouterImport = /* @__PURE__ */ __name(({ sourceFile, config }) => {
|
|
33
|
+
const imports = [
|
|
34
|
+
"t"
|
|
35
|
+
];
|
|
36
|
+
if (config) {
|
|
37
|
+
imports.push(getProcedureName(config));
|
|
38
|
+
}
|
|
39
|
+
sourceFile.addImportDeclaration({
|
|
40
|
+
moduleSpecifier: "./helpers/createRouter",
|
|
41
|
+
namedImports: imports
|
|
42
|
+
});
|
|
43
|
+
}, "generateCreateRouterImport");
|
|
44
|
+
var generateRPCImport = /* @__PURE__ */ __name((sourceFile) => {
|
|
45
|
+
sourceFile.addImportDeclaration({
|
|
46
|
+
moduleSpecifier: "@trpc/server",
|
|
47
|
+
namespaceImport: "trpc"
|
|
48
|
+
});
|
|
49
|
+
}, "generateRPCImport");
|
|
50
|
+
var generateShieldImport = /* @__PURE__ */ __name((sourceFile, options, value) => {
|
|
51
|
+
const outputDir = parseEnvValue(options.generator.output);
|
|
52
|
+
let shieldPath = getRelativePath(outputDir, "shield/shield");
|
|
53
|
+
if (typeof value === "string") {
|
|
54
|
+
shieldPath = getRelativePath(outputDir, value, true, options.schemaPath);
|
|
55
|
+
}
|
|
56
|
+
sourceFile.addImportDeclaration({
|
|
57
|
+
moduleSpecifier: shieldPath,
|
|
58
|
+
namedImports: [
|
|
59
|
+
"permissions"
|
|
60
|
+
]
|
|
61
|
+
});
|
|
62
|
+
}, "generateShieldImport");
|
|
63
|
+
var generateMiddlewareImport = /* @__PURE__ */ __name((sourceFile, options) => {
|
|
64
|
+
const outputDir = parseEnvValue(options.generator.output);
|
|
65
|
+
sourceFile.addImportDeclaration({
|
|
66
|
+
moduleSpecifier: getRelativePath(outputDir, "middleware"),
|
|
67
|
+
namedImports: [
|
|
68
|
+
"permissions"
|
|
69
|
+
]
|
|
70
|
+
});
|
|
71
|
+
}, "generateMiddlewareImport");
|
|
72
|
+
var generateRouterImport = /* @__PURE__ */ __name((sourceFile, modelNamePlural, modelNameCamelCase) => {
|
|
73
|
+
sourceFile.addImportDeclaration({
|
|
74
|
+
moduleSpecifier: `./${modelNameCamelCase}.router`,
|
|
75
|
+
namedImports: [
|
|
76
|
+
`${modelNamePlural}Router`
|
|
77
|
+
]
|
|
78
|
+
});
|
|
79
|
+
}, "generateRouterImport");
|
|
80
|
+
function generateBaseRouter(sourceFile, config, options) {
|
|
81
|
+
const outputDir = parseEnvValue(options.generator.output);
|
|
82
|
+
sourceFile.addStatements(
|
|
83
|
+
/* ts */
|
|
84
|
+
`
|
|
85
|
+
import type { Context } from '${getRelativePath(outputDir, config.contextPath, true, options.schemaPath)}';
|
|
86
|
+
`
|
|
87
|
+
);
|
|
88
|
+
if (config.trpcOptionsPath) {
|
|
89
|
+
sourceFile.addStatements(
|
|
90
|
+
/* ts */
|
|
91
|
+
`
|
|
92
|
+
import trpcOptions from '${getRelativePath(outputDir, config.trpcOptionsPath, true, options.schemaPath)}';
|
|
93
|
+
`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
sourceFile.addStatements(
|
|
97
|
+
/* ts */
|
|
98
|
+
`
|
|
99
|
+
export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptionsPath ? "trpcOptions" : ""});
|
|
100
|
+
`
|
|
101
|
+
);
|
|
102
|
+
const middlewares = [];
|
|
103
|
+
if (config.withMiddleware && typeof config.withMiddleware === "boolean") {
|
|
104
|
+
sourceFile.addStatements(
|
|
105
|
+
/* ts */
|
|
106
|
+
`
|
|
107
|
+
export const globalMiddleware = t.middleware(async ({ ctx, next }) => {
|
|
108
|
+
console.log('inside middleware!')
|
|
109
|
+
return next()
|
|
110
|
+
});`
|
|
111
|
+
);
|
|
112
|
+
middlewares.push({
|
|
113
|
+
type: "global",
|
|
114
|
+
value: (
|
|
115
|
+
/* ts */
|
|
116
|
+
`.use(globalMiddleware)`
|
|
117
|
+
)
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (config.withMiddleware && typeof config.withMiddleware === "string") {
|
|
121
|
+
sourceFile.addStatements(
|
|
122
|
+
/* ts */
|
|
123
|
+
`
|
|
124
|
+
import defaultMiddleware from '${getRelativePath(outputDir, config.withMiddleware, true, options.schemaPath)}';
|
|
125
|
+
`
|
|
126
|
+
);
|
|
127
|
+
sourceFile.addStatements(
|
|
128
|
+
/* ts */
|
|
129
|
+
`
|
|
130
|
+
export const globalMiddleware = t.middleware(defaultMiddleware);`
|
|
131
|
+
);
|
|
132
|
+
middlewares.push({
|
|
133
|
+
type: "global",
|
|
134
|
+
value: (
|
|
135
|
+
/* ts */
|
|
136
|
+
`.use(globalMiddleware)`
|
|
137
|
+
)
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (config.withShield) {
|
|
141
|
+
sourceFile.addStatements(
|
|
142
|
+
/* ts */
|
|
143
|
+
`
|
|
144
|
+
export const permissionsMiddleware = t.middleware(permissions); `
|
|
145
|
+
);
|
|
146
|
+
middlewares.push({
|
|
147
|
+
type: "shield",
|
|
148
|
+
value: (
|
|
149
|
+
/* ts */
|
|
150
|
+
`
|
|
151
|
+
.use(permissions)`
|
|
152
|
+
)
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
sourceFile.addStatements(
|
|
156
|
+
/* ts */
|
|
157
|
+
`
|
|
158
|
+
export const publicProcedure = t.procedure; `
|
|
159
|
+
);
|
|
160
|
+
if (middlewares.length > 0) {
|
|
161
|
+
const procName = getProcedureName(config);
|
|
162
|
+
middlewares.forEach((middleware, i) => {
|
|
163
|
+
if (i === 0) {
|
|
164
|
+
sourceFile.addStatements(
|
|
165
|
+
/* ts */
|
|
166
|
+
`
|
|
167
|
+
export const ${procName} = t.procedure
|
|
168
|
+
`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
sourceFile.addStatements(
|
|
172
|
+
/* ts */
|
|
173
|
+
`
|
|
174
|
+
.use(${middleware.type === "shield" ? "permissionsMiddleware" : "globalMiddleware"})
|
|
175
|
+
`
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
__name(generateBaseRouter, "generateBaseRouter");
|
|
181
|
+
function generateProcedure(sourceFile, name, typeName, modelName, opType, baseOpType, config) {
|
|
182
|
+
let input = `input${!config.withZod ? " as any" : ""}`;
|
|
183
|
+
const nameWithoutModel = name.replace(modelName, "");
|
|
184
|
+
if (nameWithoutModel === "groupBy" && config.withZod) {
|
|
185
|
+
input = "{ where: input.where, orderBy: input.orderBy, by: input.by, having: input.having, take: input.take, skip: input.skip }";
|
|
186
|
+
}
|
|
187
|
+
sourceFile.addStatements(
|
|
188
|
+
/* ts */
|
|
189
|
+
`${config.showModelNameInProcedure ? name : nameWithoutModel}: ${getProcedureName(config)}
|
|
190
|
+
${config.withZod ? `.input(${typeName})` : ""}.${getProcedureTypeByOpName(baseOpType)}(async ({ ctx, input }) => {
|
|
191
|
+
const ${name} = await ctx.prisma.${uncapitalizeFirstLetter(modelName)}.${opType.replace("One", "")}(${input});
|
|
192
|
+
return ${name};
|
|
193
|
+
}),`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
__name(generateProcedure, "generateProcedure");
|
|
197
|
+
function generateRouterSchemaImports(sourceFile, modelName, modelActions) {
|
|
198
|
+
sourceFile.addStatements(
|
|
199
|
+
/* ts */
|
|
200
|
+
[
|
|
201
|
+
// remove any duplicate import statements
|
|
202
|
+
...new Set(modelActions.map((opName) => getRouterSchemaImportByOpName(opName, modelName)))
|
|
203
|
+
].join("\n")
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
__name(generateRouterSchemaImports, "generateRouterSchemaImports");
|
|
207
|
+
var getRouterSchemaImportByOpName = /* @__PURE__ */ __name((opName, modelName) => {
|
|
208
|
+
const opType = opName.replace("OrThrow", "");
|
|
209
|
+
const inputType = getInputTypeByOpName(opType, modelName);
|
|
210
|
+
return inputType ? `import { ${inputType} } from "../schemas/${opType}${modelName}.schema"; ` : "";
|
|
211
|
+
}, "getRouterSchemaImportByOpName");
|
|
212
|
+
var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
|
|
213
|
+
let inputType;
|
|
214
|
+
switch (opName) {
|
|
215
|
+
case "findUnique":
|
|
216
|
+
inputType = `${modelName}FindUniqueSchema`;
|
|
217
|
+
break;
|
|
218
|
+
case "findFirst":
|
|
219
|
+
inputType = `${modelName}FindFirstSchema`;
|
|
220
|
+
break;
|
|
221
|
+
case "findMany":
|
|
222
|
+
inputType = `${modelName}FindManySchema`;
|
|
223
|
+
break;
|
|
224
|
+
case "findRaw":
|
|
225
|
+
inputType = `${modelName}FindRawObjectSchema`;
|
|
226
|
+
break;
|
|
227
|
+
case "createOne":
|
|
228
|
+
inputType = `${modelName}CreateOneSchema`;
|
|
229
|
+
break;
|
|
230
|
+
case "createMany":
|
|
231
|
+
inputType = `${modelName}CreateManySchema`;
|
|
232
|
+
break;
|
|
233
|
+
case "deleteOne":
|
|
234
|
+
inputType = `${modelName}DeleteOneSchema`;
|
|
235
|
+
break;
|
|
236
|
+
case "updateOne":
|
|
237
|
+
inputType = `${modelName}UpdateOneSchema`;
|
|
238
|
+
break;
|
|
239
|
+
case "deleteMany":
|
|
240
|
+
inputType = `${modelName}DeleteManySchema`;
|
|
241
|
+
break;
|
|
242
|
+
case "updateMany":
|
|
243
|
+
inputType = `${modelName}UpdateManySchema`;
|
|
244
|
+
break;
|
|
245
|
+
case "upsertOne":
|
|
246
|
+
inputType = `${modelName}UpsertSchema`;
|
|
247
|
+
break;
|
|
248
|
+
case "aggregate":
|
|
249
|
+
inputType = `${modelName}AggregateSchema`;
|
|
250
|
+
break;
|
|
251
|
+
case "aggregateRaw":
|
|
252
|
+
inputType = `${modelName}AggregateRawObjectSchema`;
|
|
253
|
+
break;
|
|
254
|
+
case "groupBy":
|
|
255
|
+
inputType = `${modelName}GroupBySchema`;
|
|
256
|
+
break;
|
|
257
|
+
default:
|
|
258
|
+
console.log("getInputTypeByOpName: ", {
|
|
259
|
+
opName,
|
|
260
|
+
modelName
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return inputType;
|
|
264
|
+
}, "getInputTypeByOpName");
|
|
265
|
+
var getProcedureTypeByOpName = /* @__PURE__ */ __name((opName) => {
|
|
266
|
+
let procType;
|
|
267
|
+
switch (opName) {
|
|
268
|
+
case "findUnique":
|
|
269
|
+
case "findFirst":
|
|
270
|
+
case "findMany":
|
|
271
|
+
case "findRaw":
|
|
272
|
+
case "aggregate":
|
|
273
|
+
case "aggregateRaw":
|
|
274
|
+
case "groupBy":
|
|
275
|
+
procType = "query";
|
|
276
|
+
break;
|
|
277
|
+
case "createOne":
|
|
278
|
+
case "createMany":
|
|
279
|
+
case "deleteOne":
|
|
280
|
+
case "updateOne":
|
|
281
|
+
case "deleteMany":
|
|
282
|
+
case "updateMany":
|
|
283
|
+
case "upsertOne":
|
|
284
|
+
procType = "mutation";
|
|
285
|
+
break;
|
|
286
|
+
default:
|
|
287
|
+
console.log("getProcedureTypeByOpName: ", {
|
|
288
|
+
opName
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
return procType;
|
|
292
|
+
}, "getProcedureTypeByOpName");
|
|
293
|
+
function resolveModelsComments(models, hiddenModels) {
|
|
294
|
+
const modelAttributeRegex = /(?:@@Gen\.)+[A-z]+\(.+\)/;
|
|
295
|
+
const attributeNameRegex = /\.+[A-Z]+\(+/i;
|
|
296
|
+
const attributeArgsRegex = /\(+[A-Z]+:.+\)/i;
|
|
297
|
+
for (const model of models) {
|
|
298
|
+
if (model.documentation) {
|
|
299
|
+
const attribute = model.documentation?.match(modelAttributeRegex)?.[0];
|
|
300
|
+
const attributeName = attribute?.match(attributeNameRegex)?.[0]?.slice(1, -1);
|
|
301
|
+
if (attributeName !== "model") continue;
|
|
302
|
+
const rawAttributeArgs = attribute?.match(attributeArgsRegex)?.[0]?.slice(1, -1);
|
|
303
|
+
const parsedAttributeArgs = {};
|
|
304
|
+
if (rawAttributeArgs) {
|
|
305
|
+
const rawAttributeArgsParts = rawAttributeArgs.split(":").map((it) => it.trim()).map((part) => part.startsWith("[") ? part : part.split(",")).flat().map((it) => it.trim());
|
|
306
|
+
for (let i = 0; i < rawAttributeArgsParts.length; i += 2) {
|
|
307
|
+
const key = rawAttributeArgsParts[i];
|
|
308
|
+
const value = rawAttributeArgsParts[i + 1];
|
|
309
|
+
parsedAttributeArgs[key] = JSON.parse(value);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (parsedAttributeArgs.hide) {
|
|
313
|
+
hiddenModels.push(model.name);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
__name(resolveModelsComments, "resolveModelsComments");
|
|
319
|
+
|
|
320
|
+
export {
|
|
321
|
+
generateCreateRouterImport,
|
|
322
|
+
generateRPCImport,
|
|
323
|
+
generateShieldImport,
|
|
324
|
+
generateMiddlewareImport,
|
|
325
|
+
generateRouterImport,
|
|
326
|
+
generateBaseRouter,
|
|
327
|
+
generateProcedure,
|
|
328
|
+
generateRouterSchemaImports,
|
|
329
|
+
getRouterSchemaImportByOpName,
|
|
330
|
+
getInputTypeByOpName,
|
|
331
|
+
getProcedureTypeByOpName,
|
|
332
|
+
resolveModelsComments
|
|
333
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
import { DMMF } from "@prisma/generator-helper";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
var configBoolean = z.enum([
|
|
5
|
+
"true",
|
|
6
|
+
"false"
|
|
7
|
+
]).transform((arg) => JSON.parse(arg));
|
|
8
|
+
var configMiddleware = z.union([
|
|
9
|
+
configBoolean,
|
|
10
|
+
z.string().default("../../../../src/middleware")
|
|
11
|
+
]);
|
|
12
|
+
var configShield = z.union([
|
|
13
|
+
configBoolean,
|
|
14
|
+
z.string().default("../../../../src/shield")
|
|
15
|
+
]);
|
|
16
|
+
var modelActionEnum = z.nativeEnum(DMMF.ModelAction);
|
|
17
|
+
var configSchema = z.object({
|
|
18
|
+
withMiddleware: configMiddleware.default("true"),
|
|
19
|
+
withShield: configShield.default("true"),
|
|
20
|
+
withZod: configBoolean.default("true"),
|
|
21
|
+
contextPath: z.string().default("../../../../src/context"),
|
|
22
|
+
trpcOptionsPath: z.string().optional(),
|
|
23
|
+
showModelNameInProcedure: configBoolean.default("true"),
|
|
24
|
+
generateModelActions: z.string().default(Object.values(DMMF.ModelAction).join(",")).transform((arg) => {
|
|
25
|
+
return arg.split(",").map((action) => modelActionEnum.parse(action.trim()));
|
|
26
|
+
})
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
configSchema
|
|
31
|
+
};
|
package/dist/config.cjs
CHANGED
|
@@ -1,21 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
2
19
|
|
|
3
|
-
|
|
4
|
-
|
|
20
|
+
// src/config.ts
|
|
21
|
+
var config_exports = {};
|
|
22
|
+
__export(config_exports, {
|
|
23
|
+
configSchema: () => configSchema
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(config_exports);
|
|
26
|
+
var import_generator_helper = require("@prisma/generator-helper");
|
|
27
|
+
var import_zod = require("zod");
|
|
28
|
+
var configBoolean = import_zod.z.enum([
|
|
29
|
+
"true",
|
|
30
|
+
"false"
|
|
31
|
+
]).transform((arg) => JSON.parse(arg));
|
|
32
|
+
var configMiddleware = import_zod.z.union([
|
|
33
|
+
configBoolean,
|
|
34
|
+
import_zod.z.string().default("../../../../src/middleware")
|
|
35
|
+
]);
|
|
36
|
+
var configShield = import_zod.z.union([
|
|
37
|
+
configBoolean,
|
|
38
|
+
import_zod.z.string().default("../../../../src/shield")
|
|
39
|
+
]);
|
|
40
|
+
var modelActionEnum = import_zod.z.nativeEnum(import_generator_helper.DMMF.ModelAction);
|
|
41
|
+
var configSchema = import_zod.z.object({
|
|
42
|
+
withMiddleware: configMiddleware.default("true"),
|
|
43
|
+
withShield: configShield.default("true"),
|
|
44
|
+
withZod: configBoolean.default("true"),
|
|
45
|
+
contextPath: import_zod.z.string().default("../../../../src/context"),
|
|
46
|
+
trpcOptionsPath: import_zod.z.string().optional(),
|
|
47
|
+
showModelNameInProcedure: configBoolean.default("true"),
|
|
48
|
+
generateModelActions: import_zod.z.string().default(Object.values(import_generator_helper.DMMF.ModelAction).join(",")).transform((arg) => {
|
|
49
|
+
return arg.split(",").map((action) => modelActionEnum.parse(action.trim()));
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
+
0 && (module.exports = {
|
|
54
|
+
configSchema
|
|
5
55
|
});
|
|
6
|
-
exports.configSchema = void 0;
|
|
7
|
-
var _generatorHelper = require("@prisma/generator-helper");
|
|
8
|
-
var _zod = require("zod");
|
|
9
|
-
const e = _zod.z.enum(["true", "false"]).transform(o => JSON.parse(o)),
|
|
10
|
-
i = _zod.z.union([e, _zod.z.string().default("../../../../src/middleware")]),
|
|
11
|
-
a = _zod.z.union([e, _zod.z.string().default("../../../../src/shield")]),
|
|
12
|
-
c = _zod.z.nativeEnum(_generatorHelper.DMMF.ModelAction);
|
|
13
|
-
const configSchema = exports.configSchema = _zod.z.object({
|
|
14
|
-
withMiddleware: i.default("true"),
|
|
15
|
-
withShield: a.default("true"),
|
|
16
|
-
withZod: e.default("true"),
|
|
17
|
-
contextPath: _zod.z.string().default("../../../../src/context"),
|
|
18
|
-
trpcOptionsPath: _zod.z.string().optional(),
|
|
19
|
-
showModelNameInProcedure: e.default("true"),
|
|
20
|
-
generateModelActions: _zod.z.string().default(Object.values(_generatorHelper.DMMF.ModelAction).join(",")).transform(o => o.split(",").map(r => c.parse(r.trim())))
|
|
21
|
-
});
|
package/dist/config.js
ADDED
package/dist/generator.cjs
CHANGED