prisma-nestjs-graphql 19.0.1 → 19.2.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/README.md CHANGED
@@ -116,9 +116,18 @@ with temporal dead zone when generating merged file.
116
116
  Type: `boolean`
117
117
  Default: `false`
118
118
 
119
- #### `purgeOutput`
119
+ #### `emitBlocks`
120
+ Emit only selected blocks. Be aware, that some blocks do depend on others, e.g. one can't emit `models` without emitting `enums`.
121
+ Type: `("args" | "inputs" | "outputs" | "models" | "enums")[]`
122
+ Default: `["args", "inputs", "outputs", "models", "enums"]`
120
123
 
121
- Delete all files in `output` folder
124
+ #### `omitModelsCount`
125
+ Omit `_count` field from models.
126
+ Type: `boolean`
127
+ Default: `false`
128
+
129
+ #### `purgeOutput`
130
+ Delete all files in `output` folder.
122
131
  Type: `boolean`
123
132
  Default: `false`
124
133
 
@@ -136,6 +145,13 @@ Type: `boolean`
136
145
  Default: `false`
137
146
  **Note**: It will break compatiblity between Prisma types and generated classes.
138
147
 
148
+ #### `unsafeCompatibleWhereUniqueInput`
149
+
150
+ This trick TypeScript and set property as non optional for all fields in `*WhereUniqueInput` classes.
151
+ See [#177](https://github.com/unlight/prisma-nestjs-graphql/issues/177) for more details.
152
+ Type: `boolean`
153
+ Default: `false`
154
+
139
155
  #### `useInputType`
140
156
 
141
157
  Since GraphQL does not support input union type, this setting map
package/generate.cjs CHANGED
@@ -86,9 +86,11 @@ function argsType(field, args) {
86
86
  });
87
87
  }
88
88
 
89
+ const BeforeGenerateField = "BeforeGenerateField";
90
+
89
91
  function combineScalarFilters(eventEmitter) {
90
92
  eventEmitter.on("BeforeInputType", beforeInputType$2);
91
- eventEmitter.on("BeforeGenerateField", beforeGenerateField);
93
+ eventEmitter.on(BeforeGenerateField, beforeGenerateField);
92
94
  eventEmitter.on("PostBegin", postBegin);
93
95
  }
94
96
  function beforeInputType$2(args) {
@@ -571,7 +573,7 @@ function getWhereUniqueAtLeastKeys(model) {
571
573
  for (const uniqueIndex of model.uniqueIndexes) {
572
574
  names.push(createFieldName(uniqueIndex));
573
575
  }
574
- return names.map((name) => `'${name}'`).join(" | ");
576
+ return names;
575
577
  }
576
578
  function createFieldName(args) {
577
579
  const { name, fields } = args;
@@ -651,9 +653,10 @@ function inputType(args) {
651
653
  const useInputType = config.useInputType.find(
652
654
  (x) => inputType2.name.includes(x.typeName)
653
655
  );
656
+ const isWhereUnique = isWhereUniqueInputType(inputType2.name);
654
657
  for (const field of inputType2.fields) {
655
658
  field.inputTypes = field.inputTypes.filter((t) => !removeTypes.has(String(t.type)));
656
- eventEmitter.emitSync("BeforeGenerateField", field, args);
659
+ eventEmitter.emitSync(BeforeGenerateField, field, args);
657
660
  const { inputTypes, isRequired, name } = field;
658
661
  if (inputTypes.length === 0) {
659
662
  continue;
@@ -669,16 +672,22 @@ function inputType(args) {
669
672
  });
670
673
  const modelField = model == null ? void 0 : model.fields.find((f) => f.name === name);
671
674
  const isCustomsApplicable = typeName === (modelField == null ? void 0 : modelField.type);
672
- const whereUniqueInputType = isWhereUniqueInputType(typeName) && model && `Prisma.AtLeast<${typeName}, ${getWhereUniqueAtLeastKeys(model)}>`;
675
+ const atLeastKeys = model && getWhereUniqueAtLeastKeys(model);
676
+ const whereUniqueInputType = isWhereUniqueInputType(typeName) && atLeastKeys && `Prisma.AtLeast<${typeName}, ${atLeastKeys.map((name2) => `'${name2}'`).join(" | ")}>`;
673
677
  const propertyType = lodash.castArray(
674
678
  (propertySettings == null ? void 0 : propertySettings.name) || whereUniqueInputType || getPropertyType({
675
679
  location,
676
680
  type: typeName
677
681
  })
678
682
  );
683
+ const hasExclamationToken = Boolean(
684
+ isWhereUnique && config.unsafeCompatibleWhereUniqueInput && (atLeastKeys == null ? void 0 : atLeastKeys.includes(name))
685
+ );
679
686
  const property = propertyStructure({
680
687
  name,
681
688
  isNullable: !isRequired,
689
+ hasExclamationToken: hasExclamationToken || void 0,
690
+ hasQuestionToken: hasExclamationToken ? false : void 0,
682
691
  propertyType,
683
692
  isList
684
693
  });
@@ -695,7 +704,7 @@ function inputType(args) {
695
704
  name: inputType2.name,
696
705
  input: true
697
706
  })) || config.decorate.some(
698
- (d) => d.name === "HideField" && d.from === "@nestjs/graphql" && d.isMatchField(name) && d.isMatchType(inputType2.name)
707
+ (d) => d.name === "HideField" && d.from === moduleSpecifier && d.isMatchField(name) && d.isMatchType(inputType2.name)
699
708
  );
700
709
  const fieldType = settings == null ? void 0 : settings.getFieldType({
701
710
  name: inputType2.name,
@@ -726,7 +735,7 @@ function inputType(args) {
726
735
  }
727
736
  assert.ok(property.decorators, "property.decorators is undefined");
728
737
  if (shouldHideField) {
729
- importDeclarations.add("HideField", "@nestjs/graphql");
738
+ importDeclarations.add("HideField", moduleSpecifier);
730
739
  property.decorators.push({ name: "HideField", arguments: [] });
731
740
  } else {
732
741
  property.decorators.push({
@@ -1159,6 +1168,8 @@ function modelOutputType(outputType, args) {
1159
1168
  importDeclarations.add("Field", nestjsGraphql$1);
1160
1169
  importDeclarations.add("ObjectType", nestjsGraphql$1);
1161
1170
  for (const field of outputType.fields) {
1171
+ if (config.omitModelsCount && field.name === "_count")
1172
+ continue;
1162
1173
  let fileType = "model";
1163
1174
  const { location, isList, type, namespace } = field.outputType;
1164
1175
  let outputTypeName = String(type);
@@ -1353,6 +1364,8 @@ function outputType(outputType2, args) {
1353
1364
  const model = models.get(modelName);
1354
1365
  const isAggregateOutput = model && /(?:Count|Avg|Sum|Min|Max)AggregateOutputType$/.test(outputType2.name) && String(outputType2.name).startsWith(model.name);
1355
1366
  const isCountOutput = (model == null ? void 0 : model.name) && outputType2.name === `${model.name}CountOutputType`;
1367
+ if (!config.emitBlocks.outputs && !isCountOutput)
1368
+ return;
1356
1369
  outputType2.name = getOutputTypeName(outputType2.name);
1357
1370
  if (isAggregateOutput) {
1358
1371
  eventEmitter.emitSync("AggregateOutput", { ...args, outputType: outputType2 });
@@ -1596,7 +1609,9 @@ function getNamespaceExportDeclaration(directory, sourceDirectory) {
1596
1609
  }
1597
1610
 
1598
1611
  function registerEnum(enumType, args) {
1599
- const { getSourceFile, enums } = args;
1612
+ const { getSourceFile, enums, config } = args;
1613
+ if (!config.emitBlocks.prismaEnums && !enums[enumType.name])
1614
+ return;
1600
1615
  const dataModelEnum = enums[enumType.name];
1601
1616
  const sourceFile = getSourceFile({
1602
1617
  name: enumType.name,
@@ -1649,6 +1664,30 @@ function warning(message) {
1649
1664
  }
1650
1665
  }
1651
1666
 
1667
+ const allEmmittedBlocks = ["prismaEnums", "schemaEnums", "models", "inputs", "args", "outputs"];
1668
+ const blocksDependencyMap = {
1669
+ enums: ["schemaEnums", "prismaEnums"],
1670
+ models: ["models", "schemaEnums"],
1671
+ inputs: ["inputs", "prismaEnums"],
1672
+ outputs: ["outputs"],
1673
+ args: ["args", "inputs", "prismaEnums"]
1674
+ };
1675
+ function createEmitBlocks(data) {
1676
+ if (!data) {
1677
+ return Object.fromEntries(allEmmittedBlocks.map((block) => [block, true]));
1678
+ }
1679
+ let blocksToEmit = {};
1680
+ for (const block of data) {
1681
+ if (!Object.keys(blocksDependencyMap).includes(block))
1682
+ continue;
1683
+ blocksToEmit = {
1684
+ ...blocksToEmit,
1685
+ ...Object.fromEntries(blocksDependencyMap[block].map((block2) => [block2, true]))
1686
+ };
1687
+ }
1688
+ return blocksToEmit;
1689
+ }
1690
+
1652
1691
  function createConfig(data) {
1653
1692
  var _a;
1654
1693
  const config = lodash.merge({}, flat.unflatten(data, { delimiter: "_" }));
@@ -1717,6 +1756,8 @@ function createConfig(data) {
1717
1756
  reExport: ReExport[String(config.reExport)] || ReExport.None,
1718
1757
  emitSingle: toBoolean(config.emitSingle),
1719
1758
  emitCompiled: toBoolean(config.emitCompiled),
1759
+ emitBlocks: createEmitBlocks(config.emitBlocks),
1760
+ omitModelsCount: toBoolean(config.omitModelsCount),
1720
1761
  $warnings,
1721
1762
  fields,
1722
1763
  purgeOutput: toBoolean(config.purgeOutput),
@@ -1725,6 +1766,9 @@ function createConfig(data) {
1725
1766
  requireSingleFieldsInWhereUniqueInput: toBoolean(
1726
1767
  config.requireSingleFieldsInWhereUniqueInput
1727
1768
  ),
1769
+ unsafeCompatibleWhereUniqueInput: toBoolean(
1770
+ config.unsafeCompatibleWhereUniqueInput
1771
+ ),
1728
1772
  graphqlScalars: config.graphqlScalars || {},
1729
1773
  decorate
1730
1774
  };
@@ -1929,17 +1973,21 @@ async function generate(args) {
1929
1973
  const { connectCallback, generator, skipAddOutputSourceFiles, dmmf } = args;
1930
1974
  const generatorOutputValue = (_a = generator.output) == null ? void 0 : _a.value;
1931
1975
  assert.ok(generatorOutputValue, "Missing generator configuration: output");
1976
+ const config = createConfig(generator.config);
1932
1977
  const eventEmitter = new AwaitEventEmitter__default["default"]();
1933
1978
  eventEmitter.on("Warning", warning);
1934
- eventEmitter.on("Model", modelData);
1935
- eventEmitter.on("EnumType", registerEnum);
1936
- eventEmitter.on("OutputType", outputType);
1937
- eventEmitter.on("ModelOutputType", modelOutputType);
1938
- eventEmitter.on("AggregateOutput", createAggregateInput);
1939
- eventEmitter.on("InputType", inputType);
1940
- eventEmitter.on("ArgsType", argsType);
1979
+ config.emitBlocks.models && eventEmitter.on("Model", modelData);
1980
+ if (config.emitBlocks.prismaEnums || config.emitBlocks.schemaEnums) {
1981
+ eventEmitter.on("EnumType", registerEnum);
1982
+ }
1983
+ if (config.emitBlocks.outputs || config.emitBlocks.models && !config.omitModelsCount) {
1984
+ eventEmitter.on("OutputType", outputType);
1985
+ }
1986
+ config.emitBlocks.models && eventEmitter.on("ModelOutputType", modelOutputType);
1987
+ config.emitBlocks.outputs && eventEmitter.on("AggregateOutput", createAggregateInput);
1988
+ config.emitBlocks.inputs && eventEmitter.on("InputType", inputType);
1989
+ config.emitBlocks.args && eventEmitter.on("ArgsType", argsType);
1941
1990
  eventEmitter.on("GenerateFiles", generateFiles);
1942
- const config = createConfig(generator.config);
1943
1991
  for (const message of config.$warnings) {
1944
1992
  eventEmitter.emitSync("Warning", message);
1945
1993
  }
package/generate.d.ts CHANGED
@@ -82,7 +82,6 @@ declare namespace DMMF {
82
82
  relationOnDelete?: string;
83
83
  relationName?: string;
84
84
  documentation?: string;
85
- [key: string]: any;
86
85
  }
87
86
  export interface FieldDefault {
88
87
  name: string;
@@ -118,25 +117,24 @@ declare namespace DMMF {
118
117
  isRequired: boolean;
119
118
  isList: boolean;
120
119
  }
121
- export type ArgType = string | InputType | SchemaEnum;
122
- export interface SchemaArgInputType {
120
+ export type TypeRef<AllowedLocations extends FieldLocation> = {
123
121
  isList: boolean;
124
- type: ArgType;
125
- location: FieldLocation;
122
+ type: string;
123
+ location: AllowedLocations;
126
124
  namespace?: FieldNamespace;
127
- }
125
+ };
126
+ export type InputTypeRef = TypeRef<'scalar' | 'inputObjectTypes' | 'enumTypes' | 'fieldRefTypes'>;
128
127
  export interface SchemaArg {
129
128
  name: string;
130
129
  comment?: string;
131
130
  isNullable: boolean;
132
131
  isRequired: boolean;
133
- inputTypes: SchemaArgInputType[];
132
+ inputTypes: InputTypeRef[];
134
133
  deprecation?: Deprecation;
135
134
  }
136
135
  export interface OutputType {
137
136
  name: string;
138
137
  fields: SchemaField[];
139
- fieldMap?: Record<string, SchemaField>;
140
138
  }
141
139
  export interface SchemaField {
142
140
  name: string;
@@ -146,23 +144,7 @@ declare namespace DMMF {
146
144
  deprecation?: Deprecation;
147
145
  documentation?: string;
148
146
  }
149
- export type TypeRefCommon = {
150
- isList: boolean;
151
- namespace?: FieldNamespace;
152
- };
153
- export type TypeRefScalar = TypeRefCommon & {
154
- location: 'scalar';
155
- type: string;
156
- };
157
- export type TypeRefOutputObject = TypeRefCommon & {
158
- location: 'outputObjectTypes';
159
- type: OutputType | string;
160
- };
161
- export type TypeRefEnum = TypeRefCommon & {
162
- location: 'enumTypes';
163
- type: SchemaEnum | string;
164
- };
165
- export type OutputTypeRef = TypeRefScalar | TypeRefOutputObject | TypeRefEnum;
147
+ export type OutputTypeRef = TypeRef<'scalar' | 'outputObjectTypes' | 'enumTypes'>;
166
148
  export interface Deprecation {
167
149
  sinceVersion: string;
168
150
  reason: string;
@@ -179,14 +161,13 @@ declare namespace DMMF {
179
161
  source?: string;
180
162
  };
181
163
  fields: SchemaArg[];
182
- fieldMap?: Record<string, SchemaArg>;
183
164
  }
184
165
  export interface FieldRefType {
185
166
  name: string;
186
167
  allowTypes: FieldRefAllowType[];
187
168
  fields: SchemaArg[];
188
169
  }
189
- export type FieldRefAllowType = TypeRefScalar | TypeRefEnum;
170
+ export type FieldRefAllowType = TypeRef<'scalar' | 'enumTypes'>;
190
171
  export interface ModelMapping {
191
172
  model: string;
192
173
  plural: string;
@@ -254,12 +235,15 @@ declare function createConfig(data: Record<string, unknown>): {
254
235
  reExport: ReExport;
255
236
  emitSingle: boolean;
256
237
  emitCompiled: boolean;
238
+ emitBlocks: Record<"models" | "inputs" | "args" | "outputs" | "prismaEnums" | "schemaEnums", boolean>;
239
+ omitModelsCount: boolean;
257
240
  $warnings: string[];
258
241
  fields: Record<string, Partial<Omit<ObjectSetting, "name">> | undefined>;
259
242
  purgeOutput: boolean;
260
243
  useInputType: ConfigInputItem[];
261
244
  noTypeId: boolean;
262
245
  requireSingleFieldsInWhereUniqueInput: boolean;
246
+ unsafeCompatibleWhereUniqueInput: boolean;
263
247
  graphqlScalars: Record<string, ImportNameSpec | undefined>;
264
248
  decorate: DecorateElement[];
265
249
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-nestjs-graphql",
3
- "version": "19.0.1",
3
+ "version": "19.2.0",
4
4
  "license": "MIT",
5
5
  "description": "Generate object types, inputs, args, etc. from prisma schema file for usage with @nestjs/graphql module",
6
6
  "bin": "bin.js",
@@ -70,10 +70,10 @@
70
70
  }
71
71
  },
72
72
  "dependencies": {
73
- "@prisma/generator-helper": "^5.1.1",
73
+ "@prisma/generator-helper": "^5.3.1",
74
74
  "await-event-emitter": "^2.0.2",
75
75
  "filenamify": "4.X",
76
- "flat": "^5.0.2",
76
+ "flat": "5.X",
77
77
  "get-relative-path": "^1.0.2",
78
78
  "graceful-fs": "^4.2.11",
79
79
  "json5": "^2.2.3",
@@ -84,28 +84,28 @@
84
84
  "ts-morph": ">=11 <=16"
85
85
  },
86
86
  "devDependencies": {
87
- "@commitlint/cli": "^17.6.7",
88
- "@commitlint/config-conventional": "^17.6.7",
89
- "@nestjs/apollo": "^12.0.7",
90
- "@nestjs/common": "^10.1.3",
91
- "@nestjs/core": "^10.1.3",
92
- "@nestjs/graphql": "^12.0.8",
93
- "@nestjs/platform-express": "^10.1.3",
94
- "@paljs/plugins": "^5.3.3",
95
- "@prisma/client": "^5.1.1",
87
+ "@commitlint/cli": "^17.7.1",
88
+ "@commitlint/config-conventional": "^17.7.0",
89
+ "@nestjs/apollo": "^12.0.9",
90
+ "@nestjs/common": "^10.2.6",
91
+ "@nestjs/core": "^10.2.6",
92
+ "@nestjs/graphql": "^12.0.9",
93
+ "@nestjs/platform-express": "^10.2.6",
94
+ "@paljs/plugins": "^6.0.7",
95
+ "@prisma/client": "^5.3.1",
96
96
  "@semantic-release/changelog": "^6.0.3",
97
97
  "@semantic-release/git": "^10.0.1",
98
- "@swc/core": "^1.3.74",
99
- "@swc/helpers": "^0.5.1",
98
+ "@swc/core": "^1.3.87",
99
+ "@swc/helpers": "^0.5.2",
100
100
  "@swc/register": "^0.1.10",
101
- "@types/flat": "^5.0.2",
102
- "@types/graceful-fs": "^4.1.6",
103
- "@types/lodash": "^4.14.196",
101
+ "@types/flat": "^5.0.3",
102
+ "@types/graceful-fs": "^4.1.7",
103
+ "@types/lodash": "^4.14.199",
104
104
  "@types/mocha": "^10.0.1",
105
- "@types/node": "^20.4.7",
105
+ "@types/node": "^20.6.3",
106
106
  "@types/pluralize": "^0.0.30",
107
- "@typescript-eslint/eslint-plugin": "^6.2.1",
108
- "@typescript-eslint/parser": "^6.2.1",
107
+ "@typescript-eslint/eslint-plugin": "^6.7.2",
108
+ "@typescript-eslint/parser": "^6.7.2",
109
109
  "apollo-server-express": "^3.10.0",
110
110
  "c8": "^8.0.1",
111
111
  "class-transformer": "^0.5.1",
@@ -113,10 +113,10 @@
113
113
  "commitizen": "^4.3.0",
114
114
  "cz-customizable": "^7.0.0",
115
115
  "decimal.js": "^10.4.3",
116
- "eslint": "^8.46.0",
117
- "eslint-import-resolver-node": "^0.3.7",
116
+ "eslint": "^8.50.0",
117
+ "eslint-import-resolver-node": "^0.3.9",
118
118
  "eslint-plugin-etc": "^2.0.3",
119
- "eslint-plugin-import": "^2.28.0",
119
+ "eslint-plugin-import": "^2.28.1",
120
120
  "eslint-plugin-only-warn": "^1.1.0",
121
121
  "eslint-plugin-prettier": "^5.0.0",
122
122
  "eslint-plugin-regexp": "^1.15.0",
@@ -124,29 +124,26 @@
124
124
  "eslint-plugin-sort-class-members": "^1.18.0",
125
125
  "eslint-plugin-unicorn": "^48.0.1",
126
126
  "eslint-plugin-wix-editor": "^3.3.0",
127
- "expect": "^29.6.2",
127
+ "expect": "^29.7.0",
128
128
  "git-branch-is": "^4.0.0",
129
- "graphql": "^16.7.1",
129
+ "graphql": "^16.8.1",
130
130
  "graphql-scalars": "^1.22.2",
131
131
  "graphql-type-json": "^0.3.2",
132
132
  "mocha": "^10.2.0",
133
133
  "ololog": "^1.1.175",
134
134
  "precise-commits": "^1.0.2",
135
- "prettier": "^3.0.1",
136
- "prisma": "^5.1.1",
135
+ "prettier": "^3.0.3",
136
+ "prisma": "^5.3.1",
137
137
  "prisma-graphql-type-decimal": "^3.0.0",
138
138
  "reflect-metadata": "^0.1.13",
139
139
  "request": "^2.88.2",
140
140
  "rxjs": "^7.8.1",
141
- "semantic-release": "^21.0.7",
141
+ "semantic-release": "^22.0.4",
142
142
  "simplytyped": "^3.3.0",
143
143
  "temp-dir": "2.X",
144
144
  "ts-node": "^10.9.1",
145
- "tslib": "^2.6.1",
146
- "typescript": "^5.1.6",
145
+ "tslib": "^2.6.2",
146
+ "typescript": "^5.2.2",
147
147
  "watchexec-bin": "^1.0.0"
148
- },
149
- "overrides": {
150
- "prisma": "^5.1.1"
151
148
  }
152
149
  }