Created
January 28, 2011 16:24
-
-
Save excellentdrums/800490 to your computer and use it in GitHub Desktop.
Using node-postgres to reuse a client until disconnecting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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