pga-sql
v0.0.6
Published
A template tagging function that returns an object compatible with the pg module's querying methods.
Downloads
25
Readme
pga-sql
A template tagging function that returns an object compatible with the pg module's querying methods.
Installation
npm install --save pga-sql
Usage
pga-sql
provides a template tagging function that returns an object compatible with node-postgres
's querying methods and, by extension, pga
's query
, parallel
, and transact
methods. Template literal variable interpolation makes writing lengthy parameterized queries much cleaner.
Regular variables may be interpolated as string literals (without applying parameterized filtering from the node-postgres
module) by prefixing them with an additional $
character. This is unsafe, and should be used carefully.
Query
const pga = require('pga');
const sql = require('pga-sql');
var db = pga({
user: 'postgres',
password: '',
database: 'postgres',
host: 'localhost',
port: 5432,
max: 10
});
var id = 1;
var table = 'test';
var query = sql`SELECT * FROM $${table} WHERE id = ${id};`;
db.query(query, function(error, result) {
if (error) {
return console.error(error);
}
console.log(result);
});