nest-prisma_doc-gen 1.0.3 → 1.0.5

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.
Files changed (52) hide show
  1. package/dist/config.type.js +16 -0
  2. package/dist/entities/dto-generator.js +7 -5
  3. package/dist/entities/entity-generator.js +4 -4
  4. package/dist/entities/field.js +7 -7
  5. package/dist/index.js +0 -1
  6. package/dist/main.js +8 -6
  7. package/dist/rules.js +1 -1
  8. package/dist/schemas/config.schema.json +79 -0
  9. package/dist/static.js +0 -22
  10. package/dist/testes.js +21 -0
  11. package/dist/types.js +1 -17
  12. package/dist/utils/config-loader.js +33 -0
  13. package/dist/utils/copy-schemas.js +10 -0
  14. package/dist/{helpers → utils}/helpers.js +3 -0
  15. package/dist/utils/loader.js +47 -0
  16. package/package.json +12 -8
  17. package/dist/entities/dto-generator.d.ts +0 -13
  18. package/dist/entities/entity-generator.d.ts +0 -12
  19. package/dist/entities/enum.d.ts +0 -22
  20. package/dist/entities/field.d.ts +0 -24
  21. package/dist/entities/model.d.ts +0 -11
  22. package/dist/field.type.d.ts +0 -7
  23. package/dist/file.d.ts +0 -11
  24. package/dist/helpers/helpers.d.ts +0 -14
  25. package/dist/helpers/loader.d.ts +0 -4
  26. package/dist/helpers/loader.js +0 -50
  27. package/dist/helpers/propeties.static.d.ts +0 -2
  28. package/dist/index.d.ts +0 -2
  29. package/dist/main.d.ts +0 -13
  30. package/dist/rules.d.ts +0 -11
  31. package/dist/static.d.ts +0 -6
  32. package/dist/types.d.ts +0 -79
  33. package/src/entities/dto-generator.ts +0 -61
  34. package/src/entities/entity-generator.ts +0 -55
  35. package/src/entities/enum.ts +0 -47
  36. package/src/entities/field.ts +0 -190
  37. package/src/entities/model.ts +0 -23
  38. package/src/entities/validator.ts +0 -17
  39. package/src/field.type.ts +0 -27
  40. package/src/file.ts +0 -34
  41. package/src/helpers/helpers.ts +0 -152
  42. package/src/helpers/loader.ts +0 -60
  43. package/src/helpers/propeties.static.ts +0 -3
  44. package/src/index.ts +0 -2
  45. package/src/main.ts +0 -68
  46. package/src/rules.ts +0 -31
  47. package/src/static.ts +0 -28
  48. package/src/types/global.d.ts +0 -1
  49. package/src/types.ts +0 -109
  50. package/tsconfig.build.json +0 -5
  51. package/tsconfig.json +0 -15
  52. /package/dist/{helpers → utils}/propeties.static.js +0 -0
package/dist/main.d.ts DELETED
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env node
2
- import { DocEnums } from "./entities/enum.js";
3
- import { DocGenModel } from "./entities/model.js";
4
- import { DocFields } from "./field.type.js";
5
- export declare class DocGen {
6
- datamodel: string;
7
- properties: Set<string>;
8
- enums: DocEnums;
9
- fields: DocFields;
10
- models: DocGenModel[];
11
- init(): Promise<void>;
12
- build(): void;
13
- }
package/dist/rules.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import { ApiExampleBuilder, ValidatorBuilder } from "./types.js";
2
- export declare class DocGenRules {
3
- ignore: string[];
4
- examples: Map<string, ApiExampleBuilder>;
5
- validators: Map<string, string[]>;
6
- constructor(params: {
7
- ignore: string[];
8
- examples: ApiExampleBuilder[];
9
- validators: ValidatorBuilder[];
10
- });
11
- }
package/dist/static.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export declare class Static {
2
- static readonly AUTO_GENERATED_COMMENT: "// AUTO-GERADO: N\u00C3O EDITAR MANUALMENTE. SUJEITO A PAULADAS!";
3
- static readonly STATIC_NAMES: readonly ["Alessandro", "Pablo", "Cláudio", "Thiago", "Guilherme", "Dalton", "Kaio", "Gustavo", "Cadu", "Neyanne", "John", "Matheus", "Davi"];
4
- static getRandomString(values: string[]): string;
5
- static getRandomNumber(min?: number, max?: number): number;
6
- }
package/dist/types.d.ts DELETED
@@ -1,79 +0,0 @@
1
- export type FieldKind = "scalar" | "object" | "enum";
2
- type FieldDefaultName = "now" | "autoincrement" | "cuid";
3
- export type FieldType = "entity" | "dto";
4
- export type DbName = string | null;
5
- export type Scalar = "String" | "Int" | "BigInt" | "Float" | "Decimal" | "Boolean" | "DateTime" | "Json" | "Bytes";
6
- export type Field = {
7
- name: string;
8
- dbName: string;
9
- kind: FieldKind;
10
- isList: boolean;
11
- isRequired: boolean;
12
- isUnique: boolean;
13
- isId: boolean;
14
- isReadOnly: boolean;
15
- hasDefaultValue: boolean;
16
- type: Scalar;
17
- isGenerated: boolean;
18
- isUpdatedAt: boolean;
19
- nativeType: any;
20
- default?: FieldDefault | string;
21
- };
22
- export interface FieldDefault {
23
- name: FieldDefaultName;
24
- args: number[];
25
- }
26
- export type DocGenModel = {
27
- name: string | null;
28
- dbName: string | null;
29
- schema: string | null;
30
- readonly fields: Field[];
31
- uniqueFields: string[][];
32
- uniqueIndexes: ModelUniqueIndexes[];
33
- primaryKey: null;
34
- documentation?: string;
35
- isGenerated?: boolean;
36
- };
37
- export type ModelUniqueIndexes = {
38
- name: string | null;
39
- fields: any[];
40
- };
41
- export declare class ValidatorBuilder {
42
- decorator: ValidatorFactory | PropertyDecorator;
43
- fields: string[];
44
- constructor(decorator: PropertyDecorator, fields: string[]);
45
- }
46
- export declare class ApiExampleBuilder {
47
- fields: string[];
48
- example: string | boolean | number;
49
- constructor(fields: string[], example: string | boolean | number);
50
- }
51
- export type Rules = {
52
- ignore: string[];
53
- examples: ApiExampleBuilder[];
54
- validators: ValidatorBuilder[];
55
- };
56
- type ValidatorFactory = (...args: any[]) => PropertyDecorator;
57
- export type Model = {
58
- name: string;
59
- dbName: string | null;
60
- schema: string | null;
61
- readonly fields: Field[];
62
- uniqueFields: string[][];
63
- uniqueIndexes: ModelUniqueIndexes[];
64
- primaryKey: null;
65
- documentation?: string;
66
- isGenerated?: boolean;
67
- };
68
- export type ModelParams = {
69
- model: Model;
70
- examples: Map<string, ApiExampleBuilder>;
71
- validators: Map<string, string[]>;
72
- };
73
- export type FieldParams = {
74
- examples: Map<string, ApiExampleBuilder>;
75
- validators: Map<string, string[]>;
76
- field: Field;
77
- fieldType: FieldType;
78
- };
79
- export {};
@@ -1,61 +0,0 @@
1
- import { DocGenFile } from "../file.js";
2
- import { Helper } from "../helpers/helpers.js";
3
- import { Static } from "../static.js";
4
- import { Model } from "../types.js";
5
- import { DocGenField } from "./field.js";
6
-
7
- export class DocGenDto {
8
- name: string;
9
- file: DocGenFile;
10
- fields: DocGenField[] = [];
11
- imports = new Set([`${Static.AUTO_GENERATED_COMMENT}`, `import { ApiProperty } from '@nestjs/swagger'`]);
12
- classValidators = new Set<string>();
13
- enums = new Set<string>();
14
-
15
- constructor(model: Model) {
16
- this.name = model.name;
17
-
18
- model.fields.forEach((field) => {
19
- if (field.isUpdatedAt || field.isId || field.name === "createdAt" || field.kind === "object") return;
20
-
21
- this.fields.push(new DocGenField(field, "dto"));
22
- });
23
-
24
- this.file = new DocGenFile({
25
- dir: "/dto",
26
- fileName: `${Helper.toKebab(this.name)}.dto.ts`,
27
- data: this.build(),
28
- });
29
- }
30
-
31
- build() {
32
- const sanitizedFields = this.fields
33
- .map((field) => {
34
- field.validators.forEach((v) => this.classValidators.add(v.name));
35
-
36
- if (field.isEntity) {
37
- this.imports.add(`import { ${field.type} } from '../entities/${Helper.toKebab(field.scalarType)}.entity'`);
38
- this.imports.add(`import { generateExample } from 'src/utils/functions/reflect'`);
39
- } else if (field.isEnum) {
40
- this.enums.add(field.type);
41
- }
42
-
43
- return field.build();
44
- })
45
- .join("\n\n");
46
-
47
- if (this.enums.size > 0) {
48
- this.classValidators.add("IsEnum");
49
- this.imports.add(`import { ${Array.from(this.enums)} } from '../enums';`);
50
- }
51
-
52
- this.imports.add(`import { ${Array.from(this.classValidators)} } from 'class-validator';`);
53
-
54
- return [
55
- `${Array.from(this.imports).join("\n")}`,
56
- `export class ${this.name}Dto {
57
- ${sanitizedFields}
58
- }`,
59
- ].join("\n\n");
60
- }
61
- }
@@ -1,55 +0,0 @@
1
- import { DocGenFile } from "../file.js";
2
- import { Helper } from "../helpers/helpers.js";
3
- import { Static } from "../static.js";
4
- import { Model } from "../types.js";
5
- import { DocGenField } from "./field.js";
6
-
7
- export class DocGenEntity {
8
- name: string;
9
- file: DocGenFile;
10
- fields: DocGenField[] = [];
11
- imports = new Set([`${Static.AUTO_GENERATED_COMMENT}`, `import { ApiProperty } from '@nestjs/swagger'`]);
12
- enums = new Set<string>();
13
-
14
- constructor(model: Model) {
15
- this.name = model.name;
16
-
17
- model.fields.forEach((field) => {
18
- if (field.kind === "object") return;
19
-
20
- this.fields.push(new DocGenField(field, "entity"));
21
- });
22
-
23
- this.file = new DocGenFile({
24
- dir: "/entity",
25
- fileName: `${Helper.toKebab(this.name)}.entity.ts`,
26
- data: this.build(),
27
- });
28
- }
29
-
30
- build() {
31
- const sanitizedFields = this.fields
32
- .map((field) => {
33
- if (field.isEntity) {
34
- this.imports.add(`import { ${field.type} } from './${Helper.toKebab(field.scalarType)}.entity'`);
35
- this.imports.add(`import { generateExample } from 'src/utils/functions/reflect'`);
36
- } else if (field.isEnum) {
37
- this.enums.add(field.type);
38
- }
39
-
40
- return field.build();
41
- })
42
- .join("\n\n");
43
-
44
- if (this.enums.size > 0) {
45
- this.imports.add(`import { ${Array.from(this.enums)} } from '../enums';`);
46
- }
47
-
48
- return [
49
- `${Array.from(this.imports).join("\n")}`,
50
- `export class ${this.name}Entity {
51
- ${sanitizedFields}
52
- }`,
53
- ].join("\n\n");
54
- }
55
- }
@@ -1,47 +0,0 @@
1
- import { DocGenFile } from "../file.js";
2
- import { DbName } from "../types.js";
3
-
4
- export type EnumValue = { name: string; dbName: DbName };
5
-
6
- export class DocGenEnum {
7
- name: string;
8
- values: EnumValue[];
9
- dbName: DbName;
10
-
11
- constructor(params: { name: string; values: EnumValue[]; dbName: DbName }) {
12
- const { dbName, name, values } = params;
13
- this.dbName = dbName;
14
- this.name = name;
15
- this.values = values;
16
- }
17
- }
18
-
19
- export class DocEnums {
20
- enums: DocGenEnum[];
21
- file: DocGenFile;
22
-
23
- constructor(enums: DocGenEnum[]) {
24
- this.enums = enums;
25
-
26
- this.file = new DocGenFile({
27
- dir: "/",
28
- fileName: "enums.ts",
29
- data: this.build(),
30
- });
31
- }
32
-
33
- build() {
34
- const enums = this.enums.map((en) => {
35
- const enumName = en.name;
36
- return `
37
- export const ${enumName} = [${en.values.map((n) => `'${n.name}'`)}] as const;
38
- export type ${enumName} = typeof ${enumName}[number];
39
- `;
40
- });
41
-
42
- return `
43
- // AUTO-GERADO: NÃO EDITAR MANUALMENTE. SUJEITO A PAULADAS!
44
- ${enums.join("\n")}
45
- `;
46
- }
47
- }
@@ -1,190 +0,0 @@
1
- import { Helper } from "../helpers/helpers.js";
2
- import { config } from "../helpers/loader.js";
3
- import { Static } from "../static.js";
4
- import { FieldDefault, Scalar, FieldKind, FieldType, Field } from "../types.js";
5
- import { Validator } from "./validator.js";
6
-
7
- const helpers = new Helper();
8
- const rules = config;
9
-
10
- export class DocGenField {
11
- name: string;
12
- isList: boolean;
13
- default?: FieldDefault | string;
14
- scalarType: Scalar;
15
- kind: FieldKind;
16
-
17
- type!: string;
18
- fieldType: FieldType;
19
-
20
- isEnum: boolean = false;
21
- isEntity: boolean = false;
22
- isUpdatedAt: boolean = false;
23
- isRequired: boolean;
24
- validators = new Set<Validator>();
25
-
26
- readonly scalarField: Field;
27
-
28
- constructor(field: Field, fieldType: FieldType) {
29
- const { name, isList, type, kind, isRequired, isUpdatedAt } = field;
30
-
31
- this.name = name;
32
- this.isList = isList;
33
- this.scalarType = type;
34
- this.kind = kind;
35
- this.isRequired = isRequired;
36
- this.scalarField = field;
37
- this.isUpdatedAt = isUpdatedAt;
38
- this.fieldType = fieldType;
39
-
40
- this.setType();
41
- this.setValidators();
42
- }
43
-
44
- private processValidator(name: string) {
45
- const validator = new Validator({ name });
46
- if (this.isList) validator.content = "{ each: true }";
47
-
48
- this.validators.add(validator);
49
- }
50
-
51
- private setValidators() {
52
- if (this.scalarType === "String" || this.scalarType === "DateTime" || this.scalarType === "Json") {
53
- this.processValidator("IsString");
54
-
55
- if (this.isRequired) {
56
- this.processValidator("IsNotEmpty");
57
- }
58
- } else if (this.scalarType === "Boolean") {
59
- this.processValidator("IsBoolean");
60
- } else if (
61
- this.scalarType === "Int" ||
62
- this.scalarType === "BigInt" ||
63
- this.scalarType === "Float" ||
64
- this.scalarType === "Decimal"
65
- ) {
66
- this.processValidator("IsNumber");
67
- }
68
-
69
- if (this.isList) {
70
- const validator = new Validator({ name: "IsArray" });
71
- this.validators.add(validator);
72
- }
73
-
74
- if (!this.isRequired) this.processValidator("IsOptional");
75
-
76
- if (this.isEnum) {
77
- this.validators.add(
78
- new Validator({
79
- name: "IsEnum",
80
- content: this.type,
81
- })
82
- );
83
- }
84
-
85
- const findedDecorators = rules.validators.get(this.name);
86
-
87
- if (findedDecorators) {
88
- findedDecorators.forEach((name) => {
89
- this.processValidator(name);
90
- });
91
- }
92
- }
93
-
94
- private setType() {
95
- if (this.kind === "enum") {
96
- this.isEnum = true;
97
- this.type = this.scalarType;
98
- } else if (this.kind === "object") {
99
- this.isEntity = true;
100
- this.type = `${this.scalarType}Entity`;
101
- } else if (this.kind === "scalar") {
102
- this.type = Helper.prismaScalarToTs(this.scalarType);
103
- }
104
- }
105
-
106
- private buildApiExample(): string[] {
107
- const fieldName = this.scalarField.name;
108
- const props: string[] = [];
109
- const scalarDbName = this.scalarField.dbName ?? "genericDbName";
110
-
111
- if (this.isEntity) {
112
- if (this.isList) {
113
- props.push(`example: [generateExample(${this.type})]`);
114
- } else {
115
- props.push(`example: generateExample(${this.type})`);
116
- }
117
- } else if (rules.examples.has(fieldName)) {
118
- props.push(`example: '${rules.examples.get(fieldName)?.example}'`);
119
- } else if (helpers.isDate(this.scalarField)) {
120
- props.push(`example: '2025-09-03T03:00:00.000Z'`);
121
- } else if (this.scalarField.isId || (this.scalarField.isReadOnly && scalarDbName.split("_").includes("id"))) {
122
- props.push(`example: 'cmfxu4njg000008l52v7t8qze'`);
123
- } else if (this.scalarField.type === "Boolean") {
124
- props.push(`example: true`);
125
- } else if (this.scalarField.kind === "enum") {
126
- props.push(`example: ${this.scalarField.type}[0]`);
127
- } else if (this.scalarField.type === "Int") {
128
- props.push(`example: ${Static.getRandomNumber()}`);
129
- } else if (this.scalarField.type === "String") {
130
- props.push(`example: 'ordinary string'`);
131
- }
132
-
133
- props.push(`required: ${this.scalarField.isRequired}`);
134
- return props;
135
- }
136
-
137
- private sanitizeValidators() {
138
- const sanitizedValidators = Array.from(this.validators).map((validator) => {
139
- return validator.build();
140
- });
141
-
142
- return sanitizedValidators;
143
- }
144
-
145
- private buildInfos() {
146
- const key = this.isEnum ? "enum" : "type";
147
-
148
- const apiType = () => {
149
- if (this.type === "Date") return `'string'`;
150
-
151
- if (this.isList && this.isEntity) {
152
- return `[${this.type}]`;
153
- } else if (this.isEnum) {
154
- return this.type;
155
- } else if (this.isEntity) {
156
- return `() => ${this.type}`;
157
- }
158
-
159
- return `'${this.type}'`;
160
- };
161
-
162
- const fieldType = () => {
163
- if (this.isList) {
164
- return `${this.type}[]`;
165
- } else {
166
- return this.type;
167
- }
168
- };
169
- const optionalFlag = this.isRequired ? "" : "?";
170
-
171
- const validators = this.sanitizeValidators();
172
- const apiExample = this.buildApiExample().join(", ");
173
-
174
- return {
175
- apiProperty: `@ApiProperty({ ${key}: ${apiType()}, ${apiExample} })`,
176
- validators,
177
- atributes: `${this.name}${optionalFlag}: ${fieldType()};`,
178
- };
179
- }
180
-
181
- build() {
182
- const { apiProperty, atributes, validators } = this.buildInfos();
183
-
184
- if (this.fieldType === "dto") {
185
- return [apiProperty, ...validators, atributes].join("\n");
186
- } else {
187
- return [apiProperty, atributes].join("\n");
188
- }
189
- }
190
- }
@@ -1,23 +0,0 @@
1
- import { Field, Model } from "../types.js";
2
- import { DocGenDto } from "./dto-generator.js";
3
- import { DocGenEntity } from "./entity-generator.js";
4
-
5
- export class DocGenModel {
6
- name: string;
7
- entitie: DocGenEntity;
8
- createDtos: DocGenDto;
9
- fields: Field[];
10
-
11
- constructor(model: Model) {
12
- this.name = model.name;
13
- this.fields = model.fields;
14
-
15
- this.entitie = new DocGenEntity(model);
16
- this.createDtos = new DocGenDto(model);
17
- }
18
-
19
- save() {
20
- this.entitie.file.save();
21
- this.createDtos.file.save();
22
- }
23
- }
@@ -1,17 +0,0 @@
1
- import { Scalar } from "../types.js";
2
-
3
- export class Validator {
4
- name: string;
5
- content: string;
6
-
7
- constructor(params: { name: string; content?: string }) {
8
- const { content, name } = params;
9
-
10
- this.name = name;
11
- this.content = content ?? "";
12
- }
13
-
14
- build() {
15
- return `@${this.name}(${this.content})`;
16
- }
17
- }
package/src/field.type.ts DELETED
@@ -1,27 +0,0 @@
1
- import { DocGenFile } from "./file.js";
2
- import { Static } from "./static.js";
3
-
4
- export class DocFields {
5
- fields: string;
6
- file: DocGenFile;
7
-
8
- constructor(fields: string) {
9
- this.fields = fields;
10
-
11
- this.file = new DocGenFile({
12
- dir: "/",
13
- fileName: "fields.ts",
14
- data: this.build(),
15
- });
16
- }
17
-
18
- build() {
19
- const content = `
20
- ${Static.AUTO_GENERATED_COMMENT}
21
- export const FIELD_NAMES = [${this.fields}] as const
22
- export type FieldName = (typeof FIELD_NAMES)[number];
23
- `;
24
-
25
- return content;
26
- }
27
- }
package/src/file.ts DELETED
@@ -1,34 +0,0 @@
1
- import { promises as fs } from "node:fs";
2
- import * as path from "node:path";
3
- import * as prettier from "prettier";
4
-
5
- const ROOT = process.cwd();
6
- const OUT_DIR = path.join(ROOT, "src/types/docgen");
7
-
8
- export class DocGenFile {
9
- outDir: string;
10
- data: string;
11
-
12
- constructor(params: { fileName: string; dir: string; data: string; customDir?: string }) {
13
- const { fileName, dir, data, customDir } = params;
14
-
15
- this.outDir = path.join(OUT_DIR, dir, fileName);
16
- this.data = data;
17
-
18
- if (customDir) this.outDir = customDir;
19
- }
20
-
21
- async save() {
22
- const dir = path.dirname(this.outDir);
23
- await fs.mkdir(dir, { recursive: true });
24
-
25
- const prettierConfig = await prettier.resolveConfig(this.outDir);
26
-
27
- const formatted = await prettier.format(this.data, {
28
- ...prettierConfig,
29
- filepath: this.outDir,
30
- });
31
-
32
- await fs.writeFile(this.outDir, formatted, "utf-8");
33
- }
34
- }