前提: 前端開發負責寫前端頁面以及node開發,node是基於egg.js,node的orm用的是sequelize
這里是有 modelMysqlName和modelLogMysqlName得到得兩個庫
本地得話在config/config.local.js里面寫了sequelize對象里面去定義
/**
* 用戶金幣兌換游戲幣記錄表
* @param { Object } body 傳入的參數
* @param { Object } body.time 時間的參數
* @param { String } body.userName 用戶名稱
* @param { String } body.userId 用戶id
* @param { String } type 判斷是不是導出
* @return { Object } list 是列表 total是篩選后的總數
*/
async gameCoinExchangeRecord({ limit, offset, time, userName = '', userId = '' }, type) {
const modelMysqlName = this.config.sequelize.datasources[0].database // 這里拿到配置得庫
const modelLogMysqlName = this.config.sequelize.datasources[1].database // 這里拿到配置得庫
// 關聯base表
const baseSql = `LEFT OUTER JOIN ${modelMysqlName}.hx_user_base_info AS baseInfo
on exchange.user_id = baseInfo.user_id
WHERE exchange.user_id LIKE '%${userId}%' ${time}
${userName ? `AND baseInfo.nickname LIKE '%${userName}%'` : ''}`
const list = await this.app.model.query( // 這里要把需要得數據一起拿到nickname AS userName等
`SELECT id, conf_exchange_id, income AS sendGameCoin, nickname AS userName, expend AS exchangeCostGold,
exchange.user_id AS userId, created_at AS exchangeTime FROM ${modelLogMysqlName}.hx_user_gold_exchange_record AS
exchange ${baseSql} ${type !== 'exportExcel' ? `LIMIT ${offset}, ${limit}` : ''}`, { type: 'SELECT' }
)
// 獲取數量
const count = await this.ctx.model.query(`
select count(exchange.id) as total FROM ${modelLogMysqlName}.hx_user_gold_exchange_record as exchange
${baseSql}`, { type: 'SELECT' })
return { list, total: count[0].total }
}
如何定義?
module.exports = () => {
// 企業微信相關配置
return {
// tars
sequelize: {
datasources: [{
delegate: 'xx',
dialect: 'xx',
baseDir: 'xx',
username: 'xx',
password: 'xx',
host: 'xx',
port: xx,
database: 'xx',
timezone: 'xx',
define: {
freezeTableName: true, // 防止修改表名為復數
underscored: true, // 防止駝峰式字段被默認轉為下划線
},
},
{
delegate: 'xx',
dialect: 'xx',
baseDir: 'xx',
username: 'xx',
password: 'xx',
host: 'xx',
port: xx,
database: 'xx',
timezone: 'xx',
define: {
freezeTableName: true, // 防止修改表名為復數
underscored: true, // 防止駝峰式字段被默認轉為下划線
},
}],
},
}
}