nest-prisma_doc-gen 1.0.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/dist/entities/dto-generator.d.ts +13 -0
- package/dist/entities/dto-generator.js +51 -0
- package/dist/entities/entity-generator.d.ts +12 -0
- package/dist/entities/entity-generator.js +47 -0
- package/dist/entities/enum.d.ts +22 -0
- package/dist/entities/enum.js +37 -0
- package/dist/entities/field.d.ts +24 -0
- package/dist/entities/field.js +169 -0
- package/dist/entities/model.d.ts +11 -0
- package/dist/entities/model.js +18 -0
- package/dist/entities/validator.js +12 -0
- package/dist/field.type.d.ts +7 -0
- package/dist/field.type.js +22 -0
- package/dist/file.d.ts +11 -0
- package/dist/file.js +26 -0
- package/dist/helpers/helpers.d.ts +14 -0
- package/dist/helpers/helpers.js +143 -0
- package/dist/helpers/loader.d.ts +4 -0
- package/dist/helpers/loader.js +50 -0
- package/dist/helpers/propeties.static.d.ts +2 -0
- package/dist/helpers/propeties.static.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/main.d.ts +13 -0
- package/dist/main.js +53 -0
- package/dist/rules.d.ts +11 -0
- package/dist/rules.js +19 -0
- package/dist/static.d.ts +6 -0
- package/dist/static.js +25 -0
- package/dist/types.d.ts +79 -0
- package/dist/types.js +17 -0
- package/package.json +40 -0
- package/src/entities/dto-generator.ts +61 -0
- package/src/entities/entity-generator.ts +55 -0
- package/src/entities/enum.ts +47 -0
- package/src/entities/field.ts +188 -0
- package/src/entities/model.ts +23 -0
- package/src/entities/validator.ts +17 -0
- package/src/field.type.ts +27 -0
- package/src/file.ts +34 -0
- package/src/helpers/helpers.ts +152 -0
- package/src/helpers/loader.ts +60 -0
- package/src/helpers/propeties.static.ts +3 -0
- package/src/index.ts +2 -0
- package/src/main.ts +69 -0
- package/src/rules.ts +31 -0
- package/src/static.ts +28 -0
- package/src/types/global.d.ts +1 -0
- package/src/types.ts +109 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import "tsx/esm";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { DocGenRules } from "../rules.js";
|
|
6
|
+
export class DocGenConfig {
|
|
7
|
+
ignore;
|
|
8
|
+
examples;
|
|
9
|
+
validators;
|
|
10
|
+
constructor(configs) {
|
|
11
|
+
const { examples, ignore, validators } = configs;
|
|
12
|
+
this.ignore = ignore;
|
|
13
|
+
this.examples = examples;
|
|
14
|
+
this.validators = validators;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Carrega o arquivo doc-gen.config.ts|.js da raiz do projeto.
|
|
18
|
+
*/
|
|
19
|
+
static async load() {
|
|
20
|
+
const ROOT = process.cwd();
|
|
21
|
+
const CONFIG_PATH_TS = path.join(ROOT, "doc-gen.config.mts");
|
|
22
|
+
const CONFIG_PATH_JS = path.join(ROOT, "doc-gen.config.mjs");
|
|
23
|
+
let configFile;
|
|
24
|
+
if (fs.existsSync(CONFIG_PATH_TS))
|
|
25
|
+
configFile = CONFIG_PATH_TS;
|
|
26
|
+
else if (fs.existsSync(CONFIG_PATH_JS))
|
|
27
|
+
configFile = CONFIG_PATH_JS;
|
|
28
|
+
if (!configFile) {
|
|
29
|
+
console.warn("⚠️ Nenhum arquivo doc-gen.config.ts|.js encontrado. Usando configuração vazia.");
|
|
30
|
+
return this.newEmpty();
|
|
31
|
+
}
|
|
32
|
+
await import("tsx/esm");
|
|
33
|
+
const imported = await import(pathToFileURL(configFile).href);
|
|
34
|
+
const rules = imported.rules;
|
|
35
|
+
if (!rules) {
|
|
36
|
+
console.warn("⚠️ O arquivo doc-gen.config.ts|.js não exporta 'rules'.");
|
|
37
|
+
return this.newEmpty();
|
|
38
|
+
}
|
|
39
|
+
return this.newWithConfigs(rules);
|
|
40
|
+
}
|
|
41
|
+
/** Cria uma instância vazia */
|
|
42
|
+
static newEmpty() {
|
|
43
|
+
return new DocGenConfig(new DocGenRules({ ignore: [], examples: [], validators: [] }));
|
|
44
|
+
}
|
|
45
|
+
/** Cria uma instância a partir de configurações */
|
|
46
|
+
static newWithConfigs(params) {
|
|
47
|
+
return new DocGenConfig(new DocGenRules(params));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export const config = await DocGenConfig.load();
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
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/main.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { DocEnums, DocGenEnum } from "./entities/enum.js";
|
|
4
|
+
import { DocGenModel } from "./entities/model.js";
|
|
5
|
+
import { DocFields } from "./field.type.js";
|
|
6
|
+
import { Helper } from "./helpers/helpers.js";
|
|
7
|
+
import prismaPkg from "@prisma/internals";
|
|
8
|
+
const { getDMMF } = prismaPkg;
|
|
9
|
+
const ROOT = process.cwd();
|
|
10
|
+
const PRISMA_DIR = path.join(ROOT, "prisma");
|
|
11
|
+
export class DocGen {
|
|
12
|
+
datamodel;
|
|
13
|
+
properties;
|
|
14
|
+
enums;
|
|
15
|
+
fields;
|
|
16
|
+
models;
|
|
17
|
+
async init() {
|
|
18
|
+
const prismaDataModel = await Helper.readPrismaFolderDatamodel(PRISMA_DIR);
|
|
19
|
+
const { datamodel } = await getDMMF({ datamodel: prismaDataModel });
|
|
20
|
+
this.enums = new DocEnums(datamodel.enums.map(({ dbName, name, values }) => {
|
|
21
|
+
return new DocGenEnum({
|
|
22
|
+
dbName: dbName ?? "",
|
|
23
|
+
name,
|
|
24
|
+
values: values,
|
|
25
|
+
});
|
|
26
|
+
}));
|
|
27
|
+
const fieldSet = new Set();
|
|
28
|
+
datamodel.models.forEach((model) => {
|
|
29
|
+
model.fields.forEach((field) => fieldSet.add(`'${field.name}'`));
|
|
30
|
+
});
|
|
31
|
+
this.fields = new DocFields(Array.from(fieldSet).toString());
|
|
32
|
+
this.models = datamodel.models.map((model) => {
|
|
33
|
+
return new DocGenModel(model);
|
|
34
|
+
});
|
|
35
|
+
this.build();
|
|
36
|
+
}
|
|
37
|
+
build() {
|
|
38
|
+
this.fields.file.save();
|
|
39
|
+
this.enums.file.save();
|
|
40
|
+
this.models.forEach((model) => {
|
|
41
|
+
model.save();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const generator = new DocGen();
|
|
46
|
+
try {
|
|
47
|
+
await generator.init();
|
|
48
|
+
console.log("✅ DTOs gerados com sucesso!");
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
console.error("❌ Erro ao gerar DTOs:", err);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
package/dist/rules.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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/rules.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class DocGenRules {
|
|
2
|
+
ignore;
|
|
3
|
+
examples;
|
|
4
|
+
validators;
|
|
5
|
+
constructor(params) {
|
|
6
|
+
const { examples, ignore, validators } = params;
|
|
7
|
+
this.ignore = ignore;
|
|
8
|
+
if (!validators.length) {
|
|
9
|
+
console.warn("[doc-gen] Nenhum validator encontrado. Verifique seu docgen.config.ts*");
|
|
10
|
+
}
|
|
11
|
+
const allFields = validators.flatMap((validator) => validator.fields);
|
|
12
|
+
const uniqueFieldsToValidate = [...new Set(allFields)];
|
|
13
|
+
this.examples = new Map(examples.flatMap((builder) => builder.fields.map((field) => [field, builder])));
|
|
14
|
+
this.validators = new Map(uniqueFieldsToValidate.map((field) => [
|
|
15
|
+
field,
|
|
16
|
+
validators.filter((validator) => validator.fields.includes(field)).map((validator) => validator.decorator.name),
|
|
17
|
+
]));
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/static.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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/static.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class Static {
|
|
2
|
+
static AUTO_GENERATED_COMMENT = "// AUTO-GERADO: NÃO EDITAR MANUALMENTE. SUJEITO A PAULADAS!";
|
|
3
|
+
static STATIC_NAMES = [
|
|
4
|
+
"Alessandro",
|
|
5
|
+
"Pablo",
|
|
6
|
+
"Cláudio",
|
|
7
|
+
"Thiago",
|
|
8
|
+
"Guilherme",
|
|
9
|
+
"Dalton",
|
|
10
|
+
"Kaio",
|
|
11
|
+
"Gustavo",
|
|
12
|
+
"Cadu",
|
|
13
|
+
"Neyanne",
|
|
14
|
+
"John",
|
|
15
|
+
"Matheus",
|
|
16
|
+
"Davi",
|
|
17
|
+
];
|
|
18
|
+
static getRandomString(values) {
|
|
19
|
+
const idx = Math.floor(Math.random() * values.length);
|
|
20
|
+
return values[idx];
|
|
21
|
+
}
|
|
22
|
+
static getRandomNumber(min = 0, max = 1000) {
|
|
23
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
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 {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class ValidatorBuilder {
|
|
2
|
+
decorator;
|
|
3
|
+
fields;
|
|
4
|
+
constructor(decorator, fields) {
|
|
5
|
+
this.decorator = decorator;
|
|
6
|
+
this.fields = fields;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class ApiExampleBuilder {
|
|
10
|
+
fields;
|
|
11
|
+
example;
|
|
12
|
+
constructor(fields, example) {
|
|
13
|
+
this.fields = fields;
|
|
14
|
+
this.example = example;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// field: Field, fieldType: FieldType
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nest-prisma_doc-gen",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Auto generates ApiProperties from schema.prisma",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"generate": "node dist/tools/generate-dtos.js"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/aleleppy/nest-prisma-doc-gen.git"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"doc-gen": "dist/main.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"scalar",
|
|
21
|
+
"prisma",
|
|
22
|
+
"nestJS",
|
|
23
|
+
"documentation",
|
|
24
|
+
"swagger",
|
|
25
|
+
"automatic",
|
|
26
|
+
"generation"
|
|
27
|
+
],
|
|
28
|
+
"author": "Alessandro Lepore",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@prisma/internals": "^6.17.1",
|
|
32
|
+
"prettier": "^3.6.2",
|
|
33
|
+
"ts-node": "^10.9.2"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^24.7.2",
|
|
37
|
+
"tsx": "^4.20.6",
|
|
38
|
+
"typescript": "^5.6.3"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
}
|