Files
CID/scripts/package/createSql.mjs
2025-10-30 19:34:04 +08:00

66 lines
2.5 KiB
JavaScript

import fs from 'fs';
import {appList, BasicFuncs, createAppId, createFunctionId} from './function.mjs';
const APP_TABLE_NAME = 'sys_app';
const FUNCTION_TABLE_NAME = 'sys_function';
let APP_SQL = `-- ----------------------------
-- Table structure for ${APP_TABLE_NAME}
-- ----------------------------
DROP TABLE IF EXISTS \`${APP_TABLE_NAME}\`;
CREATE TABLE \`${APP_TABLE_NAME}\` (
\`app_id\` bigint NOT NULL COMMENT '应用ID',
\`app_code\` varchar(50) NOT NULL COMMENT '应用code',
PRIMARY KEY (\`app_id\`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '应用' ROW_FORMAT = DYNAMIC;
`;
let FUNCTION_SQL = `-- ----------------------------
-- Table structure for ${FUNCTION_TABLE_NAME}
-- ----------------------------
DROP TABLE IF EXISTS \`${FUNCTION_TABLE_NAME}\`;
CREATE TABLE \`${FUNCTION_TABLE_NAME}\` (
\`function_id\` bigint NOT NULL COMMENT '功能编码ID',
\`function_code\` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '功能编码 应用名称:权限标识 manager edit view',
\`app_code\` varchar(50) NOT NULL COMMENT '应用code',
PRIMARY KEY (\`function_id\`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '功能编码与应用' ROW_FORMAT = DYNAMIC;
`;
const addApps = (item, index) => {
const appId = createAppId(item.name);
APP_SQL += `\nINSERT INTO \`${APP_TABLE_NAME}\` VALUES (${appId}, '${item.name}');`;
};
const addFuncs = (item, index, itemApp, indexApp) => {
const appId = createAppId(itemApp.name);
const functionId = createFunctionId(item, appId);
FUNCTION_SQL += `\nINSERT INTO \`${FUNCTION_TABLE_NAME}\` VALUES (${functionId}, '${itemApp.name}_${item}', '${itemApp.name}');`;
};
for (let indexApp = 0; indexApp < appList.length; indexApp++) {
const itemApp = appList[indexApp];
addApps(itemApp, indexApp);
let indexFunc = 0;
for (indexFunc; indexFunc < BasicFuncs.length; indexFunc++) {
const itemFunc = BasicFuncs[indexFunc];
addFuncs(itemFunc, indexFunc, itemApp, indexApp);
}
if (itemApp.functions && itemApp.functions.length > 0) {
for (const itemFunc of itemApp.functions) {
indexFunc += 1;
addFuncs(itemFunc, indexFunc, itemApp, indexApp);
}
}
}
try {
fs.writeFileSync(`./sql/${APP_TABLE_NAME}.sql`, APP_SQL);
console.log(`file written successfully :>> ${APP_TABLE_NAME}.sql\n`);
fs.writeFileSync(`./sql/${FUNCTION_TABLE_NAME}.sql`, FUNCTION_SQL);
console.log(`file written successfully :>> ${FUNCTION_TABLE_NAME}.sql\n`);
} catch (err) {
console.error(err);
}