政务数据资源中心(Government data Resource center) 03专项3期主要建设内容
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
3.3 KiB

'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) {
const models = ctx.fs.dc.models;
try {
const { id } = ctx.query;
let { user, host, database, password, port } = ctx.request.body;
const pool = new Pool({
user: user,
host: host,
database: database,
password: password,
port: port,
})
const client = await pool.connect()
const tableName = "user"
const ress = await client.query(`SELECT * from "${tableName}"`, [])
console.log(ress.rows)
ctx.status = 200;
ctx.body = { rslt: ress.rows }
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 200;
ctx.body = { message: '连接失败' }
}
}
}
module.exports = {
searchMeta,
getTableData
}