UNPKG

egg-xc-base

Version:

a base framework with egg.js

181 lines (176 loc) 6.07 kB
'use strict'; const BaseService = require('./base-service'); /** * 数据库Service基类 */ class DbService extends BaseService { // 获取helper里的数据库对象 get db() { return this.helper.db; } // 为直接使用mysql模块的escape get mql() { return require('mysql'); } /** * 直接用db1执行sql * @param {string} _sql sql语句 * @return {Promise} Promise */ async doQuery(_sql,disableLog = false) { return await this.db.doQuery(_sql, this,disableLog); } /** * 直接用db1执行sql 如果查不到就返回null,查到直接返回对象 * @param {string} _sql sql语句 * @return {Promise} 数据对象 */ async doQueryObj(_sql,disableLog = false){ let result = await this.db.doQuery(_sql, this,disableLog); if(result.length == 0) return null return result[0] } /** * 分页查询 * @param {object} info * @param {string} info.sql sql语句 * @param {number} info.page_num 页码默认1 * @param {number} info.page_size 一页多少条默认10 */ async doPage(info){ this.ctx.validate({ _sql : {type:'string'} },info) let list = [] let count = 0 let page_num = info.page_num || 1 let page_size = info.page_size || 10 page_num = typeof(page_num) == 'string' ? parseInt(page_num) : page_num page_size = typeof(page_size) == 'string' ? parseInt(page_size) : page_size let that = this await Promise.all([ async function(){ list = await that.doQuery(`${info._sql} limit ${ that.to((page_num-1)*page_size ) } , ${that.to(page_size)}`) }(), async function(){ if(!info.count_sql){ if(info._sql.lastIndexOf('order by') > -1){ info._sql = info._sql.substr(0,info._sql.lastIndexOf('order by')) } if(info._sql.lastIndexOf('ORDER BY') > -1){ info._sql = info._sql.substr(0,info._sql.lastIndexOf('ORDER BY')) } if(info._sql.lastIndexOf('ORDER by') > -1){ info._sql = info._sql.substr(0,info._sql.lastIndexOf('ORDER by')) } if(info._sql.lastIndexOf('order BY') > -1){ info._sql = info._sql.substr(0,info._sql.lastIndexOf('order BY')) } info.count_sql = `select count(*) as c from ( ${info._sql}) c` }else{ info.count_sql = `select count(*) as c from ( ${info.count_sql}) c` } let c = await that.doQuery(info.count_sql) if(c.length>0){ count =c[0].c } }() ]) return { list, count } } /** * 直接用db1执行sql 抛出错误 * @param {string} _sql sql语句 * @return {Promise} Promise */ async doQueryError(_sql,disableLog = false) { return await this.db.doQueryError(_sql, this,disableLog); } /** * 在tran执行事务的时候使用可以传递统一的conn数据库连接 * @param {object} conn 数据库连接 * @param {string} _sql sql语句 * @return {Promise} Promise */ async doQueryConn(conn, _sql) { return await this.db.doQueryConn(_sql, conn, this); } /** * 在tran执行事务的时候使用可以传递统一的conn数据库连接 如果查不到就返回null,查到直接返回对象 * @param {object} conn 数据库连接 * @param {string} _sql sql语句 * @return {Promise} 数据对象 */ async doQueryConnObj(conn, _sql){ let result = await this.db.doQueryConn(_sql, conn,this); if(result.length == 0) return null return result[0] } /** * 在tran执行事务的时候使用可以传递统一的conn数据库连接 抛出错误 * @param {object} conn 数据库连接 * @param {string} _sql sql语句 * @return {Promise} Promise */ async doQueryConnError(conn, _sql) { return await this.db.doQueryConnError(_sql, conn, this); } /** * 执行事务 * @param {asyncFunction} asyncFunction 事务执行 async回调方法 * @return {Promise} Promise */ async tran(asyncFunction) { return await this.db.tran(this, asyncFunction); } /** * 执行事务 抛出Error * @param {asyncFunction} asyncFunction 事务执行 async回调方法 * @return {Promise} Promise */ async tranError(asyncFunction) { return await this.db.tranError(this, asyncFunction); } /** * 执行多数据源sql * @param {string} dbId 数据源id * @param {string} _sql sql语句 * @return {Promise} Promise */ async doMQuery(dbId, _sql) { const conn = this.app.mysql.get(dbId); return await this.db.doQueryConn(_sql, conn, this); } /** * 执行多数据源事务 * @param {string} dbId 数据源id * @param {string} asyncFunction 事务执行 async回调方法 * @return {Promise} Promise */ async tranM(dbId, asyncFunction) { const conn = this.app.mysql.get(dbId); return this.db.tranConn(this, conn, asyncFunction); } /** * 执行多数据源事务 抛出错误 * @param {string} dbId 数据源id * @param {string} asyncFunction 事务执行 async回调方法 * @return {Promise} Promise */ async tranMError(dbId, asyncFunction) { const conn = this.app.mysql.get(dbId); return this.db.tranConnError(this, conn, asyncFunction); } /** * 防sql注入处理 * @param {object} params sql中需要处理的字段 * @return {object} object */ to(params) { return this.mql.escape(params); } } module.exports = DbService;