'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 }