Comparing version 0.12.4 to 0.13.0
"use strict"; | ||
//require('pg').defaults.ssl = true;; | ||
var pg = require('pg').native, | ||
path = require('path'), | ||
assert = require('assert'), | ||
_ = require('lodash'), | ||
_str = require('underscore.string'), | ||
Promise = require('bluebird'), | ||
fs = Promise.promisifyAll(require('fs')), | ||
handlebars = require('handlebars'), | ||
crypto = require('crypto'), | ||
util = require('./util'), | ||
makePool = require('./pool'), | ||
makeStore = require('./store').store; | ||
var pg = require('pg'), | ||
path = require('path'), | ||
assert = require('assert'), | ||
_ = require('lodash'), | ||
_str = require('underscore.string'), | ||
Promise = require('bluebird'), | ||
fs = Promise.promisifyAll(require('fs')), | ||
handlebars = require('handlebars'), | ||
crypto = require('crypto'), | ||
util = require('./util'), | ||
makePool = require('./pool'), | ||
makeStore = require('./store').store, | ||
copyFrom = require('pg-copy-streams').from, | ||
Readable = require('stream').Readable, | ||
csvStringify = require('csv-stringify'); | ||
@@ -123,3 +127,3 @@ | ||
var pool = options.pool || makePool(_.pick(options, 'poolSize', 'url')); | ||
var pool = options.pool || makePool(options); | ||
var logger = (options.logger === 'console') | ||
@@ -186,2 +190,32 @@ ? { | ||
Connection.prototype.writeRows = function(tableName, rows) { | ||
var self = this; | ||
return new Promise(function(resolve, reject) { | ||
if (!rows.length) { | ||
resolve(); | ||
return; | ||
} | ||
var columns = '(' + _.map(_.keys(rows[0]), _.snakeCase).join(', ') + ')'; | ||
var dbOutStream = self.pgConnection.driverQuery( | ||
copyFrom('COPY ' + tableName + ' ' + columns + ' FROM STDIN CSV') | ||
); | ||
var csvInStream = Readable(); | ||
csvStringify(rows, {}, function(err, csvData) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
csvInStream.on('error', reject); | ||
dbOutStream.on('error', reject); | ||
dbOutStream.on('end', resolve); | ||
csvInStream._read = function noop() {}; | ||
csvInStream.push(csvData); | ||
csvInStream.push(null); | ||
csvInStream.pipe(dbOutStream); | ||
}); | ||
}); | ||
} | ||
Connection.prototype.queryRaw = function(text, vals) { | ||
@@ -334,3 +368,2 @@ var self = this; | ||
// Execute a previously prepared statement | ||
@@ -417,2 +450,8 @@ client.exec = function(/*statement, params*/) { | ||
client.writeRows = function(rows) { | ||
return useConnection( function(conn) { | ||
return conn.writeRows(rows); | ||
}); | ||
} | ||
// Returns a promise for work completed against a connection. | ||
@@ -433,3 +472,3 @@ // | ||
assert( | ||
_.isFunction(working.then), | ||
_.isFunction(working && working.then), | ||
'Connection function must return a promise'); | ||
@@ -441,8 +480,3 @@ return working; | ||
client.close = function() { | ||
_.each(pg.pools.all, function(pool, key) { | ||
pool.drain(function() { | ||
pool.destroyAllNow(); | ||
}); | ||
delete pg.pools.all[key]; | ||
}); | ||
return pool.close(); | ||
}; | ||
@@ -449,0 +483,0 @@ |
{ | ||
"name": "dbeasy", | ||
"version": "0.12.4", | ||
"version": "0.13.0", | ||
"description": "Promise-based wrapper for postgresql with helpers for transactions.", | ||
@@ -32,3 +32,5 @@ "main": "index.js", | ||
"moment-timezone": "^0.5.4", | ||
"pg": "4.5.1", | ||
"pg": "^6.1.0", | ||
"pg-connection-string": "^0.1.3", | ||
"pg-copy-streams": "^1.2.0", | ||
"pg-native": "1.10.0", | ||
@@ -35,0 +37,0 @@ "sprintf-js": "^1.0.3", |
63
pool.js
'use strict'; | ||
var pg = require('pg'), | ||
Promise = require('bluebird'); | ||
Pool = require('pg').Pool, | ||
Promise = require('bluebird'), | ||
_ = require('lodash'), | ||
parse = require('pg-connection-string').parse; | ||
var pgConnect = Promise.promisify(pg.connect, pg); | ||
module.exports = function(options) { | ||
@@ -11,38 +12,32 @@ var pool = {}; | ||
var conString = buildConString(options); | ||
var config = buildConfig(options); | ||
// The postgres driver depends on this globally accessible module | ||
// variable to set the pool size. This is problematic if you have | ||
// multiple pools at different sizes. We must be sure to call | ||
// initPool() right away so that the value is not changed before the | ||
// pool is setup. | ||
var oldPoolSize = pg.defaults.poolSize; | ||
var poolSize = pg.defaults.poolSize = options.poolSize || pg.defaults.poolSize; | ||
var onPool = initPool(); | ||
pg.defaults.poolSize = oldPoolSize; | ||
config.max = options.poolSize || 1; | ||
config.ssl = options.ssl; | ||
var pgPool = new Pool(config); | ||
pgPool = Promise.promisifyAll(pgPool); | ||
function buildConfig(pgconf) { | ||
if (pgconf.url) { | ||
return parse(pgconf.url); | ||
} | ||
function buildConString(pgconf) { | ||
if (pgconf.url) return pgconf.url; | ||
var host = pgconf.host || "localhost"; | ||
var port = pgconf.port || 5432; | ||
var database = pgconf.database || "postgres"; | ||
var userString = pgconf.user | ||
? pgconf.user + ( | ||
pgconf.password ? ":" + pgconf.password : "" | ||
) + "@" | ||
: ""; | ||
return "postgres://"+userString+host+":"+port+"/"+database+"?ssl=on"; | ||
return _.defaults({}, { | ||
host: "localhost", | ||
port: 5432, | ||
database: "postgres" | ||
}); | ||
} | ||
function initPool() { | ||
return _useConnection(function() {}); | ||
// return _useConnection(function() {}); | ||
} | ||
function _useConnection(fn) { | ||
return pgConnect(conString) | ||
return pgPool.connectAsync() | ||
.spread(function(connection, release) { | ||
connection = { | ||
query: Promise.promisify(connection.query, connection) | ||
query: Promise.promisify(connection.query, connection), | ||
driverQuery: _.bind(connection.query, connection) | ||
}; | ||
@@ -56,6 +51,3 @@ return Promise.try(fn, connection) | ||
function useConnection(fn) { | ||
return onPool | ||
.then(function() { | ||
return _useConnection(fn); | ||
}); | ||
return _useConnection(fn); | ||
} | ||
@@ -76,3 +68,8 @@ | ||
} | ||
pool.close = close; | ||
function close() { | ||
return pgPool.end(); | ||
} | ||
return pool; | ||
}; | ||
}; |
@@ -63,4 +63,4 @@ "use strict"; | ||
return db.useConnection( function() { | ||
gotSecondConnection = false; | ||
return; | ||
gotSecondConnection = true; | ||
return Promise.resolve(); | ||
}); | ||
@@ -70,14 +70,16 @@ }); | ||
grab2Conns().then( function() { | ||
done(new Error("Did not deadlock as expected")); | ||
}, done); | ||
var deadlocked = true; | ||
grab2Conns() | ||
.finally(function() { | ||
done(new Error('Exepected deadlock')); | ||
}); | ||
// give it a little time before reporting success | ||
setTimeout(function() { | ||
assert.equal(gotFirstConnection, true); | ||
assert.equal(gotSecondConnection, false); | ||
done(); | ||
}, 1000); | ||
// give it a little time before reporting success | ||
setTimeout(function() { | ||
assert.equal(gotFirstConnection, true); | ||
assert.equal(gotSecondConnection, false); | ||
done(); | ||
}, 1000); | ||
}); | ||
}); | ||
@@ -84,0 +86,0 @@ test("don't deadlock on parallel connection requests", function(done) { |
70690
1979
11
+ Addedpg-connection-string@^0.1.3
+ Addedpg-copy-streams@^1.2.0
+ Addedgeneric-pool@2.4.3(transitive)
+ Addedjs-string-escape@1.0.1(transitive)
+ Addedobject-assign@4.1.0(transitive)
+ Addedpacket-reader@0.3.1(transitive)
+ Addedpg@6.4.2(transitive)
+ Addedpg-copy-streams@1.2.0(transitive)
+ Addedpg-pool@1.8.0(transitive)
+ Addedpgpass@1.0.5(transitive)
+ Addedsemver@4.3.2(transitive)
+ Addedsplit2@4.2.0(transitive)
- Removedgeneric-pool@2.1.1(transitive)
- Removedpacket-reader@0.2.0(transitive)
- Removedpg@4.5.1(transitive)
- Removedpgpass@0.0.3(transitive)
- Removedsemver@4.3.6(transitive)
- Removedsplit@0.3.3(transitive)
- Removedthrough@2.3.8(transitive)
Updatedpg@^6.1.0