nest-prisma_doc-gen 1.0.19 → 1.0.21

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.
@@ -1,31 +1,23 @@
1
- import { DocGenFile } from "../file.js";
2
1
  import { Helper } from "../utils/helpers.js";
3
2
  import { Static } from "../static.js";
4
3
  import { DocGenField } from "./field.js";
5
4
  import { config } from "../utils/loader.js";
6
5
  export class DocGenDto {
7
6
  name;
8
- file;
7
+ // file: DocGenFile;
9
8
  fields = [];
10
- imports = new Set([
11
- `${Static.AUTO_GENERATED_COMMENT}`,
12
- `import { ApiProperty, IntersectionType } from '@nestjs/swagger'`,
13
- `import { DefaultIdDtoDG } from '../generic.dto'`,
14
- ]);
9
+ imports = new Set([`${Static.AUTO_GENERATED_COMMENT}`, `import { ApiProperty } from '@nestjs/swagger'`]);
15
10
  classValidators = new Set();
16
11
  enums = new Set();
17
12
  constructor(model) {
18
13
  this.name = model.name;
14
+ this.classValidators.add("IsString");
15
+ this.classValidators.add("IsNotEmpty");
19
16
  for (const field of model.fields) {
20
17
  if (field.isUpdatedAt || field.isId || field.name === "createdAt" || field.kind === "object")
21
18
  continue;
22
19
  this.fields.push(new DocGenField(field, "dto"));
23
20
  }
24
- this.file = new DocGenFile({
25
- dir: "/dto",
26
- fileName: `${Helper.toKebab(this.name)}.dto.ts`,
27
- data: this.build(),
28
- });
29
21
  }
30
22
  build() {
31
23
  const sanitizedFields = this.fields
@@ -50,13 +42,16 @@ export class DocGenDto {
50
42
  this.imports.add(`import { ${Array.from(this.classValidators)} } from '${config.validatorPath}';`);
51
43
  return [
52
44
  `${Array.from(this.imports).join("\n")}`,
53
- `export class ${this.name}DtoDG {
45
+ `class ${this.name}Dto {
54
46
  ${sanitizedFields}
55
47
  }`,
56
- `export class ${this.name}WithIdDtoDG extends IntersectionType(
57
- ${this.name}DtoDG,
58
- DefaultIdDtoDG,
59
- ) {}`,
48
+ `class ${this.name}Id {
49
+ @ApiProperty({ type: 'string', example: 'cmfxu4njg000008l52v7t8qze', required: true })
50
+ @IsString()
51
+ @IsNotEmpty()
52
+ ${this.name.toLocaleLowerCase()}Id!: string;
53
+ }
54
+ `,
60
55
  ].join("\n\n");
61
56
  }
62
57
  }
@@ -79,7 +79,7 @@ export class DocGenField {
79
79
  }
80
80
  else if (this.kind === "object") {
81
81
  this.isResponse = true;
82
- this.type = `${this.scalarType}ResDG`;
82
+ this.type = `${this.scalarType}Res`;
83
83
  }
84
84
  else if (this.kind === "scalar") {
85
85
  this.type = Helper.prismaScalarToTs(this.scalarType);
@@ -8,7 +8,7 @@ export class DocGenGeneric {
8
8
  import { IsString, IsNotEmpty } from '${config.validatorPath}';
9
9
  `;
10
10
  const validatorProps = `
11
- export class DefaultIdDtoDG {
11
+ export class DefaultIdDto {
12
12
  @ApiProperty({ type: 'string', example: 'cmfxu4njg000008l52v7t8qze', required: true })
13
13
  @IsString()
14
14
  @IsNotEmpty()
@@ -1,18 +1,38 @@
1
+ import { DocGenFile } from "../file.js";
2
+ import { Helper } from "../utils/helpers.js";
1
3
  import { DocGenDto } from "./dto-generator.js";
2
4
  import { DocGenResponse } from "./response-generator.js";
3
5
  export class DocGenModel {
4
6
  name;
5
- responses;
6
- dtos;
7
+ response;
8
+ dto;
7
9
  fields;
10
+ exports;
11
+ file;
8
12
  constructor(model) {
9
13
  this.name = model.name;
10
14
  this.fields = model.fields;
11
- this.responses = new DocGenResponse(model);
12
- this.dtos = new DocGenDto(model);
15
+ this.response = new DocGenResponse(model);
16
+ this.dto = new DocGenDto(model);
17
+ this.exports = [`export * from './types/${Helper.toKebab(this.name)}'`];
18
+ const intaaa = `
19
+ export namespace ${this.name} {
20
+ export const Dto = ${this.name}Dto;
21
+ export type Dto = ${this.name}Dto;
22
+ export const Res = ${this.name}Res;
23
+ export type Res = ${this.name}Res;
24
+ export const Id = ${this.name}Id;
25
+ export type Id = ${this.name}Id;
26
+ }
27
+ `;
28
+ const data = [this.dto.build(), this.response.build(), intaaa].join("");
29
+ this.file = new DocGenFile({
30
+ dir: "/types",
31
+ fileName: `${Helper.toKebab(this.name)}.ts`,
32
+ data,
33
+ });
13
34
  }
14
35
  save() {
15
- this.responses.file.save();
16
- this.dtos.file.save();
36
+ this.file.save();
17
37
  }
18
38
  }
@@ -1,12 +1,8 @@
1
- import { DocGenFile } from "../file.js";
2
- import { Helper } from "../utils/helpers.js";
3
- import { Static } from "../static.js";
4
1
  import { DocGenField } from "./field.js";
5
2
  export class DocGenResponse {
6
3
  name;
7
- file;
4
+ // file: DocGenFile;
8
5
  fields = [];
9
- imports = new Set([`${Static.AUTO_GENERATED_COMMENT}`, `import { ApiProperty } from '@nestjs/swagger'`]);
10
6
  enums = new Set();
11
7
  constructor(model) {
12
8
  this.name = model.name;
@@ -15,31 +11,18 @@ export class DocGenResponse {
15
11
  continue;
16
12
  this.fields.push(new DocGenField(field, "res"));
17
13
  }
18
- this.file = new DocGenFile({
19
- dir: "/res",
20
- fileName: `${Helper.toKebab(this.name)}.res.ts`,
21
- data: this.build(),
22
- });
23
14
  }
24
15
  build() {
25
16
  const sanitizedFields = this.fields
26
17
  .map((field) => {
27
- if (field.isResponse) {
28
- this.imports.add(`import { ${field.type} } from './${Helper.toKebab(field.scalarType)}.res'`);
29
- this.imports.add(`import { generateExample } from 'src/utils/functions/reflect'`);
30
- }
31
- else if (field.isEnum) {
18
+ if (field.isEnum) {
32
19
  this.enums.add(field.type);
33
20
  }
34
21
  return field.build();
35
22
  })
36
23
  .join("\n\n");
37
- if (this.enums.size > 0) {
38
- this.imports.add(`import { ${Array.from(this.enums)} } from '@prisma/client';`);
39
- }
40
24
  return [
41
- `${Array.from(this.imports).join("\n")}`,
42
- `export class ${this.name}ResDG {
25
+ `class ${this.name}Res {
43
26
  ${sanitizedFields}
44
27
  }`,
45
28
  ].join("\n\n");
package/dist/main.js CHANGED
@@ -5,6 +5,8 @@ import { DocFields } from "./field.type.js";
5
5
  import prismaPkg from "@prisma/internals";
6
6
  import { PrismaUtils } from "./utils/prisma-utils.js";
7
7
  import { DocGenGeneric } from "./entities/generic.js";
8
+ import { DocGenFile } from "./file.js";
9
+ import { Helper } from "./utils/helpers.js";
8
10
  const { getDMMF } = prismaPkg;
9
11
  const ROOT = process.cwd();
10
12
  const PRISMA_DIR = path.join(ROOT, "src");
@@ -15,6 +17,8 @@ export class DocGen {
15
17
  fields;
16
18
  models;
17
19
  generic = new DocGenGeneric();
20
+ indexFile;
21
+ fieldFile;
18
22
  async init() {
19
23
  const prismaDataModel = await PrismaUtils.readPrismaFolderDatamodel(PRISMA_DIR);
20
24
  const { datamodel } = await getDMMF({ datamodel: prismaDataModel });
@@ -33,9 +37,65 @@ export class DocGen {
33
37
  build() {
34
38
  this.generic.build();
35
39
  this.fields.file.save();
40
+ const indexFileData = [`export * from './fields.types'`];
41
+ const fields = [];
42
+ const enumsSet = new Set();
43
+ const classValidatorsSet = new Set();
36
44
  for (const model of this.models) {
45
+ indexFileData.push(...model.exports);
46
+ fields.push(...model.dto.fields);
47
+ for (const e of model.dto.enums) {
48
+ enumsSet.add(e);
49
+ }
50
+ for (const classValidator of model.dto.classValidators) {
51
+ classValidatorsSet.add(classValidator);
52
+ }
37
53
  model.save();
38
54
  }
55
+ indexFileData.push("export * as DG from 'src/types/docgen/index';");
56
+ const fieldMap = new Map();
57
+ for (const field of fields) {
58
+ if (fieldMap.has(field.name))
59
+ continue;
60
+ fieldMap.set(field.name, field);
61
+ }
62
+ const imports = new Set([`import { ApiProperty } from '@nestjs/swagger'`]);
63
+ const validators = `import { ${Array.from(classValidatorsSet)} } from 'src/_nest/validators';`;
64
+ const exportTypes = [];
65
+ const fieldClasses = Array.from(fieldMap)
66
+ .map(([_, field]) => {
67
+ field.isRequired = true;
68
+ const name = Helper.capitalizeFirstSafe(field.name);
69
+ exportTypes.push(`
70
+ export const ${name} = ${name}Dto
71
+ `);
72
+ return `
73
+ class ${name}Dto {
74
+ ${field.build()}
75
+ }
76
+ `;
77
+ })
78
+ .join("\n");
79
+ imports.add(`import { ${Array.from(enumsSet)} } from '@prisma/client';`);
80
+ imports.add(validators);
81
+ const teste = `
82
+ export namespace Input {
83
+ ${exportTypes.join(";")}
84
+ }
85
+ `;
86
+ const fieldFileContent = [Array.from(imports).join("\n"), fieldClasses, teste];
87
+ this.fieldFile = new DocGenFile({
88
+ fileName: "fields.types.ts",
89
+ dir: "",
90
+ data: fieldFileContent.join("\n"),
91
+ });
92
+ this.indexFile = new DocGenFile({
93
+ fileName: "index.ts",
94
+ dir: "",
95
+ data: indexFileData.join("\n"),
96
+ });
97
+ this.indexFile.save();
98
+ this.fieldFile.save();
39
99
  }
40
100
  }
41
101
  const generator = new DocGen();
@@ -72,7 +72,7 @@ export class Helper {
72
72
  return undefined;
73
73
  }
74
74
  static toKebab(s) {
75
- return s.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
75
+ return s.replaceAll(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
76
76
  }
77
77
  importsForModel(model) {
78
78
  const needsEnum = model.fields.some((field) => field.kind === "enum");
@@ -135,4 +135,12 @@ export class Helper {
135
135
  isDate(field) {
136
136
  return field.kind === "scalar" && field.type === "DateTime";
137
137
  }
138
+ // Mais segura (remove espaços do começo e lida melhor com alguns caracteres)
139
+ static capitalizeFirstSafe(str) {
140
+ const s = str.trimStart();
141
+ if (!s)
142
+ return s;
143
+ const [first, ...rest] = Array.from(s); // lida melhor com unicode
144
+ return first.toUpperCase() + rest.join("");
145
+ }
138
146
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nest-prisma_doc-gen",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "Auto generates ApiProperties from schema.prisma",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",