Created
June 10, 2012 23:34
-
-
Save turtlesoupy/2907723 to your computer and use it in GitHub Desktop.
node-postgres pooled function decorator
This file contains hidden or 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
pg = require 'pg' | |
module.exports = pooler = | |
#Get a connection from the pool | |
acquire: (callback) -> pg.connect "tcp://postgres:postgres@localhost/dummy_db", callback | |
#Decorate a function to use the de-pooled connection as a first argument | |
pooled: (fn) -> -> | |
callerCallback = arguments[arguments.length - 1] | |
callerHasCallback = typeof callerCallback == 'function' | |
callerArgs = Array::slice.call(arguments, 0, if callerHasCallback then -1 else undefined) | |
pooler.acquire (err, pgClient) -> | |
return (callerCallback err if err?) if err? | |
pgClient.pauseDrain() | |
callerArgs.push -> | |
pgClient.resumeDrain() | |
callerCallback.apply(null, arguments) if callerHasCallback | |
fn pgClient, callerArgs... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment