政务数据资源中心(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.
 
 
 
 

72 lines
2.0 KiB

'use strict';
const { Pool } = require('pg');
// 测试数据库连接
function checkConnect(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
try {
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()
ctx.status = 200;
ctx.body = { message: client._connected ? '连接成功' : '连接失败' }
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 200;
ctx.body = { message: '连接失败' }
}
}
}
// 新增适配器
function addAdapter(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
try {
let rslt = ctx.request.body;
await models.Adapter.create(Object.assign({}, rslt))
ctx.status = 204;
ctx.body = { message: '新建适配器成功' }
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '新建适配器失败' }
}
}
}
function getAdapters(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
let errMsg = { message: '获取适配器失败' }
try {
let option = {
where: {},
order: [["id", "desc"]],
}
const res = await models.Adapter.findAll(option);
ctx.status = 200;
ctx.body = res;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = errMsg
}
}
}
module.exports = {
checkConnect,
addAdapter,
getAdapters
}