Skip to content

Instantly share code, notes, and snippets.

@turtlesoupy
Created June 10, 2012 23:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlesoupy/2907723 to your computer and use it in GitHub Desktop.
Save turtlesoupy/2907723 to your computer and use it in GitHub Desktop.
node-postgres pooled function decorator
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