SQL Bricks.js

SQL Bricks.js is a transparent, schemaless library for building and composing SQL statements.
Comparison with popular SQL-generation libraries:
Knex | 3500 | 30 | schema | javascript | transactions, migrations, promises, connection pooling |
Squel | 1000 | 3 | schemaless | coffeescript | |
node-sql | 2600 | 59 | schema | javascript | |
mongo-sql | 1700 | 49 | schemaless | javascript | |
gesundheit | 1600 | 21 | schemaless | coffeescript | uses Any-DB to wrap the DB driver |
sql-bricks | 1100 | 1 | schemaless | javascript | |
Related Libraries
- 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
- sql-bricks-sqlite adds sqlite-dialect extensions:
LIMIT
and OFFSET
OR REPLACE
, OR ABORT
, OR ROLLBACK
, OR FAIL
Use
SQLBricks' only dependency is Underscore.js.
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'}).toParams();
While toString()
is slightly easier, toParams()
is recommended because:
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:
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;
select().from('person').where({last_name: 'Rubble'}).toString();
select().from('person').join('address').on({'person.addr_id': 'address.id'});
select('*').from('person').where(or(like('last_name', 'Flint%'), {'first_name': 'Fred'}));
select('city', 'max(temp_lo)').from('weather')
.groupBy('city').having(lt('max(temp_lo)', 40))
insert('person', {'first_name': 'Fred', 'last_name': 'Flintstone'});
update('person', {'first_name': 'Fred', 'last_name': 'Flintstone'});
update('person', {'first_name': 'Fred'}).where({'last_name': 'Flintstone'}).toParams();
update('person', {'first_name': 'Fred'}).where({'last_name': 'Flintstone'}).toParams({placeholder: '?%d'});
update('person', {'first_name': 'Fred'}).where({'last_name': 'Flintstone'}).toParams({placeholder: '?'});
Documentation: http://csnw.github.io/sql-bricks
License: MIT