pga-sql

0.0.6 • Public • Published

pga-sql

npm Node.js Build Status Coverage Dependencies Status devDependencies Status MIT licensed

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);
});

Readme

Keywords

Package Sidebar

Install

npm i pga-sql

Weekly Downloads

2

Version

0.0.6

License

MIT

Unpacked Size

5.35 kB

Total Files

4

Last publish

Collaborators

  • connorwiseman