Last active
August 3, 2017 10:00
-
-
Save CatsMiaow/5fecccdfe2d82fc68ffea0ca678e7dc6 to your computer and use it in GitHub Desktop.
Revisions
-
CatsMiaow revised this gist
Apr 6, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -24,9 +24,9 @@ var testControllers = { // 트랙잭션 또는 하나의 커넥션에서 여러 쿼리를 실행할 때 사용합니다. 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 () { -
CatsMiaow revised this gist
Apr 6, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,7 @@ var testControllers = { }).then(function (rows) { res.json(rows[0]); }).catch(function (err) { // err.code 값이 있으면 MySQL 내부 오류입니다. res.status(err.code ? 500 : 400).json({ message: err.message }); }); }, -
CatsMiaow revised this gist
Apr 6, 2016 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
CatsMiaow created this gist
Apr 6, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ '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) { 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 (rows) { return testModel.setTest(conn, 'test2'); }).then(function (rows) { 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ '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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ '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 }); } };