@stryke/prisma-trpc-generator 0.2.1 → 0.2.3
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/generator.cjs +523 -0
- package/dist/generator.d.cts +1 -0
- package/dist/generator.d.ts +1 -0
- package/dist/generator.js +502 -6
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +502 -6
- package/package.json +35 -35
- package/dist/chunk-6UJ53OQ6.js +0 -19
- package/dist/chunk-GQ7ABBSV.js +0 -160
- package/dist/chunk-LVOPI6NH.js +0 -18
- package/dist/chunk-RYVIOFDO.js +0 -333
- package/dist/chunk-SHUYVCID.js +0 -6
- package/dist/chunk-V6DNCMCB.js +0 -31
- package/dist/config.cjs +0 -55
- package/dist/config.js +0 -7
- package/dist/helpers.cjs +0 -375
- package/dist/helpers.js +0 -29
- package/dist/prisma-generator.cjs +0 -523
- package/dist/prisma-generator.js +0 -10
- package/dist/project.cjs +0 -42
- package/dist/project.js +0 -7
package/dist/generator.cjs
CHANGED
|
@@ -1,2 +1,525 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// src/index.ts
|
|
28
|
+
var import_generator_helper2 = require("@prisma/generator-helper");
|
|
29
|
+
|
|
30
|
+
// src/prisma-generator.ts
|
|
31
|
+
var import_internals2 = require("@prisma/internals");
|
|
32
|
+
var import_node_fs2 = require("fs");
|
|
33
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
34
|
+
var import_pluralize = __toESM(require("pluralize"), 1);
|
|
35
|
+
var import_prisma_generator = require("prisma-trpc-shield-generator/lib/prisma-generator");
|
|
36
|
+
var import_prisma_generator2 = require("prisma-zod-generator/lib/prisma-generator");
|
|
37
|
+
|
|
38
|
+
// src/config.ts
|
|
39
|
+
var import_generator_helper = require("@prisma/generator-helper");
|
|
40
|
+
var import_zod = require("zod");
|
|
41
|
+
var configBoolean = import_zod.z.enum([
|
|
42
|
+
"true",
|
|
43
|
+
"false"
|
|
44
|
+
]).transform((arg) => JSON.parse(arg));
|
|
45
|
+
var configMiddleware = import_zod.z.union([
|
|
46
|
+
configBoolean,
|
|
47
|
+
import_zod.z.string().default("../../../../src/middleware")
|
|
48
|
+
]);
|
|
49
|
+
var configShield = import_zod.z.union([
|
|
50
|
+
configBoolean,
|
|
51
|
+
import_zod.z.string().default("../../../../src/shield")
|
|
52
|
+
]);
|
|
53
|
+
var modelActionEnum = import_zod.z.nativeEnum(import_generator_helper.DMMF.ModelAction);
|
|
54
|
+
var configSchema = import_zod.z.object({
|
|
55
|
+
withMiddleware: configMiddleware.default("true"),
|
|
56
|
+
withShield: configShield.default("true"),
|
|
57
|
+
withZod: configBoolean.default("true"),
|
|
58
|
+
contextPath: import_zod.z.string().default("../../../../src/context"),
|
|
59
|
+
trpcOptionsPath: import_zod.z.string().optional(),
|
|
60
|
+
showModelNameInProcedure: configBoolean.default("true"),
|
|
61
|
+
generateModelActions: import_zod.z.string().default(Object.values(import_generator_helper.DMMF.ModelAction).join(",")).transform((arg) => {
|
|
62
|
+
return arg.split(",").map((action) => modelActionEnum.parse(action.trim()));
|
|
63
|
+
})
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// src/helpers.ts
|
|
67
|
+
var import_internals = require("@prisma/internals");
|
|
68
|
+
|
|
69
|
+
// src/utils/getRelativePath.ts
|
|
70
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
71
|
+
function getRelativePath(outputPath, filePath, isOutsideOutputPath, schemaPath) {
|
|
72
|
+
const fromPath = import_node_path.default.join(outputPath, "routers", "helpers");
|
|
73
|
+
let toPath = import_node_path.default.join(outputPath, filePath);
|
|
74
|
+
if (isOutsideOutputPath) {
|
|
75
|
+
const schemaPathSplit = schemaPath?.split(import_node_path.default.sep);
|
|
76
|
+
const schemaPathWithoutFileAndExtension = schemaPathSplit.slice(0, schemaPathSplit.length - 1).join(import_node_path.default.posix.sep);
|
|
77
|
+
toPath = import_node_path.default.join(schemaPathWithoutFileAndExtension, filePath);
|
|
78
|
+
}
|
|
79
|
+
const newPath = import_node_path.default.relative(fromPath, toPath).split(import_node_path.default.sep).join(import_node_path.default.posix.sep);
|
|
80
|
+
return newPath;
|
|
81
|
+
}
|
|
82
|
+
__name(getRelativePath, "getRelativePath");
|
|
83
|
+
|
|
84
|
+
// src/utils/uncapitalizeFirstLetter.ts
|
|
85
|
+
var uncapitalizeFirstLetter = /* @__PURE__ */ __name((str) => {
|
|
86
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
87
|
+
}, "uncapitalizeFirstLetter");
|
|
88
|
+
|
|
89
|
+
// src/helpers.ts
|
|
90
|
+
var getProcedureName = /* @__PURE__ */ __name((config) => {
|
|
91
|
+
return config.withShield ? "shieldedProcedure" : config.withMiddleware ? "protectedProcedure" : "publicProcedure";
|
|
92
|
+
}, "getProcedureName");
|
|
93
|
+
var generateCreateRouterImport = /* @__PURE__ */ __name(({ sourceFile, config }) => {
|
|
94
|
+
const imports = [
|
|
95
|
+
"t"
|
|
96
|
+
];
|
|
97
|
+
if (config) {
|
|
98
|
+
imports.push(getProcedureName(config));
|
|
99
|
+
}
|
|
100
|
+
sourceFile.addImportDeclaration({
|
|
101
|
+
moduleSpecifier: "./helpers/createRouter",
|
|
102
|
+
namedImports: imports
|
|
103
|
+
});
|
|
104
|
+
}, "generateCreateRouterImport");
|
|
105
|
+
var generateRPCImport = /* @__PURE__ */ __name((sourceFile) => {
|
|
106
|
+
sourceFile.addImportDeclaration({
|
|
107
|
+
moduleSpecifier: "@trpc/server",
|
|
108
|
+
namespaceImport: "trpc"
|
|
109
|
+
});
|
|
110
|
+
}, "generateRPCImport");
|
|
111
|
+
var generateShieldImport = /* @__PURE__ */ __name((sourceFile, options, value) => {
|
|
112
|
+
const outputDir = (0, import_internals.parseEnvValue)(options.generator.output);
|
|
113
|
+
let shieldPath = getRelativePath(outputDir, "shield/shield");
|
|
114
|
+
if (typeof value === "string") {
|
|
115
|
+
shieldPath = getRelativePath(outputDir, value, true, options.schemaPath);
|
|
116
|
+
}
|
|
117
|
+
sourceFile.addImportDeclaration({
|
|
118
|
+
moduleSpecifier: shieldPath,
|
|
119
|
+
namedImports: [
|
|
120
|
+
"permissions"
|
|
121
|
+
]
|
|
122
|
+
});
|
|
123
|
+
}, "generateShieldImport");
|
|
124
|
+
var generateRouterImport = /* @__PURE__ */ __name((sourceFile, modelNamePlural, modelNameCamelCase) => {
|
|
125
|
+
sourceFile.addImportDeclaration({
|
|
126
|
+
moduleSpecifier: `./${modelNameCamelCase}.router`,
|
|
127
|
+
namedImports: [
|
|
128
|
+
`${modelNamePlural}Router`
|
|
129
|
+
]
|
|
130
|
+
});
|
|
131
|
+
}, "generateRouterImport");
|
|
132
|
+
function generateBaseRouter(sourceFile, config, options) {
|
|
133
|
+
const outputDir = (0, import_internals.parseEnvValue)(options.generator.output);
|
|
134
|
+
sourceFile.addStatements(
|
|
135
|
+
/* ts */
|
|
136
|
+
`
|
|
137
|
+
import type { Context } from '${getRelativePath(outputDir, config.contextPath, true, options.schemaPath)}';
|
|
138
|
+
`
|
|
139
|
+
);
|
|
140
|
+
if (config.trpcOptionsPath) {
|
|
141
|
+
sourceFile.addStatements(
|
|
142
|
+
/* ts */
|
|
143
|
+
`
|
|
144
|
+
import trpcOptions from '${getRelativePath(outputDir, config.trpcOptionsPath, true, options.schemaPath)}';
|
|
145
|
+
`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
sourceFile.addStatements(
|
|
149
|
+
/* ts */
|
|
150
|
+
`
|
|
151
|
+
export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptionsPath ? "trpcOptions" : ""});
|
|
152
|
+
`
|
|
153
|
+
);
|
|
154
|
+
const middlewares = [];
|
|
155
|
+
if (config.withMiddleware && typeof config.withMiddleware === "boolean") {
|
|
156
|
+
sourceFile.addStatements(
|
|
157
|
+
/* ts */
|
|
158
|
+
`
|
|
159
|
+
export const globalMiddleware = t.middleware(async ({ ctx, next }) => {
|
|
160
|
+
console.log('inside middleware!')
|
|
161
|
+
return next()
|
|
162
|
+
});`
|
|
163
|
+
);
|
|
164
|
+
middlewares.push({
|
|
165
|
+
type: "global",
|
|
166
|
+
value: (
|
|
167
|
+
/* ts */
|
|
168
|
+
`.use(globalMiddleware)`
|
|
169
|
+
)
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (config.withMiddleware && typeof config.withMiddleware === "string") {
|
|
173
|
+
sourceFile.addStatements(
|
|
174
|
+
/* ts */
|
|
175
|
+
`
|
|
176
|
+
import defaultMiddleware from '${getRelativePath(outputDir, config.withMiddleware, true, options.schemaPath)}';
|
|
177
|
+
`
|
|
178
|
+
);
|
|
179
|
+
sourceFile.addStatements(
|
|
180
|
+
/* ts */
|
|
181
|
+
`
|
|
182
|
+
export const globalMiddleware = t.middleware(defaultMiddleware);`
|
|
183
|
+
);
|
|
184
|
+
middlewares.push({
|
|
185
|
+
type: "global",
|
|
186
|
+
value: (
|
|
187
|
+
/* ts */
|
|
188
|
+
`.use(globalMiddleware)`
|
|
189
|
+
)
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
if (config.withShield) {
|
|
193
|
+
sourceFile.addStatements(
|
|
194
|
+
/* ts */
|
|
195
|
+
`
|
|
196
|
+
export const permissionsMiddleware = t.middleware(permissions); `
|
|
197
|
+
);
|
|
198
|
+
middlewares.push({
|
|
199
|
+
type: "shield",
|
|
200
|
+
value: (
|
|
201
|
+
/* ts */
|
|
202
|
+
`
|
|
203
|
+
.use(permissions)`
|
|
204
|
+
)
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
sourceFile.addStatements(
|
|
208
|
+
/* ts */
|
|
209
|
+
`
|
|
210
|
+
export const publicProcedure = t.procedure; `
|
|
211
|
+
);
|
|
212
|
+
if (middlewares.length > 0) {
|
|
213
|
+
const procName = getProcedureName(config);
|
|
214
|
+
middlewares.forEach((middleware, i) => {
|
|
215
|
+
if (i === 0) {
|
|
216
|
+
sourceFile.addStatements(
|
|
217
|
+
/* ts */
|
|
218
|
+
`
|
|
219
|
+
export const ${procName} = t.procedure
|
|
220
|
+
`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
sourceFile.addStatements(
|
|
224
|
+
/* ts */
|
|
225
|
+
`
|
|
226
|
+
.use(${middleware.type === "shield" ? "permissionsMiddleware" : "globalMiddleware"})
|
|
227
|
+
`
|
|
228
|
+
);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
__name(generateBaseRouter, "generateBaseRouter");
|
|
233
|
+
function generateProcedure(sourceFile, name, typeName, modelName, opType, baseOpType, config) {
|
|
234
|
+
let input = `input${!config.withZod ? " as any" : ""}`;
|
|
235
|
+
const nameWithoutModel = name.replace(modelName, "");
|
|
236
|
+
if (nameWithoutModel === "groupBy" && config.withZod) {
|
|
237
|
+
input = "{ where: input.where, orderBy: input.orderBy, by: input.by, having: input.having, take: input.take, skip: input.skip }";
|
|
238
|
+
}
|
|
239
|
+
sourceFile.addStatements(
|
|
240
|
+
/* ts */
|
|
241
|
+
`${config.showModelNameInProcedure ? name : nameWithoutModel}: ${getProcedureName(config)}
|
|
242
|
+
${config.withZod ? `.input(${typeName})` : ""}.${getProcedureTypeByOpName(baseOpType)}(async ({ ctx, input }) => {
|
|
243
|
+
const ${name} = await ctx.prisma.${uncapitalizeFirstLetter(modelName)}.${opType.replace("One", "")}(${input});
|
|
244
|
+
return ${name};
|
|
245
|
+
}),`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
__name(generateProcedure, "generateProcedure");
|
|
249
|
+
function generateRouterSchemaImports(sourceFile, modelName, modelActions) {
|
|
250
|
+
sourceFile.addStatements(
|
|
251
|
+
/* ts */
|
|
252
|
+
[
|
|
253
|
+
// remove any duplicate import statements
|
|
254
|
+
...new Set(modelActions.map((opName) => getRouterSchemaImportByOpName(opName, modelName)))
|
|
255
|
+
].join("\n")
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
__name(generateRouterSchemaImports, "generateRouterSchemaImports");
|
|
259
|
+
var getRouterSchemaImportByOpName = /* @__PURE__ */ __name((opName, modelName) => {
|
|
260
|
+
const opType = opName.replace("OrThrow", "");
|
|
261
|
+
const inputType = getInputTypeByOpName(opType, modelName);
|
|
262
|
+
return inputType ? `import { ${inputType} } from "../schemas/${opType}${modelName}.schema"; ` : "";
|
|
263
|
+
}, "getRouterSchemaImportByOpName");
|
|
264
|
+
var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
|
|
265
|
+
let inputType;
|
|
266
|
+
switch (opName) {
|
|
267
|
+
case "findUnique":
|
|
268
|
+
inputType = `${modelName}FindUniqueSchema`;
|
|
269
|
+
break;
|
|
270
|
+
case "findFirst":
|
|
271
|
+
inputType = `${modelName}FindFirstSchema`;
|
|
272
|
+
break;
|
|
273
|
+
case "findMany":
|
|
274
|
+
inputType = `${modelName}FindManySchema`;
|
|
275
|
+
break;
|
|
276
|
+
case "findRaw":
|
|
277
|
+
inputType = `${modelName}FindRawObjectSchema`;
|
|
278
|
+
break;
|
|
279
|
+
case "createOne":
|
|
280
|
+
inputType = `${modelName}CreateOneSchema`;
|
|
281
|
+
break;
|
|
282
|
+
case "createMany":
|
|
283
|
+
inputType = `${modelName}CreateManySchema`;
|
|
284
|
+
break;
|
|
285
|
+
case "deleteOne":
|
|
286
|
+
inputType = `${modelName}DeleteOneSchema`;
|
|
287
|
+
break;
|
|
288
|
+
case "updateOne":
|
|
289
|
+
inputType = `${modelName}UpdateOneSchema`;
|
|
290
|
+
break;
|
|
291
|
+
case "deleteMany":
|
|
292
|
+
inputType = `${modelName}DeleteManySchema`;
|
|
293
|
+
break;
|
|
294
|
+
case "updateMany":
|
|
295
|
+
inputType = `${modelName}UpdateManySchema`;
|
|
296
|
+
break;
|
|
297
|
+
case "upsertOne":
|
|
298
|
+
inputType = `${modelName}UpsertSchema`;
|
|
299
|
+
break;
|
|
300
|
+
case "aggregate":
|
|
301
|
+
inputType = `${modelName}AggregateSchema`;
|
|
302
|
+
break;
|
|
303
|
+
case "aggregateRaw":
|
|
304
|
+
inputType = `${modelName}AggregateRawObjectSchema`;
|
|
305
|
+
break;
|
|
306
|
+
case "groupBy":
|
|
307
|
+
inputType = `${modelName}GroupBySchema`;
|
|
308
|
+
break;
|
|
309
|
+
default:
|
|
310
|
+
console.log("getInputTypeByOpName: ", {
|
|
311
|
+
opName,
|
|
312
|
+
modelName
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
return inputType;
|
|
316
|
+
}, "getInputTypeByOpName");
|
|
317
|
+
var getProcedureTypeByOpName = /* @__PURE__ */ __name((opName) => {
|
|
318
|
+
let procType;
|
|
319
|
+
switch (opName) {
|
|
320
|
+
case "findUnique":
|
|
321
|
+
case "findFirst":
|
|
322
|
+
case "findMany":
|
|
323
|
+
case "findRaw":
|
|
324
|
+
case "aggregate":
|
|
325
|
+
case "aggregateRaw":
|
|
326
|
+
case "groupBy":
|
|
327
|
+
procType = "query";
|
|
328
|
+
break;
|
|
329
|
+
case "createOne":
|
|
330
|
+
case "createMany":
|
|
331
|
+
case "deleteOne":
|
|
332
|
+
case "updateOne":
|
|
333
|
+
case "deleteMany":
|
|
334
|
+
case "updateMany":
|
|
335
|
+
case "upsertOne":
|
|
336
|
+
procType = "mutation";
|
|
337
|
+
break;
|
|
338
|
+
default:
|
|
339
|
+
console.log("getProcedureTypeByOpName: ", {
|
|
340
|
+
opName
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return procType;
|
|
344
|
+
}, "getProcedureTypeByOpName");
|
|
345
|
+
function resolveModelsComments(models, hiddenModels) {
|
|
346
|
+
const modelAttributeRegex = /(?:@@Gen\.)+[A-z]+\(.+\)/;
|
|
347
|
+
const attributeNameRegex = /\.+[A-Z]+\(+/i;
|
|
348
|
+
const attributeArgsRegex = /\(+[A-Z]+:.+\)/i;
|
|
349
|
+
for (const model of models) {
|
|
350
|
+
if (model.documentation) {
|
|
351
|
+
const attribute = model.documentation?.match(modelAttributeRegex)?.[0];
|
|
352
|
+
const attributeName = attribute?.match(attributeNameRegex)?.[0]?.slice(1, -1);
|
|
353
|
+
if (attributeName !== "model") continue;
|
|
354
|
+
const rawAttributeArgs = attribute?.match(attributeArgsRegex)?.[0]?.slice(1, -1);
|
|
355
|
+
const parsedAttributeArgs = {};
|
|
356
|
+
if (rawAttributeArgs) {
|
|
357
|
+
const rawAttributeArgsParts = rawAttributeArgs.split(":").map((it) => it.trim()).map((part) => part.startsWith("[") ? part : part.split(",")).flat().map((it) => it.trim());
|
|
358
|
+
for (let i = 0; i < rawAttributeArgsParts.length; i += 2) {
|
|
359
|
+
const key = rawAttributeArgsParts[i];
|
|
360
|
+
const value = rawAttributeArgsParts[i + 1];
|
|
361
|
+
parsedAttributeArgs[key] = JSON.parse(value);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (parsedAttributeArgs.hide) {
|
|
365
|
+
hiddenModels.push(model.name);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
__name(resolveModelsComments, "resolveModelsComments");
|
|
371
|
+
|
|
372
|
+
// src/project.ts
|
|
373
|
+
var import_ts_morph = require("ts-morph");
|
|
374
|
+
var compilerOptions = {
|
|
375
|
+
target: import_ts_morph.ScriptTarget.ES2019,
|
|
376
|
+
module: import_ts_morph.ModuleKind.CommonJS,
|
|
377
|
+
emitDecoratorMetadata: true,
|
|
378
|
+
experimentalDecorators: true,
|
|
379
|
+
esModuleInterop: true
|
|
380
|
+
};
|
|
381
|
+
var project = new import_ts_morph.Project({
|
|
382
|
+
compilerOptions: {
|
|
383
|
+
...compilerOptions
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// src/utils/removeDir.ts
|
|
388
|
+
var import_node_fs = require("fs");
|
|
389
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
390
|
+
async function removeDir(dirPath, onlyContent) {
|
|
391
|
+
const dirEntries = await import_node_fs.promises.readdir(dirPath, {
|
|
392
|
+
withFileTypes: true
|
|
393
|
+
});
|
|
394
|
+
await Promise.all(dirEntries.map(async (dirEntry) => {
|
|
395
|
+
const fullPath = import_node_path2.default.join(dirPath, dirEntry.name);
|
|
396
|
+
return dirEntry.isDirectory() ? removeDir(fullPath, false) : import_node_fs.promises.unlink(fullPath);
|
|
397
|
+
}));
|
|
398
|
+
if (!onlyContent) {
|
|
399
|
+
await import_node_fs.promises.rmdir(dirPath);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
__name(removeDir, "removeDir");
|
|
403
|
+
|
|
404
|
+
// src/prisma-generator.ts
|
|
405
|
+
async function generate(options) {
|
|
406
|
+
const outputDir = (0, import_internals2.parseEnvValue)(options.generator.output);
|
|
407
|
+
const results = configSchema.safeParse(options.generator.config);
|
|
408
|
+
if (!results.success) throw new Error("Invalid options passed");
|
|
409
|
+
const config = results.data;
|
|
410
|
+
await import_node_fs2.promises.mkdir(outputDir, {
|
|
411
|
+
recursive: true
|
|
412
|
+
});
|
|
413
|
+
await removeDir(outputDir, true);
|
|
414
|
+
if (config.withZod) {
|
|
415
|
+
await (0, import_prisma_generator2.generate)(options);
|
|
416
|
+
}
|
|
417
|
+
if (config.withShield === true) {
|
|
418
|
+
const shieldOutputPath = import_node_path3.default.join(outputDir, "./shield");
|
|
419
|
+
await (0, import_prisma_generator.generate)({
|
|
420
|
+
...options,
|
|
421
|
+
generator: {
|
|
422
|
+
...options.generator,
|
|
423
|
+
output: {
|
|
424
|
+
...options.generator.output,
|
|
425
|
+
value: shieldOutputPath
|
|
426
|
+
},
|
|
427
|
+
config: {
|
|
428
|
+
...options.generator.config,
|
|
429
|
+
contextPath: config.contextPath
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
const prismaClientProvider = options.otherGenerators.find((it) => (0, import_internals2.parseEnvValue)(it.provider) === "prisma-client-js");
|
|
435
|
+
const prismaClientDmmf = await (0, import_internals2.getDMMF)({
|
|
436
|
+
datamodel: options.datamodel,
|
|
437
|
+
previewFeatures: prismaClientProvider?.previewFeatures
|
|
438
|
+
});
|
|
439
|
+
const modelOperations = prismaClientDmmf.mappings.modelOperations;
|
|
440
|
+
const models = prismaClientDmmf.datamodel.models;
|
|
441
|
+
const hiddenModels = [];
|
|
442
|
+
resolveModelsComments(models, hiddenModels);
|
|
443
|
+
const createRouter = project.createSourceFile(import_node_path3.default.resolve(outputDir, "routers", "helpers", "createRouter.ts"), void 0, {
|
|
444
|
+
overwrite: true
|
|
445
|
+
});
|
|
446
|
+
generateRPCImport(createRouter);
|
|
447
|
+
if (config.withShield) {
|
|
448
|
+
generateShieldImport(createRouter, options, config.withShield);
|
|
449
|
+
}
|
|
450
|
+
generateBaseRouter(createRouter, config, options);
|
|
451
|
+
createRouter.formatText({
|
|
452
|
+
indentSize: 2
|
|
453
|
+
});
|
|
454
|
+
const appRouter = project.createSourceFile(import_node_path3.default.resolve(outputDir, "routers", `index.ts`), void 0, {
|
|
455
|
+
overwrite: true
|
|
456
|
+
});
|
|
457
|
+
generateCreateRouterImport({
|
|
458
|
+
sourceFile: appRouter
|
|
459
|
+
});
|
|
460
|
+
const routerStatements = [];
|
|
461
|
+
for (const modelOperation of modelOperations) {
|
|
462
|
+
const { model, ...operations } = modelOperation;
|
|
463
|
+
if (hiddenModels.includes(model)) continue;
|
|
464
|
+
const modelActions = Object.keys(operations).filter((opType) => config.generateModelActions.includes(opType.replace("One", "")));
|
|
465
|
+
if (!modelActions.length) continue;
|
|
466
|
+
const plural = (0, import_pluralize.default)(model.toLowerCase());
|
|
467
|
+
generateRouterImport(appRouter, plural, model);
|
|
468
|
+
const modelRouter = project.createSourceFile(import_node_path3.default.resolve(outputDir, "routers", `${model}.router.ts`), void 0, {
|
|
469
|
+
overwrite: true
|
|
470
|
+
});
|
|
471
|
+
generateCreateRouterImport({
|
|
472
|
+
sourceFile: modelRouter,
|
|
473
|
+
config
|
|
474
|
+
});
|
|
475
|
+
if (config.withZod) {
|
|
476
|
+
generateRouterSchemaImports(modelRouter, model, modelActions);
|
|
477
|
+
}
|
|
478
|
+
modelRouter.addStatements(
|
|
479
|
+
/* ts */
|
|
480
|
+
`
|
|
481
|
+
export const ${plural}Router = t.router({`
|
|
482
|
+
);
|
|
483
|
+
for (const opType of modelActions) {
|
|
484
|
+
const opNameWithModel = operations[opType];
|
|
485
|
+
const baseOpType = opType.replace("OrThrow", "");
|
|
486
|
+
generateProcedure(modelRouter, opNameWithModel, getInputTypeByOpName(baseOpType, model), model, opType, baseOpType, config);
|
|
487
|
+
}
|
|
488
|
+
modelRouter.addStatements(
|
|
489
|
+
/* ts */
|
|
490
|
+
`
|
|
491
|
+
})`
|
|
492
|
+
);
|
|
493
|
+
modelRouter.formatText({
|
|
494
|
+
indentSize: 2
|
|
495
|
+
});
|
|
496
|
+
routerStatements.push(
|
|
497
|
+
/* ts */
|
|
498
|
+
`
|
|
499
|
+
${model.toLowerCase()}: ${plural}Router`
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
appRouter.addStatements(
|
|
503
|
+
/* ts */
|
|
504
|
+
`
|
|
505
|
+
export const appRouter = t.router({${routerStatements.join()}})
|
|
506
|
+
`
|
|
507
|
+
);
|
|
508
|
+
appRouter.formatText({
|
|
509
|
+
indentSize: 2
|
|
510
|
+
});
|
|
511
|
+
await project.save();
|
|
512
|
+
}
|
|
513
|
+
__name(generate, "generate");
|
|
514
|
+
|
|
515
|
+
// src/index.ts
|
|
516
|
+
(0, import_generator_helper2.generatorHandler)({
|
|
517
|
+
onManifest: /* @__PURE__ */ __name(() => ({
|
|
518
|
+
defaultOutput: "./generated",
|
|
519
|
+
prettyName: "Prisma tRPC Generator",
|
|
520
|
+
requiresGenerators: [
|
|
521
|
+
"prisma-client-js"
|
|
522
|
+
]
|
|
523
|
+
}), "onManifest"),
|
|
524
|
+
onGenerate: generate
|
|
525
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#! /usr/bin/env node
|