Skip to content

mfine15/node-postgres-named

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Synopsis

Want to use postgres with node? node-postgres has you covered.

Want named parameters? Well, postgres itself doesn't support them, and brianc has sagely opted to keep his library small and close to the postgres specification.

But code full of random numeric tokens (e.g. $1) isn't very readable. This module lets you monkeypatch node-postgres or node-postgres-pure to support named parameters.

Go from this...

client.query('SELECT name FROM person WHERE name = $1 AND tenure <= $2 AND age <= $3',
             ['Ursus', 2.5, 24],
             function (results) { console.log(results); });
             

to this...

client.query('SELECT name FROM person WHERE name = $name AND tenure <= $tenure AND age <= $age',
             {'name': 'Ursus', 'tenure': 2.5, 'age': 24},
             function (results) { console.log(results); });
     

Tokens are identified with \$[a-zA-Z]([a-zA-Z0-9_\-]*)\b. In other words: they must begin with a letter, and can contain only alphanumerics, underscores, and dashes.

Usage

Create a client as usual, then call the patch function on it. It will be patched in-place.

var pg = require('pg'); 
var named = require('node-postgres-named');
var client = new pg.Client(conString);
named.patch(client);

Now both of the above call styles (with a list of values, or a dictionary of named parameters) will work.

Shout Out

Big ups to Mike "ApeChimp" Atkins for the suggested implemention.

About

Named parameters for node-postgres.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%