1.0.1 • Published 6 years ago

where-pg v1.0.1

Weekly downloads
4
License
Apache-2.0
Repository
github
Last release
6 years ago

where

Building parameterized SQL where part from javascript objects. See ./test/index.test.js how to use with node-postgres module.

  const { Pool } = require('pg');
  const where = require('where');

  const connStr = 'postgresql://user:pwd@localhost:5432/db_name';
  const pool = new Pool({ connectionString: connStr});

  const  o = {id: '>:3', name: 'LIKE:Jo%'};
  const w = where(o);
  const res = await pool.query('select * from demo_table' + w.queryText, q.values);

: separates operator and value:

simple = {id: '>:5'};
w = where(simple)
// w = {queryText: 'id > $1', values:[5]}

AND is always used as an operator between conditions:

simple = {a: 1, b: 2};
w = where(simple)
// w = {queryText: "a = $1 AND b = $2", values:[1, 2]}

ANY is used for arrays:

simple = { id: [1, 2, 3] }
w = where(simple)
// {queryText: 'id ANY = ($1)', values: [[1, 2, 3]] }

Use suffix between _0 and _9 to set multiple conditions on the same field:

simple = { id: '>:5', id_1: '<:10' }
w = where(simple)
// w = {queryText: id > $1 AND id < $2, values:[5, 10]}

Use keys _order_by for ORDER BY, limit for LIMIT and offset for OFFSET.

simple = {a: 1, b: 2, _order_by: 'id', _limit: 10, offset: 101};