hekireki 0.0.6 → 0.0.8
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/common/format/index.js +1 -1
- package/dist/common/helper/get-variable-name-helper.d.ts +1 -1
- package/dist/common/helper/get-variable-name-helper.js +1 -3
- package/dist/common/helper/get-variable-schema-name-helper.d.ts +1 -1
- package/dist/common/helper/get-variable-schema-name-helper.js +1 -1
- package/dist/common/helper/group-by-model-helper.d.ts +1 -1
- package/dist/common/type/index.d.ts +2 -2
- package/dist/common/validator/is-fields-validation.d.ts +1 -1
- package/dist/common/validator/is-fields-validation.js +1 -3
- package/dist/generator/mermaid/generator/generate-er-content.d.ts +4 -5
- package/dist/generator/mermaid/generator/generate-er-content.js +2 -3
- package/dist/generator/mermaid/generator/generate-model-fields.d.ts +4 -5
- package/dist/generator/mermaid/generator/generate-model-fields.js +9 -11
- package/dist/generator/mermaid/generator/generate-model-info.d.ts +2 -2
- package/dist/generator/mermaid/generator/generate-model-info.js +1 -5
- package/dist/generator/mermaid/generator/generate-relation-line.d.ts +2 -2
- package/dist/generator/mermaid/generator/generate-relation-line.js +2 -2
- package/dist/generator/mermaid/index.d.ts +7 -9
- package/dist/generator/mermaid/index.js +8 -37
- package/dist/generator/mermaid/output/index.d.ts +3 -3
- package/dist/generator/mermaid/output/index.js +3 -3
- package/dist/generator/mermaid/relationship/build-relation-line.d.ts +14 -0
- package/dist/generator/mermaid/relationship/build-relation-line.js +37 -0
- package/dist/generator/mermaid/relationship/index.d.ts +12 -0
- package/dist/generator/mermaid/relationship/index.js +30 -0
- package/dist/generator/mermaid/type/index.d.ts +9 -10
- package/dist/generator/mermaid/validator/exclude-many-to-one-relations.d.ts +1 -1
- package/dist/generator/mermaid/validator/exclude-many-to-one-relations.js +1 -1
- package/dist/generator/mermaid/validator/extract-relations.d.ts +2 -2
- package/dist/generator/mermaid/validator/extract-relations.js +1 -1
- package/dist/generator/mermaid/validator/is-relation.d.ts +2 -2
- package/dist/generator/mermaid/validator/is-relationship.d.ts +7 -0
- package/dist/generator/mermaid/validator/is-relationship.js +11 -0
- package/dist/generator/mermaid/validator/parse-relation.d.ts +2 -2
- package/dist/generator/mermaid/validator/parse-relation.js +0 -4
- package/dist/generator/mermaid/validator/remove-duplicate-relations.d.ts +1 -1
- package/dist/generator/mermaid-er/generator/generate-er-content.d.ts +7 -0
- package/dist/generator/mermaid-er/generator/generate-er-content.js +22 -0
- package/dist/generator/mermaid-er/generator/generate-model-fields.d.ts +7 -0
- package/dist/generator/mermaid-er/generator/generate-model-fields.js +27 -0
- package/dist/generator/mermaid-er/generator/generate-model-info.d.ts +8 -0
- package/dist/generator/mermaid-er/generator/generate-model-info.js +13 -0
- package/dist/generator/mermaid-er/generator/generate-relation-line.d.ts +7 -0
- package/dist/generator/mermaid-er/generator/generate-relation-line.js +16 -0
- package/dist/generator/mermaid-er/index.d.ts +9 -0
- package/dist/generator/mermaid-er/index.js +39 -0
- package/dist/generator/mermaid-er/output/index.d.ts +8 -0
- package/dist/generator/mermaid-er/output/index.js +24 -0
- package/dist/generator/mermaid-er/relationship/build-relation-line.d.ts +14 -0
- package/dist/generator/mermaid-er/relationship/build-relation-line.js +37 -0
- package/dist/generator/mermaid-er/type/index.d.ts +10 -0
- package/dist/generator/mermaid-er/type/index.js +2 -0
- package/dist/generator/mermaid-er/validator/exclude-many-to-one-relations.d.ts +7 -0
- package/dist/generator/mermaid-er/validator/exclude-many-to-one-relations.js +12 -0
- package/dist/generator/mermaid-er/validator/extract-relations.d.ts +7 -0
- package/dist/generator/mermaid-er/validator/extract-relations.js +25 -0
- package/dist/generator/mermaid-er/validator/is-relationship.d.ts +7 -0
- package/dist/generator/mermaid-er/validator/is-relationship.js +11 -0
- package/dist/generator/mermaid-er/validator/parse-relation.d.ts +8 -0
- package/dist/generator/mermaid-er/validator/parse-relation.js +24 -0
- package/dist/generator/mermaid-er/validator/remove-duplicate-relations.d.ts +6 -0
- package/dist/generator/mermaid-er/validator/remove-duplicate-relations.js +11 -0
- package/dist/generator/valibot/generator/generate-valibot-infer-input.d.ts +1 -1
- package/dist/generator/valibot/generator/generate-valibot-properties.d.ts +1 -1
- package/dist/generator/valibot/generator/generate-valibot-properties.js +4 -6
- package/dist/generator/valibot/generator/generate-valibot-schema.d.ts +1 -1
- package/dist/generator/valibot/generator/generate-valibot-schemas.d.ts +1 -1
- package/dist/generator/valibot/generator/generate-valibot-schemas.js +1 -1
- package/dist/generator/valibot/generator/generate-valibot.d.ts +2 -2
- package/dist/generator/valibot/generator/generate-valibot.js +5 -7
- package/dist/generator/valibot/index.d.ts +3 -3
- package/dist/generator/valibot/index.js +10 -10
- package/dist/generator/valibot/validator/is-valibot-documentation.js +2 -2
- package/dist/generator/zod/generator/generate-zod-infer.d.ts +1 -1
- package/dist/generator/zod/generator/generate-zod-properties.d.ts +1 -1
- package/dist/generator/zod/generator/generate-zod-properties.js +4 -6
- package/dist/generator/zod/generator/generate-zod-schema.d.ts +1 -1
- package/dist/generator/zod/generator/generate-zod-schemas.d.ts +1 -1
- package/dist/generator/zod/generator/generate-zod-schemas.js +1 -1
- package/dist/generator/zod/generator/generate-zod.d.ts +2 -2
- package/dist/generator/zod/generator/generate-zod.js +5 -7
- package/dist/generator/zod/index.d.ts +3 -3
- package/dist/generator/zod/index.js +10 -10
- package/dist/generator/zod/validator/is-zod-documentation.js +2 -2
- package/package.json +54 -54
|
@@ -0,0 +1,13 @@
|
|
|
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 [` ${model.name} {`, ...(0, generate_model_fields_1.generateModelFields)(model), ' }'];
|
|
13
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRelationLine = generateRelationLine;
|
|
4
|
+
const build_relation_line_1 = require("../relationship/build-relation-line");
|
|
5
|
+
/**
|
|
6
|
+
* generate relation line
|
|
7
|
+
* @param { Relation } relation
|
|
8
|
+
* @returns { string } relation line
|
|
9
|
+
*/
|
|
10
|
+
function generateRelationLine(relation) {
|
|
11
|
+
const cardinality = (0, build_relation_line_1.buildRelationLine)(relation.type);
|
|
12
|
+
if (!cardinality) {
|
|
13
|
+
throw new Error(`Unknown relation type: ${relation.type}`);
|
|
14
|
+
}
|
|
15
|
+
return ` ${relation.fromModel} ${cardinality} ${relation.toModel} : "(${relation.fromField}) - (${relation.toField})"`;
|
|
16
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import type { GeneratorOptions } from '@prisma/generator-helper';
|
|
3
|
+
export type Config = {
|
|
4
|
+
output?: string;
|
|
5
|
+
file?: string | string[];
|
|
6
|
+
};
|
|
7
|
+
export declare const ER_HEADER: readonly ["```mermaid", "erDiagram"];
|
|
8
|
+
export declare const ER_FOOTER: readonly ["```"];
|
|
9
|
+
export declare function main(options: GeneratorOptions): Promise<void>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ER_FOOTER = exports.ER_HEADER = void 0;
|
|
5
|
+
exports.main = main;
|
|
6
|
+
const output_1 = require("./output");
|
|
7
|
+
const generator_helper_1 = require("@prisma/generator-helper");
|
|
8
|
+
const generate_er_content_1 = require("./generator/generate-er-content");
|
|
9
|
+
const DEFAULT_CONFIG = {
|
|
10
|
+
output: './mermaid-er',
|
|
11
|
+
file: 'ER.md',
|
|
12
|
+
};
|
|
13
|
+
// ER diagram header
|
|
14
|
+
exports.ER_HEADER = ['```mermaid', 'erDiagram'];
|
|
15
|
+
// ER diagram footer
|
|
16
|
+
exports.ER_FOOTER = ['```'];
|
|
17
|
+
// main function
|
|
18
|
+
async function main(options) {
|
|
19
|
+
const config = {
|
|
20
|
+
output: options.generator.output?.value ?? DEFAULT_CONFIG.output,
|
|
21
|
+
file: options.generator.config?.file ?? DEFAULT_CONFIG.file,
|
|
22
|
+
};
|
|
23
|
+
const models = options.dmmf.datamodel.models;
|
|
24
|
+
const content = (0, generate_er_content_1.generateERContent)(models);
|
|
25
|
+
if (!config.output) {
|
|
26
|
+
throw new Error('output is required');
|
|
27
|
+
}
|
|
28
|
+
(0, output_1.OutputFile)(content, config);
|
|
29
|
+
}
|
|
30
|
+
// prisma generator handler
|
|
31
|
+
(0, generator_helper_1.generatorHandler)({
|
|
32
|
+
onManifest() {
|
|
33
|
+
return {
|
|
34
|
+
defaultOutput: './mermaid-er',
|
|
35
|
+
prettyName: 'Hekireki-ER',
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
onGenerate: main,
|
|
39
|
+
});
|
|
@@ -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,14 @@
|
|
|
1
|
+
declare const RELATIONSHIPS: {
|
|
2
|
+
readonly 'zero-one': "|o";
|
|
3
|
+
readonly one: "||";
|
|
4
|
+
readonly 'zero-many': "}o";
|
|
5
|
+
readonly many: "}|";
|
|
6
|
+
};
|
|
7
|
+
export type Relationship = keyof typeof RELATIONSHIPS;
|
|
8
|
+
/**
|
|
9
|
+
* Builds a relationship line for mermaid from a string.
|
|
10
|
+
* @param { string } input
|
|
11
|
+
* @returns { string }
|
|
12
|
+
*/
|
|
13
|
+
export declare function buildRelationLine(input: string): string;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildRelationLine = buildRelationLine;
|
|
4
|
+
const is_relationship_1 = require("../validator/is-relationship");
|
|
5
|
+
const RELATIONSHIPS = {
|
|
6
|
+
'zero-one': '|o',
|
|
7
|
+
one: '||',
|
|
8
|
+
'zero-many': '}o',
|
|
9
|
+
many: '}|',
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Builds a relationship line for mermaid from a string.
|
|
13
|
+
* @param { string } input
|
|
14
|
+
* @returns { string }
|
|
15
|
+
*/
|
|
16
|
+
function buildRelationLine(input) {
|
|
17
|
+
const parts = input.split('-to-');
|
|
18
|
+
if (parts.length !== 2) {
|
|
19
|
+
throw new Error(`Invalid input format: ${input}`);
|
|
20
|
+
}
|
|
21
|
+
const [toRaw, optionalFlag] = parts[1].includes('-optional')
|
|
22
|
+
? [parts[1].replace('-optional', ''), 'optional']
|
|
23
|
+
: [parts[1], ''];
|
|
24
|
+
const from = parts[0];
|
|
25
|
+
const to = toRaw;
|
|
26
|
+
const isOptional = optionalFlag === 'optional';
|
|
27
|
+
if (!((0, is_relationship_1.isRelationship)(from) && (0, is_relationship_1.isRelationship)(to))) {
|
|
28
|
+
throw new Error(`Invalid relationship string: ${input}`);
|
|
29
|
+
}
|
|
30
|
+
const fromSymbol = RELATIONSHIPS[from];
|
|
31
|
+
const toSymbol = RELATIONSHIPS[to];
|
|
32
|
+
if (!(fromSymbol && toSymbol)) {
|
|
33
|
+
throw new Error(`Invalid relationship string: ${input}`);
|
|
34
|
+
}
|
|
35
|
+
const connector = isOptional ? '..' : '--';
|
|
36
|
+
return `${fromSymbol}${connector}${toSymbol}`;
|
|
37
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DMMF } from '@prisma/generator-helper';
|
|
2
|
+
export type Relation = {
|
|
3
|
+
fromModel: string;
|
|
4
|
+
toModel: string;
|
|
5
|
+
fromField: string;
|
|
6
|
+
toField: string;
|
|
7
|
+
type: string;
|
|
8
|
+
};
|
|
9
|
+
export type Model = Readonly<DMMF.Model>;
|
|
10
|
+
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,25 @@
|
|
|
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
|
+
* @param { Model } model
|
|
9
|
+
* @returns { readonly string[] }
|
|
10
|
+
*/
|
|
11
|
+
function extractRelations(model) {
|
|
12
|
+
const relations = [];
|
|
13
|
+
// @relation annotation
|
|
14
|
+
if (model.documentation) {
|
|
15
|
+
const annotationRelations = model.documentation
|
|
16
|
+
.split('\n')
|
|
17
|
+
.map((line) => {
|
|
18
|
+
const relation = (0, parse_relation_1.parseRelation)(line);
|
|
19
|
+
return relation ? (0, generate_relation_line_1.generateRelationLine)(relation) : null;
|
|
20
|
+
})
|
|
21
|
+
.filter((line) => line !== null);
|
|
22
|
+
relations.push(...annotationRelations);
|
|
23
|
+
}
|
|
24
|
+
return relations;
|
|
25
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRelationship = isRelationship;
|
|
4
|
+
/**
|
|
5
|
+
* isRelationship
|
|
6
|
+
* @param { string } key
|
|
7
|
+
* @returns { key is Relationship }
|
|
8
|
+
*/
|
|
9
|
+
function isRelationship(key) {
|
|
10
|
+
return ['zero-one', 'one', 'zero-many', 'many'].includes(key);
|
|
11
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseRelation = parseRelation;
|
|
4
|
+
/**
|
|
5
|
+
* parse relation
|
|
6
|
+
* @function parseRelation
|
|
7
|
+
* @param line
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
function parseRelation(line) {
|
|
11
|
+
const relationRegex = /^@relation\s+(\w+)\.(\w+)\s+(\w+)\.(\w+)\s+(\w+-to-\w+)$/;
|
|
12
|
+
const match = line.trim().match(relationRegex);
|
|
13
|
+
if (!match) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const [, fromModel, fromField, toModel, toField, relationType] = match;
|
|
17
|
+
return {
|
|
18
|
+
fromModel,
|
|
19
|
+
fromField,
|
|
20
|
+
toModel,
|
|
21
|
+
toField,
|
|
22
|
+
type: relationType,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.removeDuplicateRelations = removeDuplicateRelations;
|
|
4
|
+
/**
|
|
5
|
+
* remove duplicate relations
|
|
6
|
+
* @param { readonly string[] } relations
|
|
7
|
+
* @returns { readonly string[] }
|
|
8
|
+
*/
|
|
9
|
+
function removeDuplicateRelations(relations) {
|
|
10
|
+
return [...new Set(relations)];
|
|
11
|
+
}
|
|
@@ -7,14 +7,12 @@ function generateValibotProperties(modelFields, config) {
|
|
|
7
7
|
.map((field) => {
|
|
8
8
|
// @relation, @v, @z exclude
|
|
9
9
|
const cleanDoc = field.comment
|
|
10
|
-
.filter((line) => !(line.includes(
|
|
11
|
-
|
|
12
|
-
line.includes("@z")))
|
|
13
|
-
.join("\n")
|
|
10
|
+
.filter((line) => !(line.includes('@relation') || line.includes('@v') || line.includes('@z')))
|
|
11
|
+
.join('\n')
|
|
14
12
|
.trim();
|
|
15
|
-
const docComment = config?.comment && cleanDoc ? ` /**\n * ${cleanDoc}\n */\n` :
|
|
13
|
+
const docComment = config?.comment && cleanDoc ? ` /**\n * ${cleanDoc}\n */\n` : '';
|
|
16
14
|
return `${docComment} ${field.fieldName}: v.${field.validation}`;
|
|
17
15
|
})
|
|
18
|
-
.join(
|
|
16
|
+
.join(',\n');
|
|
19
17
|
return fields;
|
|
20
18
|
}
|
|
@@ -12,7 +12,7 @@ const generate_valibot_schema_1 = require("./generate-valibot-schema");
|
|
|
12
12
|
*/
|
|
13
13
|
function generateValibotSchemas(modelFields, config) {
|
|
14
14
|
const modelName = modelFields[0].modelName;
|
|
15
|
-
const modelDoc = modelFields[0].documentation ||
|
|
15
|
+
const modelDoc = modelFields[0].documentation || '';
|
|
16
16
|
const fields = (0, generate_valibot_properties_1.generateValibotProperties)(modelFields, config);
|
|
17
17
|
if (!(modelDoc || !config?.comment)) {
|
|
18
18
|
return (0, generate_valibot_schema_1.generateValibotSchema)(modelName, fields, config);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Config } from
|
|
2
|
-
import type { Model } from
|
|
1
|
+
import type { Config } from '..';
|
|
2
|
+
import type { Model } from '../../../common/type';
|
|
3
3
|
/**
|
|
4
4
|
* Generate Valibot schemas and types
|
|
5
5
|
* @param models - The models to generate the Valibot schemas and types for
|
|
@@ -17,7 +17,7 @@ const VALIBOT_IMPORT = `import * as v from 'valibot'\n`;
|
|
|
17
17
|
function generateValibot(models, config) {
|
|
18
18
|
const modelInfos = models.map((model) => {
|
|
19
19
|
return {
|
|
20
|
-
documentation: model.documentation ??
|
|
20
|
+
documentation: model.documentation ?? '',
|
|
21
21
|
name: model.name,
|
|
22
22
|
fields: model.fields,
|
|
23
23
|
};
|
|
@@ -39,16 +39,14 @@ function generateValibot(models, config) {
|
|
|
39
39
|
const valibots = Object.values(groupedByModel).map((fields) => {
|
|
40
40
|
return {
|
|
41
41
|
generateValibotSchema: (0, generate_valibot_schemas_1.generateValibotSchemas)(fields, config),
|
|
42
|
-
generateValibotInfer: config.type ===
|
|
43
|
-
? (0, generate_valibot_infer_input_1.generateValibotInferInput)(fields[0].modelName, config)
|
|
44
|
-
: "",
|
|
42
|
+
generateValibotInfer: config.type === 'true' ? (0, generate_valibot_infer_input_1.generateValibotInferInput)(fields[0].modelName, config) : '',
|
|
45
43
|
};
|
|
46
44
|
});
|
|
47
45
|
return [
|
|
48
46
|
VALIBOT_IMPORT,
|
|
49
|
-
|
|
47
|
+
'',
|
|
50
48
|
valibots
|
|
51
49
|
.flatMap(({ generateValibotSchema, generateValibotInfer }) => [generateValibotSchema, generateValibotInfer].filter(Boolean))
|
|
52
|
-
.join(
|
|
53
|
-
].join(
|
|
50
|
+
.join('\n\n'),
|
|
51
|
+
].join('\n');
|
|
54
52
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import type { GeneratorOptions } from
|
|
2
|
+
import type { GeneratorOptions } from '@prisma/generator-helper';
|
|
3
3
|
export type Config = {
|
|
4
4
|
output?: string;
|
|
5
5
|
file?: string | string[];
|
|
6
|
-
schemaName?:
|
|
7
|
-
typeName?:
|
|
6
|
+
schemaName?: 'PascalCase' | 'camelCase' | string | string[];
|
|
7
|
+
typeName?: 'PascalCase' | 'camelCase' | string | string[];
|
|
8
8
|
type?: boolean | string | string[];
|
|
9
9
|
comment?: boolean | string | string[];
|
|
10
10
|
};
|
|
@@ -10,10 +10,10 @@ const generate_valibot_1 = require("./generator/generate-valibot");
|
|
|
10
10
|
const prettier_1 = require("prettier");
|
|
11
11
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
12
12
|
const DEFAULT_CONFIG = {
|
|
13
|
-
output:
|
|
14
|
-
file:
|
|
15
|
-
schemaName:
|
|
16
|
-
typeName:
|
|
13
|
+
output: './valibot',
|
|
14
|
+
file: 'index.ts',
|
|
15
|
+
schemaName: 'PascalCase',
|
|
16
|
+
typeName: 'PascalCase',
|
|
17
17
|
type: false,
|
|
18
18
|
comment: false,
|
|
19
19
|
};
|
|
@@ -24,30 +24,30 @@ async function main(options) {
|
|
|
24
24
|
schemaName: options.generator.config?.schemaName ?? DEFAULT_CONFIG.schemaName,
|
|
25
25
|
typeName: options.generator.config?.typeName ?? DEFAULT_CONFIG.typeName,
|
|
26
26
|
type: options.generator.config?.type ?? DEFAULT_CONFIG.type,
|
|
27
|
-
comment: options.generator.config?.comment ===
|
|
27
|
+
comment: options.generator.config?.comment === 'true',
|
|
28
28
|
};
|
|
29
29
|
const content = (0, generate_valibot_1.generateValibot)(options.dmmf.datamodel.models, config);
|
|
30
30
|
const code = await (0, prettier_1.format)(content, {
|
|
31
|
-
parser:
|
|
31
|
+
parser: 'typescript',
|
|
32
32
|
printWidth: 100,
|
|
33
33
|
singleQuote: true,
|
|
34
34
|
semi: false,
|
|
35
35
|
});
|
|
36
36
|
if (!config.output) {
|
|
37
|
-
throw new Error(
|
|
37
|
+
throw new Error('output is required');
|
|
38
38
|
}
|
|
39
39
|
if (!node_fs_1.default.existsSync(config.output)) {
|
|
40
40
|
node_fs_1.default.mkdirSync(config.output, { recursive: true });
|
|
41
41
|
}
|
|
42
|
-
const file = config.file ??
|
|
42
|
+
const file = config.file ?? 'index.ts';
|
|
43
43
|
const filePath = `${config.output}/${file}`;
|
|
44
44
|
node_fs_1.default.writeFileSync(filePath, code);
|
|
45
45
|
}
|
|
46
46
|
(0, generator_helper_1.generatorHandler)({
|
|
47
47
|
onManifest() {
|
|
48
48
|
return {
|
|
49
|
-
defaultOutput:
|
|
50
|
-
prettyName:
|
|
49
|
+
defaultOutput: './valibot/',
|
|
50
|
+
prettyName: 'Hekireki-Valibot',
|
|
51
51
|
};
|
|
52
52
|
},
|
|
53
53
|
onGenerate: main,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isValibotDocumentValidation = isValibotDocumentValidation;
|
|
4
|
-
const VALIDATION_PATTERNS = [
|
|
4
|
+
const VALIDATION_PATTERNS = ['@v.'];
|
|
5
5
|
function isValibotDocumentValidation(documentation) {
|
|
6
6
|
if (!documentation)
|
|
7
7
|
return [];
|
|
8
8
|
return documentation
|
|
9
|
-
.split(
|
|
9
|
+
.split('\n')
|
|
10
10
|
.filter((line) => !VALIDATION_PATTERNS.some((pattern) => line.includes(pattern)))
|
|
11
11
|
.map((line) => line.trim())
|
|
12
12
|
.filter(Boolean);
|
|
@@ -13,14 +13,12 @@ function generateZodProperties(modelFields, config) {
|
|
|
13
13
|
.map((field) => {
|
|
14
14
|
// @relation, @v, @z exclude
|
|
15
15
|
const cleanDoc = field.comment
|
|
16
|
-
.filter((line) => !(line.includes(
|
|
17
|
-
|
|
18
|
-
line.includes("@z")))
|
|
19
|
-
.join("\n")
|
|
16
|
+
.filter((line) => !(line.includes('@relation') || line.includes('@v') || line.includes('@z')))
|
|
17
|
+
.join('\n')
|
|
20
18
|
.trim();
|
|
21
|
-
const docComment = config?.comment && cleanDoc ? ` /**\n * ${cleanDoc}\n */\n` :
|
|
19
|
+
const docComment = config?.comment && cleanDoc ? ` /**\n * ${cleanDoc}\n */\n` : '';
|
|
22
20
|
return `${docComment} ${field.fieldName}: z.${field.validation}`;
|
|
23
21
|
})
|
|
24
|
-
.join(
|
|
22
|
+
.join(',\n');
|
|
25
23
|
return fields;
|
|
26
24
|
}
|
|
@@ -11,7 +11,7 @@ const generate_zod_schema_1 = require("./generate-zod-schema");
|
|
|
11
11
|
*/
|
|
12
12
|
function generateZodSchemas(modelFields, config) {
|
|
13
13
|
const modelName = modelFields[0].modelName;
|
|
14
|
-
const modelDoc = modelFields[0].documentation ||
|
|
14
|
+
const modelDoc = modelFields[0].documentation || '';
|
|
15
15
|
const fields = (0, generate_zod_properties_1.generateZodProperties)(modelFields, config);
|
|
16
16
|
if (!(modelDoc || !config?.comment)) {
|
|
17
17
|
return (0, generate_zod_schema_1.generateZodSchema)(modelName, fields, config);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Config } from
|
|
2
|
-
import type { Model } from
|
|
1
|
+
import type { Config } from '..';
|
|
2
|
+
import type { Model } from '../../../common/type';
|
|
3
3
|
/**
|
|
4
4
|
* Generate Zod schemas and types
|
|
5
5
|
* @param models - The models to generate the Zod schemas and types for
|
|
@@ -17,7 +17,7 @@ const ZOD_IMPORT = `import { z } from 'zod'\n`;
|
|
|
17
17
|
function generateZod(models, config) {
|
|
18
18
|
const modelInfos = models.map((model) => {
|
|
19
19
|
return {
|
|
20
|
-
documentation: model.documentation ??
|
|
20
|
+
documentation: model.documentation ?? '',
|
|
21
21
|
name: model.name,
|
|
22
22
|
fields: model.fields,
|
|
23
23
|
};
|
|
@@ -39,16 +39,14 @@ function generateZod(models, config) {
|
|
|
39
39
|
const zods = Object.values(groupedByModel).map((fields) => {
|
|
40
40
|
return {
|
|
41
41
|
generateZodSchema: (0, generate_zod_schemas_1.generateZodSchemas)(fields, config),
|
|
42
|
-
generateZodInfer: config.type ===
|
|
43
|
-
? (0, generate_zod_infer_1.generateZodInfer)(fields[0].modelName, config)
|
|
44
|
-
: "",
|
|
42
|
+
generateZodInfer: config.type === 'true' ? (0, generate_zod_infer_1.generateZodInfer)(fields[0].modelName, config) : '',
|
|
45
43
|
};
|
|
46
44
|
});
|
|
47
45
|
return [
|
|
48
46
|
ZOD_IMPORT,
|
|
49
|
-
|
|
47
|
+
'',
|
|
50
48
|
zods
|
|
51
49
|
.flatMap(({ generateZodSchema, generateZodInfer }) => [generateZodSchema, generateZodInfer].filter(Boolean))
|
|
52
|
-
.join(
|
|
53
|
-
].join(
|
|
50
|
+
.join('\n\n'),
|
|
51
|
+
].join('\n');
|
|
54
52
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import type { GeneratorOptions } from
|
|
2
|
+
import type { GeneratorOptions } from '@prisma/generator-helper';
|
|
3
3
|
export type Config = {
|
|
4
4
|
output?: string;
|
|
5
5
|
file?: string | string[];
|
|
6
|
-
schemaName?:
|
|
7
|
-
typeName?:
|
|
6
|
+
schemaName?: 'PascalCase' | 'camelCase' | string | string[];
|
|
7
|
+
typeName?: 'PascalCase' | 'camelCase' | string | string[];
|
|
8
8
|
type?: boolean | string | string[];
|
|
9
9
|
comment?: boolean | string | string[];
|
|
10
10
|
};
|