Socket
Socket
Sign inDemoInstall

pg

Package Overview
Dependencies
Maintainers
1
Versions
224
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pg - npm Package Compare versions

Comparing version 0.12.3 to 0.13.0

lib/pool.js

76

lib/index.js

@@ -5,15 +5,14 @@ var EventEmitter = require('events').EventEmitter;

var defaults = require(__dirname + '/defaults');
var pool = require(__dirname + '/pool');
var types = require(__dirname + '/types');
var Connection = require(__dirname + '/connection');
//external genericPool module
var genericPool = require('generic-pool');
//cache of existing client pools
var pools = {};
var PG = function(clientConstructor) {
EventEmitter.call(this);
this.Client = clientConstructor;
this.Connection = require(__dirname + '/connection');
this.Query = clientConstructor.Query;
this.defaults = defaults;
this.Client = pool.Client = clientConstructor;
this.Query = this.Client.Query;
this.pools = pool;
this.types = types;
this.Connection = Connection;
};

@@ -24,4 +23,5 @@

PG.prototype.end = function() {
Object.keys(pools).forEach(function(name) {
var pool = pools[name];
var self = this;
Object.keys(self.pools.all).forEach(function(key) {
var pool = self.pools.all[key];
pool.drain(function() {

@@ -34,47 +34,12 @@ pool.destroyAllNow();

PG.prototype.connect = function(config, callback) {
var self = this;
var c = config;
var cb = callback;
//allow for no config to be passed
if(typeof c === 'function') {
cb = c;
c = defaults;
if(typeof config == "function") {
callback = config;
config = null;
}
//get unique pool name even if object was used as config
var poolName = typeof(c) === 'string' ? c : c.user+c.host+c.port+c.database;
var pool = pools[poolName];
if(pool) { return pool.acquire(cb); }
pool = pools[poolName] = genericPool.Pool({
name: poolName,
create: function(callback) {
var client = new self.Client(c);
client.connect(function(err) {
if(err) { return callback(err); }
//handle connected client background errors by emitting event
//via the pg object and then removing errored client from the pool
client.on('error', function(e) {
self.emit('error', e, client);
pool.destroy(client);
});
callback(null, client);
});
client.on('drain', function() {
pool.release(client);
});
},
destroy: function(client) {
client.end();
},
max: defaults.poolSize,
idleTimeoutMillis: defaults.poolIdleTimeout,
reapIntervalMillis: defaults.reapIntervalMillis,
log: defaults.poolLog
});
return pool.acquire(cb);
var pool = this.pools.getOrCreate(config);
pool.connect(callback);
if(!pool.listeners('error').length) {
//propagate errors up to pg object
pool.on('error', this.emit.bind(this, 'error'));
}
};

@@ -102,2 +67,1 @@

module.exports.types = require('./types');
{ "name": "pg",
"version": "0.12.3",
"version": "0.13.0",
"description": "PostgreSQL client - pure javascript & libpq with the same API",

@@ -4,0 +4,0 @@ "keywords" : ["postgres", "pg", "libpq", "postgre", "database", "rdbms"],

@@ -13,3 +13,9 @@ var helper = require(__dirname + '/test-helper');

assert.isNull(err);
client.end();
client.query('SELECT NOW()');
client.once('drain', function() {
setTimeout(function() {
helper.pg.end();
}, 10);
});
}));

@@ -12,3 +12,3 @@ var helper = require(__dirname + "/../test-helper");

test("connection #" + i + " executes", function() {
helper.pg.connect(helper.config, function(err, client) {
helper.pg.connect(helper.config, function(err, client, done) {
assert.isNull(err);

@@ -23,3 +23,4 @@ client.query("select * from person", function(err, result) {

query.on('end',function() {
sink.add()
sink.add();
done();
})

@@ -26,0 +27,0 @@ })

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc