Last active
August 29, 2015 14:24
-
-
Save gtomitsuka/a7becea517e83aad2e73 to your computer and use it in GitHub Desktop.
Promisified node-postgres
This file contains 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
var pg = require('pg'); | |
var Promise = require('bluebird'); | |
exports.connect = function(config){ | |
return new Promise(function(resolve, reject){ | |
pg.connect(config, function(error, connection, next){ | |
if(error) | |
throw error; | |
connection.queryAsync = function(){ | |
var args = arguments.length === 1 ? [arguments[0]] : | |
Array.apply(null, arguments); | |
return new Promise(function(resolve, reject){ | |
args.push(function(error, result){ | |
if(error) | |
throw error; | |
return resolve([result, next]); | |
}); | |
connection.query.apply(connection, args); | |
}); | |
} | |
resolve(connection); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment