PostgreSQL dialect for SQLBricks
This is a lightweight, schemaless library helping you to generate statements for PostgreSQL.
It is based on sql-bricks and adds PostgreSQL specific things into it.
You might also want to take a look at pg-bricks, which adds query execution, connections and transaction handling on top of this library.
Usage
var sql = require('sql-bricks-postgres');
var sql = PostgresBricks;
sql.select().from('user').where({name: 'Fred'}).toParams();
sql.select().from('user').where({name: 'Fred'}).toString();
You can read about basic flavor of how this thing works in sql-bricks documentation. Here go PostgreSQL specifics.
LIMIT and OFFSET
sql.select().from('user').limit(10).offset(20).toString()
RETURNING
sql.update('user', {name: 'John'}).where({id: 1}).returning('*')
sql.delete('job').where({finished: true}).returning('id')
UPDATE ... FROM
sql.update('setting', {value: sql('V.value')})
.from('val as V').where({name: sql('V.name')}).toString()
DELETE ... USING
sql.delete('user').using('address')
.where('user.addr_fk', sql('address.pk'))
ON CONFLICT ... DO NOTHING / DO UPDATE ...
The most popular use case is probably UPSERT:
sql.insert('user', {name: 'Alex', age: 34})
.onConflict('name').doUpdate('age')
sql.insert('user', {name: 'Alex', age: 34})
.onConflict('name').doUpdate()
sql.insert('user', {name: 'Alex', age: 34})
.onConflict('name').doUpdate()
.set(sql('name = coalesce(EXCLUDED.name, $1), age = $2 + 10', t1, t2))
Other clauses such as DO NOTHING
, ON CONSTRAINT
and WHERE
are
also supported:
sql.insert('user', ...).onConflict('name').where({is_active: true})
.doNothing()
sql.insert('user', ...).onConflict().onConstraint('name_idx')
.doUpdate().where(sql('is_active'))
FROM VALUES
VALUES
statement is a handy way to provide data with a query. It is most known in a context of INSERT
, but could be used for other things like altering selects and doing mass updates:
var data = [{name: 'a', value: 1}, {name: 'b', value: 2}];
sql.select().from(sql.values(data)).toString();
sql.update('setting s', {value: sql('v.value')})
.from(sql.values({name: 'a', value: 1}).as('v').columns())
.where('s.name', sql('v.name')}).toString()
Sometimes you need types on values columns for query to work. You can use .types()
method to provide them:
var data = {i: 1, f: 1.5, b: true, s: 'hi'};
insert('domain', _.keys(data))
.select().from(sql.values(data).as('v').columns().types())
.where(sql.not(sql.exists(
select('1').from('domain d')
.where({'d.job_id': sql('v.job_id'), 'd.domain': sql('v.domain')}))))
When type can't detected by value, e.g. you have null
, no cast will be added.
However, you can specify types explicitly:
sql.values({field: null}).types({field: 'int'}).toString()
ILIKE
ILIKE
is a case insensitive LIKE
statement
sql.select("text").from("example").where(sql.ilike("text", "%EASY%"))
PostgreSQL Type Compatability
Supports node-postgres toPostgres()
conventions to format Javascript appropriately for PostgreSQL.
See postgres-interval for an example of this pattern in action. (index.js#L14-L22)
Even Harder Things
PostgreSQL has lots of functions and operators so it's inpractical to support everything,
instead simple fallback is offered:
select().from('time_limit')
.where(sql('tsrange(start, end) @> tsrange($1, $2)', t1, t2))
Note $<number>
placeholders.