Last active
July 13, 2016 22:33
-
-
Save mkulke/26adfe886610b8eb207f6c371654b5c9 to your computer and use it in GitHub Desktop.
node pg tuple query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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