Skip to content

Instantly share code, notes, and snippets.

@gtomitsuka
Last active August 29, 2015 14:24
Show Gist options
  • Save gtomitsuka/a7becea517e83aad2e73 to your computer and use it in GitHub Desktop.
Save gtomitsuka/a7becea517e83aad2e73 to your computer and use it in GitHub Desktop.
Promisified node-postgres
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