hekireki 0.0.1
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/README.md +361 -0
- package/dist/common/format/index.d.ts +1 -0
- package/dist/common/format/index.js +12 -0
- package/dist/common/helper/get-camel-case-schema-name-helper.d.ts +7 -0
- package/dist/common/helper/get-camel-case-schema-name-helper.js +14 -0
- package/dist/common/helper/get-pascal-case-schema-name-helper.d.ts +8 -0
- package/dist/common/helper/get-pascal-case-schema-name-helper.js +15 -0
- package/dist/common/helper/get-variable-name-helper.d.ts +9 -0
- package/dist/common/helper/get-variable-name-helper.js +17 -0
- package/dist/common/helper/get-variable-schema-name-helper.d.ts +9 -0
- package/dist/common/helper/get-variable-schema-name-helper.js +17 -0
- package/dist/common/helper/group-by-model-helper.d.ts +8 -0
- package/dist/common/helper/group-by-model-helper.js +18 -0
- package/dist/common/text/capitalize.d.ts +18 -0
- package/dist/common/text/capitalize.js +23 -0
- package/dist/common/text/decapitalize.d.ts +17 -0
- package/dist/common/text/decapitalize.js +22 -0
- package/dist/common/type/index.d.ts +16 -0
- package/dist/common/type/index.js +2 -0
- package/dist/common/validator/is-fields-validation.d.ts +13 -0
- package/dist/common/validator/is-fields-validation.js +13 -0
- package/dist/generator/mermaid/generator/generate-er-content.d.ts +8 -0
- package/dist/generator/mermaid/generator/generate-er-content.js +23 -0
- package/dist/generator/mermaid/generator/generate-model-fields.d.ts +8 -0
- package/dist/generator/mermaid/generator/generate-model-fields.js +29 -0
- package/dist/generator/mermaid/generator/generate-model-info.d.ts +8 -0
- package/dist/generator/mermaid/generator/generate-model-info.js +17 -0
- package/dist/generator/mermaid/generator/generate-relation-line.d.ts +8 -0
- package/dist/generator/mermaid/generator/generate-relation-line.js +17 -0
- package/dist/generator/mermaid/index.d.ts +10 -0
- package/dist/generator/mermaid/index.js +73 -0
- package/dist/generator/mermaid/output/index.d.ts +8 -0
- package/dist/generator/mermaid/output/index.js +24 -0
- package/dist/generator/mermaid/type/index.d.ts +11 -0
- package/dist/generator/mermaid/type/index.js +2 -0
- package/dist/generator/mermaid/validator/exclude-many-to-one-relations.d.ts +7 -0
- package/dist/generator/mermaid/validator/exclude-many-to-one-relations.js +12 -0
- package/dist/generator/mermaid/validator/extract-relations.d.ts +8 -0
- package/dist/generator/mermaid/validator/extract-relations.js +26 -0
- package/dist/generator/mermaid/validator/is-relation.d.ts +2 -0
- package/dist/generator/mermaid/validator/is-relation.js +32 -0
- package/dist/generator/mermaid/validator/parse-relation.d.ts +8 -0
- package/dist/generator/mermaid/validator/parse-relation.js +28 -0
- package/dist/generator/mermaid/validator/remove-duplicate-relations.d.ts +7 -0
- package/dist/generator/mermaid/validator/remove-duplicate-relations.js +12 -0
- package/dist/generator/valibot/generator/generate-valibot-infer-input.d.ts +8 -0
- package/dist/generator/valibot/generator/generate-valibot-infer-input.js +16 -0
- package/dist/generator/valibot/generator/generate-valibot-properties.d.ts +8 -0
- package/dist/generator/valibot/generator/generate-valibot-properties.js +20 -0
- package/dist/generator/valibot/generator/generate-valibot-schema.d.ts +9 -0
- package/dist/generator/valibot/generator/generate-valibot-schema.js +15 -0
- package/dist/generator/valibot/generator/generate-valibot-schemas.d.ts +15 -0
- package/dist/generator/valibot/generator/generate-valibot-schemas.js +21 -0
- package/dist/generator/valibot/generator/generate-valibot.d.ts +9 -0
- package/dist/generator/valibot/generator/generate-valibot.js +54 -0
- package/dist/generator/valibot/index.d.ts +10 -0
- package/dist/generator/valibot/index.js +53 -0
- package/dist/generator/valibot/validator/is-valibot-documentation.d.ts +1 -0
- package/dist/generator/valibot/validator/is-valibot-documentation.js +13 -0
- package/dist/generator/valibot/validator/is-valibot-validation.d.ts +7 -0
- package/dist/generator/valibot/validator/is-valibot-validation.js +15 -0
- package/dist/generator/zod/generator/generate-zod-infer.d.ts +8 -0
- package/dist/generator/zod/generator/generate-zod-infer.js +16 -0
- package/dist/generator/zod/generator/generate-zod-properties.d.ts +14 -0
- package/dist/generator/zod/generator/generate-zod-properties.js +26 -0
- package/dist/generator/zod/generator/generate-zod-schema.d.ts +9 -0
- package/dist/generator/zod/generator/generate-zod-schema.js +15 -0
- package/dist/generator/zod/generator/generate-zod-schemas.d.ts +14 -0
- package/dist/generator/zod/generator/generate-zod-schemas.js +20 -0
- package/dist/generator/zod/generator/generate-zod.d.ts +9 -0
- package/dist/generator/zod/generator/generate-zod.js +54 -0
- package/dist/generator/zod/index.d.ts +10 -0
- package/dist/generator/zod/index.js +53 -0
- package/dist/generator/zod/validator/is-zod-documentation.d.ts +6 -0
- package/dist/generator/zod/validator/is-zod-documentation.js +18 -0
- package/dist/generator/zod/validator/is-zod-validation.d.ts +6 -0
- package/dist/generator/zod/validator/is-zod-validation.js +14 -0
- package/package.json +56 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateERContent = generateERContent;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const extract_relations_1 = require("../validator/extract-relations");
|
|
6
|
+
const generate_model_info_1 = require("../generator/generate-model-info");
|
|
7
|
+
const remove_duplicate_relations_1 = require("../validator/remove-duplicate-relations");
|
|
8
|
+
/**
|
|
9
|
+
* generate ER content
|
|
10
|
+
* @function generateERContent
|
|
11
|
+
* @param models
|
|
12
|
+
* @returns ER content
|
|
13
|
+
*/
|
|
14
|
+
function generateERContent(models) {
|
|
15
|
+
// extract all relations
|
|
16
|
+
const allRelations = models.flatMap(extract_relations_1.extractRelations);
|
|
17
|
+
// remove duplicate relations
|
|
18
|
+
const uniqueRelations = (0, remove_duplicate_relations_1.removeDuplicateRelations)(allRelations);
|
|
19
|
+
// collect all model info
|
|
20
|
+
const modelInfos = models.flatMap(generate_model_info_1.generateModelInfo);
|
|
21
|
+
// build ER diagram
|
|
22
|
+
return [...__1.ER_HEADER, ...uniqueRelations, ...modelInfos, ...__1.ER_FOOTER];
|
|
23
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateModelFields = generateModelFields;
|
|
4
|
+
const ZOD_ANNOTATION = "@z.";
|
|
5
|
+
const VALIBOT_ANNOTATION = "@v.";
|
|
6
|
+
/**
|
|
7
|
+
* generate model fields
|
|
8
|
+
* @function generateModelFields
|
|
9
|
+
* @param model
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
function generateModelFields(model) {
|
|
13
|
+
return model.fields
|
|
14
|
+
.map((field) => {
|
|
15
|
+
if (field.relationName) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const commentPart = field.documentation
|
|
19
|
+
? field.documentation
|
|
20
|
+
.split("\n")
|
|
21
|
+
.filter((line) => !(line.includes(ZOD_ANNOTATION) ||
|
|
22
|
+
line.includes(VALIBOT_ANNOTATION)))
|
|
23
|
+
.join("\n")
|
|
24
|
+
.trim()
|
|
25
|
+
: "";
|
|
26
|
+
return ` ${field.type} ${field.name} ${commentPart ? `"${commentPart}"` : ""}`;
|
|
27
|
+
})
|
|
28
|
+
.filter((field) => field !== null);
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateModelInfo = generateModelInfo;
|
|
4
|
+
const generate_model_fields_1 = require("./generate-model-fields");
|
|
5
|
+
/**
|
|
6
|
+
* generate model info
|
|
7
|
+
* @function generateModelInfo
|
|
8
|
+
* @param model
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
function generateModelInfo(model) {
|
|
12
|
+
return [
|
|
13
|
+
` ${model.name} {`,
|
|
14
|
+
...(0, generate_model_fields_1.generateModelFields)(model),
|
|
15
|
+
" }",
|
|
16
|
+
];
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRelationLine = generateRelationLine;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
/**
|
|
6
|
+
* generate relation line
|
|
7
|
+
* @function generateRelationLine
|
|
8
|
+
* @param relation
|
|
9
|
+
* @returns string
|
|
10
|
+
*/
|
|
11
|
+
function generateRelationLine(relation) {
|
|
12
|
+
const cardinality = __1.CARDINALITY_MAP[relation.type];
|
|
13
|
+
if (!cardinality) {
|
|
14
|
+
throw new Error(`Unknown relation type: ${relation.type}`);
|
|
15
|
+
}
|
|
16
|
+
return ` ${relation.fromModel} ${cardinality} ${relation.toModel} : "(${relation.fromField}) - (${relation.toField})"`;
|
|
17
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { GeneratorOptions } from "@prisma/generator-helper";
|
|
2
|
+
import type { RelationType } from "./type";
|
|
3
|
+
export type Config = {
|
|
4
|
+
output?: string;
|
|
5
|
+
file?: string | string[];
|
|
6
|
+
};
|
|
7
|
+
export declare const CARDINALITY_MAP: Record<RelationType, string>;
|
|
8
|
+
export declare const ER_HEADER: readonly ["```mermaid", "erDiagram"];
|
|
9
|
+
export declare const ER_FOOTER: readonly ["```"];
|
|
10
|
+
export declare function main(options: GeneratorOptions): Promise<void>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ER_FOOTER = exports.ER_HEADER = exports.CARDINALITY_MAP = void 0;
|
|
4
|
+
exports.main = main;
|
|
5
|
+
const output_1 = require("./output");
|
|
6
|
+
const generator_helper_1 = require("@prisma/generator-helper");
|
|
7
|
+
const generate_er_content_1 = require("./generator/generate-er-content");
|
|
8
|
+
const DEFAULT_CONFIG = {
|
|
9
|
+
output: "./mermaid-er",
|
|
10
|
+
file: "ER.md",
|
|
11
|
+
};
|
|
12
|
+
exports.CARDINALITY_MAP = {
|
|
13
|
+
// Required Relationships
|
|
14
|
+
// Both sides required (one-to-one)
|
|
15
|
+
"one-to-one": "||--||", // 1 --- 1
|
|
16
|
+
// One side singular, the other multiple
|
|
17
|
+
"one-to-many": "||--o{", // 1 --- * (also known as one-to-zero-many)
|
|
18
|
+
"many-to-one": "}o--||", // * --- 1
|
|
19
|
+
// Both sides multiple (many-to-many)
|
|
20
|
+
"many-to-many": "}o--o{", // * --- *
|
|
21
|
+
// One side required, one side optional (0..1)
|
|
22
|
+
"one-to-zero-one": "||--o|", // 1 --- 0..1
|
|
23
|
+
"zero-one-to-one": "o|--||", // 0..1 --- 1
|
|
24
|
+
// Both sides optional singular (0..1 on both sides)
|
|
25
|
+
"zero-to-one": "o|--o|", // 0..1 --- 0..1
|
|
26
|
+
"zero-to-zero-one": "o|--o|", // Alias for zero-to-one
|
|
27
|
+
// One side optional singular, the other multiple
|
|
28
|
+
"zero-to-many": "o|--o{", // 0 --- *
|
|
29
|
+
"zero-one-to-many": "o|--o{", // 0..1 --- *
|
|
30
|
+
"many-to-zero-one": "}o--o|", // * --- 0..1
|
|
31
|
+
// Optional Relationships (using dotted lines)
|
|
32
|
+
"one-to-one-optional": "||..||", // 1..1
|
|
33
|
+
"one-to-many-optional": "||..o{", // 1..*
|
|
34
|
+
"many-to-one-optional": "}o..||", // *..1
|
|
35
|
+
"many-to-many-optional": "}o..o{", // *..*
|
|
36
|
+
"one-to-zero-one-optional": "||..o|", // 1..0..1
|
|
37
|
+
"zero-one-to-one-optional": "o|..||", // 0..1..1
|
|
38
|
+
"zero-to-one-optional": "o|..o|", // 0..1 (both sides optional singular)
|
|
39
|
+
"zero-to-many-optional": "o|..o{", // 0..* (optional multiple)
|
|
40
|
+
"zero-one-to-many-optional": "o|..o{", // 0..1 --- * (optional)
|
|
41
|
+
"many-to-zero-one-optional": "}o..o|", // *..0..1
|
|
42
|
+
// Nuanced Patterns (Aliases)
|
|
43
|
+
"many-to-zero-many": "}o..o{", // * --- 0..* (equivalent to many-to-many-optional)
|
|
44
|
+
"zero-many-to-many": "o{..}o", // 0..* --- * (left side optional multiple, right side required multiple)
|
|
45
|
+
"zero-many-to-zero-many": "o{..o{", // both sides optional multiple
|
|
46
|
+
};
|
|
47
|
+
// ER diagram header
|
|
48
|
+
exports.ER_HEADER = ["```mermaid", "erDiagram"];
|
|
49
|
+
// ER diagram footer
|
|
50
|
+
exports.ER_FOOTER = ["```"];
|
|
51
|
+
// main function
|
|
52
|
+
async function main(options) {
|
|
53
|
+
const config = {
|
|
54
|
+
output: options.generator.output?.value ?? DEFAULT_CONFIG.output,
|
|
55
|
+
file: options.generator.config?.file ?? DEFAULT_CONFIG.file,
|
|
56
|
+
};
|
|
57
|
+
const models = options.dmmf.datamodel.models;
|
|
58
|
+
const content = (0, generate_er_content_1.generateERContent)(models);
|
|
59
|
+
if (!config.output) {
|
|
60
|
+
throw new Error("output is required");
|
|
61
|
+
}
|
|
62
|
+
(0, output_1.OutputFile)(content, config);
|
|
63
|
+
}
|
|
64
|
+
// prisma generator handler
|
|
65
|
+
(0, generator_helper_1.generatorHandler)({
|
|
66
|
+
onManifest() {
|
|
67
|
+
return {
|
|
68
|
+
defaultOutput: "./mermaid-er",
|
|
69
|
+
prettyName: "ekireki-ER",
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
onGenerate: main,
|
|
73
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Config } from "..";
|
|
2
|
+
import type { ERContent } from "../type";
|
|
3
|
+
/**
|
|
4
|
+
* Output the ER content to a file
|
|
5
|
+
* @param content - The ER content
|
|
6
|
+
* @param config - The configuration
|
|
7
|
+
*/
|
|
8
|
+
export declare function OutputFile(content: ERContent, config: Config): void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.OutputFile = OutputFile;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
/**
|
|
9
|
+
* Output the ER content to a file
|
|
10
|
+
* @param content - The ER content
|
|
11
|
+
* @param config - The configuration
|
|
12
|
+
*/
|
|
13
|
+
function OutputFile(content, config) {
|
|
14
|
+
const outputDir = config.output;
|
|
15
|
+
if (!outputDir) {
|
|
16
|
+
throw new Error("output is required");
|
|
17
|
+
}
|
|
18
|
+
if (!node_fs_1.default.existsSync(outputDir)) {
|
|
19
|
+
node_fs_1.default.mkdirSync(outputDir, { recursive: true });
|
|
20
|
+
}
|
|
21
|
+
const file = config.file ?? "ER.md";
|
|
22
|
+
const filePath = `${outputDir}/${file}`;
|
|
23
|
+
node_fs_1.default.writeFileSync(filePath, content.join("\n"), { encoding: "utf-8" });
|
|
24
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DMMF } from "@prisma/generator-helper";
|
|
2
|
+
export type RelationType = "one-to-one" | "one-to-many" | "many-to-one" | "many-to-many" | "one-to-zero-one" | "zero-one-to-one" | "zero-to-one" | "zero-to-zero-one" | "zero-to-many" | "zero-one-to-many" | "many-to-zero-one" | "one-to-one-optional" | "one-to-many-optional" | "many-to-one-optional" | "many-to-many-optional" | "one-to-zero-one-optional" | "zero-one-to-one-optional" | "zero-to-one-optional" | "zero-to-many-optional" | "zero-one-to-many-optional" | "many-to-zero-one-optional" | "many-to-zero-many" | "zero-many-to-many" | "zero-many-to-zero-many";
|
|
3
|
+
export type Relation = {
|
|
4
|
+
fromModel: string;
|
|
5
|
+
toModel: string;
|
|
6
|
+
fromField: string;
|
|
7
|
+
toField: string;
|
|
8
|
+
type: RelationType;
|
|
9
|
+
};
|
|
10
|
+
export type Model = Readonly<DMMF.Model>;
|
|
11
|
+
export type ERContent = readonly string[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.excludeManyToOneRelations = excludeManyToOneRelations;
|
|
4
|
+
/**
|
|
5
|
+
* exclude many-to-one relations
|
|
6
|
+
* @function excludeManyToOneRelations
|
|
7
|
+
* @param relations
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
function excludeManyToOneRelations(relations) {
|
|
11
|
+
return [...new Set(relations)].filter((r) => !r.includes("many-to-one"));
|
|
12
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractRelations = extractRelations;
|
|
4
|
+
const generate_relation_line_1 = require("../generator/generate-relation-line");
|
|
5
|
+
const parse_relation_1 = require("./parse-relation");
|
|
6
|
+
/**
|
|
7
|
+
* extract relations from model
|
|
8
|
+
* @function extractRelations
|
|
9
|
+
* @param model
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
function extractRelations(model) {
|
|
13
|
+
const relations = [];
|
|
14
|
+
// @r annotation
|
|
15
|
+
if (model.documentation) {
|
|
16
|
+
const annotationRelations = model.documentation
|
|
17
|
+
.split("\n")
|
|
18
|
+
.map((line) => {
|
|
19
|
+
const relation = (0, parse_relation_1.parseRelation)(line);
|
|
20
|
+
return relation ? (0, generate_relation_line_1.generateRelationLine)(relation) : null;
|
|
21
|
+
})
|
|
22
|
+
.filter((line) => line !== null);
|
|
23
|
+
relations.push(...annotationRelations);
|
|
24
|
+
}
|
|
25
|
+
return relations;
|
|
26
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRelation = isRelation;
|
|
4
|
+
const VALID_RELATIONS = new Set([
|
|
5
|
+
"one-to-one",
|
|
6
|
+
"one-to-many",
|
|
7
|
+
"many-to-one",
|
|
8
|
+
"many-to-many",
|
|
9
|
+
"one-to-zero-one",
|
|
10
|
+
"zero-one-to-one",
|
|
11
|
+
"zero-to-one",
|
|
12
|
+
"zero-to-zero-one",
|
|
13
|
+
"zero-to-many",
|
|
14
|
+
"zero-one-to-many",
|
|
15
|
+
"many-to-zero-one",
|
|
16
|
+
"one-to-one-optional",
|
|
17
|
+
"one-to-many-optional",
|
|
18
|
+
"many-to-one-optional",
|
|
19
|
+
"many-to-many-optional",
|
|
20
|
+
"one-to-zero-one-optional",
|
|
21
|
+
"zero-one-to-one-optional",
|
|
22
|
+
"zero-to-one-optional",
|
|
23
|
+
"zero-to-many-optional",
|
|
24
|
+
"zero-one-to-many-optional",
|
|
25
|
+
"many-to-zero-one-optional",
|
|
26
|
+
"many-to-zero-many",
|
|
27
|
+
"zero-many-to-many",
|
|
28
|
+
"zero-many-to-zero-many",
|
|
29
|
+
]);
|
|
30
|
+
function isRelation(value) {
|
|
31
|
+
return typeof value === "string" && VALID_RELATIONS.has(value);
|
|
32
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseRelation = parseRelation;
|
|
4
|
+
const is_relation_1 = require("./is-relation");
|
|
5
|
+
/**
|
|
6
|
+
* parse relation
|
|
7
|
+
* @function parseRelation
|
|
8
|
+
* @param line
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
function parseRelation(line) {
|
|
12
|
+
const relationRegex = /^@relation\s+(\w+)\.(\w+)\s+(\w+)\.(\w+)\s+(\w+-to-\w+)$/;
|
|
13
|
+
const match = line.trim().match(relationRegex);
|
|
14
|
+
if (!match) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const [, fromModel, fromField, toModel, toField, relationType] = match;
|
|
18
|
+
if (!(0, is_relation_1.isRelation)(relationType)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
fromModel,
|
|
23
|
+
fromField,
|
|
24
|
+
toModel,
|
|
25
|
+
toField,
|
|
26
|
+
type: relationType,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.removeDuplicateRelations = removeDuplicateRelations;
|
|
4
|
+
/**
|
|
5
|
+
* remove duplicate relations
|
|
6
|
+
* @function removeDuplicateRelations
|
|
7
|
+
* @param relations
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
function removeDuplicateRelations(relations) {
|
|
11
|
+
return [...new Set(relations)];
|
|
12
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Config } from "..";
|
|
2
|
+
/**
|
|
3
|
+
* Generate the infer input type for the model
|
|
4
|
+
* @param modelName - The name of the model
|
|
5
|
+
* @param config - The configuration for the generator
|
|
6
|
+
* @returns The generated infer input type
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateValibotInferInput(modelName: string, config: Config): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateValibotInferInput = generateValibotInferInput;
|
|
4
|
+
const get_variable_name_helper_1 = require("../../../common/helper/get-variable-name-helper");
|
|
5
|
+
const get_variable_schema_name_helper_1 = require("../../../common/helper/get-variable-schema-name-helper");
|
|
6
|
+
/**
|
|
7
|
+
* Generate the infer input type for the model
|
|
8
|
+
* @param modelName - The name of the model
|
|
9
|
+
* @param config - The configuration for the generator
|
|
10
|
+
* @returns The generated infer input type
|
|
11
|
+
*/
|
|
12
|
+
function generateValibotInferInput(modelName, config) {
|
|
13
|
+
const typeName = (0, get_variable_name_helper_1.getVariableNameHelper)(modelName, config);
|
|
14
|
+
const schemaName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(modelName, config);
|
|
15
|
+
return `export type ${typeName} = v.InferInput<typeof ${schemaName}>`;
|
|
16
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateValibotProperties = generateValibotProperties;
|
|
4
|
+
function generateValibotProperties(modelFields, config) {
|
|
5
|
+
const fields = modelFields
|
|
6
|
+
.filter((field) => field.validation)
|
|
7
|
+
.map((field) => {
|
|
8
|
+
// @relation, @v, @z exclude
|
|
9
|
+
const cleanDoc = field.comment
|
|
10
|
+
.filter((line) => !(line.includes("@relation") ||
|
|
11
|
+
line.includes("@v") ||
|
|
12
|
+
line.includes("@z")))
|
|
13
|
+
.join("\n")
|
|
14
|
+
.trim();
|
|
15
|
+
const docComment = config?.comment && cleanDoc ? ` /**\n * ${cleanDoc}\n */\n` : "";
|
|
16
|
+
return `${docComment} ${field.fieldName}: v.${field.validation}`;
|
|
17
|
+
})
|
|
18
|
+
.join(",\n");
|
|
19
|
+
return fields;
|
|
20
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Config } from "..";
|
|
2
|
+
/**
|
|
3
|
+
* Generate Valibot schema
|
|
4
|
+
* @param modelName - The name of the model
|
|
5
|
+
* @param fields - The fields of the model
|
|
6
|
+
* @param config - The configuration for the generator
|
|
7
|
+
* @returns The generated Valibot schema
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateValibotSchema(modelName: string, fields: string, config: Config): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateValibotSchema = generateValibotSchema;
|
|
4
|
+
const get_variable_schema_name_helper_1 = require("../../../common/helper/get-variable-schema-name-helper");
|
|
5
|
+
/**
|
|
6
|
+
* Generate Valibot schema
|
|
7
|
+
* @param modelName - The name of the model
|
|
8
|
+
* @param fields - The fields of the model
|
|
9
|
+
* @param config - The configuration for the generator
|
|
10
|
+
* @returns The generated Valibot schema
|
|
11
|
+
*/
|
|
12
|
+
function generateValibotSchema(modelName, fields, config) {
|
|
13
|
+
const schemaName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(modelName, config);
|
|
14
|
+
return `export const ${schemaName} = v.object({\n${fields}\n})`;
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Config } from "..";
|
|
2
|
+
/**
|
|
3
|
+
* generate valibot schemas
|
|
4
|
+
* @function generateValibotSchemas
|
|
5
|
+
* @param modelFields
|
|
6
|
+
* @param config
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateValibotSchemas(modelFields: {
|
|
10
|
+
documentation: string;
|
|
11
|
+
modelName: string;
|
|
12
|
+
fieldName: string;
|
|
13
|
+
validation: string | null;
|
|
14
|
+
comment: string[];
|
|
15
|
+
}[], config: Config): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateValibotSchemas = generateValibotSchemas;
|
|
4
|
+
const generate_valibot_properties_1 = require("./generate-valibot-properties");
|
|
5
|
+
const generate_valibot_schema_1 = require("./generate-valibot-schema");
|
|
6
|
+
/**
|
|
7
|
+
* generate valibot schemas
|
|
8
|
+
* @function generateValibotSchemas
|
|
9
|
+
* @param modelFields
|
|
10
|
+
* @param config
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
function generateValibotSchemas(modelFields, config) {
|
|
14
|
+
const modelName = modelFields[0].modelName;
|
|
15
|
+
const modelDoc = modelFields[0].documentation || "";
|
|
16
|
+
const fields = (0, generate_valibot_properties_1.generateValibotProperties)(modelFields, config);
|
|
17
|
+
if (!(modelDoc || !config?.comment)) {
|
|
18
|
+
return (0, generate_valibot_schema_1.generateValibotSchema)(modelName, fields, config);
|
|
19
|
+
}
|
|
20
|
+
return `${(0, generate_valibot_schema_1.generateValibotSchema)(modelName, fields, config)}`;
|
|
21
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Config } from "..";
|
|
2
|
+
import type { Model } from "../../../common/type";
|
|
3
|
+
/**
|
|
4
|
+
* Generate Valibot schemas and types
|
|
5
|
+
* @param models - The models to generate the Valibot schemas and types for
|
|
6
|
+
* @param config - The configuration for the generator
|
|
7
|
+
* @returns The generated Valibot schemas and types
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateValibot(models: readonly Model[], config: Config): string;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateValibot = generateValibot;
|
|
4
|
+
const is_valibot_documentation_1 = require("../validator/is-valibot-documentation");
|
|
5
|
+
const is_valibot_validation_1 = require("../validator/is-valibot-validation");
|
|
6
|
+
const is_fields_validation_1 = require("../../../common/validator/is-fields-validation");
|
|
7
|
+
const group_by_model_helper_1 = require("../../../common/helper/group-by-model-helper");
|
|
8
|
+
const generate_valibot_schemas_1 = require("./generate-valibot-schemas");
|
|
9
|
+
const generate_valibot_infer_input_1 = require("./generate-valibot-infer-input");
|
|
10
|
+
const VALIBOT_IMPORT = `import * as v from 'valibot'\n`;
|
|
11
|
+
/**
|
|
12
|
+
* Generate Valibot schemas and types
|
|
13
|
+
* @param models - The models to generate the Valibot schemas and types for
|
|
14
|
+
* @param config - The configuration for the generator
|
|
15
|
+
* @returns The generated Valibot schemas and types
|
|
16
|
+
*/
|
|
17
|
+
function generateValibot(models, config) {
|
|
18
|
+
const modelInfos = models.map((model) => {
|
|
19
|
+
return {
|
|
20
|
+
documentation: model.documentation ?? "",
|
|
21
|
+
name: model.name,
|
|
22
|
+
fields: model.fields,
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
const modelFields = modelInfos.map((model) => {
|
|
26
|
+
const fields = model.fields.map((field) => ({
|
|
27
|
+
documentation: model.documentation,
|
|
28
|
+
modelName: model.name,
|
|
29
|
+
fieldName: field.name,
|
|
30
|
+
comment: (0, is_valibot_documentation_1.isValibotDocumentValidation)(field.documentation),
|
|
31
|
+
validation: (0, is_valibot_validation_1.isValibotValidation)(field.documentation),
|
|
32
|
+
}));
|
|
33
|
+
return fields;
|
|
34
|
+
});
|
|
35
|
+
// null exclude
|
|
36
|
+
const validFields = (0, is_fields_validation_1.isFieldsValidation)(modelFields);
|
|
37
|
+
// group by model
|
|
38
|
+
const groupedByModel = (0, group_by_model_helper_1.groupByModelHelper)(validFields);
|
|
39
|
+
const valibots = Object.values(groupedByModel).map((fields) => {
|
|
40
|
+
return {
|
|
41
|
+
generateValibotSchema: (0, generate_valibot_schemas_1.generateValibotSchemas)(fields, config),
|
|
42
|
+
generateValibotInfer: config.type === "true"
|
|
43
|
+
? (0, generate_valibot_infer_input_1.generateValibotInferInput)(fields[0].modelName, config)
|
|
44
|
+
: "",
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
return [
|
|
48
|
+
VALIBOT_IMPORT,
|
|
49
|
+
"",
|
|
50
|
+
valibots
|
|
51
|
+
.flatMap(({ generateValibotSchema, generateValibotInfer }) => [generateValibotSchema, generateValibotInfer].filter(Boolean))
|
|
52
|
+
.join("\n\n"),
|
|
53
|
+
].join("\n");
|
|
54
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { GeneratorOptions } from "@prisma/generator-helper";
|
|
2
|
+
export type Config = {
|
|
3
|
+
output?: string;
|
|
4
|
+
file?: string | string[];
|
|
5
|
+
schemaName?: "PascalCase" | "camelCase" | string | string[];
|
|
6
|
+
typeName?: "PascalCase" | "camelCase" | string | string[];
|
|
7
|
+
type?: boolean | string | string[];
|
|
8
|
+
comment?: boolean | string | string[];
|
|
9
|
+
};
|
|
10
|
+
export declare function main(options: GeneratorOptions): Promise<void>;
|