Skip to content

Instantly share code, notes, and snippets.

@excellentdrums
Created January 28, 2011 16:24
Show Gist options
  • Save excellentdrums/800490 to your computer and use it in GitHub Desktop.
Save excellentdrums/800490 to your computer and use it in GitHub Desktop.
Using node-postgres to reuse a client until disconnecting
function Base(client) {
this.client = client;
}
Base.prototype.truncate = function(callback) {
self.client.query("TRUNCATE users;", function(err, result) {
callback(err, !result.rowCount);
});
}
Base.prototype.disconnect = function() {
if (this.client.queryQueue.length === 0) {
this.client.end();
} else {
this.client.on('drain', this.client.end.bind(this.client));
}
}
var client = new pg.Client({
user: 'me',
password: 'whatever',
database: 'test',
host: 'localhost',
port: 5432
});
client.connect();
var ORM = {
Base: new Base(client)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment