自动同步服务器的Entity

Entity生成 添加 任意键值 和 字段可为undefined

Revert "Entity生成 添加 任意键值 和 字段可为undefined"

This reverts commit a65490cfb92ca5b9da1803cfc7eef569d796f651.

Revert "自动同步服务器的Entity"

This reverts commit f24de490ec66ce9617246d7f20dd889153eea65e.

eps 自动生成 entity.d.ts 描述文件
This commit is contained in:
zhisenyang 2022-06-17 18:49:06 +08:00
parent 68858f794c
commit 9691fa2b5f
3 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,36 @@
export default {
// 实体生成
entity: {
// 是否生成
enable: true,
mapping: [
{
// 自定义匹配
custom: ({ entityName, propertyName, type }) => {
// status原本是tinyint如果是1的话== true是可以的但是不能 === true请谨慎使用
if (propertyName === "status" && type == "tinyint") return "boolean";
//如果没有返回null或者不返回则继续遍历其他匹配规则
return null;
}
},
{
// 返回类型
type: "string",
// 匹配列类型
includes: ["varchar", "text"]
},
{
type: "Date",
includes: ["datetime", "date"]
},
{
type: "number",
includes: ["tinyint", "int", "decimal"]
},
{
type: "BigInt",
includes: ["bigint"]
}
]
}
};

View File

@ -3,6 +3,8 @@ import { isEmpty, last } from "lodash";
import { createDir, firstUpperCase, readFile, toCamel } from "../../utils"; import { createDir, firstUpperCase, readFile, toCamel } from "../../utils";
import { createWriteStream } from "fs"; import { createWriteStream } from "fs";
import { join } from "path"; import { join } from "path";
// import * as config from "/@/cool/config";
import config from "./config";
// 临时目录路径 // 临时目录路径
const tempPath = join(__dirname, "../../temp"); const tempPath = join(__dirname, "../../temp");
@ -211,9 +213,70 @@ export async function createEps({ list, service }: any) {
}) })
) )
); );
if (config.entity.enable) createEntity(list);
} }
// 获取描述 // 获取描述
export function getEps() { export function getEps() {
return JSON.stringify(readFile(join(tempPath, "eps.json"))); return JSON.stringify(readFile(join(tempPath, "eps.json")));
} }
function getType({ entityName, propertyName, type }) {
for (const map of config.entity.mapping) {
if (map.custom) {
const resType = map.custom({ entityName, propertyName, type });
if (resType) return resType;
}
if (map.includes?.includes(type)) return map.type;
}
return type;
}
// 创建Entity描述文件
export function createEntity(list: any[]) {
const t2: any[] = [];
for (const item of list) {
if (!item.name) continue;
const t = [`declare interface ${item.name} {`];
for (const col of item.columns) {
// 描述
t.push("\n");
t.push("/**\n");
t.push(` * ${col.comment}\n`);
t.push(" */\n");
t.push(
`${col.propertyName}?: ${getType({
entityName: item.name,
propertyName: col.propertyName,
type: col.type
})};`
);
}
t.push("\n");
t.push("/**\n");
t.push(` * 任意键值\n`);
t.push(" */\n");
t.push(`[key: string]: any;`);
t.push("}");
t2.push(t);
}
// 文本内容
const content = prettier.format(t2.map((e) => e.join("")).join("\n\n"), {
parser: "typescript",
useTabs: true,
tabWidth: 4,
endOfLine: "lf",
semi: true,
singleQuote: false,
printWidth: 100,
trailingComma: "none"
});
// 创建 entity 描述文件
createWriteStream(join(tempPath, "entity.d.ts"), {
flags: "w"
}).write(content);
}

1
src/env.d.ts vendored
View File

@ -1,4 +1,5 @@
/// <reference types="@cool-vue/crud" /> /// <reference types="@cool-vue/crud" />
/// <reference types="../build/cool/temp/service" /> /// <reference types="../build/cool/temp/service" />
/// <reference types="../build/cool/temp/entity" />
declare const __EPS__: string; declare const __EPS__: string;