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