Skip to content

Instantly share code, notes, and snippets.

@mkulke
Last active July 13, 2016 22:33
Show Gist options
  • Select an option

  • Save mkulke/26adfe886610b8eb207f6c371654b5c9 to your computer and use it in GitHub Desktop.

Select an option

Save mkulke/26adfe886610b8eb207f6c371654b5c9 to your computer and use it in GitHub Desktop.
node pg tuple query
CREATE TABLE test (
id integer,
col_a text,
col_b text,
PRIMARY KEY(id)
);
INSERT INTO test VALUES (0, 'abc', 'def');
INSERT INTO test VALUES (1, 'abc', 'ghi');
INSERT INTO test VALUES (2, 'jkl', 'ghi');
var pg = require('pg');
// instantiate a new client
// the client will read connection information from
// the same environment varaibles used by postgres cli tools
var client = new pg.Client();
var testo = JSON.stringify([['abc','def'],['abc','ghi']]);
// connect to our database
client.connect(function (err) {
if (err) throw err;
// execute a query on our database
client.query(`
select * from test
where (col_a, col_b) IN (
select value->>0, value->>1
from json_array_elements($1)
)`, [ testo ], function (err, result) {
if (err) throw err;
// just print the result to the console
console.log(result.rows[0]); // outputs: { name: 'brianc' }
// disconnect the client
client.end(function (err) {
if (err) throw err;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment