Skip to content

Instantly share code, notes, and snippets.

@ericf
Last active December 17, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericf/5527261 to your computer and use it in GitHub Desktop.
Save ericf/5527261 to your computer and use it in GitHub Desktop.
This is how I've been using node-postgres to create parameterized queries to PostgreSQL which may contain one or more updates to a row. Stay safe, but flexible :) https://github.com/brianc/node-postgres
var pg = require('pg'),
config = require('../config'),
GUEST_BY_ID = 'SELECT * FROM guests WHERE id=$1 LIMIT 1',
UPDATE_GUEST = 'UPDATE guests SET $UPDATES WHERE id=$1',
UPDATE_SCHEMA = {
title : true,
name : true,
email : true,
is_attending: true
};
exports.loadGuest = loadGuest;
exports.updateGuest = updateGuest;
function runQuery(query, values, callback) {
pg.connect(config.database, function (err, db, done) {
if (err) { return callback(err); }
db.query(query, values, function (err, results) {
done();
callback(err, results && results.rows[0]);
});
});
}
function loadGuest(id, callback) {
runQuery(GUEST_BY_ID, [id], callback);
}
function updateGuest(id, changes, callback) {
var values = [id],
updates = [],
query;
Object.keys(UPDATE_SCHEMA).forEach(function (col) {
if (col in changes) {
updates.push(col + '=$' + values.push(changes[col]));
}
});
query = UPDATE_GUEST.replace('$UPDATES', updates.join(', '));
runQuery(query, values, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment