@pegasusheavy/nestjs-prisma-graphql 1.2.2 → 1.3.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/LICENSE +1 -0
- package/dist/generate.d.ts +41 -40
- package/dist/generate.js +727 -538
- package/dist/generate.js.map +1 -1
- package/dist/index.d.ts +1 -17
- package/dist/index.js +827 -546
- package/dist/index.js.map +1 -1
- package/package.json +33 -33
package/dist/index.js
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ok } from 'assert';
|
|
1
|
+
import { existsSync, readdirSync, rmdirSync as rmdirSync$1 } from 'fs';
|
|
3
2
|
import { createRequire } from 'module';
|
|
3
|
+
import { arch, platform } from 'os';
|
|
4
|
+
import generatorHelper from '@prisma/generator-helper';
|
|
4
5
|
import { memoize, mapKeys, merge, trim, castArray, uniqWith, isEqual, countBy, startCase, camelCase, keyBy, remove, partition, kebabCase, isObject, omit, cloneDeep } from 'lodash-es';
|
|
5
6
|
import { Project, QuoteKind, StructureKind } from 'ts-morph';
|
|
7
|
+
import { ok as ok$1 } from 'assert';
|
|
6
8
|
import JSON52 from 'json5';
|
|
7
9
|
import pupa from 'pupa';
|
|
8
10
|
import getRelativePath from 'get-relative-path';
|
|
9
11
|
import outmatch2 from 'outmatch';
|
|
10
|
-
import { existsSync, rmdirSync } from 'fs';
|
|
11
12
|
import { unflatten } from 'flat';
|
|
12
13
|
import filenamify from 'filenamify';
|
|
13
14
|
import pluralize from 'pluralize';
|
|
14
15
|
|
|
15
16
|
// src/index.ts
|
|
17
|
+
function ok(value, message) {
|
|
18
|
+
ok$1(
|
|
19
|
+
value,
|
|
20
|
+
message
|
|
21
|
+
);
|
|
22
|
+
}
|
|
16
23
|
|
|
17
24
|
// src/helpers/is-many-and-return.ts
|
|
18
25
|
function isManyAndReturnOutputType(name) {
|
|
@@ -31,8 +38,10 @@ function argsType(field, args) {
|
|
|
31
38
|
if (["queryRaw", "executeRaw"].includes(field.name)) {
|
|
32
39
|
return;
|
|
33
40
|
}
|
|
34
|
-
if (isManyAndReturnOutputType(field.name))
|
|
35
|
-
|
|
41
|
+
if (isManyAndReturnOutputType(field.name)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const { eventEmitter, getModelName: getModelName2, typeNames } = args;
|
|
36
45
|
let className = pascalCase(`${field.name}Args`);
|
|
37
46
|
const modelName = getModelName2(className) ?? "";
|
|
38
47
|
switch (className) {
|
|
@@ -47,8 +56,8 @@ function argsType(field, args) {
|
|
|
47
56
|
}
|
|
48
57
|
const inputType2 = {
|
|
49
58
|
constraints: { maxNumFields: null, minNumFields: null },
|
|
50
|
-
|
|
51
|
-
|
|
59
|
+
fields: [...field.args],
|
|
60
|
+
name: className
|
|
52
61
|
};
|
|
53
62
|
if (!field.args.some((x) => x.name === "_count") && [`${modelName}AggregateArgs`, `${modelName}GroupByArgs`].includes(className)) {
|
|
54
63
|
const names = ["Count", "Avg", "Sum", "Min", "Max"];
|
|
@@ -63,24 +72,24 @@ function argsType(field, args) {
|
|
|
63
72
|
continue;
|
|
64
73
|
}
|
|
65
74
|
inputType2.fields.push({
|
|
66
|
-
name: `_${name.toLowerCase()}`,
|
|
67
|
-
isRequired: false,
|
|
68
|
-
isNullable: true,
|
|
69
75
|
inputTypes: [
|
|
70
76
|
{
|
|
77
|
+
isList: false,
|
|
71
78
|
location: "inputObjectTypes",
|
|
72
|
-
type: `${modelName}${name}AggregateInput
|
|
73
|
-
isList: false
|
|
79
|
+
type: `${modelName}${name}AggregateInput`
|
|
74
80
|
}
|
|
75
|
-
]
|
|
81
|
+
],
|
|
82
|
+
isNullable: true,
|
|
83
|
+
isRequired: false,
|
|
84
|
+
name: `_${name.toLowerCase()}`
|
|
76
85
|
});
|
|
77
86
|
}
|
|
78
87
|
}
|
|
79
88
|
eventEmitter.emitSync("InputType", {
|
|
80
89
|
...args,
|
|
81
|
-
|
|
90
|
+
classDecoratorName: "ArgsType",
|
|
82
91
|
fileType: "args",
|
|
83
|
-
|
|
92
|
+
inputType: inputType2
|
|
84
93
|
});
|
|
85
94
|
}
|
|
86
95
|
|
|
@@ -105,7 +114,7 @@ function beforeGenerateField(field) {
|
|
|
105
114
|
if (fieldInput.location !== "inputObjectTypes") {
|
|
106
115
|
continue;
|
|
107
116
|
}
|
|
108
|
-
const fieldInputType =
|
|
117
|
+
const fieldInputType = fieldInput.type;
|
|
109
118
|
if (isContainBogus(fieldInputType)) {
|
|
110
119
|
fieldInput.type = replaceBogus(fieldInputType);
|
|
111
120
|
}
|
|
@@ -196,32 +205,98 @@ function createAggregateInput(args) {
|
|
|
196
205
|
const className = `${outputType2.name}Input`;
|
|
197
206
|
const inputType2 = {
|
|
198
207
|
constraints: { maxNumFields: null, minNumFields: null },
|
|
199
|
-
name: className,
|
|
200
208
|
fields: outputType2.fields.map((x) => ({
|
|
201
|
-
name: x.name,
|
|
202
|
-
isNullable: x.isNullable ?? true,
|
|
203
|
-
isRequired: false,
|
|
204
209
|
inputTypes: [
|
|
205
210
|
{
|
|
206
211
|
isList: false,
|
|
207
|
-
|
|
208
|
-
|
|
212
|
+
location: "scalar",
|
|
213
|
+
type: "true"
|
|
209
214
|
}
|
|
210
|
-
]
|
|
211
|
-
|
|
215
|
+
],
|
|
216
|
+
isNullable: x.isNullable ?? true,
|
|
217
|
+
isRequired: false,
|
|
218
|
+
name: x.name
|
|
219
|
+
})),
|
|
220
|
+
name: className
|
|
212
221
|
};
|
|
213
222
|
eventEmitter.emitSync("InputType", {
|
|
214
223
|
...args,
|
|
215
|
-
|
|
224
|
+
classDecoratorName: "InputType",
|
|
216
225
|
fileType: "input",
|
|
217
|
-
|
|
226
|
+
inputType: inputType2
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// src/handlers/decimal-helpers.ts
|
|
231
|
+
function generateDecimalHelpers(args) {
|
|
232
|
+
const { output, project } = args;
|
|
233
|
+
const rootDirectory = project.getDirectory(output) ?? project.createDirectory(output);
|
|
234
|
+
const sourceFile = rootDirectory.createSourceFile("decimal-helpers.ts", void 0, {
|
|
235
|
+
overwrite: true
|
|
236
|
+
});
|
|
237
|
+
const helpersCode = `/**
|
|
238
|
+
* Decimal Helpers for Prisma GraphQL
|
|
239
|
+
*
|
|
240
|
+
* This module provides utilities for transforming Decimal values
|
|
241
|
+
* between GraphQL and Prisma, compatible with Prisma 7+.
|
|
242
|
+
*
|
|
243
|
+
* These functions replace the prisma-graphql-type-decimal package
|
|
244
|
+
* which is incompatible with Prisma 7's new client structure.
|
|
245
|
+
*/
|
|
246
|
+
|
|
247
|
+
import Decimal from 'decimal.js';
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Reconstruct a Decimal from a serialized object.
|
|
251
|
+
* Prisma serializes Decimals as objects with d, e, s properties.
|
|
252
|
+
*/
|
|
253
|
+
function createDecimalFromObject(object: { d: number[]; e: number; s: number }): Decimal {
|
|
254
|
+
return Object.create(Decimal.prototype, {
|
|
255
|
+
d: { value: object.d },
|
|
256
|
+
e: { value: object.e },
|
|
257
|
+
s: { value: object.s },
|
|
218
258
|
});
|
|
219
259
|
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Transform input values to Decimal instances.
|
|
263
|
+
* Used as a class-transformer Transform decorator argument.
|
|
264
|
+
*
|
|
265
|
+
* @param params - The transform parameters from class-transformer
|
|
266
|
+
* @returns The transformed Decimal or array of Decimals
|
|
267
|
+
*/
|
|
268
|
+
export function transformToDecimal({ value }: { value: unknown }): Decimal | Decimal[] | null | undefined {
|
|
269
|
+
if (value == null) return value as null | undefined;
|
|
270
|
+
|
|
271
|
+
if (Array.isArray(value)) {
|
|
272
|
+
return value.map((v) => {
|
|
273
|
+
if (v instanceof Decimal) return v;
|
|
274
|
+
if (typeof v === 'object' && v !== null && 'd' in v && 'e' in v && 's' in v) {
|
|
275
|
+
return createDecimalFromObject(v as { d: number[]; e: number; s: number });
|
|
276
|
+
}
|
|
277
|
+
return new Decimal(v as string | number);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (value instanceof Decimal) return value;
|
|
282
|
+
if (typeof value === 'object' && value !== null && 'd' in value && 'e' in value && 's' in value) {
|
|
283
|
+
return createDecimalFromObject(value as { d: number[]; e: number; s: number });
|
|
284
|
+
}
|
|
285
|
+
return new Decimal(value as string | number);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Re-export Decimal class for convenience
|
|
290
|
+
*/
|
|
291
|
+
export { Decimal };
|
|
292
|
+
`;
|
|
293
|
+
sourceFile.addStatements(helpersCode);
|
|
294
|
+
}
|
|
220
295
|
function emitSingle(emitter) {
|
|
221
296
|
emitter.on("ClassProperty", classProperty);
|
|
222
297
|
}
|
|
223
298
|
function classProperty(property, eventArguments) {
|
|
224
|
-
const {
|
|
299
|
+
const { isList, location, propertyType } = eventArguments;
|
|
225
300
|
if (["inputObjectTypes", "outputObjectTypes"].includes(location) && !isList) {
|
|
226
301
|
const [safeTypes, instanceofTypes] = partition(
|
|
227
302
|
propertyType,
|
|
@@ -234,7 +309,7 @@ function classProperty(property, eventArguments) {
|
|
|
234
309
|
var ImportDeclarationMap = class extends Map {
|
|
235
310
|
add(name, value, isTypeOnly) {
|
|
236
311
|
if (!this.has(name)) {
|
|
237
|
-
const structure = typeof value === "string" ? { moduleSpecifier: value, namedImports: [{ name }]
|
|
312
|
+
const structure = typeof value === "string" ? { isTypeOnly, moduleSpecifier: value, namedImports: [{ name }] } : value;
|
|
238
313
|
this.set(name, structure);
|
|
239
314
|
}
|
|
240
315
|
}
|
|
@@ -245,43 +320,46 @@ var ImportDeclarationMap = class extends Map {
|
|
|
245
320
|
const typeOnlyKey = `type:${name}`;
|
|
246
321
|
if (!this.has(typeOnlyKey) && !this.has(name)) {
|
|
247
322
|
this.set(typeOnlyKey, {
|
|
323
|
+
isTypeOnly: true,
|
|
248
324
|
moduleSpecifier,
|
|
249
|
-
namedImports: [{ name }]
|
|
250
|
-
isTypeOnly: true
|
|
325
|
+
namedImports: [{ name }]
|
|
251
326
|
});
|
|
252
327
|
}
|
|
253
328
|
}
|
|
254
329
|
create(args) {
|
|
255
|
-
const {
|
|
256
|
-
let name = args
|
|
330
|
+
const { defaultImport, from, isTypeOnly, namedImport, namespaceImport } = args;
|
|
331
|
+
let { name } = args;
|
|
257
332
|
const value = {
|
|
333
|
+
defaultImport: void 0,
|
|
334
|
+
isTypeOnly,
|
|
258
335
|
moduleSpecifier: from,
|
|
259
336
|
namedImports: [],
|
|
260
|
-
|
|
261
|
-
namespaceImport: void 0,
|
|
262
|
-
isTypeOnly
|
|
337
|
+
namespaceImport: void 0
|
|
263
338
|
};
|
|
264
|
-
if (namedImport === true && namespaceImport) {
|
|
339
|
+
if (namedImport === true && namespaceImport !== void 0) {
|
|
265
340
|
value.namedImports = [{ name: namespaceImport }];
|
|
266
341
|
name = namespaceImport;
|
|
267
|
-
} else if (defaultImport) {
|
|
342
|
+
} else if (defaultImport === void 0) {
|
|
343
|
+
if (namespaceImport === void 0) {
|
|
344
|
+
value.namedImports = [{ name }];
|
|
345
|
+
} else {
|
|
346
|
+
value.namespaceImport = namespaceImport;
|
|
347
|
+
name = namespaceImport;
|
|
348
|
+
}
|
|
349
|
+
} else {
|
|
268
350
|
value.defaultImport = defaultImport === true ? name : defaultImport;
|
|
269
351
|
name = value.defaultImport;
|
|
270
|
-
} else if (namespaceImport) {
|
|
271
|
-
value.namespaceImport = namespaceImport;
|
|
272
|
-
name = namespaceImport;
|
|
273
|
-
} else {
|
|
274
|
-
value.namedImports = [{ name }];
|
|
275
352
|
}
|
|
276
|
-
const key = isTypeOnly ? `type:${name}` : name;
|
|
277
|
-
if (
|
|
278
|
-
|
|
353
|
+
const key = isTypeOnly === true ? `type:${name}` : name;
|
|
354
|
+
if (this.has(key)) {
|
|
355
|
+
return;
|
|
279
356
|
}
|
|
357
|
+
this.set(key, value);
|
|
280
358
|
}
|
|
281
359
|
*toStatements() {
|
|
282
360
|
const iterator = this.values();
|
|
283
361
|
let result = iterator.next();
|
|
284
|
-
while (result.value) {
|
|
362
|
+
while (result.value !== void 0) {
|
|
285
363
|
yield {
|
|
286
364
|
...result.value,
|
|
287
365
|
kind: StructureKind.ImportDeclaration
|
|
@@ -312,8 +390,8 @@ async function generateFiles(args) {
|
|
|
312
390
|
for (const decorator of property.decorators ?? []) {
|
|
313
391
|
const fullName = classDeclaration?.getProperty(property.name)?.getDecorator(decorator.name)?.getFullName();
|
|
314
392
|
ok(
|
|
315
|
-
fullName,
|
|
316
|
-
`Cannot get full name of decorator of class ${statement.name}`
|
|
393
|
+
fullName !== void 0,
|
|
394
|
+
`Cannot get full name of decorator of class ${String(statement.name)}`
|
|
317
395
|
);
|
|
318
396
|
decorator.name = fullName;
|
|
319
397
|
}
|
|
@@ -376,6 +454,20 @@ async function generateFiles(args) {
|
|
|
376
454
|
statements: [...imports.toStatements(), ...enums, ...classes]
|
|
377
455
|
});
|
|
378
456
|
}
|
|
457
|
+
const sourceFiles = project.getSourceFiles();
|
|
458
|
+
const sourceFileCount = sourceFiles.length;
|
|
459
|
+
const globalConsole3 = globalThis.console;
|
|
460
|
+
const logMessage = (msg) => {
|
|
461
|
+
globalConsole3.log(msg);
|
|
462
|
+
};
|
|
463
|
+
logMessage(
|
|
464
|
+
`nestjs-prisma-graphql: saving ${String(sourceFileCount)} source files to ${output}`
|
|
465
|
+
);
|
|
466
|
+
if (sourceFileCount === 0) {
|
|
467
|
+
throw new Error(
|
|
468
|
+
"nestjs-prisma-graphql: project has 0 source files \u2014 nothing to write"
|
|
469
|
+
);
|
|
470
|
+
}
|
|
379
471
|
if (config.emitCompiled) {
|
|
380
472
|
project.compilerOptions.set({
|
|
381
473
|
declaration: true,
|
|
@@ -391,7 +483,21 @@ async function generateFiles(args) {
|
|
|
391
483
|
eventEmitter.emitSync("Warning", errors);
|
|
392
484
|
}
|
|
393
485
|
} else {
|
|
394
|
-
|
|
486
|
+
const BATCH_SIZE = 100;
|
|
487
|
+
if (sourceFileCount > BATCH_SIZE) {
|
|
488
|
+
logMessage(
|
|
489
|
+
`nestjs-prisma-graphql: using batched save for large schema (${String(sourceFileCount)} files)`
|
|
490
|
+
);
|
|
491
|
+
for (let i = 0; i < sourceFileCount; i += BATCH_SIZE) {
|
|
492
|
+
const batch = sourceFiles.slice(i, i + BATCH_SIZE);
|
|
493
|
+
logMessage(
|
|
494
|
+
`nestjs-prisma-graphql: saving batch ${String(Math.floor(i / BATCH_SIZE) + 1)}/${String(Math.ceil(sourceFileCount / BATCH_SIZE))} (${String(batch.length)} files)`
|
|
495
|
+
);
|
|
496
|
+
await Promise.all(batch.map(async (sf) => await sf.save()));
|
|
497
|
+
}
|
|
498
|
+
} else {
|
|
499
|
+
await project.save();
|
|
500
|
+
}
|
|
395
501
|
}
|
|
396
502
|
}
|
|
397
503
|
|
|
@@ -407,17 +513,23 @@ function fileTypeByLocation(fieldLocation) {
|
|
|
407
513
|
case "enumTypes": {
|
|
408
514
|
return "enum";
|
|
409
515
|
}
|
|
516
|
+
case "fieldRefTypes":
|
|
517
|
+
case "scalar":
|
|
518
|
+
default: {
|
|
519
|
+
return "object";
|
|
520
|
+
}
|
|
410
521
|
}
|
|
411
|
-
return "object";
|
|
412
522
|
}
|
|
413
523
|
function relativePath(from, to) {
|
|
414
|
-
|
|
415
|
-
|
|
524
|
+
let fromPath = from;
|
|
525
|
+
let toPath = to;
|
|
526
|
+
if (!fromPath.startsWith("/")) {
|
|
527
|
+
fromPath = `/${fromPath}`;
|
|
416
528
|
}
|
|
417
|
-
if (!
|
|
418
|
-
|
|
529
|
+
if (!toPath.startsWith("/")) {
|
|
530
|
+
toPath = `/${toPath}`;
|
|
419
531
|
}
|
|
420
|
-
let result = getRelativePath(
|
|
532
|
+
let result = getRelativePath(fromPath, toPath);
|
|
421
533
|
if (!result.startsWith(".")) {
|
|
422
534
|
result = `./${result}`;
|
|
423
535
|
}
|
|
@@ -486,7 +598,7 @@ function getGraphqlImport(args) {
|
|
|
486
598
|
}
|
|
487
599
|
function getGraphqlInputType(inputTypes, pattern) {
|
|
488
600
|
let result;
|
|
489
|
-
inputTypes = inputTypes.filter((t) => !["null", "Null"].includes(
|
|
601
|
+
inputTypes = inputTypes.filter((t) => !["null", "Null"].includes(t.type));
|
|
490
602
|
inputTypes = uniqWith(inputTypes, isEqual);
|
|
491
603
|
if (inputTypes.length === 1) {
|
|
492
604
|
return inputTypes[0];
|
|
@@ -499,46 +611,44 @@ function getGraphqlInputType(inputTypes, pattern) {
|
|
|
499
611
|
return result;
|
|
500
612
|
}
|
|
501
613
|
}
|
|
502
|
-
if (pattern) {
|
|
614
|
+
if (pattern !== null && pattern !== void 0 && pattern.length > 0) {
|
|
503
615
|
if (pattern.startsWith("matcher:") || pattern.startsWith("match:")) {
|
|
504
616
|
const { 1: patternValue } = pattern.split(":", 2);
|
|
505
617
|
const isMatch = outmatch2(patternValue, { separator: false });
|
|
506
|
-
result = inputTypes.find((x) => isMatch(
|
|
507
|
-
if (result) {
|
|
618
|
+
result = inputTypes.find((x) => isMatch(x.type));
|
|
619
|
+
if (result !== void 0) {
|
|
508
620
|
return result;
|
|
509
621
|
}
|
|
510
622
|
}
|
|
511
|
-
result = inputTypes.find((x) =>
|
|
512
|
-
if (result) {
|
|
623
|
+
result = inputTypes.find((x) => x.type.includes(pattern));
|
|
624
|
+
if (result !== void 0) {
|
|
513
625
|
return result;
|
|
514
626
|
}
|
|
515
627
|
}
|
|
516
628
|
result = inputTypes.find((x) => x.location === "inputObjectTypes");
|
|
517
|
-
if (result) {
|
|
629
|
+
if (result !== void 0) {
|
|
518
630
|
return result;
|
|
519
631
|
}
|
|
520
|
-
if (countTypes.enumTypes && countTypes.scalar && inputTypes.some((x) => x.type === "Json" && x.location === "scalar")) {
|
|
632
|
+
if ((countTypes.enumTypes ?? 0) > 0 && (countTypes.scalar ?? 0) > 0 && inputTypes.some((x) => x.type === "Json" && x.location === "scalar")) {
|
|
521
633
|
result = inputTypes.find((x) => x.type === "Json" && x.location === "scalar");
|
|
522
|
-
if (result) {
|
|
634
|
+
if (result !== void 0) {
|
|
523
635
|
return result;
|
|
524
636
|
}
|
|
525
637
|
}
|
|
526
|
-
if ((countTypes.scalar >= 1 || countTypes.enumTypes >= 1) && countTypes.fieldRefTypes === 1) {
|
|
638
|
+
if (((countTypes.scalar ?? 0) >= 1 || (countTypes.enumTypes ?? 0) >= 1) && countTypes.fieldRefTypes === 1) {
|
|
527
639
|
result = inputTypes.find(
|
|
528
640
|
(x) => (x.location === "scalar" || x.location === "enumTypes") && x.isList
|
|
529
641
|
);
|
|
530
|
-
if (result) {
|
|
642
|
+
if (result !== void 0) {
|
|
531
643
|
return result;
|
|
532
644
|
}
|
|
533
|
-
result = inputTypes.find(
|
|
534
|
-
|
|
535
|
-
);
|
|
536
|
-
if (result) {
|
|
645
|
+
result = inputTypes.find((x) => x.location === "scalar" || x.location === "enumTypes");
|
|
646
|
+
if (result !== void 0) {
|
|
537
647
|
return result;
|
|
538
648
|
}
|
|
539
649
|
}
|
|
540
650
|
throw new TypeError(
|
|
541
|
-
`Cannot get matching input type from ${inputTypes.map((x) => x.type).join(", ")
|
|
651
|
+
`Cannot get matching input type from ${inputTypes.length > 0 ? inputTypes.map((x) => x.type).join(", ") : "zero length inputTypes"}`
|
|
542
652
|
);
|
|
543
653
|
}
|
|
544
654
|
|
|
@@ -579,7 +689,7 @@ function getPropertyType(args) {
|
|
|
579
689
|
return [type];
|
|
580
690
|
}
|
|
581
691
|
if (location === "enumTypes") {
|
|
582
|
-
const enumType =
|
|
692
|
+
const enumType = `\`\${${type}}\``;
|
|
583
693
|
return [enumType];
|
|
584
694
|
}
|
|
585
695
|
if (location === "scalar") {
|
|
@@ -600,8 +710,8 @@ function getWhereUniqueAtLeastKeys(model) {
|
|
|
600
710
|
return names;
|
|
601
711
|
}
|
|
602
712
|
function createFieldName(args) {
|
|
603
|
-
const {
|
|
604
|
-
return name
|
|
713
|
+
const { fields, name } = args;
|
|
714
|
+
return name !== null && name !== void 0 && name.length > 0 ? name : fields.join("_");
|
|
605
715
|
}
|
|
606
716
|
|
|
607
717
|
// src/helpers/is-where-unique-input-type.ts
|
|
@@ -610,29 +720,28 @@ function isWhereUniqueInputType(name) {
|
|
|
610
720
|
}
|
|
611
721
|
function propertyStructure(args) {
|
|
612
722
|
const {
|
|
723
|
+
hasExclamationToken,
|
|
724
|
+
hasQuestionToken,
|
|
725
|
+
isList,
|
|
613
726
|
isNullable,
|
|
614
|
-
propertyType,
|
|
615
727
|
name,
|
|
616
|
-
|
|
617
|
-
hasQuestionToken,
|
|
618
|
-
hasExclamationToken
|
|
728
|
+
propertyType
|
|
619
729
|
} = args;
|
|
620
|
-
const
|
|
730
|
+
const typeString = propertyType.map((typeItem) => isList ? `Array<${typeItem}>` : typeItem).join(" | ");
|
|
621
731
|
return {
|
|
732
|
+
decorators: [],
|
|
733
|
+
hasExclamationToken: hasExclamationToken ?? !isNullable,
|
|
734
|
+
hasQuestionToken: hasQuestionToken ?? isNullable,
|
|
622
735
|
kind: StructureKind.Property,
|
|
736
|
+
leadingTrivia: "\n",
|
|
623
737
|
name,
|
|
624
|
-
type
|
|
625
|
-
hasQuestionToken: hasQuestionToken ?? isNullable,
|
|
626
|
-
hasExclamationToken: hasExclamationToken ?? !isNullable,
|
|
627
|
-
decorators: [],
|
|
628
|
-
leadingTrivia: "\n"
|
|
738
|
+
type: typeString
|
|
629
739
|
};
|
|
630
740
|
}
|
|
631
741
|
|
|
632
742
|
// src/handlers/input-type.ts
|
|
633
743
|
function inputType(args) {
|
|
634
744
|
const {
|
|
635
|
-
circularDependencies,
|
|
636
745
|
classDecoratorName,
|
|
637
746
|
classTransformerTypeModels,
|
|
638
747
|
config,
|
|
@@ -641,41 +750,41 @@ function inputType(args) {
|
|
|
641
750
|
fileType,
|
|
642
751
|
getModelName: getModelName2,
|
|
643
752
|
getSourceFile,
|
|
644
|
-
inputType:
|
|
753
|
+
inputType: inputTypeArg,
|
|
645
754
|
models,
|
|
646
755
|
output,
|
|
647
756
|
removeTypes,
|
|
648
757
|
typeNames
|
|
649
758
|
} = args;
|
|
650
|
-
typeNames.add(
|
|
759
|
+
typeNames.add(inputTypeArg.name);
|
|
651
760
|
const importDeclarations = new ImportDeclarationMap();
|
|
652
761
|
const sourceFile = getSourceFile({
|
|
653
|
-
name:
|
|
762
|
+
name: inputTypeArg.name,
|
|
654
763
|
type: fileType
|
|
655
764
|
});
|
|
656
765
|
const classStructure = {
|
|
657
|
-
kind: StructureKind.Class,
|
|
658
|
-
isExported: true,
|
|
659
|
-
name: inputType2.name,
|
|
660
766
|
decorators: [
|
|
661
767
|
{
|
|
662
|
-
|
|
663
|
-
|
|
768
|
+
arguments: [],
|
|
769
|
+
name: classDecoratorName
|
|
664
770
|
}
|
|
665
771
|
],
|
|
772
|
+
isExported: true,
|
|
773
|
+
kind: StructureKind.Class,
|
|
774
|
+
name: inputTypeArg.name,
|
|
666
775
|
properties: []
|
|
667
776
|
};
|
|
668
|
-
const modelName = getModelName2(
|
|
777
|
+
const modelName = getModelName2(inputTypeArg.name) ?? "";
|
|
669
778
|
const model = models.get(modelName);
|
|
670
779
|
const modelFieldSettings = model && fieldSettings.get(model.name);
|
|
671
780
|
const moduleSpecifier = "@nestjs/graphql";
|
|
672
781
|
const lazyTypes = /* @__PURE__ */ new Set();
|
|
673
782
|
importDeclarations.set("Field", {
|
|
674
|
-
|
|
675
|
-
|
|
783
|
+
moduleSpecifier,
|
|
784
|
+
namedImports: [{ name: "Field" }]
|
|
676
785
|
}).set(classDecoratorName, {
|
|
677
|
-
|
|
678
|
-
|
|
786
|
+
moduleSpecifier,
|
|
787
|
+
namedImports: [{ name: classDecoratorName }]
|
|
679
788
|
});
|
|
680
789
|
if (config.esmCompatible) {
|
|
681
790
|
const typeRegistryPath = relativePath(
|
|
@@ -686,11 +795,11 @@ function inputType(args) {
|
|
|
686
795
|
importDeclarations.add("getType", typeRegistryPath);
|
|
687
796
|
}
|
|
688
797
|
const useInputType = config.useInputType.find(
|
|
689
|
-
(x) =>
|
|
798
|
+
(x) => inputTypeArg.name.includes(x.typeName)
|
|
690
799
|
);
|
|
691
|
-
const isWhereUnique = isWhereUniqueInputType(
|
|
692
|
-
for (const field of
|
|
693
|
-
field.inputTypes = field.inputTypes.filter((t) => !removeTypes.has(
|
|
800
|
+
const isWhereUnique = isWhereUniqueInputType(inputTypeArg.name);
|
|
801
|
+
for (const field of inputTypeArg.fields) {
|
|
802
|
+
field.inputTypes = field.inputTypes.filter((t) => !removeTypes.has(t.type));
|
|
694
803
|
eventEmitter.emitSync(BeforeGenerateField, field, args);
|
|
695
804
|
const { inputTypes, isRequired, name } = field;
|
|
696
805
|
if (inputTypes.length === 0) {
|
|
@@ -699,34 +808,36 @@ function inputType(args) {
|
|
|
699
808
|
const usePattern = useInputType?.ALL ?? useInputType?.[name];
|
|
700
809
|
const graphqlInputType = getGraphqlInputType(inputTypes, usePattern);
|
|
701
810
|
const { isList, location, type } = graphqlInputType;
|
|
702
|
-
const typeName =
|
|
811
|
+
const typeName = type;
|
|
703
812
|
const settings = modelFieldSettings?.get(name);
|
|
704
813
|
const propertySettings = settings?.getPropertyType({
|
|
705
|
-
|
|
706
|
-
|
|
814
|
+
input: true,
|
|
815
|
+
name: inputTypeArg.name
|
|
707
816
|
});
|
|
708
817
|
const modelField = model?.fields.find((f) => f.name === name);
|
|
709
818
|
const isCustomsApplicable = typeName === modelField?.type;
|
|
710
819
|
const atLeastKeys = model && getWhereUniqueAtLeastKeys(model);
|
|
711
820
|
const whereUniqueInputTypeValue = isWhereUniqueInputType(typeName) && atLeastKeys && `Prisma.AtLeast<${typeName}, ${atLeastKeys.map((n) => `'${n}'`).join(" | ")}>`;
|
|
712
821
|
const propertyType = castArray(
|
|
713
|
-
propertySettings?.name ??
|
|
822
|
+
propertySettings?.name ?? whereUniqueInputTypeValue ?? getPropertyType({
|
|
714
823
|
location,
|
|
715
824
|
type: typeName
|
|
716
|
-
})
|
|
825
|
+
})
|
|
717
826
|
);
|
|
718
827
|
const hasExclamationToken = Boolean(
|
|
719
828
|
isWhereUnique && config.unsafeCompatibleWhereUniqueInput && atLeastKeys?.includes(name)
|
|
720
829
|
);
|
|
721
830
|
const property = propertyStructure({
|
|
722
|
-
name,
|
|
723
|
-
isNullable: !isRequired,
|
|
724
831
|
hasExclamationToken: hasExclamationToken || void 0,
|
|
725
832
|
hasQuestionToken: hasExclamationToken ? false : void 0,
|
|
726
|
-
|
|
727
|
-
|
|
833
|
+
isList,
|
|
834
|
+
isNullable: !isRequired,
|
|
835
|
+
name,
|
|
836
|
+
propertyType
|
|
728
837
|
});
|
|
729
|
-
classStructure.properties
|
|
838
|
+
if (classStructure.properties) {
|
|
839
|
+
classStructure.properties.push(property);
|
|
840
|
+
}
|
|
730
841
|
if (propertySettings) {
|
|
731
842
|
importDeclarations.create({ ...propertySettings });
|
|
732
843
|
} else if (propertyType.some((p) => p.includes("Prisma.Decimal"))) {
|
|
@@ -737,14 +848,14 @@ function inputType(args) {
|
|
|
737
848
|
let graphqlType;
|
|
738
849
|
let useGetType = false;
|
|
739
850
|
const shouldHideField = settings?.shouldHideField({
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
})
|
|
743
|
-
(d) => d.name === "HideField" && d.from === moduleSpecifier && d.isMatchField(name) && d.isMatchType(
|
|
851
|
+
input: true,
|
|
852
|
+
name: inputTypeArg.name
|
|
853
|
+
}) ?? config.decorate.some(
|
|
854
|
+
(d) => d.name === "HideField" && d.from === moduleSpecifier && d.isMatchField(name) && d.isMatchType(inputTypeArg.name)
|
|
744
855
|
);
|
|
745
856
|
const fieldType = settings?.getFieldType({
|
|
746
|
-
|
|
747
|
-
|
|
857
|
+
input: true,
|
|
858
|
+
name: inputTypeArg.name
|
|
748
859
|
});
|
|
749
860
|
if (fieldType && isCustomsApplicable && !shouldHideField) {
|
|
750
861
|
graphqlType = fieldType.name;
|
|
@@ -752,29 +863,28 @@ function inputType(args) {
|
|
|
752
863
|
} else {
|
|
753
864
|
const graphqlImport = getGraphqlImport({
|
|
754
865
|
config,
|
|
755
|
-
|
|
866
|
+
getSourceFile,
|
|
756
867
|
location,
|
|
757
|
-
|
|
758
|
-
|
|
868
|
+
sourceFile,
|
|
869
|
+
typeName
|
|
759
870
|
});
|
|
760
871
|
graphqlType = graphqlImport.name;
|
|
761
|
-
let referenceName = propertyType[0];
|
|
762
872
|
if (location === "enumTypes") {
|
|
763
|
-
const parts =
|
|
764
|
-
|
|
873
|
+
const parts = String(propertyType[0]).split(" ");
|
|
874
|
+
parts.at(-1);
|
|
765
875
|
}
|
|
766
876
|
const shouldUseLazyType = config.esmCompatible && location === "inputObjectTypes";
|
|
767
|
-
if (graphqlImport.name ===
|
|
877
|
+
if (graphqlImport.name === inputTypeArg.name && shouldUseLazyType) {
|
|
768
878
|
lazyTypes.add(graphqlImport.name);
|
|
769
879
|
useGetType = true;
|
|
770
|
-
} else if (graphqlImport.specifier && !importDeclarations.has(graphqlImport.name) && graphqlImport.name !==
|
|
880
|
+
} else if (graphqlImport.specifier !== null && graphqlImport.specifier !== void 0 && !importDeclarations.has(graphqlImport.name) && graphqlImport.name !== inputTypeArg.name) {
|
|
771
881
|
if (shouldUseLazyType) {
|
|
772
882
|
importDeclarations.addType(graphqlImport.name, graphqlImport.specifier);
|
|
773
883
|
lazyTypes.add(graphqlImport.name);
|
|
774
884
|
} else {
|
|
775
885
|
importDeclarations.set(graphqlImport.name, {
|
|
776
|
-
|
|
777
|
-
|
|
886
|
+
moduleSpecifier: graphqlImport.specifier,
|
|
887
|
+
namedImports: [{ name: graphqlImport.name }]
|
|
778
888
|
});
|
|
779
889
|
}
|
|
780
890
|
}
|
|
@@ -782,10 +892,13 @@ function inputType(args) {
|
|
|
782
892
|
useGetType = true;
|
|
783
893
|
}
|
|
784
894
|
}
|
|
785
|
-
ok(
|
|
895
|
+
ok(
|
|
896
|
+
property.decorators !== void 0 && property.decorators !== null,
|
|
897
|
+
"property.decorators is undefined"
|
|
898
|
+
);
|
|
786
899
|
if (shouldHideField) {
|
|
787
900
|
importDeclarations.add("HideField", moduleSpecifier);
|
|
788
|
-
property.decorators.push({ name: "HideField"
|
|
901
|
+
property.decorators.push({ arguments: [], name: "HideField" });
|
|
789
902
|
} else {
|
|
790
903
|
let typeExpression;
|
|
791
904
|
if (useGetType) {
|
|
@@ -794,14 +907,14 @@ function inputType(args) {
|
|
|
794
907
|
typeExpression = isList ? `() => [${graphqlType}]` : `() => ${graphqlType}`;
|
|
795
908
|
}
|
|
796
909
|
property.decorators.push({
|
|
797
|
-
name: "Field",
|
|
798
910
|
arguments: [
|
|
799
911
|
typeExpression,
|
|
800
912
|
JSON52.stringify({
|
|
801
913
|
...settings?.fieldArguments(),
|
|
802
914
|
nullable: !isRequired
|
|
803
915
|
})
|
|
804
|
-
]
|
|
916
|
+
],
|
|
917
|
+
name: "Field"
|
|
805
918
|
});
|
|
806
919
|
if (graphqlType === "GraphQLDecimal") {
|
|
807
920
|
const decimalHelpersPath = relativePath(
|
|
@@ -813,12 +926,12 @@ function inputType(args) {
|
|
|
813
926
|
importDeclarations.add("Type", "class-transformer");
|
|
814
927
|
property.decorators.push(
|
|
815
928
|
{
|
|
816
|
-
|
|
817
|
-
|
|
929
|
+
arguments: ["() => Object"],
|
|
930
|
+
name: "Type"
|
|
818
931
|
},
|
|
819
932
|
{
|
|
820
|
-
|
|
821
|
-
|
|
933
|
+
arguments: ["transformToDecimal"],
|
|
934
|
+
name: "Transform"
|
|
822
935
|
}
|
|
823
936
|
);
|
|
824
937
|
} else if (location === "inputObjectTypes" && (modelField?.type === "Decimal" || [
|
|
@@ -835,44 +948,47 @@ function inputType(args) {
|
|
|
835
948
|
"updateMany",
|
|
836
949
|
"upsert",
|
|
837
950
|
"where"
|
|
838
|
-
].includes(name) || classTransformerTypeModels.has(getModelName2(graphqlType) ?? "") || modelField?.kind === "object" && models.get(modelField.type)
|
|
951
|
+
].includes(name) || classTransformerTypeModels.has(getModelName2(graphqlType) ?? "") || modelField?.kind === "object" && models.get(modelField.type)?.fields.some(
|
|
839
952
|
(f) => f.kind === "object" && classTransformerTypeModels.has(f.type)
|
|
840
|
-
))) {
|
|
953
|
+
) === true)) {
|
|
841
954
|
importDeclarations.add("Type", "class-transformer");
|
|
842
955
|
if (useGetType) {
|
|
843
956
|
property.decorators.push({
|
|
844
|
-
|
|
845
|
-
|
|
957
|
+
arguments: [`getType('${graphqlType}')`],
|
|
958
|
+
name: "Type"
|
|
846
959
|
});
|
|
847
960
|
} else {
|
|
848
|
-
property.decorators.push({
|
|
961
|
+
property.decorators.push({ arguments: [`() => ${graphqlType}`], name: "Type" });
|
|
849
962
|
}
|
|
850
963
|
}
|
|
851
964
|
if (isCustomsApplicable) {
|
|
852
965
|
for (const options of settings ?? []) {
|
|
853
966
|
if ((options.kind === "Decorator" && options.input && options.match?.(name)) ?? true) {
|
|
854
967
|
property.decorators.push({
|
|
855
|
-
|
|
856
|
-
|
|
968
|
+
arguments: options.arguments,
|
|
969
|
+
name: options.name
|
|
857
970
|
});
|
|
858
|
-
ok(
|
|
971
|
+
ok(
|
|
972
|
+
options.from !== void 0 && options.from !== null && options.from !== "",
|
|
973
|
+
"Missed 'from' part in configuration or field setting"
|
|
974
|
+
);
|
|
859
975
|
importDeclarations.create(options);
|
|
860
976
|
}
|
|
861
977
|
}
|
|
862
978
|
}
|
|
863
979
|
for (const decorate of config.decorate) {
|
|
864
|
-
if (decorate.isMatchField(name) && decorate.isMatchType(
|
|
980
|
+
if (decorate.isMatchField(name) && decorate.isMatchType(inputTypeArg.name)) {
|
|
865
981
|
property.decorators.push({
|
|
866
|
-
|
|
867
|
-
|
|
982
|
+
arguments: decorate.arguments?.map((x) => pupa(x, { propertyType })),
|
|
983
|
+
name: decorate.name
|
|
868
984
|
});
|
|
869
985
|
importDeclarations.create(decorate);
|
|
870
986
|
}
|
|
871
987
|
}
|
|
872
988
|
}
|
|
873
989
|
eventEmitter.emitSync("ClassProperty", property, {
|
|
874
|
-
location,
|
|
875
990
|
isList,
|
|
991
|
+
location,
|
|
876
992
|
propertyType
|
|
877
993
|
});
|
|
878
994
|
}
|
|
@@ -882,7 +998,7 @@ function inputType(args) {
|
|
|
882
998
|
];
|
|
883
999
|
if (config.esmCompatible) {
|
|
884
1000
|
statements.push(`
|
|
885
|
-
registerType('${
|
|
1001
|
+
registerType('${inputTypeArg.name}', ${inputTypeArg.name});`);
|
|
886
1002
|
}
|
|
887
1003
|
sourceFile.set({
|
|
888
1004
|
statements
|
|
@@ -890,8 +1006,8 @@ registerType('${inputType2.name}', ${inputType2.name});`);
|
|
|
890
1006
|
}
|
|
891
1007
|
var ObjectSettings = class extends Array {
|
|
892
1008
|
shouldHideField({
|
|
893
|
-
name,
|
|
894
1009
|
input = false,
|
|
1010
|
+
name,
|
|
895
1011
|
output = false
|
|
896
1012
|
}) {
|
|
897
1013
|
const hideField = this.find((s) => s.name === "HideField");
|
|
@@ -900,8 +1016,8 @@ var ObjectSettings = class extends Array {
|
|
|
900
1016
|
);
|
|
901
1017
|
}
|
|
902
1018
|
getFieldType({
|
|
903
|
-
name,
|
|
904
1019
|
input,
|
|
1020
|
+
name,
|
|
905
1021
|
output
|
|
906
1022
|
}) {
|
|
907
1023
|
const fieldType = this.find((s) => s.kind === "FieldType");
|
|
@@ -920,8 +1036,8 @@ var ObjectSettings = class extends Array {
|
|
|
920
1036
|
return fieldType;
|
|
921
1037
|
}
|
|
922
1038
|
getPropertyType({
|
|
923
|
-
name,
|
|
924
1039
|
input,
|
|
1040
|
+
name,
|
|
925
1041
|
output
|
|
926
1042
|
}) {
|
|
927
1043
|
const propertyType = this.find((s) => s.kind === "PropertyType");
|
|
@@ -944,19 +1060,20 @@ var ObjectSettings = class extends Array {
|
|
|
944
1060
|
const resultArguments = [objectTypeOptions];
|
|
945
1061
|
const objectType = this.find((s) => s.kind === "ObjectType");
|
|
946
1062
|
if (objectType && isObject(objectType.arguments)) {
|
|
947
|
-
const name = objectType.arguments
|
|
1063
|
+
const { name } = objectType.arguments;
|
|
948
1064
|
merge(objectTypeOptions, omit(objectType.arguments, "name"));
|
|
949
|
-
if (name) {
|
|
1065
|
+
if (name !== null && name !== void 0 && name !== "") {
|
|
950
1066
|
resultArguments.unshift(name);
|
|
951
1067
|
}
|
|
952
1068
|
}
|
|
953
1069
|
return resultArguments.map((x) => JSON52.stringify(x));
|
|
954
1070
|
}
|
|
955
1071
|
fieldArguments() {
|
|
956
|
-
const
|
|
957
|
-
if (
|
|
958
|
-
return
|
|
1072
|
+
const fieldItem = this.find((item) => item.kind === "Field");
|
|
1073
|
+
if (fieldItem) {
|
|
1074
|
+
return fieldItem.arguments;
|
|
959
1075
|
}
|
|
1076
|
+
return void 0;
|
|
960
1077
|
}
|
|
961
1078
|
};
|
|
962
1079
|
function createObjectSettings(args) {
|
|
@@ -966,18 +1083,23 @@ function createObjectSettings(args) {
|
|
|
966
1083
|
const documentationLines = [];
|
|
967
1084
|
let fieldElement = result.find((item) => item.kind === "Field");
|
|
968
1085
|
if (!fieldElement) {
|
|
969
|
-
|
|
970
|
-
|
|
1086
|
+
const newFieldElement = {
|
|
1087
|
+
arguments: {},
|
|
1088
|
+
from: "",
|
|
1089
|
+
input: false,
|
|
971
1090
|
kind: "Field",
|
|
972
|
-
|
|
1091
|
+
model: false,
|
|
1092
|
+
name: "",
|
|
1093
|
+
output: false
|
|
973
1094
|
};
|
|
1095
|
+
fieldElement = newFieldElement;
|
|
974
1096
|
}
|
|
975
1097
|
for (const line of textLines) {
|
|
976
1098
|
const match = /^@(?<name>\w+(\.(\w+))?)\((?<args>.*)\)/.exec(line);
|
|
977
|
-
const {
|
|
978
|
-
line,
|
|
1099
|
+
const { documentLine, element } = createSettingElement({
|
|
979
1100
|
config,
|
|
980
1101
|
fieldElement,
|
|
1102
|
+
line,
|
|
981
1103
|
match
|
|
982
1104
|
});
|
|
983
1105
|
if (element) {
|
|
@@ -988,14 +1110,14 @@ function createObjectSettings(args) {
|
|
|
988
1110
|
}
|
|
989
1111
|
}
|
|
990
1112
|
return {
|
|
991
|
-
|
|
992
|
-
|
|
1113
|
+
documentation: documentationLines.filter(Boolean).join("\n") || void 0,
|
|
1114
|
+
settings: result
|
|
993
1115
|
};
|
|
994
1116
|
}
|
|
995
1117
|
function createSettingElement({
|
|
996
|
-
line,
|
|
997
1118
|
config,
|
|
998
1119
|
fieldElement,
|
|
1120
|
+
line,
|
|
999
1121
|
match
|
|
1000
1122
|
}) {
|
|
1001
1123
|
const result = {
|
|
@@ -1003,75 +1125,95 @@ function createSettingElement({
|
|
|
1003
1125
|
element: void 0
|
|
1004
1126
|
};
|
|
1005
1127
|
if (line.startsWith("@deprecated")) {
|
|
1006
|
-
|
|
1007
|
-
|
|
1128
|
+
const DEPRECATED_PREFIX_LENGTH = 11;
|
|
1129
|
+
const updatedFieldElement = {
|
|
1130
|
+
...fieldElement,
|
|
1131
|
+
arguments: {
|
|
1132
|
+
...fieldElement.arguments,
|
|
1133
|
+
deprecationReason: trim(line.slice(DEPRECATED_PREFIX_LENGTH))
|
|
1134
|
+
}
|
|
1135
|
+
};
|
|
1136
|
+
result.element = updatedFieldElement;
|
|
1008
1137
|
return result;
|
|
1009
1138
|
}
|
|
1010
1139
|
if (line.startsWith("@complexity")) {
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1140
|
+
const COMPLEXITY_PREFIX_LENGTH = 11;
|
|
1141
|
+
const MIN_COMPLEXITY = 1;
|
|
1142
|
+
let n = Number.parseInt(trim(line.slice(COMPLEXITY_PREFIX_LENGTH)), 10);
|
|
1143
|
+
if (Number.isNaN(n) || n < MIN_COMPLEXITY) {
|
|
1144
|
+
n = MIN_COMPLEXITY;
|
|
1145
|
+
}
|
|
1146
|
+
const updatedFieldElement = {
|
|
1147
|
+
...fieldElement,
|
|
1148
|
+
arguments: {
|
|
1149
|
+
...fieldElement.arguments,
|
|
1150
|
+
complexity: n
|
|
1151
|
+
}
|
|
1152
|
+
};
|
|
1153
|
+
result.element = updatedFieldElement;
|
|
1015
1154
|
return result;
|
|
1016
1155
|
}
|
|
1017
1156
|
const name = match?.groups?.name;
|
|
1018
|
-
if (!(match && name)) {
|
|
1157
|
+
if (!(match && name !== void 0 && name !== "")) {
|
|
1019
1158
|
result.documentLine = line;
|
|
1020
1159
|
return result;
|
|
1021
1160
|
}
|
|
1022
1161
|
const element = {
|
|
1023
|
-
kind: "Decorator",
|
|
1024
|
-
name: "",
|
|
1025
1162
|
arguments: [],
|
|
1163
|
+
from: "",
|
|
1026
1164
|
input: false,
|
|
1027
|
-
|
|
1165
|
+
kind: "Decorator",
|
|
1028
1166
|
model: false,
|
|
1029
|
-
|
|
1167
|
+
name: "",
|
|
1168
|
+
output: false
|
|
1030
1169
|
};
|
|
1031
1170
|
result.element = element;
|
|
1032
1171
|
if (name === "TypeGraphQL.omit" || name === "HideField") {
|
|
1033
1172
|
Object.assign(element, hideFieldDecorator(match));
|
|
1034
1173
|
return result;
|
|
1035
1174
|
}
|
|
1036
|
-
if (["FieldType", "PropertyType"].includes(name) && match.groups?.args) {
|
|
1175
|
+
if (["FieldType", "PropertyType"].includes(name) && match.groups?.args !== void 0 && match.groups.args !== "") {
|
|
1037
1176
|
const options2 = customType(match.groups.args);
|
|
1038
|
-
|
|
1177
|
+
const namespaceConfig2 = options2.namespace !== void 0 && options2.namespace !== "" ? config.fields[options2.namespace] : void 0;
|
|
1178
|
+
merge(element, namespaceConfig2, options2, {
|
|
1039
1179
|
kind: name
|
|
1040
1180
|
});
|
|
1041
1181
|
return result;
|
|
1042
1182
|
}
|
|
1043
|
-
if (name === "ObjectType" && match.groups?.args) {
|
|
1183
|
+
if (name === "ObjectType" && match.groups?.args !== void 0 && match.groups.args !== "") {
|
|
1044
1184
|
element.kind = "ObjectType";
|
|
1045
1185
|
const options2 = customType(match.groups.args);
|
|
1046
|
-
if (typeof options2[0] === "string" && options2[0]) {
|
|
1186
|
+
if (typeof options2[0] === "string" && options2[0] !== "") {
|
|
1047
1187
|
options2.name = options2[0];
|
|
1048
1188
|
}
|
|
1049
1189
|
if (isObject(options2[1])) {
|
|
1050
1190
|
merge(options2, options2[1]);
|
|
1051
1191
|
}
|
|
1052
1192
|
element.arguments = {
|
|
1053
|
-
|
|
1054
|
-
|
|
1193
|
+
isAbstract: options2.isAbstract,
|
|
1194
|
+
name: options2.name
|
|
1055
1195
|
};
|
|
1056
1196
|
return result;
|
|
1057
1197
|
}
|
|
1058
|
-
if (name === "Directive" && match.groups?.args) {
|
|
1198
|
+
if (name === "Directive" && match.groups?.args !== void 0 && match.groups.args !== "") {
|
|
1059
1199
|
const options2 = customType(match.groups.args);
|
|
1060
|
-
merge(element, {
|
|
1061
|
-
|
|
1062
|
-
namespace: false,
|
|
1200
|
+
merge(element, { from: "@nestjs/graphql", model: true }, options2, {
|
|
1201
|
+
arguments: Array.isArray(options2.arguments) ? options2.arguments.map((s) => JSON52.stringify(s)) : options2.arguments,
|
|
1063
1202
|
kind: "Decorator",
|
|
1064
|
-
|
|
1203
|
+
name,
|
|
1204
|
+
namespace: false
|
|
1065
1205
|
});
|
|
1066
1206
|
return result;
|
|
1067
1207
|
}
|
|
1068
1208
|
const namespace = getNamespace(name);
|
|
1069
1209
|
element.namespaceImport = namespace;
|
|
1210
|
+
const args = match.groups?.args ?? "";
|
|
1070
1211
|
const options = {
|
|
1071
|
-
|
|
1072
|
-
|
|
1212
|
+
arguments: args.split(",").map((s) => trim(s)).filter(Boolean),
|
|
1213
|
+
name
|
|
1073
1214
|
};
|
|
1074
|
-
|
|
1215
|
+
const namespaceConfig = namespace !== void 0 && namespace !== "" ? config.fields[namespace] : void 0;
|
|
1216
|
+
merge(element, namespaceConfig, options);
|
|
1075
1217
|
return result;
|
|
1076
1218
|
}
|
|
1077
1219
|
function customType(args) {
|
|
@@ -1083,7 +1225,8 @@ function customType(args) {
|
|
|
1083
1225
|
Object.assign(result, options);
|
|
1084
1226
|
const namespace = getNamespace(options.name);
|
|
1085
1227
|
result.namespace = namespace;
|
|
1086
|
-
|
|
1228
|
+
const optionsWithName = options;
|
|
1229
|
+
if (optionsWithName.name !== void 0 && optionsWithName.name !== "" && optionsWithName.name.includes(".")) {
|
|
1087
1230
|
result.namespaceImport = namespace;
|
|
1088
1231
|
}
|
|
1089
1232
|
if (typeof options.match === "string" || Array.isArray(options.match)) {
|
|
@@ -1093,29 +1236,30 @@ function customType(args) {
|
|
|
1093
1236
|
}
|
|
1094
1237
|
function hideFieldDecorator(match) {
|
|
1095
1238
|
const result = {
|
|
1096
|
-
name: "HideField",
|
|
1097
1239
|
arguments: [],
|
|
1098
|
-
from: "@nestjs/graphql",
|
|
1099
1240
|
defaultImport: void 0,
|
|
1100
|
-
|
|
1101
|
-
match: void 0
|
|
1241
|
+
from: "@nestjs/graphql",
|
|
1242
|
+
match: void 0,
|
|
1243
|
+
name: "HideField",
|
|
1244
|
+
namespaceImport: void 0
|
|
1102
1245
|
};
|
|
1103
|
-
|
|
1246
|
+
const args = match.groups?.args;
|
|
1247
|
+
if (args === void 0 || args === "") {
|
|
1104
1248
|
result.output = true;
|
|
1105
1249
|
return result;
|
|
1106
1250
|
}
|
|
1107
|
-
if (
|
|
1108
|
-
const options = parseArgs(
|
|
1251
|
+
if (args.includes("{") && args.includes("}")) {
|
|
1252
|
+
const options = parseArgs(args);
|
|
1109
1253
|
result.output = Boolean(options.output);
|
|
1110
1254
|
result.input = Boolean(options.input);
|
|
1111
1255
|
if (typeof options.match === "string" || Array.isArray(options.match)) {
|
|
1112
1256
|
result.match = outmatch2(options.match, { separator: false });
|
|
1113
1257
|
}
|
|
1114
1258
|
} else {
|
|
1115
|
-
if (/output:\s*true/.test(
|
|
1259
|
+
if (/output:\s*true/.test(args)) {
|
|
1116
1260
|
result.output = true;
|
|
1117
1261
|
}
|
|
1118
|
-
if (/input:\s*true/.test(
|
|
1262
|
+
if (/input:\s*true/.test(args)) {
|
|
1119
1263
|
result.input = true;
|
|
1120
1264
|
}
|
|
1121
1265
|
}
|
|
@@ -1133,12 +1277,22 @@ function parseArgs(string) {
|
|
|
1133
1277
|
}
|
|
1134
1278
|
}
|
|
1135
1279
|
function getNamespace(name) {
|
|
1136
|
-
if (name === void 0) {
|
|
1280
|
+
if (name === void 0 || name === null) {
|
|
1281
|
+
return void 0;
|
|
1282
|
+
}
|
|
1283
|
+
if (typeof name !== "string") {
|
|
1284
|
+
return void 0;
|
|
1285
|
+
}
|
|
1286
|
+
let result = name;
|
|
1287
|
+
if (result === "") {
|
|
1137
1288
|
return void 0;
|
|
1138
1289
|
}
|
|
1139
|
-
let result = String(name);
|
|
1140
1290
|
if (result.includes(".")) {
|
|
1141
|
-
|
|
1291
|
+
const parts = result.split(".");
|
|
1292
|
+
result = parts[0] ?? "";
|
|
1293
|
+
}
|
|
1294
|
+
if (result === "") {
|
|
1295
|
+
return void 0;
|
|
1142
1296
|
}
|
|
1143
1297
|
return result;
|
|
1144
1298
|
}
|
|
@@ -1146,12 +1300,12 @@ function getNamespace(name) {
|
|
|
1146
1300
|
// src/handlers/model-data.ts
|
|
1147
1301
|
function modelData(model, args) {
|
|
1148
1302
|
const {
|
|
1303
|
+
classTransformerTypeModels,
|
|
1149
1304
|
config,
|
|
1150
|
-
modelNames,
|
|
1151
|
-
models,
|
|
1152
|
-
modelFields,
|
|
1153
1305
|
fieldSettings,
|
|
1154
|
-
|
|
1306
|
+
modelFields,
|
|
1307
|
+
modelNames,
|
|
1308
|
+
models
|
|
1155
1309
|
} = args;
|
|
1156
1310
|
modelNames.push(model.name);
|
|
1157
1311
|
models.set(model.name, model);
|
|
@@ -1160,10 +1314,10 @@ function modelData(model, args) {
|
|
|
1160
1314
|
const fieldSettingsValue = /* @__PURE__ */ new Map();
|
|
1161
1315
|
fieldSettings.set(model.name, fieldSettingsValue);
|
|
1162
1316
|
for (const field of model.fields) {
|
|
1163
|
-
if (field.documentation) {
|
|
1317
|
+
if (field.documentation !== null && field.documentation !== void 0 && field.documentation.length > 0) {
|
|
1164
1318
|
const { documentation, settings } = createObjectSettings({
|
|
1165
|
-
|
|
1166
|
-
|
|
1319
|
+
config,
|
|
1320
|
+
text: field.documentation
|
|
1167
1321
|
});
|
|
1168
1322
|
field.documentation = documentation;
|
|
1169
1323
|
fieldSettingsValue.set(field.name, settings);
|
|
@@ -1182,8 +1336,14 @@ function createComment(documentation, settings) {
|
|
|
1182
1336
|
for (const line of documentationLines) {
|
|
1183
1337
|
commentLines.push(` * ${line}`);
|
|
1184
1338
|
}
|
|
1185
|
-
const
|
|
1186
|
-
|
|
1339
|
+
const fieldArgs = settings?.fieldArguments();
|
|
1340
|
+
let deprecationReason;
|
|
1341
|
+
if (fieldArgs === void 0) {
|
|
1342
|
+
deprecationReason = void 0;
|
|
1343
|
+
} else {
|
|
1344
|
+
deprecationReason = fieldArgs.deprecationReason;
|
|
1345
|
+
}
|
|
1346
|
+
if (deprecationReason !== void 0 && deprecationReason !== null && deprecationReason.length > 0) {
|
|
1187
1347
|
commentLines.push(` * @deprecated ${deprecationReason}`);
|
|
1188
1348
|
}
|
|
1189
1349
|
commentLines.push(" */\n");
|
|
@@ -1198,6 +1358,9 @@ function buildDependencyGraph(models) {
|
|
|
1198
1358
|
}
|
|
1199
1359
|
for (const model of models) {
|
|
1200
1360
|
const dependencies = graph.get(model.name);
|
|
1361
|
+
if (!dependencies) {
|
|
1362
|
+
continue;
|
|
1363
|
+
}
|
|
1201
1364
|
for (const field of model.fields) {
|
|
1202
1365
|
if (field.kind === "object" && field.type !== model.name) {
|
|
1203
1366
|
if (graph.has(field.type)) {
|
|
@@ -1217,7 +1380,7 @@ function detectCircularDependencies(graph) {
|
|
|
1217
1380
|
visited.add(node);
|
|
1218
1381
|
recursionStack.add(node);
|
|
1219
1382
|
path.push(node);
|
|
1220
|
-
const dependencies = graph.get(node)
|
|
1383
|
+
const dependencies = graph.get(node) ?? /* @__PURE__ */ new Set();
|
|
1221
1384
|
for (const dep of dependencies) {
|
|
1222
1385
|
if (!visited.has(dep)) {
|
|
1223
1386
|
dfs(dep);
|
|
@@ -1267,7 +1430,9 @@ function modelOutputType(outputType2, args) {
|
|
|
1267
1430
|
models,
|
|
1268
1431
|
output
|
|
1269
1432
|
} = args;
|
|
1270
|
-
if (isManyAndReturnOutputType(outputType2.name))
|
|
1433
|
+
if (isManyAndReturnOutputType(outputType2.name)) {
|
|
1434
|
+
return;
|
|
1435
|
+
}
|
|
1271
1436
|
const model = models.get(outputType2.name);
|
|
1272
1437
|
ok(model, `Cannot find model by name ${outputType2.name}`);
|
|
1273
1438
|
const sourceFile = getSourceFile({
|
|
@@ -1297,16 +1462,14 @@ function modelOutputType(outputType2, args) {
|
|
|
1297
1462
|
const decorator = classStructure.decorators.find((d) => d.name === "ObjectType");
|
|
1298
1463
|
ok(decorator, "ObjectType decorator not found");
|
|
1299
1464
|
let modelSettings;
|
|
1300
|
-
if (model.documentation) {
|
|
1465
|
+
if (model.documentation !== null && model.documentation !== void 0 && model.documentation.length > 0) {
|
|
1301
1466
|
const objectTypeOptions = {};
|
|
1302
1467
|
const { documentation, settings } = createObjectSettings({
|
|
1303
1468
|
config,
|
|
1304
1469
|
text: model.documentation
|
|
1305
1470
|
});
|
|
1306
|
-
if (documentation) {
|
|
1307
|
-
|
|
1308
|
-
classStructure.leadingTrivia = createComment(documentation);
|
|
1309
|
-
}
|
|
1471
|
+
if (documentation !== void 0 && documentation.length > 0) {
|
|
1472
|
+
classStructure.leadingTrivia ??= createComment(documentation);
|
|
1310
1473
|
objectTypeOptions.description = documentation;
|
|
1311
1474
|
}
|
|
1312
1475
|
decorator.arguments = settings.getObjectTypeArguments(objectTypeOptions);
|
|
@@ -1324,10 +1487,12 @@ function modelOutputType(outputType2, args) {
|
|
|
1324
1487
|
importDeclarations.add("getType", typeRegistryPath);
|
|
1325
1488
|
}
|
|
1326
1489
|
for (const field of outputType2.fields) {
|
|
1327
|
-
if (config.omitModelsCount && field.name === "_count")
|
|
1490
|
+
if (config.omitModelsCount && field.name === "_count") {
|
|
1491
|
+
continue;
|
|
1492
|
+
}
|
|
1328
1493
|
let fileType = "model";
|
|
1329
1494
|
const { isList, location, namespace, type } = field.outputType;
|
|
1330
|
-
let outputTypeName =
|
|
1495
|
+
let outputTypeName = type;
|
|
1331
1496
|
if (namespace !== "model") {
|
|
1332
1497
|
fileType = "output";
|
|
1333
1498
|
outputTypeName = getOutputTypeName(outputTypeName);
|
|
@@ -1349,15 +1514,12 @@ function modelOutputType(outputType2, args) {
|
|
|
1349
1514
|
})
|
|
1350
1515
|
);
|
|
1351
1516
|
propertyType.splice(1, propertyType.length);
|
|
1352
|
-
if (field.isNullable && !isList) {
|
|
1517
|
+
if (field.isNullable === true && !isList) {
|
|
1353
1518
|
propertyType.push("null");
|
|
1354
1519
|
}
|
|
1355
1520
|
let graphqlType;
|
|
1356
1521
|
let useGetType = false;
|
|
1357
|
-
if (fieldType) {
|
|
1358
|
-
graphqlType = fieldType.name;
|
|
1359
|
-
importDeclarations.create({ ...fieldType });
|
|
1360
|
-
} else {
|
|
1522
|
+
if (fieldType === void 0) {
|
|
1361
1523
|
const graphqlImport = getGraphqlImport({
|
|
1362
1524
|
config,
|
|
1363
1525
|
fileType,
|
|
@@ -1369,7 +1531,7 @@ function modelOutputType(outputType2, args) {
|
|
|
1369
1531
|
typeName: outputTypeName
|
|
1370
1532
|
});
|
|
1371
1533
|
graphqlType = graphqlImport.name;
|
|
1372
|
-
if (graphqlImport.name !== outputType2.name && graphqlImport.specifier) {
|
|
1534
|
+
if (graphqlImport.name !== outputType2.name && graphqlImport.specifier !== null && graphqlImport.specifier !== void 0 && graphqlImport.specifier.length > 0) {
|
|
1373
1535
|
const isCircular = config.esmCompatible && location === "outputObjectTypes" && namespace === "model" && hasCircularDependency(circularDependencies, outputType2.name, outputTypeName);
|
|
1374
1536
|
if (isCircular) {
|
|
1375
1537
|
importDeclarations.addType(graphqlImport.name, graphqlImport.specifier);
|
|
@@ -1379,6 +1541,9 @@ function modelOutputType(outputType2, args) {
|
|
|
1379
1541
|
importDeclarations.add(graphqlImport.name, graphqlImport.specifier);
|
|
1380
1542
|
}
|
|
1381
1543
|
}
|
|
1544
|
+
} else {
|
|
1545
|
+
graphqlType = fieldType.name;
|
|
1546
|
+
importDeclarations.create({ ...fieldType });
|
|
1382
1547
|
}
|
|
1383
1548
|
const property = propertyStructure({
|
|
1384
1549
|
hasExclamationToken: true,
|
|
@@ -1388,17 +1553,19 @@ function modelOutputType(outputType2, args) {
|
|
|
1388
1553
|
name: field.name,
|
|
1389
1554
|
propertyType
|
|
1390
1555
|
});
|
|
1391
|
-
if (typeof property.leadingTrivia === "string" && modelField?.documentation) {
|
|
1556
|
+
if (typeof property.leadingTrivia === "string" && modelField?.documentation !== null && modelField?.documentation !== void 0 && modelField.documentation.length > 0) {
|
|
1392
1557
|
property.leadingTrivia += createComment(modelField.documentation, settings);
|
|
1393
1558
|
}
|
|
1394
|
-
classStructure.properties
|
|
1395
|
-
|
|
1559
|
+
if (classStructure.properties !== void 0) {
|
|
1560
|
+
classStructure.properties.push(property);
|
|
1561
|
+
}
|
|
1562
|
+
if (propertySettings !== void 0) {
|
|
1396
1563
|
importDeclarations.create({ ...propertySettings });
|
|
1397
1564
|
} else if (propertyType.some((p) => p.includes("Prisma.Decimal"))) {
|
|
1398
1565
|
importDeclarations.add("Prisma", config.prismaClientImport);
|
|
1399
1566
|
}
|
|
1400
|
-
ok(property.decorators, "property.decorators is undefined");
|
|
1401
|
-
const shouldHideField = settings?.shouldHideField({ name: outputType2.name, output: true }) || config.decorate.some(
|
|
1567
|
+
ok(property.decorators !== void 0, "property.decorators is undefined");
|
|
1568
|
+
const shouldHideField = settings?.shouldHideField({ name: outputType2.name, output: true }) === true || config.decorate.some(
|
|
1402
1569
|
(d) => d.name === "HideField" && d.from === "@nestjs/graphql" && d.isMatchField(field.name) && d.isMatchType(outputTypeName)
|
|
1403
1570
|
);
|
|
1404
1571
|
if (shouldHideField) {
|
|
@@ -1419,7 +1586,7 @@ function modelOutputType(outputType2, args) {
|
|
|
1419
1586
|
defaultValue: ["number", "string", "boolean"].includes(
|
|
1420
1587
|
typeof modelField?.default
|
|
1421
1588
|
) ? modelField?.default : void 0,
|
|
1422
|
-
description: modelField?.documentation,
|
|
1589
|
+
description: modelField?.documentation !== null && modelField?.documentation !== void 0 && modelField.documentation !== "" ? modelField.documentation : void 0,
|
|
1423
1590
|
nullable: Boolean(field.isNullable)
|
|
1424
1591
|
})
|
|
1425
1592
|
],
|
|
@@ -1431,7 +1598,10 @@ function modelOutputType(outputType2, args) {
|
|
|
1431
1598
|
arguments: setting.arguments,
|
|
1432
1599
|
name: setting.name
|
|
1433
1600
|
});
|
|
1434
|
-
ok(
|
|
1601
|
+
ok(
|
|
1602
|
+
setting.from && setting.from.length > 0,
|
|
1603
|
+
"Missed 'from' part in configuration or field setting"
|
|
1604
|
+
);
|
|
1435
1605
|
importDeclarations.create(setting);
|
|
1436
1606
|
}
|
|
1437
1607
|
}
|
|
@@ -1499,13 +1669,14 @@ function noAtomicOperations(eventEmitter) {
|
|
|
1499
1669
|
eventEmitter.on("BeforeGenerateFiles", beforeGenerateFiles);
|
|
1500
1670
|
}
|
|
1501
1671
|
function beforeInputType2(args) {
|
|
1502
|
-
const {
|
|
1672
|
+
const { getModelName: getModelName2, inputType: inputType2 } = args;
|
|
1503
1673
|
for (const field of inputType2.fields) {
|
|
1504
1674
|
const fieldName = field.name;
|
|
1505
1675
|
field.inputTypes = field.inputTypes.filter((it) => {
|
|
1506
|
-
const inputTypeName =
|
|
1676
|
+
const inputTypeName = it.type;
|
|
1507
1677
|
const modelName = getModelName2(inputTypeName);
|
|
1508
|
-
|
|
1678
|
+
const isModelNameValid = modelName !== null && modelName !== void 0 && modelName.length > 0;
|
|
1679
|
+
if (isAtomicOperation(inputTypeName) || isModelNameValid && isListInput(inputTypeName, modelName, fieldName)) {
|
|
1509
1680
|
return false;
|
|
1510
1681
|
}
|
|
1511
1682
|
return true;
|
|
@@ -1516,7 +1687,8 @@ function beforeGenerateFiles(args) {
|
|
|
1516
1687
|
const { project } = args;
|
|
1517
1688
|
for (const sourceFile of project.getSourceFiles()) {
|
|
1518
1689
|
const className = sourceFile.getClass(() => true)?.getName();
|
|
1519
|
-
|
|
1690
|
+
const isClassNameValid = className !== void 0 && className !== null && className.length > 0;
|
|
1691
|
+
if (isClassNameValid && isAtomicOperation(className)) {
|
|
1520
1692
|
project.removeSourceFile(sourceFile);
|
|
1521
1693
|
}
|
|
1522
1694
|
}
|
|
@@ -1542,11 +1714,13 @@ function outputType(outputTypeArg, args) {
|
|
|
1542
1714
|
const { config, eventEmitter, fieldSettings, getModelName: getModelName2, getSourceFile, models } = args;
|
|
1543
1715
|
const importDeclarations = new ImportDeclarationMap();
|
|
1544
1716
|
const fileType = "output";
|
|
1545
|
-
const modelName = getModelName2(outputTypeArg.name)
|
|
1546
|
-
const model = models.get(modelName);
|
|
1547
|
-
const isAggregateOutput = model && /(?:Count|Avg|Sum|Min|Max)AggregateOutputType$/.test(outputTypeArg.name) &&
|
|
1548
|
-
const isCountOutput = model?.name && outputTypeArg.name === `${model.name}CountOutputType`;
|
|
1549
|
-
if (
|
|
1717
|
+
const modelName = getModelName2(outputTypeArg.name);
|
|
1718
|
+
const model = modelName === void 0 ? void 0 : models.get(modelName);
|
|
1719
|
+
const isAggregateOutput = model !== void 0 && /(?:Count|Avg|Sum|Min|Max)AggregateOutputType$/.test(outputTypeArg.name) && outputTypeArg.name.startsWith(model.name);
|
|
1720
|
+
const isCountOutput = model?.name !== void 0 && model.name !== "" && outputTypeArg.name === `${model.name}CountOutputType`;
|
|
1721
|
+
if (config.emitBlocks.outputs || isCountOutput) ; else {
|
|
1722
|
+
return;
|
|
1723
|
+
}
|
|
1550
1724
|
outputTypeArg.name = getOutputTypeName(outputTypeArg.name);
|
|
1551
1725
|
if (isAggregateOutput) {
|
|
1552
1726
|
eventEmitter.emitSync("AggregateOutput", { ...args, outputType: outputTypeArg });
|
|
@@ -1571,8 +1745,15 @@ function outputType(outputTypeArg, args) {
|
|
|
1571
1745
|
importDeclarations.add("ObjectType", nestjsGraphql2);
|
|
1572
1746
|
for (const field of outputTypeArg.fields) {
|
|
1573
1747
|
const { isList, location, type } = field.outputType;
|
|
1574
|
-
const outputTypeName = getOutputTypeName(
|
|
1575
|
-
|
|
1748
|
+
const outputTypeName = getOutputTypeName(type);
|
|
1749
|
+
let settings;
|
|
1750
|
+
if (isCountOutput) {
|
|
1751
|
+
settings = void 0;
|
|
1752
|
+
} else if (model === void 0) {
|
|
1753
|
+
settings = void 0;
|
|
1754
|
+
} else {
|
|
1755
|
+
settings = fieldSettings.get(model.name)?.get(field.name);
|
|
1756
|
+
}
|
|
1576
1757
|
const propertySettings = settings?.getPropertyType({
|
|
1577
1758
|
name: outputTypeArg.name,
|
|
1578
1759
|
output: true
|
|
@@ -1593,7 +1774,7 @@ function outputType(outputTypeArg, args) {
|
|
|
1593
1774
|
propertyType
|
|
1594
1775
|
});
|
|
1595
1776
|
classStructure.properties?.push(property);
|
|
1596
|
-
if (propertySettings) {
|
|
1777
|
+
if (propertySettings !== void 0) {
|
|
1597
1778
|
importDeclarations.create({ ...propertySettings });
|
|
1598
1779
|
} else if (propertyType.some((p) => p.includes("Prisma.Decimal"))) {
|
|
1599
1780
|
importDeclarations.add("Prisma", config.prismaClientImport);
|
|
@@ -1602,14 +1783,14 @@ function outputType(outputTypeArg, args) {
|
|
|
1602
1783
|
const shouldHideField = settings?.shouldHideField({
|
|
1603
1784
|
name: outputTypeArg.name,
|
|
1604
1785
|
output: true
|
|
1605
|
-
}) || config.decorate.some(
|
|
1786
|
+
}) === true || config.decorate.some(
|
|
1606
1787
|
(d) => d.name === "HideField" && d.from === "@nestjs/graphql" && d.isMatchField(field.name) && d.isMatchType(outputTypeName)
|
|
1607
1788
|
);
|
|
1608
1789
|
const fieldType = settings?.getFieldType({
|
|
1609
1790
|
name: outputTypeArg.name,
|
|
1610
1791
|
output: true
|
|
1611
1792
|
});
|
|
1612
|
-
if (fieldType && isCustomsApplicable && !shouldHideField) {
|
|
1793
|
+
if (fieldType !== void 0 && isCustomsApplicable && !shouldHideField) {
|
|
1613
1794
|
graphqlType = fieldType.name;
|
|
1614
1795
|
importDeclarations.create({ ...fieldType });
|
|
1615
1796
|
} else {
|
|
@@ -1624,14 +1805,14 @@ function outputType(outputTypeArg, args) {
|
|
|
1624
1805
|
});
|
|
1625
1806
|
const referenceName = location === "enumTypes" ? getEnumName(propertyType[0]) : propertyType[0];
|
|
1626
1807
|
graphqlType = graphqlImport.name;
|
|
1627
|
-
if (graphqlImport.specifier && !importDeclarations.has(graphqlImport.name) && (graphqlImport.name !== outputTypeArg.name && !shouldHideField || shouldHideField && referenceName === graphqlImport.name)) {
|
|
1808
|
+
if (graphqlImport.specifier !== null && graphqlImport.specifier !== void 0 && graphqlImport.specifier.length > 0 && !importDeclarations.has(graphqlImport.name) && (graphqlImport.name !== outputTypeArg.name && !shouldHideField || shouldHideField && referenceName === graphqlImport.name)) {
|
|
1628
1809
|
importDeclarations.set(graphqlImport.name, {
|
|
1629
1810
|
moduleSpecifier: graphqlImport.specifier,
|
|
1630
1811
|
namedImports: [{ name: graphqlImport.name }]
|
|
1631
1812
|
});
|
|
1632
1813
|
}
|
|
1633
1814
|
}
|
|
1634
|
-
ok(property.decorators, "property.decorators is undefined");
|
|
1815
|
+
ok(property.decorators !== void 0, "property.decorators is undefined");
|
|
1635
1816
|
if (shouldHideField) {
|
|
1636
1817
|
importDeclarations.add("HideField", nestjsGraphql2);
|
|
1637
1818
|
property.decorators.push({ arguments: [], name: "HideField" });
|
|
@@ -1648,12 +1829,16 @@ function outputType(outputTypeArg, args) {
|
|
|
1648
1829
|
});
|
|
1649
1830
|
if (isCustomsApplicable) {
|
|
1650
1831
|
for (const options of settings ?? []) {
|
|
1651
|
-
|
|
1832
|
+
const shouldApplyDecorator = options.kind === "Decorator" && options.output && (options.match?.(field.name) ?? true);
|
|
1833
|
+
if (shouldApplyDecorator) {
|
|
1652
1834
|
property.decorators.push({
|
|
1653
1835
|
arguments: options.arguments,
|
|
1654
1836
|
name: options.name
|
|
1655
1837
|
});
|
|
1656
|
-
ok(
|
|
1838
|
+
ok(
|
|
1839
|
+
options.from !== null && options.from !== void 0 && options.from.length > 0,
|
|
1840
|
+
"Missed 'from' part in configuration or field setting"
|
|
1841
|
+
);
|
|
1657
1842
|
importDeclarations.create(options);
|
|
1658
1843
|
}
|
|
1659
1844
|
}
|
|
@@ -1669,11 +1854,15 @@ function outputType(outputTypeArg, args) {
|
|
|
1669
1854
|
statements: [...importDeclarations.toStatements(), classStructure]
|
|
1670
1855
|
});
|
|
1671
1856
|
}
|
|
1857
|
+
var rmdirTyped = rmdirSync$1;
|
|
1858
|
+
var rmdirSync = (path) => {
|
|
1859
|
+
rmdirTyped(path);
|
|
1860
|
+
};
|
|
1672
1861
|
function purgeOutput(emitter) {
|
|
1673
1862
|
emitter.on("Begin", begin);
|
|
1674
1863
|
emitter.on("End", end);
|
|
1675
1864
|
}
|
|
1676
|
-
function begin({
|
|
1865
|
+
function begin({ output, project }) {
|
|
1677
1866
|
const sourceFiles = project.getDirectory(output)?.getDescendantSourceFiles();
|
|
1678
1867
|
if (sourceFiles) {
|
|
1679
1868
|
for (const sourceFile of sourceFiles) {
|
|
@@ -1681,9 +1870,10 @@ function begin({ project, output }) {
|
|
|
1681
1870
|
}
|
|
1682
1871
|
}
|
|
1683
1872
|
}
|
|
1684
|
-
function end({
|
|
1873
|
+
function end({ output, project }) {
|
|
1685
1874
|
const directories = project.getDirectory(output)?.getDescendantDirectories().filter((directory) => directory.getSourceFiles().length === 0).map((directory) => directory.getPath());
|
|
1686
|
-
|
|
1875
|
+
const directoryList = directories ?? [];
|
|
1876
|
+
for (const directory of directoryList) {
|
|
1687
1877
|
try {
|
|
1688
1878
|
rmdirSync(directory);
|
|
1689
1879
|
} catch {
|
|
@@ -1701,7 +1891,7 @@ function reExport(emitter) {
|
|
|
1701
1891
|
emitter.on("BeforeGenerateFiles", beforeGenerateFiles2);
|
|
1702
1892
|
}
|
|
1703
1893
|
function beforeGenerateFiles2(args) {
|
|
1704
|
-
const {
|
|
1894
|
+
const { config, output, project } = args;
|
|
1705
1895
|
const rootDirectory = project.getDirectoryOrThrow(output);
|
|
1706
1896
|
if (["Directories" /* Directories */, "All" /* All */].includes(config.reExport)) {
|
|
1707
1897
|
const directories = [...rootDirectory.getDescendantDirectories()];
|
|
@@ -1748,7 +1938,9 @@ function beforeGenerateFiles2(args) {
|
|
|
1748
1938
|
const exportDeclarations = [];
|
|
1749
1939
|
const directories = rootDirectory.getDirectories();
|
|
1750
1940
|
for (const directory of directories) {
|
|
1751
|
-
if (directory.getBaseName() === "node_modules")
|
|
1941
|
+
if (directory.getBaseName() === "node_modules") {
|
|
1942
|
+
continue;
|
|
1943
|
+
}
|
|
1752
1944
|
const indexFile = directory.getSourceFile("index.ts");
|
|
1753
1945
|
if (indexFile) {
|
|
1754
1946
|
const dirName = directory.getBaseName();
|
|
@@ -1770,9 +1962,9 @@ function beforeGenerateFiles2(args) {
|
|
|
1770
1962
|
function getExportDeclaration2(directory, sourceFile) {
|
|
1771
1963
|
let moduleSpecifier = directory.getRelativePathAsModuleSpecifierTo(sourceFile);
|
|
1772
1964
|
if (!moduleSpecifier.endsWith(".js") && !moduleSpecifier.endsWith(".ts")) {
|
|
1773
|
-
moduleSpecifier
|
|
1965
|
+
moduleSpecifier += ".js";
|
|
1774
1966
|
} else if (moduleSpecifier.endsWith(".ts")) {
|
|
1775
|
-
moduleSpecifier = moduleSpecifier.slice(0, -3)
|
|
1967
|
+
moduleSpecifier = `${moduleSpecifier.slice(0, -3)}.js`;
|
|
1776
1968
|
}
|
|
1777
1969
|
return {
|
|
1778
1970
|
kind: StructureKind.ExportDeclaration,
|
|
@@ -1782,7 +1974,7 @@ function getExportDeclaration2(directory, sourceFile) {
|
|
|
1782
1974
|
function getNamespaceExportDeclaration(directory, sourceDirectory) {
|
|
1783
1975
|
let moduleSpecifier = directory.getRelativePathAsModuleSpecifierTo(sourceDirectory);
|
|
1784
1976
|
if (!moduleSpecifier.endsWith("/index.js")) {
|
|
1785
|
-
moduleSpecifier
|
|
1977
|
+
moduleSpecifier += "/index.js";
|
|
1786
1978
|
}
|
|
1787
1979
|
return {
|
|
1788
1980
|
kind: StructureKind.ExportDeclaration,
|
|
@@ -1790,74 +1982,16 @@ function getNamespaceExportDeclaration(directory, sourceDirectory) {
|
|
|
1790
1982
|
};
|
|
1791
1983
|
}
|
|
1792
1984
|
|
|
1793
|
-
// src/handlers/
|
|
1794
|
-
function
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
}
|
|
1803
|
-
return [name, { description: documentation }];
|
|
1804
|
-
}).filter((entry) => entry !== null)
|
|
1805
|
-
);
|
|
1806
|
-
}
|
|
1807
|
-
|
|
1808
|
-
// src/handlers/register-enum.ts
|
|
1809
|
-
function registerEnum(enumType, args) {
|
|
1810
|
-
const { config, enums, getSourceFile } = args;
|
|
1811
|
-
if (!config.emitBlocks.prismaEnums && !enums[enumType.name]) return;
|
|
1812
|
-
const dataModelEnum = enums[enumType.name];
|
|
1813
|
-
const enumTypesData = dataModelEnum?.values ?? [];
|
|
1814
|
-
const sourceFile = getSourceFile({
|
|
1815
|
-
name: enumType.name,
|
|
1816
|
-
type: "enum"
|
|
1817
|
-
});
|
|
1818
|
-
const importDeclarations = new ImportDeclarationMap();
|
|
1819
|
-
importDeclarations.set("registerEnumType", {
|
|
1820
|
-
moduleSpecifier: "@nestjs/graphql",
|
|
1821
|
-
namedImports: [{ name: "registerEnumType" }]
|
|
1985
|
+
// src/handlers/register-all-types.ts
|
|
1986
|
+
function generateRegisterAllTypes(args) {
|
|
1987
|
+
const { config, output, project } = args;
|
|
1988
|
+
if (!config.esmCompatible) {
|
|
1989
|
+
return;
|
|
1990
|
+
}
|
|
1991
|
+
const rootDirectory = project.getDirectory(output) ?? project.createDirectory(output);
|
|
1992
|
+
const sourceFile = rootDirectory.createSourceFile("register-all-types.ts", void 0, {
|
|
1993
|
+
overwrite: true
|
|
1822
1994
|
});
|
|
1823
|
-
const valuesMap = extractEnumValueDocs(enumTypesData);
|
|
1824
|
-
const filteredValuesMap = Object.fromEntries(
|
|
1825
|
-
Object.entries(valuesMap).filter(([, v]) => Object.keys(v).length > 0)
|
|
1826
|
-
);
|
|
1827
|
-
const hasValuesMap = Object.keys(filteredValuesMap).length > 0;
|
|
1828
|
-
const formattedValuesMap = hasValuesMap ? JSON.stringify(filteredValuesMap, null, 2).replace(/"([^"]+)":/g, "$1:") : "";
|
|
1829
|
-
const valuesMapEntry = hasValuesMap ? `, valuesMap: ${formattedValuesMap}` : "";
|
|
1830
|
-
const enumStructure = {
|
|
1831
|
-
kind: StructureKind.Enum,
|
|
1832
|
-
isExported: true,
|
|
1833
|
-
name: enumType.name,
|
|
1834
|
-
members: enumType.values.map((v) => ({
|
|
1835
|
-
name: v,
|
|
1836
|
-
initializer: JSON.stringify(v)
|
|
1837
|
-
}))
|
|
1838
|
-
};
|
|
1839
|
-
sourceFile.set({
|
|
1840
|
-
statements: [
|
|
1841
|
-
...importDeclarations.toStatements(),
|
|
1842
|
-
enumStructure,
|
|
1843
|
-
"\n",
|
|
1844
|
-
`registerEnumType(${enumType.name}, { name: '${enumType.name}', description: ${JSON.stringify(
|
|
1845
|
-
dataModelEnum?.documentation
|
|
1846
|
-
)}${valuesMapEntry} })`
|
|
1847
|
-
]
|
|
1848
|
-
});
|
|
1849
|
-
}
|
|
1850
|
-
function generateRegisterAllTypes(args) {
|
|
1851
|
-
const { config, output, project } = args;
|
|
1852
|
-
if (!config.esmCompatible) {
|
|
1853
|
-
return;
|
|
1854
|
-
}
|
|
1855
|
-
const rootDirectory = project.getDirectory(output) || project.createDirectory(output);
|
|
1856
|
-
const sourceFile = rootDirectory.createSourceFile(
|
|
1857
|
-
"register-all-types.ts",
|
|
1858
|
-
void 0,
|
|
1859
|
-
{ overwrite: true }
|
|
1860
|
-
);
|
|
1861
1995
|
const importPaths = [];
|
|
1862
1996
|
const typeNames = [];
|
|
1863
1997
|
const allSourceFiles = project.getSourceFiles(`${output}/**/*.ts`);
|
|
@@ -1867,7 +2001,7 @@ function generateRegisterAllTypes(args) {
|
|
|
1867
2001
|
continue;
|
|
1868
2002
|
}
|
|
1869
2003
|
const fileText = file.getText();
|
|
1870
|
-
const registerMatch =
|
|
2004
|
+
const registerMatch = /registerType\(['"]([^'"]+)['"]/.exec(fileText);
|
|
1871
2005
|
if (registerMatch) {
|
|
1872
2006
|
const relPath = relativePath(sourceFile.getFilePath(), filePath);
|
|
1873
2007
|
importPaths.push(relPath);
|
|
@@ -1921,6 +2055,76 @@ export type RegisteredTypeName = typeof registeredTypes[number];
|
|
|
1921
2055
|
sourceFile.addStatements(fileContent);
|
|
1922
2056
|
}
|
|
1923
2057
|
|
|
2058
|
+
// src/handlers/prisma-enum-doc.ts
|
|
2059
|
+
function extractEnumValueDocs(values) {
|
|
2060
|
+
return Object.fromEntries(
|
|
2061
|
+
values.map((value) => {
|
|
2062
|
+
const { name } = value;
|
|
2063
|
+
const { documentation } = value;
|
|
2064
|
+
if (typeof documentation !== "string") {
|
|
2065
|
+
return null;
|
|
2066
|
+
}
|
|
2067
|
+
if (documentation.startsWith("@deprecated")) {
|
|
2068
|
+
return [name, { deprecationReason: documentation.slice(11).trim() }];
|
|
2069
|
+
}
|
|
2070
|
+
return [name, { description: documentation }];
|
|
2071
|
+
}).filter((entry) => entry !== null)
|
|
2072
|
+
);
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
// src/handlers/register-enum.ts
|
|
2076
|
+
function registerEnum(enumType, args) {
|
|
2077
|
+
const { config, enums, getSourceFile } = args;
|
|
2078
|
+
const enumTypeAsRecord = enumType;
|
|
2079
|
+
const rawName = enumTypeAsRecord.name;
|
|
2080
|
+
const enumName = typeof rawName === "string" ? rawName : String(rawName);
|
|
2081
|
+
const enumValue = enums[enumName];
|
|
2082
|
+
if (!config.emitBlocks.prismaEnums && enumValue === void 0) {
|
|
2083
|
+
return;
|
|
2084
|
+
}
|
|
2085
|
+
const dataModelEnum = enumValue;
|
|
2086
|
+
const enumTypesData = dataModelEnum?.values ?? [];
|
|
2087
|
+
const sourceFile = getSourceFile({
|
|
2088
|
+
name: enumName,
|
|
2089
|
+
type: "enum"
|
|
2090
|
+
});
|
|
2091
|
+
const importDeclarations = new ImportDeclarationMap();
|
|
2092
|
+
importDeclarations.set("registerEnumType", {
|
|
2093
|
+
moduleSpecifier: "@nestjs/graphql",
|
|
2094
|
+
namedImports: [{ name: "registerEnumType" }]
|
|
2095
|
+
});
|
|
2096
|
+
const valuesMap = extractEnumValueDocs(enumTypesData);
|
|
2097
|
+
const filteredValuesMap = Object.fromEntries(
|
|
2098
|
+
Object.entries(valuesMap).filter(([, v]) => Object.keys(v).length > 0)
|
|
2099
|
+
);
|
|
2100
|
+
const hasValuesMap = Object.keys(filteredValuesMap).length > 0;
|
|
2101
|
+
const formattedValuesMap = hasValuesMap ? JSON.stringify(filteredValuesMap, null, 2).replace(/"([^"]+)":/g, "$1:") : "";
|
|
2102
|
+
const valuesMapEntry = hasValuesMap ? `, valuesMap: ${formattedValuesMap}` : "";
|
|
2103
|
+
const enumTypeRecord = enumType;
|
|
2104
|
+
const rawValues = enumTypeRecord.values;
|
|
2105
|
+
const enumValues = Array.isArray(rawValues) ? rawValues.map((v) => String(v)) : [];
|
|
2106
|
+
const enumStructure = {
|
|
2107
|
+
isExported: true,
|
|
2108
|
+
kind: StructureKind.Enum,
|
|
2109
|
+
members: enumValues.map((v) => ({
|
|
2110
|
+
initializer: JSON.stringify(v),
|
|
2111
|
+
name: v
|
|
2112
|
+
})),
|
|
2113
|
+
name: enumName
|
|
2114
|
+
};
|
|
2115
|
+
const enumTypeName = enumName;
|
|
2116
|
+
sourceFile.set({
|
|
2117
|
+
statements: [
|
|
2118
|
+
...importDeclarations.toStatements(),
|
|
2119
|
+
enumStructure,
|
|
2120
|
+
"\n",
|
|
2121
|
+
`registerEnumType(${enumTypeName}, { name: '${enumTypeName}', description: ${JSON.stringify(
|
|
2122
|
+
dataModelEnum?.documentation
|
|
2123
|
+
)}${valuesMapEntry} })`
|
|
2124
|
+
]
|
|
2125
|
+
});
|
|
2126
|
+
}
|
|
2127
|
+
|
|
1924
2128
|
// src/handlers/require-single-fields-in-whereunique-input.ts
|
|
1925
2129
|
function requireSingleFieldsInWhereUniqueInput(eventEmitter) {
|
|
1926
2130
|
eventEmitter.on("BeforeInputType", beforeInputType3);
|
|
@@ -1935,17 +2139,17 @@ function beforeInputType3(args) {
|
|
|
1935
2139
|
field.isNullable = false;
|
|
1936
2140
|
}
|
|
1937
2141
|
}
|
|
2142
|
+
|
|
2143
|
+
// src/handlers/type-registry.ts
|
|
1938
2144
|
function generateTypeRegistry(args) {
|
|
1939
2145
|
const { config, output, project } = args;
|
|
1940
2146
|
if (!config.esmCompatible) {
|
|
1941
2147
|
return;
|
|
1942
2148
|
}
|
|
1943
|
-
const rootDirectory = project.getDirectory(output)
|
|
1944
|
-
const sourceFile = rootDirectory.createSourceFile(
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
{ overwrite: true }
|
|
1948
|
-
);
|
|
2149
|
+
const rootDirectory = project.getDirectory(output) ?? project.createDirectory(output);
|
|
2150
|
+
const sourceFile = rootDirectory.createSourceFile("type-registry.ts", void 0, {
|
|
2151
|
+
overwrite: true
|
|
2152
|
+
});
|
|
1949
2153
|
const registryCode = `/**
|
|
1950
2154
|
* Type Registry for ESM Circular Dependency Resolution
|
|
1951
2155
|
*
|
|
@@ -2076,81 +2280,17 @@ export function validateRegistry(expectedTypes: string[]): void {
|
|
|
2076
2280
|
sourceFile.addStatements(registryCode);
|
|
2077
2281
|
}
|
|
2078
2282
|
|
|
2079
|
-
// src/handlers/decimal-helpers.ts
|
|
2080
|
-
function generateDecimalHelpers(args) {
|
|
2081
|
-
const { config, output, project } = args;
|
|
2082
|
-
const rootDirectory = project.getDirectory(output) || project.createDirectory(output);
|
|
2083
|
-
const sourceFile = rootDirectory.createSourceFile(
|
|
2084
|
-
"decimal-helpers.ts",
|
|
2085
|
-
void 0,
|
|
2086
|
-
{ overwrite: true }
|
|
2087
|
-
);
|
|
2088
|
-
const helpersCode = `/**
|
|
2089
|
-
* Decimal Helpers for Prisma GraphQL
|
|
2090
|
-
*
|
|
2091
|
-
* This module provides utilities for transforming Decimal values
|
|
2092
|
-
* between GraphQL and Prisma, compatible with Prisma 7+.
|
|
2093
|
-
*
|
|
2094
|
-
* These functions replace the prisma-graphql-type-decimal package
|
|
2095
|
-
* which is incompatible with Prisma 7's new client structure.
|
|
2096
|
-
*/
|
|
2097
|
-
|
|
2098
|
-
import Decimal from 'decimal.js';
|
|
2099
|
-
|
|
2100
|
-
/**
|
|
2101
|
-
* Reconstruct a Decimal from a serialized object.
|
|
2102
|
-
* Prisma serializes Decimals as objects with d, e, s properties.
|
|
2103
|
-
*/
|
|
2104
|
-
function createDecimalFromObject(object: { d: number[]; e: number; s: number }): Decimal {
|
|
2105
|
-
return Object.create(Decimal.prototype, {
|
|
2106
|
-
d: { value: object.d },
|
|
2107
|
-
e: { value: object.e },
|
|
2108
|
-
s: { value: object.s },
|
|
2109
|
-
});
|
|
2110
|
-
}
|
|
2111
|
-
|
|
2112
|
-
/**
|
|
2113
|
-
* Transform input values to Decimal instances.
|
|
2114
|
-
* Used as a class-transformer Transform decorator argument.
|
|
2115
|
-
*
|
|
2116
|
-
* @param params - The transform parameters from class-transformer
|
|
2117
|
-
* @returns The transformed Decimal or array of Decimals
|
|
2118
|
-
*/
|
|
2119
|
-
export function transformToDecimal({ value }: { value: unknown }): Decimal | Decimal[] | null | undefined {
|
|
2120
|
-
if (value == null) return value as null | undefined;
|
|
2121
|
-
|
|
2122
|
-
if (Array.isArray(value)) {
|
|
2123
|
-
return value.map((v) => {
|
|
2124
|
-
if (v instanceof Decimal) return v;
|
|
2125
|
-
if (typeof v === 'object' && v !== null && 'd' in v && 'e' in v && 's' in v) {
|
|
2126
|
-
return createDecimalFromObject(v as { d: number[]; e: number; s: number });
|
|
2127
|
-
}
|
|
2128
|
-
return new Decimal(v as string | number);
|
|
2129
|
-
});
|
|
2130
|
-
}
|
|
2131
|
-
|
|
2132
|
-
if (value instanceof Decimal) return value;
|
|
2133
|
-
if (typeof value === 'object' && value !== null && 'd' in value && 'e' in value && 's' in value) {
|
|
2134
|
-
return createDecimalFromObject(value as { d: number[]; e: number; s: number });
|
|
2135
|
-
}
|
|
2136
|
-
return new Decimal(value as string | number);
|
|
2137
|
-
}
|
|
2138
|
-
|
|
2139
|
-
/**
|
|
2140
|
-
* Re-export Decimal class for convenience
|
|
2141
|
-
*/
|
|
2142
|
-
export { Decimal };
|
|
2143
|
-
`;
|
|
2144
|
-
sourceFile.addStatements(helpersCode);
|
|
2145
|
-
}
|
|
2146
|
-
|
|
2147
2283
|
// src/handlers/warning.ts
|
|
2284
|
+
var globalConsole = globalThis.console;
|
|
2285
|
+
var log = (msg) => {
|
|
2286
|
+
globalConsole.log(msg);
|
|
2287
|
+
};
|
|
2148
2288
|
function warning(message) {
|
|
2149
2289
|
if (Array.isArray(message)) {
|
|
2150
|
-
|
|
2151
|
-
|
|
2290
|
+
log("nestjs-prisma-graphql:");
|
|
2291
|
+
log(message.join("\n"));
|
|
2152
2292
|
} else {
|
|
2153
|
-
|
|
2293
|
+
log(`nestjs-prisma-graphql: ${message}`);
|
|
2154
2294
|
}
|
|
2155
2295
|
}
|
|
2156
2296
|
|
|
@@ -2164,44 +2304,51 @@ var allEmmittedBlocks = [
|
|
|
2164
2304
|
"outputs"
|
|
2165
2305
|
];
|
|
2166
2306
|
var blocksDependencyMap = {
|
|
2307
|
+
args: ["args", "inputs", "prismaEnums"],
|
|
2167
2308
|
enums: ["schemaEnums", "prismaEnums"],
|
|
2168
|
-
models: ["models", "schemaEnums"],
|
|
2169
2309
|
inputs: ["inputs", "prismaEnums"],
|
|
2170
|
-
|
|
2171
|
-
|
|
2310
|
+
models: ["models", "schemaEnums"],
|
|
2311
|
+
outputs: ["outputs"]
|
|
2172
2312
|
};
|
|
2173
2313
|
function createEmitBlocks(data) {
|
|
2174
2314
|
if (!data) {
|
|
2175
|
-
|
|
2315
|
+
const entries = allEmmittedBlocks.map((block) => [block, true]);
|
|
2316
|
+
const allBlocks = Object.fromEntries(entries);
|
|
2317
|
+
return allBlocks;
|
|
2176
2318
|
}
|
|
2177
|
-
|
|
2319
|
+
const initialBlocks = {};
|
|
2320
|
+
let currentBlocks = initialBlocks;
|
|
2178
2321
|
for (const block of data) {
|
|
2179
|
-
if (!Object.keys(blocksDependencyMap).includes(block))
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2322
|
+
if (!Object.keys(blocksDependencyMap).includes(block)) {
|
|
2323
|
+
continue;
|
|
2324
|
+
}
|
|
2325
|
+
const blockEntries = blocksDependencyMap[block].map(
|
|
2326
|
+
(emittedBlock) => [emittedBlock, true]
|
|
2327
|
+
);
|
|
2328
|
+
const newBlocks = Object.fromEntries(blockEntries);
|
|
2329
|
+
currentBlocks = {
|
|
2330
|
+
...currentBlocks,
|
|
2331
|
+
...newBlocks
|
|
2185
2332
|
};
|
|
2186
2333
|
}
|
|
2187
|
-
return
|
|
2334
|
+
return currentBlocks;
|
|
2188
2335
|
}
|
|
2189
2336
|
|
|
2190
2337
|
// src/helpers/create-config.ts
|
|
2191
2338
|
function createConfig(data) {
|
|
2192
2339
|
const config = merge({}, unflatten(data, { delimiter: "_" }));
|
|
2193
2340
|
const $warnings = [];
|
|
2194
|
-
const
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
outputFilePattern = trim(
|
|
2341
|
+
const defaultPattern = `{model}/{name}.{type}.ts`;
|
|
2342
|
+
const outputFilePatternValue = config.outputFilePattern;
|
|
2343
|
+
const configOutputFilePattern = typeof outputFilePatternValue === "string" ? outputFilePatternValue : defaultPattern;
|
|
2344
|
+
const sanitizedStep1 = configOutputFilePattern.replaceAll("..", "/").replaceAll(/\/+/g, "/");
|
|
2345
|
+
const outputFilePattern = trim(sanitizedStep1, "/");
|
|
2199
2346
|
if (outputFilePattern !== configOutputFilePattern) {
|
|
2200
2347
|
$warnings.push(
|
|
2201
2348
|
`Due to invalid filepath 'outputFilePattern' changed to '${outputFilePattern}'`
|
|
2202
2349
|
);
|
|
2203
2350
|
}
|
|
2204
|
-
if (config.reExportAll) {
|
|
2351
|
+
if (config.reExportAll !== void 0 && config.reExportAll !== null && config.reExportAll !== false) {
|
|
2205
2352
|
$warnings.push(`Option 'reExportAll' is deprecated, use 'reExport' instead`);
|
|
2206
2353
|
if (toBoolean(config.reExportAll)) {
|
|
2207
2354
|
config.reExport = "All";
|
|
@@ -2213,105 +2360,118 @@ function createConfig(data) {
|
|
|
2213
2360
|
).filter(({ 1: value }) => typeof value === "object").map(([name, value]) => {
|
|
2214
2361
|
const fieldSetting = {
|
|
2215
2362
|
arguments: [],
|
|
2216
|
-
|
|
2363
|
+
defaultImport: toBoolean(value.defaultImport) ? true : value.defaultImport,
|
|
2364
|
+
from: value.from,
|
|
2217
2365
|
input: toBoolean(value.input),
|
|
2218
2366
|
model: toBoolean(value.model),
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
namespaceImport: value.namespaceImport
|
|
2367
|
+
namespaceImport: value.namespaceImport,
|
|
2368
|
+
output: toBoolean(value.output)
|
|
2222
2369
|
};
|
|
2223
2370
|
return [name, fieldSetting];
|
|
2224
2371
|
})
|
|
2225
2372
|
);
|
|
2226
2373
|
const decorate = [];
|
|
2227
|
-
const
|
|
2228
|
-
|
|
2229
|
-
);
|
|
2374
|
+
const decorateConfig = config.decorate !== void 0 && config.decorate !== null ? config.decorate : {};
|
|
2375
|
+
const configDecorate = Object.values(decorateConfig);
|
|
2230
2376
|
for (const element of configDecorate) {
|
|
2231
|
-
if (
|
|
2377
|
+
if (element === void 0 || element === null) {
|
|
2378
|
+
continue;
|
|
2379
|
+
}
|
|
2232
2380
|
ok(
|
|
2233
|
-
element.from && element.name,
|
|
2381
|
+
element.from !== void 0 && element.from !== "" && element.name !== void 0 && element.name !== "",
|
|
2234
2382
|
`Missed 'from' or 'name' part in configuration for decorate`
|
|
2235
2383
|
);
|
|
2236
2384
|
decorate.push({
|
|
2385
|
+
arguments: element.arguments !== void 0 && element.arguments !== "" ? JSON52.parse(element.arguments) : void 0,
|
|
2386
|
+
defaultImport: toBoolean(element.defaultImport) ? true : element.defaultImport,
|
|
2387
|
+
from: element.from,
|
|
2237
2388
|
isMatchField: outmatch2(element.field, { separator: false }),
|
|
2238
2389
|
isMatchType: outmatch2(element.type, { separator: false }),
|
|
2239
|
-
from: element.from,
|
|
2240
2390
|
name: element.name,
|
|
2241
2391
|
namedImport: toBoolean(element.namedImport),
|
|
2242
|
-
|
|
2243
|
-
namespaceImport: element.namespaceImport,
|
|
2244
|
-
arguments: element.arguments ? JSON52.parse(element.arguments) : void 0
|
|
2392
|
+
namespaceImport: element.namespaceImport
|
|
2245
2393
|
});
|
|
2246
2394
|
}
|
|
2247
2395
|
const customImport = [];
|
|
2248
|
-
const
|
|
2249
|
-
|
|
2250
|
-
);
|
|
2396
|
+
const customImportConfig = config.customImport !== void 0 && config.customImport !== null ? config.customImport : {};
|
|
2397
|
+
const configCustomImport = Object.values(customImportConfig);
|
|
2251
2398
|
for (const element of configCustomImport) {
|
|
2252
|
-
if (
|
|
2399
|
+
if (element === void 0 || element === null) {
|
|
2400
|
+
continue;
|
|
2401
|
+
}
|
|
2253
2402
|
ok(
|
|
2254
|
-
element.from && element.name,
|
|
2403
|
+
element.from !== void 0 && element.from !== "" && element.name !== void 0 && element.name !== "",
|
|
2255
2404
|
`Missed 'from' or 'name' part in configuration for customImport`
|
|
2256
2405
|
);
|
|
2257
2406
|
customImport.push({
|
|
2407
|
+
defaultImport: toBoolean(element.defaultImport) ? true : element.defaultImport,
|
|
2258
2408
|
from: element.from,
|
|
2259
2409
|
name: element.name,
|
|
2260
2410
|
namedImport: toBoolean(element.namedImport),
|
|
2261
|
-
defaultImport: toBoolean(element.defaultImport) ? true : element.defaultImport,
|
|
2262
2411
|
namespaceImport: element.namespaceImport
|
|
2263
2412
|
});
|
|
2264
2413
|
}
|
|
2265
2414
|
return {
|
|
2266
|
-
|
|
2267
|
-
tsConfigFilePath: createTsConfigFilePathValue(config.tsConfigFilePath),
|
|
2268
|
-
prismaClientImport: createPrismaImport(config.prismaClientImport),
|
|
2415
|
+
$warnings,
|
|
2269
2416
|
combineScalarFilters: toBoolean(config.combineScalarFilters),
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
emitSingle: toBoolean(config.emitSingle),
|
|
2273
|
-
emitCompiled: toBoolean(config.emitCompiled),
|
|
2417
|
+
customImport,
|
|
2418
|
+
decorate,
|
|
2274
2419
|
emitBlocks: createEmitBlocks(config.emitBlocks),
|
|
2275
|
-
|
|
2276
|
-
|
|
2420
|
+
emitCompiled: toBoolean(config.emitCompiled),
|
|
2421
|
+
emitSingle: toBoolean(config.emitSingle),
|
|
2422
|
+
esmCompatible: toBoolean(config.esmCompatible),
|
|
2277
2423
|
fields,
|
|
2278
|
-
|
|
2279
|
-
|
|
2424
|
+
graphqlScalars: config.graphqlScalars ?? {},
|
|
2425
|
+
noAtomicOperations: toBoolean(config.noAtomicOperations),
|
|
2280
2426
|
noTypeId: toBoolean(config.noTypeId),
|
|
2427
|
+
omitModelsCount: toBoolean(config.omitModelsCount),
|
|
2428
|
+
outputFilePattern,
|
|
2429
|
+
prismaClientImport: createPrismaImport(config.prismaClientImport),
|
|
2430
|
+
purgeOutput: toBoolean(config.purgeOutput),
|
|
2431
|
+
reExport: ReExport[String(config.reExport)] ?? "None" /* None */,
|
|
2281
2432
|
requireSingleFieldsInWhereUniqueInput: toBoolean(
|
|
2282
2433
|
config.requireSingleFieldsInWhereUniqueInput
|
|
2283
2434
|
),
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
customImport,
|
|
2290
|
-
esmCompatible: toBoolean(config.esmCompatible)
|
|
2435
|
+
tsConfigFilePath: createTsConfigFilePathValue(config.tsConfigFilePath),
|
|
2436
|
+
unsafeCompatibleWhereUniqueInput: toBoolean(config.unsafeCompatibleWhereUniqueInput),
|
|
2437
|
+
useInputType: createUseInputType(
|
|
2438
|
+
config.useInputType
|
|
2439
|
+
)
|
|
2291
2440
|
};
|
|
2292
2441
|
}
|
|
2293
|
-
var
|
|
2294
|
-
|
|
2442
|
+
var existsSyncTyped = existsSync;
|
|
2443
|
+
var tsConfigFileExistsRaw = memoize((filePath) => {
|
|
2444
|
+
const exists = existsSyncTyped(filePath);
|
|
2445
|
+
return exists;
|
|
2295
2446
|
});
|
|
2447
|
+
var tsConfigFileExists = tsConfigFileExistsRaw;
|
|
2296
2448
|
function createTsConfigFilePathValue(value) {
|
|
2297
|
-
if (typeof value === "string"
|
|
2298
|
-
|
|
2449
|
+
if (typeof value === "string" && value !== "") {
|
|
2450
|
+
return value;
|
|
2451
|
+
}
|
|
2452
|
+
const fileExists = tsConfigFileExists("tsconfig.json");
|
|
2453
|
+
if (fileExists) {
|
|
2454
|
+
return "tsconfig.json";
|
|
2455
|
+
}
|
|
2456
|
+
return void 0;
|
|
2299
2457
|
}
|
|
2300
2458
|
function createPrismaImport(value) {
|
|
2301
|
-
if (typeof value === "string"
|
|
2459
|
+
if (typeof value === "string" && value !== "") {
|
|
2460
|
+
return value;
|
|
2461
|
+
}
|
|
2302
2462
|
return "@prisma/client";
|
|
2303
2463
|
}
|
|
2304
2464
|
function createUseInputType(data) {
|
|
2305
|
-
if (
|
|
2465
|
+
if (data === void 0 || data === null) {
|
|
2306
2466
|
return [];
|
|
2307
2467
|
}
|
|
2308
2468
|
const result = [];
|
|
2309
2469
|
for (const [typeName, useInputs] of Object.entries(data)) {
|
|
2310
2470
|
const entry = {
|
|
2311
|
-
|
|
2312
|
-
|
|
2471
|
+
ALL: void 0,
|
|
2472
|
+
typeName
|
|
2313
2473
|
};
|
|
2314
|
-
if (useInputs.ALL) {
|
|
2474
|
+
if (useInputs.ALL !== void 0 && useInputs.ALL !== "") {
|
|
2315
2475
|
entry.ALL = useInputs.ALL;
|
|
2316
2476
|
delete useInputs.ALL;
|
|
2317
2477
|
}
|
|
@@ -2329,7 +2489,7 @@ function generateFileName(args) {
|
|
|
2329
2489
|
const { getModelName: getModelName2, name, template, type } = args;
|
|
2330
2490
|
const rawPath = pupa(template, {
|
|
2331
2491
|
get model() {
|
|
2332
|
-
const result = getModelName2(name)
|
|
2492
|
+
const result = getModelName2(name) ?? "prisma";
|
|
2333
2493
|
return kebabCase(result);
|
|
2334
2494
|
},
|
|
2335
2495
|
get name() {
|
|
@@ -2354,61 +2514,19 @@ function generateFileName(args) {
|
|
|
2354
2514
|
|
|
2355
2515
|
// src/helpers/factory-get-source-file.ts
|
|
2356
2516
|
function factoryGetSourceFile(args) {
|
|
2357
|
-
const {
|
|
2358
|
-
return function getSourceFile(
|
|
2359
|
-
const { name, type } =
|
|
2517
|
+
const { getModelName: getModelName2, output, outputFilePattern, project } = args;
|
|
2518
|
+
return function getSourceFile(getSourceFileArgs) {
|
|
2519
|
+
const { name, type } = getSourceFileArgs;
|
|
2360
2520
|
let filePath = generateFileName({
|
|
2361
2521
|
getModelName: getModelName2,
|
|
2362
2522
|
name,
|
|
2363
|
-
|
|
2364
|
-
|
|
2523
|
+
template: outputFilePattern,
|
|
2524
|
+
type
|
|
2365
2525
|
});
|
|
2366
2526
|
filePath = `${output}/${filePath}`;
|
|
2367
|
-
return project.getSourceFile(filePath)
|
|
2527
|
+
return project.getSourceFile(filePath) ?? project.createSourceFile(filePath, void 0, { overwrite: true });
|
|
2368
2528
|
};
|
|
2369
2529
|
}
|
|
2370
|
-
function createGetModelName(modelNames) {
|
|
2371
|
-
return memoize(tryGetName);
|
|
2372
|
-
function tryGetName(name) {
|
|
2373
|
-
return getModelName({ modelNames, name });
|
|
2374
|
-
}
|
|
2375
|
-
}
|
|
2376
|
-
function getModelName(args) {
|
|
2377
|
-
const { modelNames, name } = args;
|
|
2378
|
-
for (const keyword of splitKeywords) {
|
|
2379
|
-
const [test] = name.split(keyword, 1);
|
|
2380
|
-
if (modelNames.includes(test)) {
|
|
2381
|
-
return test;
|
|
2382
|
-
}
|
|
2383
|
-
}
|
|
2384
|
-
for (const keyword of endsWithKeywords) {
|
|
2385
|
-
const [test] = name.split(keyword).slice(-1);
|
|
2386
|
-
if (modelNames.includes(test)) {
|
|
2387
|
-
return test;
|
|
2388
|
-
}
|
|
2389
|
-
}
|
|
2390
|
-
for (const [start, end2] of middleKeywords) {
|
|
2391
|
-
let test = name.slice(start.length).slice(0, -end2.length);
|
|
2392
|
-
if (modelNames.includes(test) && name.startsWith(start) && name.endsWith(end2)) {
|
|
2393
|
-
return test;
|
|
2394
|
-
}
|
|
2395
|
-
test = name.slice(0, -(start + end2).length);
|
|
2396
|
-
if (modelNames.includes(test) && name.endsWith(start + end2)) {
|
|
2397
|
-
return test;
|
|
2398
|
-
}
|
|
2399
|
-
}
|
|
2400
|
-
if (name.slice(-19) === "CompoundUniqueInput") {
|
|
2401
|
-
const test = name.slice(0, -19);
|
|
2402
|
-
const models = modelNames.filter((x) => test.startsWith(x)).sort((a, b) => b.length - a.length);
|
|
2403
|
-
return models[0];
|
|
2404
|
-
}
|
|
2405
|
-
if (name.slice(-5) === "Count") {
|
|
2406
|
-
const test = name.slice(0, -5);
|
|
2407
|
-
if (modelNames.includes(test)) {
|
|
2408
|
-
return test;
|
|
2409
|
-
}
|
|
2410
|
-
}
|
|
2411
|
-
}
|
|
2412
2530
|
var splitKeywords = [
|
|
2413
2531
|
"CreateInput",
|
|
2414
2532
|
"CreateMany",
|
|
@@ -2488,28 +2606,85 @@ var middleKeywords = [
|
|
|
2488
2606
|
["GroupBy", "Args"],
|
|
2489
2607
|
["OrderBy", "Args"]
|
|
2490
2608
|
];
|
|
2609
|
+
function createGetModelName(modelNames) {
|
|
2610
|
+
return memoize(tryGetName);
|
|
2611
|
+
function tryGetName(name) {
|
|
2612
|
+
return getModelName({ modelNames, name });
|
|
2613
|
+
}
|
|
2614
|
+
}
|
|
2615
|
+
function getModelName(args) {
|
|
2616
|
+
const { modelNames, name } = args;
|
|
2617
|
+
for (const keyword of splitKeywords) {
|
|
2618
|
+
const [test] = name.split(keyword, 1);
|
|
2619
|
+
if (test !== void 0 && modelNames.includes(test)) {
|
|
2620
|
+
return test;
|
|
2621
|
+
}
|
|
2622
|
+
}
|
|
2623
|
+
for (const keyword of endsWithKeywords) {
|
|
2624
|
+
const test = name.split(keyword).at(-1);
|
|
2625
|
+
if (test !== void 0 && modelNames.includes(test)) {
|
|
2626
|
+
return test;
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
for (const [start, end2] of middleKeywords) {
|
|
2630
|
+
let test = name.slice(start.length).slice(0, -end2.length);
|
|
2631
|
+
if (modelNames.includes(test) && name.startsWith(start) && name.endsWith(end2)) {
|
|
2632
|
+
return test;
|
|
2633
|
+
}
|
|
2634
|
+
test = name.slice(0, -(start + end2).length);
|
|
2635
|
+
if (modelNames.includes(test) && name.endsWith(start + end2)) {
|
|
2636
|
+
return test;
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
if (name.endsWith("CompoundUniqueInput")) {
|
|
2640
|
+
const test = name.slice(0, -19);
|
|
2641
|
+
const models = modelNames.filter((x) => test.startsWith(x)).sort((a, b) => b.length - a.length);
|
|
2642
|
+
return models[0];
|
|
2643
|
+
}
|
|
2644
|
+
if (name.endsWith("Count")) {
|
|
2645
|
+
const test = name.slice(0, -5);
|
|
2646
|
+
if (modelNames.includes(test)) {
|
|
2647
|
+
return test;
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
return void 0;
|
|
2651
|
+
}
|
|
2491
2652
|
|
|
2492
2653
|
// src/generate.ts
|
|
2493
|
-
var
|
|
2494
|
-
var
|
|
2654
|
+
var createRequireTyped = createRequire;
|
|
2655
|
+
var requireCjs = createRequireTyped(String(import.meta.url));
|
|
2656
|
+
var AwaitEventEmitter = requireCjs(
|
|
2657
|
+
"await-event-emitter"
|
|
2658
|
+
);
|
|
2659
|
+
var AwaitEventEmitterClass = AwaitEventEmitter.default;
|
|
2495
2660
|
async function generate(args) {
|
|
2496
2661
|
const { connectCallback, dmmf, generator, skipAddOutputSourceFiles } = args;
|
|
2497
|
-
const generatorOutputValue = generator.output?.value;
|
|
2498
|
-
ok(generatorOutputValue, "Missing generator configuration: output");
|
|
2662
|
+
const generatorOutputValue = generator.output?.value ?? "";
|
|
2663
|
+
ok(generatorOutputValue !== "", "Missing generator configuration: output");
|
|
2499
2664
|
const config = createConfig(generator.config);
|
|
2500
|
-
const eventEmitter = new
|
|
2665
|
+
const eventEmitter = new AwaitEventEmitterClass();
|
|
2501
2666
|
eventEmitter.on("Warning", warning);
|
|
2502
|
-
config.emitBlocks.models
|
|
2667
|
+
if (config.emitBlocks.models) {
|
|
2668
|
+
eventEmitter.on("Model", modelData);
|
|
2669
|
+
}
|
|
2503
2670
|
if (config.emitBlocks.prismaEnums || config.emitBlocks.schemaEnums) {
|
|
2504
2671
|
eventEmitter.on("EnumType", registerEnum);
|
|
2505
2672
|
}
|
|
2506
2673
|
if (config.emitBlocks.outputs || config.emitBlocks.models && !config.omitModelsCount) {
|
|
2507
2674
|
eventEmitter.on("OutputType", outputType);
|
|
2508
2675
|
}
|
|
2509
|
-
config.emitBlocks.models
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
config.emitBlocks.
|
|
2676
|
+
if (config.emitBlocks.models) {
|
|
2677
|
+
eventEmitter.on("ModelOutputType", modelOutputType);
|
|
2678
|
+
}
|
|
2679
|
+
if (config.emitBlocks.outputs) {
|
|
2680
|
+
eventEmitter.on("AggregateOutput", createAggregateInput);
|
|
2681
|
+
}
|
|
2682
|
+
if (config.emitBlocks.inputs) {
|
|
2683
|
+
eventEmitter.on("InputType", inputType);
|
|
2684
|
+
}
|
|
2685
|
+
if (config.emitBlocks.args) {
|
|
2686
|
+
eventEmitter.on("ArgsType", argsType);
|
|
2687
|
+
}
|
|
2513
2688
|
eventEmitter.on("GenerateFiles", generateFiles);
|
|
2514
2689
|
for (const message of config.$warnings) {
|
|
2515
2690
|
eventEmitter.emitSync("Warning", message);
|
|
@@ -2522,18 +2697,30 @@ async function generate(args) {
|
|
|
2522
2697
|
skipLoadingLibFiles: !config.emitCompiled,
|
|
2523
2698
|
tsConfigFilePath: config.tsConfigFilePath
|
|
2524
2699
|
});
|
|
2525
|
-
if (
|
|
2700
|
+
if (skipAddOutputSourceFiles !== true) {
|
|
2526
2701
|
project.addSourceFilesAtPaths([
|
|
2527
2702
|
`${generatorOutputValue}/**/*.ts`,
|
|
2528
2703
|
`!${generatorOutputValue}/**/*.d.ts`
|
|
2529
2704
|
]);
|
|
2530
2705
|
}
|
|
2531
|
-
config.combineScalarFilters
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
config.
|
|
2535
|
-
|
|
2536
|
-
|
|
2706
|
+
if (config.combineScalarFilters) {
|
|
2707
|
+
combineScalarFilters(eventEmitter);
|
|
2708
|
+
}
|
|
2709
|
+
if (config.noAtomicOperations) {
|
|
2710
|
+
noAtomicOperations(eventEmitter);
|
|
2711
|
+
}
|
|
2712
|
+
if (config.reExport !== "None" /* None */) {
|
|
2713
|
+
reExport(eventEmitter);
|
|
2714
|
+
}
|
|
2715
|
+
if (config.emitSingle) {
|
|
2716
|
+
emitSingle(eventEmitter);
|
|
2717
|
+
}
|
|
2718
|
+
if (config.purgeOutput) {
|
|
2719
|
+
purgeOutput(eventEmitter);
|
|
2720
|
+
}
|
|
2721
|
+
if (config.requireSingleFieldsInWhereUniqueInput) {
|
|
2722
|
+
requireSingleFieldsInWhereUniqueInput(eventEmitter);
|
|
2723
|
+
}
|
|
2537
2724
|
const models = /* @__PURE__ */ new Map();
|
|
2538
2725
|
const modelNames = [];
|
|
2539
2726
|
const modelFields = /* @__PURE__ */ new Map();
|
|
@@ -2547,7 +2734,8 @@ async function generate(args) {
|
|
|
2547
2734
|
});
|
|
2548
2735
|
const { datamodel, schema } = JSON.parse(JSON.stringify(dmmf));
|
|
2549
2736
|
const removeTypes = /* @__PURE__ */ new Set();
|
|
2550
|
-
const
|
|
2737
|
+
const datamodelTypes = datamodel.types ?? [];
|
|
2738
|
+
const allModels = [...datamodel.models, ...datamodelTypes];
|
|
2551
2739
|
const dependencyGraph = buildDependencyGraph(allModels);
|
|
2552
2740
|
const circularDependencies = detectCircularDependencies(dependencyGraph);
|
|
2553
2741
|
const eventArguments = {
|
|
@@ -2579,7 +2767,7 @@ async function generate(args) {
|
|
|
2579
2767
|
for (const model of datamodel.models) {
|
|
2580
2768
|
await eventEmitter.emit("Model", model, eventArguments);
|
|
2581
2769
|
}
|
|
2582
|
-
for (const model of
|
|
2770
|
+
for (const model of datamodelTypes) {
|
|
2583
2771
|
await eventEmitter.emit("Model", model, eventArguments);
|
|
2584
2772
|
}
|
|
2585
2773
|
const { enumTypes, inputObjectTypes, outputObjectTypes } = schema;
|
|
@@ -2624,23 +2812,51 @@ async function generate(args) {
|
|
|
2624
2812
|
await eventEmitter.emit("BeforeGenerateFiles", eventArguments);
|
|
2625
2813
|
await eventEmitter.emit("GenerateFiles", eventArguments);
|
|
2626
2814
|
await eventEmitter.emit("End", eventArguments);
|
|
2627
|
-
for (const name of Object.keys(
|
|
2815
|
+
for (const name of Object.keys(
|
|
2816
|
+
eventEmitter._events
|
|
2817
|
+
)) {
|
|
2628
2818
|
eventEmitter.off(name);
|
|
2629
2819
|
}
|
|
2630
2820
|
}
|
|
2631
2821
|
|
|
2632
2822
|
// src/index.ts
|
|
2823
|
+
var createRequireTyped2 = createRequire;
|
|
2824
|
+
var requireCjs2 = createRequireTyped2(String(import.meta.url));
|
|
2825
|
+
var gracefulFsRaw = requireCjs2("graceful-fs");
|
|
2826
|
+
var gracefulFs = gracefulFsRaw;
|
|
2827
|
+
var nodeFsRaw = requireCjs2("fs");
|
|
2828
|
+
var nodeFs = nodeFsRaw;
|
|
2829
|
+
gracefulFs.gracefulify(nodeFs);
|
|
2830
|
+
var globalConsole2 = globalThis.console;
|
|
2831
|
+
var logError = (msg) => {
|
|
2832
|
+
globalConsole2.log(msg);
|
|
2833
|
+
};
|
|
2834
|
+
var processTyped = process;
|
|
2835
|
+
processTyped.on("unhandledRejection", (reason) => {
|
|
2836
|
+
logError(`nestjs-prisma-graphql: unhandled rejection: ${String(reason)}`);
|
|
2837
|
+
if (reason instanceof Error && typeof reason.stack === "string") {
|
|
2838
|
+
logError(reason.stack);
|
|
2839
|
+
}
|
|
2840
|
+
});
|
|
2841
|
+
processTyped.on("uncaughtException", (error) => {
|
|
2842
|
+
const err = error;
|
|
2843
|
+
logError(`nestjs-prisma-graphql: uncaught exception: ${err.message}`);
|
|
2844
|
+
if (typeof err.stack === "string") {
|
|
2845
|
+
logError(err.stack);
|
|
2846
|
+
}
|
|
2847
|
+
});
|
|
2633
2848
|
var { generatorHandler } = generatorHelper;
|
|
2634
|
-
|
|
2849
|
+
var getProcessEnv = () => {
|
|
2850
|
+
const globalProcess = process;
|
|
2851
|
+
const processLike = globalProcess;
|
|
2852
|
+
return processLike.env;
|
|
2853
|
+
};
|
|
2854
|
+
function isGeneratorDisabled(options, env = getProcessEnv()) {
|
|
2635
2855
|
const envVarsToCheck = [
|
|
2636
2856
|
"DISABLE_NESTJS_PRISMA_GRAPHQL",
|
|
2637
|
-
// Most specific
|
|
2638
2857
|
"CI_SKIP_PRISMA_GRAPHQL",
|
|
2639
|
-
// CI-specific
|
|
2640
2858
|
"PRISMA_GENERATOR_SKIP",
|
|
2641
|
-
// Common convention
|
|
2642
2859
|
"SKIP_PRISMA_GENERATE"
|
|
2643
|
-
// Alternative
|
|
2644
2860
|
];
|
|
2645
2861
|
for (const envVar of envVarsToCheck) {
|
|
2646
2862
|
const value = env[envVar];
|
|
@@ -2654,15 +2870,80 @@ function isGeneratorDisabled(options, env = process.env) {
|
|
|
2654
2870
|
}
|
|
2655
2871
|
return false;
|
|
2656
2872
|
}
|
|
2873
|
+
function countFilesRecursive(dir) {
|
|
2874
|
+
let count = 0;
|
|
2875
|
+
const readDirSyncTyped = readdirSync;
|
|
2876
|
+
const rawEntries = readDirSyncTyped(dir, { withFileTypes: true });
|
|
2877
|
+
const entries = rawEntries;
|
|
2878
|
+
for (const entry of entries) {
|
|
2879
|
+
const isDir = typeof entry.isDirectory === "function" ? entry.isDirectory() : false;
|
|
2880
|
+
if (isDir) {
|
|
2881
|
+
count += countFilesRecursive(`${dir}/${entry.name}`);
|
|
2882
|
+
} else {
|
|
2883
|
+
count++;
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
return count;
|
|
2887
|
+
}
|
|
2888
|
+
var globalConsole22 = globalThis.console;
|
|
2889
|
+
var log2 = (msg) => {
|
|
2890
|
+
globalConsole22.log(msg);
|
|
2891
|
+
};
|
|
2657
2892
|
generatorHandler({
|
|
2658
2893
|
async onGenerate(options) {
|
|
2659
2894
|
if (isGeneratorDisabled(options)) {
|
|
2660
|
-
|
|
2661
|
-
"
|
|
2895
|
+
log2(
|
|
2896
|
+
"nestjs-prisma-graphql: generation skipped (disabled via environment variable or config)"
|
|
2662
2897
|
);
|
|
2663
2898
|
return;
|
|
2664
2899
|
}
|
|
2665
|
-
|
|
2900
|
+
const outputPath = options.generator.output?.value ?? "<unknown>";
|
|
2901
|
+
const startTime = Date.now();
|
|
2902
|
+
const archTyped = arch;
|
|
2903
|
+
const platformTyped = platform;
|
|
2904
|
+
const archValue = String(archTyped());
|
|
2905
|
+
const platformValue = String(platformTyped());
|
|
2906
|
+
const globalProcess2Raw = process;
|
|
2907
|
+
const processWithVersion = globalProcess2Raw;
|
|
2908
|
+
const nodeVersion = processWithVersion.version;
|
|
2909
|
+
log2(
|
|
2910
|
+
[
|
|
2911
|
+
"nestjs-prisma-graphql: starting generation",
|
|
2912
|
+
`(arch=${archValue}, platform=${platformValue}, node=${nodeVersion}, output=${outputPath})`
|
|
2913
|
+
].join(" ")
|
|
2914
|
+
);
|
|
2915
|
+
try {
|
|
2916
|
+
await generate(options);
|
|
2917
|
+
} catch (error) {
|
|
2918
|
+
const elapsed2 = Date.now() - startTime;
|
|
2919
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2920
|
+
const stack = error instanceof Error ? error.stack : void 0;
|
|
2921
|
+
log2(
|
|
2922
|
+
`nestjs-prisma-graphql: generation FAILED after ${String(elapsed2)}ms: ${message}`
|
|
2923
|
+
);
|
|
2924
|
+
if (typeof stack === "string") {
|
|
2925
|
+
log2(stack);
|
|
2926
|
+
}
|
|
2927
|
+
throw error;
|
|
2928
|
+
}
|
|
2929
|
+
const elapsed = Date.now() - startTime;
|
|
2930
|
+
const existsSyncTyped2 = existsSync;
|
|
2931
|
+
const pathExists = typeof outputPath === "string" && outputPath !== "<unknown>" && existsSyncTyped2(outputPath);
|
|
2932
|
+
if (pathExists) {
|
|
2933
|
+
const fileCount = countFilesRecursive(outputPath);
|
|
2934
|
+
log2(
|
|
2935
|
+
`nestjs-prisma-graphql: generated ${String(fileCount)} files in ${String(elapsed)}ms`
|
|
2936
|
+
);
|
|
2937
|
+
if (fileCount === 0) {
|
|
2938
|
+
const msg = "nestjs-prisma-graphql: generation produced 0 files \u2014 this likely indicates a silent failure";
|
|
2939
|
+
log2(msg);
|
|
2940
|
+
throw new Error(msg);
|
|
2941
|
+
}
|
|
2942
|
+
} else {
|
|
2943
|
+
const msg = `nestjs-prisma-graphql: output directory not found after generation: ${outputPath}`;
|
|
2944
|
+
log2(msg);
|
|
2945
|
+
throw new Error(msg);
|
|
2946
|
+
}
|
|
2666
2947
|
},
|
|
2667
2948
|
onManifest() {
|
|
2668
2949
|
return {
|