const Automate = require('sequelize-automate-freesun') const moment = require('moment'); //@toto删除数据库源时 同步删除该数据源资源目录树下元数据 //数据源 数据库配置信息不能更改 若需更改则需要删除数据源重新配置 async function handleTask(app, task) { const transaction = await app.fs.dc.orm.transaction(); try { const { models } = app.fs.dc const dataSource = await models.DataSource.findOne({ where: { id: task.dataSourceId } }); if (dataSource) { const dbOptions = createDbOptions(dataSource.config); const automate = new Automate(dbOptions, {}); const tables = await automate.getTables(); const dataToSave = { code: dataSource.config.database, name: dataSource.config.database, catalog: dataSource.mountPath, parent: null, description: null, type: "库", createBy: 1, createAt: moment(), updateAt: null, user: { id: 1, name: "超级管理员", username: "SuperAdmin" }, attributesParam: null, catalogKey: 'rc' } //初始化数据 新增 数据库 表 字段索引外键数据 let databaseFind = await models.MetadataDatabase.findOne({ where: { code: dataSource.config.database, type: '库', catalog: dataSource.mountPath, }//目录树下库只会存在一个 判断是否有库类型元数据 没有则新增库和表元数据 有库数据则比较更新表类型元数据 全量更新字段索引数据 }) if (databaseFind) { //更新表类型元数据 const newTableNames = [] Object.keys(tables).forEach(key => { newTableNames.push(key) }); //删除不存在表元数据 await models.MetadataDatabase.destroy({ where: { name: { $notIn: newTableNames }, type: '表', catalog: dataSource.mountPath, } }) //录入新增的表元数据 const metaDatabaseTables = await models.MetadataDatabase.findAll({ where: { type: '表', catalog: dataSource.mountPath, } }) const tableBodys = [] Object.keys(tables).forEach(table => { if (!metaDatabaseTables.find(s => s.code == table)) { dataToSave.parent = databaseFind.id; dataToSave.name = table; dataToSave.code = table; dataToSave.type = '表'; const tableObj = { ...dataToSave } tableBodys.push(tableObj) } }); await models.MetadataDatabase.bulkCreate(tableBodys, { returning: true }); } else { //新增库、表类型数据 //库类型元数据存储 const databaseRslt = await models.MetadataDatabase.create(dataToSave) //表元数据存储 if (databaseRslt && databaseRslt.id) { const tableBodys = [] Object.keys(tables).forEach(table => { dataToSave.parent = databaseRslt.id; dataToSave.name = table; dataToSave.code = table; dataToSave.type = '表'; const tableObj = { ...dataToSave } tableBodys.push(tableObj) }); await models.MetadataDatabase.bulkCreate(tableBodys, { returning: true }); } } const metaDatabaseTables = await models.MetadataDatabase.findAll({ where: { type: '表', catalog: dataSource.mountPath, } }) //字段/索引/外键全量更新 先删除之前的字段 再录入新的数据 await models.MetadataDatabase.destroy({ where: { type: { $in: ['字段', '索引', '外键'] }, catalog: dataSource.mountPath, } }) const fieldBodys = [] for (let table of metaDatabaseTables) { if(!tables[table.name].structures){ console.log(table) return; } Object.keys(tables[table.name].structures).forEach(key => { dataToSave.parent = table.id; dataToSave.name = tables[table.name].structures[key].comment || key; dataToSave.code = key; dataToSave.type = '字段'; const tableObj = { ...dataToSave } fieldBodys.push(tableObj) }) } const fieldRslt = await models.MetadataDatabase.bulkCreate(fieldBodys); if (fieldRslt) { //日志记录 } } } catch (error) { // await transaction.rollback(); app.fs.logger.error(`sechedule: handleTask, error: ${error}`); } } function createDbOptions(params) { const dbOptions = { database: params.database, username: params.user, password: params.password, dialect: 'postgres', host: params.host, port: params.port, define: { underscored: false, freezeTableName: false, charset: 'utf8mb4', timezone: '+00: 00', dialectOptions: { collate: 'utf8_general_ci', }, timestamps: false, }, } return dbOptions } module.exports = { handleTask }