mas-server 2.0.0 → 2.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.
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = {
4
- status: 1,
5
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getType = void 0;
4
- function getType(obj) {
5
- // "".toLocaleLowerCase
6
- return Object.prototype.toString
7
- .call(obj)
8
- .slice(8)
9
- .replace("]", "")
10
- .toLocaleLowerCase();
11
- }
12
- exports.getType = getType;
@@ -1,55 +0,0 @@
1
- import state from "./state";
2
- import { sqlQuery } from "./esaysql";
3
- import c from "ansi-colors";
4
- /** 添加相关表名 */
5
- const replenishForm = async (formName: string, key: string) => {
6
- // state.status = 0;
7
- const replenishData = {
8
- create_time: "`create_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP",
9
- update_time:
10
- "`update_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
11
- is_delete: "`is_delete` INT DEFAULT 0",
12
- };
13
- console.info(c.yellow(`${formName}缺少字段${key}`));
14
- await sqlQuery(`alter table ${formName} add ${replenishData[key]}`);
15
- console.info(c.yellow("已为您自动添加"));
16
- };
17
- /** 获取相关表名,并检查每个表是否含有 is_delete,create_time,update_time,没有则添加 */
18
- export default async function checkSqlKey() {
19
- let dbname = state.config.database;
20
- // 获得数据库相关表名
21
- let formName = (await sqlQuery(
22
- `select table_name
23
- from information_schema.tables
24
- where table_schema='${dbname}'`
25
- )) as any;
26
- const sqlform = {};
27
- formName = formName.map((item) => item["TABLE_NAME"]);
28
- const listKeyArr = {};
29
- // 获得相关表字段,并且没有
30
- for (let item of formName) {
31
- let listkey: any = await sqlQuery(
32
- `SELECT DATA_TYPE,COLUMN_NAME FROM information_schema.COLUMNS
33
- WHERE table_name = '${item}' AND
34
- \`TABLE_SCHEMA\`='${dbname}'`
35
- );
36
- listKeyArr[item] = JSON.parse(JSON.stringify(listkey));
37
- listkey = listkey.map((item) => item["COLUMN_NAME"]);
38
- if (!listkey.includes("create_time")) {
39
- await replenishForm(item, "create_time");
40
- listkey.push("create_time");
41
- }
42
- if (!listkey.includes("update_time")) {
43
- await replenishForm(item, "update_time");
44
- listkey.push("update_time");
45
- }
46
- if (!listkey.includes("is_delete")) {
47
- await replenishForm(item, "is_delete");
48
- listkey.push("is_delete");
49
- }
50
- sqlform[item] = listkey;
51
- }
52
- state.listKey = JSON.parse(JSON.stringify(listKeyArr));
53
- state.sqlForm = { ...sqlform };
54
- return sqlform;
55
- }
@@ -1,37 +0,0 @@
1
- import { sqlQuery } from "./esaysql";
2
- import state from "./state";
3
- import { createSqlForm } from "./type";
4
-
5
- export default async (form: { [key: string]: createSqlForm }) => {
6
- const formArr = Object.keys(state.sqlForm);
7
- for (let k in form) {
8
- let data = form[k];
9
- if (formArr.includes(k.toLowerCase())) {
10
- continue;
11
- }
12
- const sqldata = `CREATE TABLE ${k.toLowerCase()}
13
- (
14
- \`id\` INT AUTO_INCREMENT PRIMARY KEY COMMENT '唯一主键',
15
- ${data
16
- .map(
17
- (item) =>
18
- `\`${item.key}\` ${
19
- item.type.toLowerCase() == "str"
20
- ? `VARCHAR(${item.length || "100"})`
21
- : item.type.toLowerCase() == "int"
22
- ? "INT"
23
- : item.type.toLowerCase() == "float"
24
- ? "FLOAT"
25
- : "VARCHAR(100)"
26
- } ${item.notNull ? "NOT NULL" : ""} COMMENT '${item.des || ""}'`
27
- )
28
- .join(",")},
29
- \`create_time\` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
30
- \`update_time\` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
31
- \`is_delete\` INT DEFAULT 0 COMMENT '是否被删除'
32
- )`;
33
- await sqlQuery(sqldata, 1);
34
- console.log(`尝试创建${k}表完成`);
35
- }
36
- console.log("创建表完成");
37
- };
@@ -1,44 +0,0 @@
1
- const mysql = require("mysql");
2
- import state from "./state";
3
- import c from "ansi-colors";
4
- export function sqlInit(config: {
5
- host: string;
6
- user: string;
7
- password: string;
8
- database: string;
9
- }) {
10
- state.config = config;
11
- state.connection = mysql.createConnection(config);
12
- return new Promise((resolve, reject) => {
13
- state.connection.connect(function (err) {
14
- if (err) {
15
- console.error("error connecting: " + err.stack);
16
- throw Error("连接数据库失败!");
17
- } else {
18
- resolve(true);
19
- }
20
- });
21
- });
22
- }
23
- export function sqlEnd() {
24
- state.connection.end();
25
- }
26
- export function sqlQuery(sqldata, details = 0) {
27
- return new Promise((resolve, reject) => {
28
- state.connection.query(sqldata, (error, sqlRes) => {
29
- if (details) {
30
- if (error) {
31
- reject(error);
32
- console.info(error);
33
- } else resolve(sqlRes);
34
- }
35
- if (!sqlRes) {
36
- console.info(error, sqlRes);
37
- console.info(c.red("错误:数据库语句错误"));
38
- resolve(undefined);
39
- } else if (Number.isInteger(sqlRes.affectedRows))
40
- resolve(sqlRes.affectedRows);
41
- else resolve(JSON.parse(JSON.stringify(sqlRes)));
42
- });
43
- });
44
- }
@@ -1,68 +0,0 @@
1
- import checkSqlKey from "./checkSqlkey";
2
- import createSqlFormScript from "./createSqlForm";
3
- import { sqlEnd, sqlInit, sqlQuery } from "./esaysql";
4
- import massql from "./masSql";
5
- import state from "./state";
6
- import * as fs from "fs";
7
- import type { createSqlFormType, sqlForm, sqlState } from "./type";
8
- /** 创建表名 */
9
- async function createSqlForm(
10
- config: sqlState["config"],
11
- createForm: createSqlFormType
12
- ) {
13
- sqlInit(config);
14
- await checkSqlKey();
15
- await createSqlFormScript(createForm);
16
- sqlEnd();
17
- }
18
- export function getsqlFormData(path?: string) {
19
- if (!state.sqlForm) {
20
- throw Error("请先初始化数据库!");
21
- }
22
- const sqlForm = JSON.parse(JSON.stringify(state.sqlForm));
23
- for (let k in sqlForm) {
24
- sqlForm[k].push(`@!${k}`);
25
- }
26
- if (path) {
27
- if (!path.includes(".json")) {
28
- throw Error("写入路径错误");
29
- }
30
-
31
- if (fs.existsSync(path)) {
32
- const data = require(path);
33
- if (JSON.stringify(data) != JSON.stringify(sqlForm)) {
34
- fs.writeFileSync(path, `${JSON.stringify(sqlForm)}`);
35
- }
36
- } else {
37
- fs.writeFileSync(path, `${JSON.stringify(sqlForm)}`);
38
- }
39
- }
40
- return sqlForm;
41
- }
42
- export function getsqlFormDataType(): {
43
- [key: string]: { typeData: any; typeObj: any };
44
- } {
45
- if (!state.listKey) {
46
- throw Error("请先初始化数据库!");
47
- }
48
- let tarr = { int: "number", float: "number" };
49
- let btarr = { int: "Number", float: "Number" };
50
- let nnn = { is_delete: "", update_time: "", create_time: "" };
51
- let res = {};
52
- for (let key in state.listKey) {
53
- let typeObj = "{";
54
- let typeData = "{";
55
- for (let item of state.listKey[key]) {
56
- if (nnn[item.COLUMN_NAME] === "") continue;
57
- typeObj += `${item.COLUMN_NAME}?:${tarr[item.DATA_TYPE] || "string"};`;
58
- if (item.COLUMN_NAME==="id") continue;
59
- typeData += `${item.COLUMN_NAME}:${btarr[item.DATA_TYPE] || "String"},`;
60
- }
61
- typeData=typeData.slice(0,typeData.length-1) + "}";
62
- typeObj += "}";
63
- res[key] = { typeData, typeObj };
64
- }
65
- return res;
66
- }
67
- export { sqlEnd, sqlInit, state, sqlQuery, checkSqlKey, createSqlForm, massql };
68
- export type { createSqlFormType, sqlState, sqlForm };