npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sql-bricks-postgres

v0.6.0

Published

Transparent, Schemaless SQL Generation for the PostgreSQL

Downloads

49,071

Readme

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

// in node:
var sql = require('sql-bricks-postgres');
// in the browser:
var sql = PostgresBricks;

sql.select().from('user').where({name: 'Fred'}).toParams();
// {text: 'SELECT * FROM "user" WHERE name = $1', values: ['Fred']}

sql.select().from('user').where({name: 'Fred'}).toString();
// SELECT * FROM "user" WHERE name = 'Fred'

// NOTE: never use .toString() to execute a query,
//       leave values for db library to quote

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()
// SELECT * FROM "user" LIMIT 10 OFFSET 20

RETURNING

sql.update('user', {name: 'John'}).where({id: 1}).returning('*')
// UPDATE "user" SET name = 'John' WHERE id = 1 RETURNING *

sql.delete('job').where({finished: true}).returning('id')
// DELETE FROM 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()
// UPDATE setting SET value = V.value
//   FROM val as V WHERE name = V.name

DELETE ... USING

sql.delete('user').using('address')
   .where('user.addr_fk', sql('address.pk'))
// DELETE FROM user USING address WHERE user.addr_fk = 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')
// INSERT INTO "user" (name) VALUES ('Alex', 34)
//     ON CONFLICT (name) DO UPDATE SET age = EXCLUDED.age

// sql-bricks-postgres will update all fields if none are specified
sql.insert('user', {name: 'Alex', age: 34})
   .onConflict('name').doUpdate()
// INSERT INTO "user" (name) VALUES ('Alex', 34)
//   ON CONFLICT (name)
//   DO UPDATE SET name = EXCLUDED.name, age = EXCLUDED.age

// manipulate the data in the `DO UPDATE`:
sql.insert('user', {name: 'Alex', age: 34})
    .onConflict('name').doUpdate()
    .set(sql('name = coalesce(EXCLUDED.name, $1), age = $2 + 10', t1, t2))
// INSERT INTO "user" (name) VALUES ('Alex', 34)
//   ON CONFLICT (name)
//   DO UPDATE SET name = coalesce(EXCLUDED.name, $3), age = $4 + 10

Other clauses such as DO NOTHING, ON CONSTRAINT and WHERE are also supported:

sql.insert('user', ...).onConflict('name').where({is_active: true})
   .doNothing()
// INSERT INTO "user" ... VALUES ...
//     ON CONFLICT (name) WHERE is_active = true DO NOTHING

sql.insert('user', ...).onConflict().onConstraint('name_idx')
    .doUpdate().where(sql('is_active'))
// INSERT INTO "user" ... VALUES ...
//     ON CONFLICT ON CONSTRAINT name_idx
//     DO UPDATE SET ... WHERE 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();
// SELECT * FROM (VALUES ('a', 1), ('b', 2))

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()
// UPDATE setting s SET value = v.value
//   FROM (VALUES ('a', 1)) v (name, value) WHERE s.name = v.name

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')}))))
// INSERT INTO domain (i, f, b, s)
// SELECT * FROM (VALUES ($5::int, $6::float, $7::bool, $8)) v (i, f, b, s)
// WHERE NOT EXISTS
//    (SELECT 1 FROM domain d WHERE d.job_id = v.job_id AND d.domain = 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()
// VALUES (null::int)

ILIKE

ILIKE is a case insensitive LIKE statement

sql.select("text").from("example").where(sql.ilike("text", "%EASY%"))
// SELECT text FROM example WHERE text ILIKE '%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))
// SELECT * FROM time_limit
// WHERE tsrange(start, end) @> tsrange($1, $2)

Note $<number> placeholders.