nest-prisma_doc-gen 1.0.8 → 1.0.10
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.
|
@@ -40,7 +40,7 @@ export class DocGenDto {
|
|
|
40
40
|
.join("\n\n");
|
|
41
41
|
if (this.enums.size > 0) {
|
|
42
42
|
this.classValidators.add("IsEnum");
|
|
43
|
-
this.imports.add(`import { ${Array.from(this.enums)} } from '
|
|
43
|
+
this.imports.add(`import { ${Array.from(this.enums)} } from '@prisma/client';`);
|
|
44
44
|
}
|
|
45
45
|
this.imports.add(`import { ${Array.from(this.classValidators)} } from 'src/utils/validators';`);
|
|
46
46
|
return [
|
|
@@ -35,7 +35,7 @@ export class DocGenEntity {
|
|
|
35
35
|
})
|
|
36
36
|
.join("\n\n");
|
|
37
37
|
if (this.enums.size > 0) {
|
|
38
|
-
this.imports.add(`import { ${Array.from(this.enums)} } from '
|
|
38
|
+
this.imports.add(`import { ${Array.from(this.enums)} } from '@prisma/client';`);
|
|
39
39
|
}
|
|
40
40
|
return [
|
|
41
41
|
`${Array.from(this.imports).join("\n")}`,
|
package/dist/main.js
CHANGED
|
@@ -3,11 +3,11 @@ import * as path from "node:path";
|
|
|
3
3
|
import { DocEnums, DocGenEnum } from "./entities/enum.js";
|
|
4
4
|
import { DocGenModel } from "./entities/model.js";
|
|
5
5
|
import { DocFields } from "./field.type.js";
|
|
6
|
-
import { Helper } from "./utils/helpers.js";
|
|
7
6
|
import prismaPkg from "@prisma/internals";
|
|
7
|
+
import { PrismaUtils } from "./utils/prisma-utils.js";
|
|
8
8
|
const { getDMMF } = prismaPkg;
|
|
9
9
|
const ROOT = process.cwd();
|
|
10
|
-
const PRISMA_DIR = path.join(ROOT, "
|
|
10
|
+
const PRISMA_DIR = path.join(ROOT, "src");
|
|
11
11
|
export class DocGen {
|
|
12
12
|
datamodel;
|
|
13
13
|
properties;
|
|
@@ -15,8 +15,9 @@ export class DocGen {
|
|
|
15
15
|
fields;
|
|
16
16
|
models;
|
|
17
17
|
async init() {
|
|
18
|
-
const prismaDataModel = await
|
|
18
|
+
const prismaDataModel = await PrismaUtils.readPrismaFolderDatamodel(PRISMA_DIR);
|
|
19
19
|
const { datamodel } = await getDMMF({ datamodel: prismaDataModel });
|
|
20
|
+
console.log(datamodel);
|
|
20
21
|
this.enums = new DocEnums(datamodel.enums.map(({ dbName, name, values }) => {
|
|
21
22
|
return new DocGenEnum({
|
|
22
23
|
dbName: dbName ?? "",
|
package/dist/utils/helpers.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { promises as fs } from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
1
|
export class Helper {
|
|
4
2
|
static prismaScalarToTs(s) {
|
|
5
3
|
switch (s) {
|
|
@@ -83,44 +81,38 @@ export class Helper {
|
|
|
83
81
|
hasDate: model.fields.some((field) => field.kind === "scalar" && field.type === "DateTime"),
|
|
84
82
|
};
|
|
85
83
|
}
|
|
86
|
-
static async readPrismaFolderDatamodel(dir) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return `${head}\n${tail}`;
|
|
119
|
-
}
|
|
120
|
-
// caso não exista schema.prisma, apenas concatena
|
|
121
|
-
const contents = await Promise.all(files.map(async (f) => (f.startsWith("\n// ---- ") ? f : fs.readFile(f, "utf-8"))));
|
|
122
|
-
return contents.join("\n");
|
|
123
|
-
}
|
|
84
|
+
// static async readPrismaFolderDatamodel(dir: string): Promise<string> {
|
|
85
|
+
// const files: string[] = [];
|
|
86
|
+
// const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
87
|
+
// const mainSchemaPath = path.join(dir, "schema.prisma");
|
|
88
|
+
// try {
|
|
89
|
+
// const mainStat = await fs.stat(mainSchemaPath);
|
|
90
|
+
// if (mainStat.isFile()) files.push(mainSchemaPath);
|
|
91
|
+
// } catch {}
|
|
92
|
+
// // varrer demais itens (exceto migrations e o schema.prisma já incluído)
|
|
93
|
+
// for (const entry of entries) {
|
|
94
|
+
// if (entry.name === "migrations") continue;
|
|
95
|
+
// const full = path.join(dir, entry.name);
|
|
96
|
+
// if (entry.isDirectory()) {
|
|
97
|
+
// const nested = await this.readPrismaFolderDatamodel(full);
|
|
98
|
+
// if (nested.trim()) files.push(`\n// ---- ${full} ----\n${nested}`);
|
|
99
|
+
// } else if (entry.isFile() && entry.name.endsWith(".prisma") && full !== mainSchemaPath) {
|
|
100
|
+
// const content = await fs.readFile(full, "utf-8");
|
|
101
|
+
// files.push(`\n// ---- ${full} ----\n${content}`);
|
|
102
|
+
// }
|
|
103
|
+
// }
|
|
104
|
+
// // se não houver nada além de subpastas, retorna o que juntou nelas
|
|
105
|
+
// if (!files.length) return "";
|
|
106
|
+
// // quando schema.prisma existe, ele já está no topo do array
|
|
107
|
+
// if (files[0] === mainSchemaPath) {
|
|
108
|
+
// const head = await fs.readFile(mainSchemaPath, "utf-8");
|
|
109
|
+
// const tail = files.slice(1).join("\n");
|
|
110
|
+
// return `${head}\n${tail}`;
|
|
111
|
+
// }
|
|
112
|
+
// // caso não exista schema.prisma, apenas concatena
|
|
113
|
+
// const contents = await Promise.all(files.map(async (f) => (f.startsWith("\n// ---- ") ? f : fs.readFile(f, "utf-8"))));
|
|
114
|
+
// return contents.join("\n");
|
|
115
|
+
// }
|
|
124
116
|
findTypeForField(field) {
|
|
125
117
|
if (field.kind === "scalar") {
|
|
126
118
|
const base = Helper.prismaScalarToTs(field.type);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { promises as fs } from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
export class PrismaUtils {
|
|
4
|
+
static async readPrismaFolderDatamodel(dir) {
|
|
5
|
+
const prismaFiles = [];
|
|
6
|
+
// percorre recursivamente e guarda TODOS os .prisma (exceto dentro de migrations)
|
|
7
|
+
const walk = async (currentDir) => {
|
|
8
|
+
const entries = await fs.readdir(currentDir, { withFileTypes: true });
|
|
9
|
+
for (const entry of entries) {
|
|
10
|
+
if (entry.name === "migrations")
|
|
11
|
+
continue;
|
|
12
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
13
|
+
if (entry.isDirectory()) {
|
|
14
|
+
await walk(fullPath);
|
|
15
|
+
}
|
|
16
|
+
else if (entry.isFile() && entry.name.endsWith(".prisma")) {
|
|
17
|
+
prismaFiles.push(fullPath);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
await walk(dir);
|
|
22
|
+
if (!prismaFiles.length)
|
|
23
|
+
return "";
|
|
24
|
+
// garantir que main.prisma (se existir) venha primeiro
|
|
25
|
+
prismaFiles.sort((a, b) => {
|
|
26
|
+
const aIsMain = path.basename(a) === "main.prisma";
|
|
27
|
+
const bIsMain = path.basename(b) === "main.prisma";
|
|
28
|
+
if (aIsMain && !bIsMain)
|
|
29
|
+
return -1;
|
|
30
|
+
if (!aIsMain && bIsMain)
|
|
31
|
+
return 1;
|
|
32
|
+
// fallback: ordem alfabética estável
|
|
33
|
+
return a.localeCompare(b);
|
|
34
|
+
});
|
|
35
|
+
const chunks = [];
|
|
36
|
+
for (const file of prismaFiles) {
|
|
37
|
+
const content = await fs.readFile(file, "utf-8");
|
|
38
|
+
chunks.push(`\n// ---- ${file} ----\n${content}`);
|
|
39
|
+
}
|
|
40
|
+
return chunks.join("\n");
|
|
41
|
+
}
|
|
42
|
+
}
|