Skip to content

Add Connection Pooling #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ function PostgresDB(options) {

this.closed = false;

this.pg_config = options;
this.pool = new pg.Pool(options);
};
module.exports = PostgresDB;

PostgresDB.prototype = Object.create(DB.prototype);

PostgresDB.prototype.close = function(callback) {
this.closed = true;
this.pool.end();

if (callback) callback();
};

Expand All @@ -39,7 +41,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
* }
* snapshot: PostgresSnapshot
*/
pg.connect(this.pg_config, function(err, client, done) {
this.pool.connect(function(err, client, done) {
if (err) {
done(client);
callback(err);
Expand Down Expand Up @@ -123,7 +125,7 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
// snapshot). A snapshot with a version of zero is returned if the docuemnt
// has never been created in the database.
PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, callback) {
pg.connect(this.pg_config, function(err, client, done) {
this.pool.connect(function(err, client, done) {
if (err) {
done(client);
callback(err);
Expand Down Expand Up @@ -173,7 +175,7 @@ PostgresDB.prototype.getSnapshot = function(collection, id, fields, options, cal
//
// Callback should be called as callback(error, [list of ops]);
PostgresDB.prototype.getOps = function(collection, id, from, to, options, callback) {
pg.connect(this.pg_config, function(err, client, done) {
this.pool.connect(function(err, client, done) {
if (err) {
done(client);
callback(err);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"keywords": ["sharedb", "sharejs", "share", "postgres"],
"repository": "share/sharedb-postgres",
"dependencies": {
"pg": "^4.5.1",
"pg": "^7.4.1",
"sharedb": "^1.0.0-beta.7"
}
}
}