'use strict'; 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 } } } module.exports = { searchMeta }