DbConn.jsjavascript
// var config = { // database: 'protocol', // username: 'root', // password: '123456', // host: 'localhost', // port: 3306 // }; var sequelize = new Sequelize(config.database, config.username, config.password, { host: config.host, port: config.port, dialect: 'mysql', logging: console.log, pool: { max: 5, min: 0, idle: 30000 }, timestamps: false //最好關掉timestamps , 框架自動幫你添加時間到UpdatedAt上邊 }); sequelize .authenticate() .then(() => { console.log('Connection has been established successfully.'); }) .catch(err => { console.error('Unable to connect to the database:', err); }); module.exports = sequelize;
/** * 經過sql原生語句獲取對應表數據 */ exports.getTableDataBySql = function (sql, cb) { DbConn .query(sql, {type: DbConn.QueryTypes.SELECT}) .then(function (result) { cb(null, result) }) .catch(err => { cb(err) }); } /** * 可使用 ?條件添加參數查詢 * @param replacements 若是傳遞一個數組,? 會按數組的順序被依次替換 */ exports.getTableLimitDataBySql = function (sql, replacements, cb) { DbConn .query(sql, { replacements: replacements, type: DbConn.QueryTypes.SELECT }) .then(function (result) { cb(null, result) }) .catch(err => { cb(err) }); } /** * 執行原始sql插入數據 * @param replacements 若是傳遞一個數組,? 會按數組的順序被依次替換 */ exports.addDataBySql = function (sql, replacements, cb) { DbConn .query(sql, { replacements: replacements, type: DbConn.QueryTypes.INSERT }) .then(function (result) { console.log(result) cb(null, result) }) .catch(err => { console.log(err, 'err') cb(err) }); } /** * 執行原始sql更新數據 * @param replacements 若是傳遞一個數組,? 會按數組的順序被依次替換 */ exports.updateDataBySql = function (sql, replacements, cb) { DbConn .query(sql, { replacements: replacements, type: DbConn.QueryTypes.UPDATE }) .then(function (result) { cb(null, result) }) .catch(err => { cb(err) }); } exports.deleteDataBySql = function (sql, replacements, cb) { DbConn .query(sql, { replacements: replacements, type: DbConn.QueryTypes.DELETE }) .then(function (result) { cb(null, result) }) .catch(err => { cb(err) }); }
/** * 用事務執行插入 * @param sql * @param transaction * @param replacements * @returns {*} */ exports.addDataBySqlT = function (sql,transaction, replacements) { return DbConn .query(sql, { replacements: replacements, transaction:transaction, type: DbConn.QueryTypes.INSERT }) } /** * 用事務執行更新 * @param sql * @param replacements * @param cb */ exports.updateDataBySqlT = function (sql, transaction, replacements) { return DbConn .query(sql, { replacements: replacements, transaction:transaction, type: DbConn.QueryTypes.UPDATE }) } //main.js /** * 測試事務 * @param callback */ function f(callback) { var sql = "INSERT INTO table1(version, data) VALUES (?,?)" var sql2 = "INSERT INTO table2(name) VALUES (?)" var sql1 = "UPDATE table1 SET data = ? WHERE data = ?" DbManager.transactionManager().transaction(function (t){ // 要確保全部的查詢鏈都有return返回 return DbManager.addDataBySqlT(sql,t,["aaaa","bbbb"]).then(function (result) { return DbManager.addDataBySqlT(sql2,t,["ccc"]).then(function (err,ids) { return DbManager.updateDataBySqlT(sql1,t,["aaaa","bbbb"]).then(function (result) { //表示最終返回的結果 return ids[0] }); }) }) }).then(function (result) { // Transaction 會自動提交 // result 是事務回調中使用promise鏈中執行結果 callback(null,result) }).catch(function (err) { // Transaction 會自動回滾 // err 是事務回調中使用promise鏈中的異常結果 //callback(err,null) }); }