'use strict'; // 新增数据源 function addDataSource(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; try { let rslt = ctx.request.body; await models.DataSource.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 getDataSource(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { page, limit, name } = ctx.query; let errMsg = { message: '获取数据源失败' } const Op = ctx.fs.dc.ORM.Op; try { let searchWhere = {} let option = { where: searchWhere, order: [["id", "desc"]], } if (name) { searchWhere.name = { // 模糊查询 [Op.like]: '%' + name + '%', }; } option.where = searchWhere let limit_ = limit || 10; let page_ = page || 1; let offset = (page_ - 1) * limit_; if (limit && page) { option.limit = limit_ option.offset = offset } const res = await models.DataSource.findAndCount(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 = { addDataSource, getDataSource }