Last active
August 3, 2017 10:00
-
-
Save CatsMiaow/5fecccdfe2d82fc68ffea0ca678e7dc6 to your computer and use it in GitHub Desktop.
Node.js - MySQL에서 Bluebird 사용하기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var db = require('./db'); | |
var testModel = require('./model'); | |
var testControllers = { | |
// 모델에서 db.single() 함수를 사용한 예제입니다. | |
// 쿼리를 실행하고 커넥션을 반환합니다. | |
test1: function (req, res) { | |
testModel.getTest('test1').then(function (rows) { | |
if (rows.length < 1) { | |
throw new Error('No Result'); | |
} | |
// ... | |
return testModel.getTest('test2'); | |
}).then(function (rows) { | |
res.json(rows[0]); | |
}).catch(function (err) { | |
// err.code 값이 있으면 MySQL 내부 오류입니다. | |
res.status(err.code ? 500 : 400).json({ message: err.message }); | |
}); | |
}, | |
// 모델에서 conn.queryAsync() 함수를 사용한 예제입니다. | |
// 트랙잭션 또는 하나의 커넥션에서 여러 쿼리를 실행할 때 사용합니다. | |
test2: function (req, res) { | |
db.trans(function (conn) { // db.query(function (conn) { ... }); | |
return testModel.setTest(conn, 'test1').then(function (result) { | |
return testModel.setTest(conn, 'test2'); | |
}).then(function (result) { | |
return testModel.setTest(conn, 'test3'); | |
}); | |
}).then(function () { | |
// Query Success, Transaction COMMIT | |
}).catch(function () { | |
// Query Error, Transaction ROLLBACK | |
}); | |
} | |
}; | |
module.exports = testControllers; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var mysql = require('mysql'); | |
var Promise = require('bluebird'); | |
var using = Promise.using; | |
var pool; | |
Promise.promisifyAll(require('mysql/lib/Connection').prototype); | |
Promise.promisifyAll(require('mysql/lib/Pool').prototype); | |
pool = mysql.createPool(config.mysql); | |
function getConnection() { | |
return pool.getConnectionAsync().disposer(function (connection) { | |
return connection.release(); | |
}); | |
} | |
function getTransaction() { | |
return pool.getConnectionAsync().then(function (connection) { | |
return connection.beginTransactionAsync().then(function () { | |
return connection; | |
}); | |
}).disposer(function (connection, promise) { | |
var result = promise.isFulfilled() ? connection.commitAsync() : connection.rollbackAsync(); | |
return result.finally(function () { | |
connection.release(); | |
}); | |
}); | |
} | |
module.exports = { | |
// 간편 쿼리 | |
single: function (sql, values) { | |
return using(getConnection(), function (connection) { | |
return connection.queryAsync({ | |
sql: sql, | |
values: values | |
// nestTables: true, | |
// typeCast: false, | |
// timeout: 10000 | |
}); | |
}); | |
}, | |
// 연달아 쿼리 | |
query: function (callback) { | |
return using(getConnection(), function (connection) { | |
return callback(connection); | |
}); | |
}, | |
// 트랜잭션 | |
trans: function (callback) { | |
return using(getTransaction(), function (connection) { | |
return callback(connection); | |
}); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var db = require('./db'); | |
module.exports = { | |
getTest: function (value) { | |
return db.single('SELECT * FROM table WHERE field = ?', [value]); | |
}, | |
setTest: function (conn, value) { | |
return conn.queryAsync('INSERT INTO table SET ?', { | |
test: value | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
orm 쓰는 것보다 깔끔해서 좋아요~ 감사합니다.