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

v3.0.1

Published

Transparent, Schemaless SQL Generation

Downloads

134,142

Readme

SQL Bricks.js

Build Status

SQL Bricks.js is a transparent, schemaless library for building and composing SQL statements.

  • The core library supports all SQL-92 clauses for SELECT, INSERT, UPDATE and DELETE (with the exception of asc/desc/collate options for orderBy(), see #73)
    • Postgres extensions are at https://github.com/Suor/sql-bricks-postgres
    • MySQL extensions are at https://github.com/tamarzil/mysql-bricks
    • SQLite extensions are at https://github.com/CSNW/sql-bricks-sqlite
  • Over 200 tests
  • Easy-to-use, comprehensive docs
  • Single source file (~1,100 lines)
  • No production dependencies and only 1 dev dependency (Mocha.js)

Comparison with other SQL-generation JS libraries:

library | lines | files | schema | other notes
--------------- | ----- | ----- | ---------- | -------------- Knex | 20k | ~50 | schema | transactions, migrations, promises, connection pooling Squel | 1.7k | 1 | schemaless | node-sql | 2.6k | ~60 | schema | mongo-sql | 1.7k | ~50 | schemaless | sql-bricks | 1.1k | 1 | schemaless |

Related Libraries

  • mysql-bricks adds mysql-dialect extensions:
    • INSERT ... ON DUPLICATE KEY UPDATE ...
    • INSERT IGNORE ...
    • LIMIT (SELECT / UPDATE / DELETE)
    • OFFSET
    • ORDER BY (UPDATE / DELETE)
  • sql-bricks-sqlite adds sqlite-dialect extensions:
    • LIMIT and OFFSET
    • OR REPLACE, OR ABORT, OR ROLLBACK, OR FAIL
  • sql-bricks-postgres adds postgres-dialect extensions:
    • LIMIT and OFFSET
    • RETURNING
    • UPDATE ... FROM
    • DELETE ... USING
    • FROM VALUES
  • pg-bricks adds:
    • connections
    • transactions
    • query execution
    • data accessors
  • A Layer Above Database Connectors adds:
    • A common way to access to relational databases (SQLite & Postgres as of Oct 2019)
    • A pool of connections in order to allow transactions in an asynchronous context;
    • A way to augment your connector with your SQL query builder (has a sql-bricks plugin)

Use

In the browser:

var select = SqlBricks.select;

In node:

var select = require('sql-bricks').select;

A simple select via .toString() and .toParams():

select().from('person').where({last_name: 'Rubble'}).toString();
// "SELECT * FROM person WHERE last_name = 'Rubble'"

select().from('person').where({last_name: 'Rubble'}).toParams();
// {"text": "SELECT * FROM person WHERE last_name = $1", "values": ["Rubble"]}

While toString() is slightly easier, toParams() is recommended because:

  • It provides robust protection against SQL injection attacks (toString() just does basic escaping)
  • It provides better support for complex data types (objects, arrays, etc, are passed directly to your database driver instead of being "stringified")
  • It provides more helpful error messages (see https://github.com/Suor/sql-bricks-postgres/issues/10)

Examples

The SQLBricks API is comprehensive, supporting all of SQL-92 for select/insert/update/delete. It is also quite flexible; in most places arguments can be passed in a variety of ways (arrays, objects, separate arguments, etc). That said, here are some of the most common operations:

// convenience variables (for node; for the browser: "var sql = SqlBricks;")
var sql = require('sql-bricks');
var select = sql.select, insert = sql.insert, update = sql.update;
var or = sql.or, like = sql.like, lt = sql.lt;

// WHERE: (.toString() is optional; JS will call it automatically in most cases)
select().from('person').where({last_name: 'Rubble'}).toString();
// SELECT * FROM person WHERE last_name = 'Rubble'

// JOINs:
select().from('person').join('address').on({'person.addr_id': 'address.id'});
// SELECT * FROM person INNER JOIN address ON person.addr_id = address.id

// Nested WHERE criteria:
select('*').from('person').where(or(like('last_name', 'Flint%'), {'first_name': 'Fred'}));
// SELECT * FROM person WHERE last_name LIKE 'Flint%' OR first_name = 'Fred'

// GROUP BY / HAVING
select('city', 'max(temp_lo)').from('weather')
  .groupBy('city').having(lt('max(temp_lo)', 40))
// SELECT city, max(temp_lo) FROM weather
// GROUP BY city HAVING max(temp_lo) < 40

// INSERT
insert('person', {'first_name': 'Fred', 'last_name': 'Flintstone'});
// INSERT INTO person (first_name, last_name) VALUES ('Fred', 'Flintstone')

// UPDATE
update('person', {'first_name': 'Fred', 'last_name': 'Flintstone'});
// UPDATE person SET first_name = 'Fred', last_name = 'Flintstone'


// Parameterized SQL
update('person', {'first_name': 'Fred'}).where({'last_name': 'Flintstone'}).toParams();
// {"text": "UPDATE person SET first_name = $1 WHERE last_name = $2", "values": ["Fred", "Flintstone"]}

// SQLite-style params
update('person', {'first_name': 'Fred'}).where({'last_name': 'Flintstone'}).toParams({placeholder: '?%d'});
// {"text": "UPDATE person SET first_name = ?1 WHERE last_name = ?2", "values": ["Fred", "Flintstone"]}

// MySQL-style params
update('person', {'first_name': 'Fred'}).where({'last_name': 'Flintstone'}).toParams({placeholder: '?'});
// {"text": "UPDATE person SET first_name = ? WHERE last_name = ?", "values": ["Fred", "Flintstone"]}

Full documentation: https://csnw.github.io/sql-bricks

License: MIT