prismadoc 1.0.37 → 1.0.39

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.
@@ -13,6 +13,7 @@ export class DocGenDto {
13
13
  ]);
14
14
  classValidators = new Set();
15
15
  enums = new Set();
16
+ hasJson = false;
16
17
  enumImportPath;
17
18
  mainEnumNames;
18
19
  constructor(model, enumImportPath, mainEnumNames) {
@@ -40,6 +41,8 @@ export class DocGenDto {
40
41
  else if (field.isEnum) {
41
42
  this.enums.add(field.type);
42
43
  }
44
+ if (field.isJson)
45
+ this.hasJson = true;
43
46
  return `class ${Helper.capitalizeFirstSafe(field.name)}Dto {
44
47
  ${field.build()}
45
48
  }`;
@@ -52,10 +55,14 @@ export class DocGenDto {
52
55
  if (localEnums.length > 0) {
53
56
  this.imports.add(`import { ${localEnums.join(", ")} } from '${this.enumImportPath}';`);
54
57
  }
55
- if (prismaEnums.length > 0) {
56
- this.imports.add(`import { ${prismaEnums.join(", ")} } from '@prisma/client';`);
58
+ const prismaNamedImports = [...prismaEnums, ...(this.hasJson ? ["Prisma"] : [])];
59
+ if (prismaNamedImports.length > 0) {
60
+ this.imports.add(`import { ${prismaNamedImports.join(", ")} } from '@prisma/client';`);
57
61
  }
58
62
  }
63
+ else if (this.hasJson) {
64
+ this.imports.add(`import { Prisma } from '@prisma/client';`);
65
+ }
59
66
  this.imports.add(`import { ${Array.from(this.classValidators)} } from '${config.validatorPath}';`);
60
67
  const intersections = this.fields.map((field) => Helper.capitalizeFirstSafe(field.name) + "Dto");
61
68
  const intersectionClassName = `class ${this.name}Dto`;
@@ -12,6 +12,7 @@ export class DocGenField {
12
12
  type;
13
13
  fieldType;
14
14
  isEnum = false;
15
+ isJson = false;
15
16
  isResponse = false;
16
17
  isUpdatedAt = false;
17
18
  isRequired;
@@ -43,12 +44,15 @@ export class DocGenField {
43
44
  if (this.scalarType === "DateTime") {
44
45
  this.processValidator({ name: "IsDateString" });
45
46
  }
46
- else if (this.scalarType === "String" || this.scalarType === "Json") {
47
+ else if (this.scalarType === "String") {
47
48
  this.processValidator({ name: "IsString" });
48
49
  if (this.isRequired) {
49
50
  this.processValidator({ name: "IsNotEmpty" });
50
51
  }
51
52
  }
53
+ else if (this.scalarType === "Json") {
54
+ this.processValidator({ name: "IsJSON" });
55
+ }
52
56
  else if (this.scalarType === "Boolean") {
53
57
  this.processValidator({ name: "IsBoolean" });
54
58
  }
@@ -92,7 +96,13 @@ export class DocGenField {
92
96
  this.type = `${this.scalarType}Res`;
93
97
  }
94
98
  else if (this.kind === "scalar") {
95
- this.type = Helper.prismaScalarToTs(this.scalarType);
99
+ if (this.scalarType === "Json") {
100
+ this.isJson = true;
101
+ this.type = "Prisma.JsonValue";
102
+ }
103
+ else {
104
+ this.type = Helper.prismaScalarToTs(this.scalarType);
105
+ }
96
106
  }
97
107
  }
98
108
  getRuledExample(fieldName) {
@@ -136,7 +146,7 @@ export class DocGenField {
136
146
  else if (this.scalarField.type === "String") {
137
147
  props.push(`example: 'ordinary string'`);
138
148
  }
139
- if (this.type === "object") {
149
+ if (this.isJson) {
140
150
  props.push(`additionalProperties: true`);
141
151
  }
142
152
  else {
@@ -153,6 +163,8 @@ export class DocGenField {
153
163
  buildInfos() {
154
164
  const key = this.isEnum ? "enum" : "type";
155
165
  const apiType = () => {
166
+ if (this.isJson)
167
+ return `'object'`;
156
168
  if (this.type === "Date")
157
169
  return `'string'`;
158
170
  if (this.isArray && this.isResponse) {
@@ -170,11 +182,16 @@ export class DocGenField {
170
182
  if (this.isArray) {
171
183
  return `${this.type}[]`;
172
184
  }
185
+ else if (!this.isRequired && this.fieldType === "res") {
186
+ return `${this.type} | null`;
187
+ }
173
188
  else {
174
189
  return this.type;
175
190
  }
176
191
  };
177
- const optionalFlag = this.isRequired ? "!" : "?";
192
+ let optionalFlag = "?";
193
+ if (this.isRequired || this.fieldType === "res")
194
+ optionalFlag = "!";
178
195
  const validators = this.sanitizeValidators();
179
196
  const apiExample = this.buildApiExample().join(", ");
180
197
  return {
@@ -68,6 +68,8 @@ export class DocGenModel {
68
68
  for (const e of this.response.enums) {
69
69
  this.dto.enums.add(e);
70
70
  }
71
+ if (this.response.hasJson)
72
+ this.dto.hasJson = true;
71
73
  const dtoResult = this.dto.build();
72
74
  const data = [dtoResult, responseResult, intaaa].join("");
73
75
  const fileDir = servicePrefix ? `/${servicePrefix}/types` : "/types";
@@ -5,6 +5,7 @@ export class DocGenResponse {
5
5
  // file: DocGenFile;
6
6
  fields = [];
7
7
  enums = new Set();
8
+ hasJson = false;
8
9
  constructor(model) {
9
10
  this.name = model.name;
10
11
  for (const field of model.fields) {
@@ -19,6 +20,8 @@ export class DocGenResponse {
19
20
  if (field.isEnum) {
20
21
  this.enums.add(field.type);
21
22
  }
23
+ if (field.isJson)
24
+ this.hasJson = true;
22
25
  return `class ${Helper.capitalizeFirstSafe(field.name)}Res {
23
26
  ${field.build()}
24
27
  }`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prismadoc",
3
- "version": "1.0.37",
3
+ "version": "1.0.39",
4
4
  "description": "Auto generates ApiProperties from schema.prisma",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",