'use strict'; const { Pool } = require('pg'); function searchMeta(opts) { return async function (ctx, next) { let errMsg = { message: '搜索元数据失败' } try { const models = ctx.fs.dc.models; const { keywords } = ctx.query; const where = { }; if (keywords) { where['$or'] = [{ name: { $iLike: `%${keywords}%` } } ] } const findObj = { include: [ { model: models.TagDatabase, include: [{ model: models.Tag, }] }], where: { ...where, type: { $in: ['表', '库'] } } } const findObj2 = { include: [ { model: models.TagFile, include: [{ model: models.Tag, }] }], where: where } const findObj3 = { include: [ { model: models.TagRestapi, include: [{ model: models.Tag, }] }], where: where } const rslt1 = await models.MetadataDatabase.findAndCountAll(findObj); const rslt2 = await models.MetadataFile.findAndCountAll(findObj2); const rslt3 = await models.MetadataRestapi.findAndCountAll(findObj3); const fileRslt = rslt2.rows.map(s => { return { ...s.dataValues, type: '文件', tagDatabases: s.tagFiles } }) const restapiRslt = rslt3.rows.map(s => { return { ...s.dataValues, type: '接口', tagDatabases: s.tagRestapis } }) ctx.status = 200; ctx.body = { count: rslt1.count + rslt2.count + rslt3.count, rows: rslt1.rows.concat(fileRslt).concat(restapiRslt) }; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } // 下载表数据 function getTableData(opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.query; const metaData = await models.MetadataDatabase.findOne({ where: { id: id } }) let rslt = [], metaDataChildren = [] const { dbConfig } = opts; metaDataChildren = await models.MetadataDatabase.findAll({ where: { parent: id, type: '字段' } }) if (metaData.datasourceConfig) { let { user, host, database, password, port } = metaData.datasourceConfig; const pool = new Pool({ user: user, host: host, database: database, password: password, port: port }) const client = await pool.connect() const tableName = metaData.name; const ress = await client.query(`SELECT * from "${tableName}"`, []) rslt = ress.rows } else { let databaseFind = await models.MetadataDatabase.findOne({ where: { id: metaData.parent, type: '库' } }) if (databaseFind) { let dbconfigObj = { user: dbConfig.split('/')[0], host: dbConfig.split('/')[2], database: databaseFind.code, password: dbConfig.split('/')[1], port: dbConfig.split('/')[3], } let { user, host, database, password, port } = dbconfigObj; const pool = new Pool({ user: user, host: host, database: database, password: password, port: port }) const client = await pool.connect() const tableName = metaData.name; const ress = await client.query(`SELECT * from "${tableName}"`, []) rslt = ress.rows } else { rslt = [] } } ctx.status = 200; ctx.body = { rslt: rslt, metaDataChildren: metaDataChildren.map(s => s.code) } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 200; ctx.body = { message: '连接失败', rslt: [], metaDataChildren: [] } } } } module.exports = { searchMeta, getTableData }