Skip to content

Instantly share code, notes, and snippets.

@brianc
brianc / gist:f906bacc17409203aee0
Last active December 22, 2023 00:47
Some thoughts on node-postgres in web applications

Some thoughts on using node-postgres in a web application

This is the approach I've been using for the past year or so. I'm sure I'll change and it will change as I grow & am exposed to more ideas, but it's worked alright for me so far.

Pooling:

I would definitely use a single pool of clients throughout the application. node-postgres ships with a pool implementation that has always met my needs, but it's also fine to just use the require('pg').Client prototype and implement your own pool if you know what you're doing & have some custom requirements on the pool.

{
"color_scheme": "Packages/Theme - Spacegray/base16-ocean.dark.tmTheme",
"hot_exit": false,
"remember_open_files": false,
"font_size": 16,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"save_on_focus_lost": true,
"ignored_packages":
[
var http = require('http')
var pg = require('pg')
//creating a single, global client
//will potentially cause two requests two issue queries
//interleaved within one another...
//do not do this!
var client = new Client()
client.connect()
var assert = require('assert')
var set = function(obj, key, val) {
var split = key.split('.')
var prop = null
//single length key needs no depth traversal
if(split.length == 1) {
obj[key] = val;
return obj;
}
split.forEach(function(part, i) {
@brianc
brianc / gist:d4b144dae6dcd53829cc
Created June 18, 2014 14:22
delete your hipchat history
//steps
//1) go to hipchat web app and view a page of chat history
//2) paste the code below into your webinspector console & hit enter
//3) eat a banana
$('.delete form').submit(function(event) {
event.preventDefault();
var form = $(this);
$.ajax({type: form.attr('method'), url: form.attr('action'), data: form.serialize()});
}).submit();
@brianc
brianc / gist:d825fd26c672781e4534
Last active June 29, 2016 00:44
How we load our elasticsearch indices
//stream a postgres table directly into an elastic search index
//you need to have your index created and your type mappings defined in elastic search
//and then you just do this:
//bash: npm i pg-readable pg.js clumpy stretchypants
var es = require('stretchypants').index('your_index').type('your_type_mapping')
var clumpy = require('clumpy')
var query = require('pg-readable')
var through2 = require('through2')
var map = function(mapFn) {
return function(chunk, enc, cb) {
cb(null, this.push(mapFn(chunk)))
}
}
var explode = function(maybe) {
if(maybe) {
throw new Error('errmahgerrd! someone set us up the bomb!')
}
}
var defuse = function() {
try {
explode(true)
} catch(e) {
var omf = require('omf')
var app = require('../app')
var querystring = require('querystring')
var assert = require('assert')
omf(app, function(app) {
var search = function(params, tests) {
app.get('/sourcing?' + querystring.stringify(params), function(res) {
it('has json body', function() {
assert(this.response.body)
var omf = require('omf')
var app = require('../app')
var assert = require('assert')
omf(app, function(app) {
describe('some request', function() {
var params = {
has_retail: 'false',