node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »


squel - SQL query string builder

Build Status CDNJS NPM module NPM downloads Join the chat at Follow on Twitter

A flexible and powerful SQL query string builder for Javascript.

Full documentation (guide and API) at

Note: The latest Squel version only works on Node 0.12 or above. Please use Squel 4.4.1 for Node <0.12. The old 4.x docs are also still available.


  • Works in node.js and in the browser.
  • Supports the standard SQL queries: SELECT, UPDATE, INSERT and DELETE.
  • Supports non-standard commands for popular DB engines such as MySQL.
  • Supports paramterized queries for safe value escaping.
  • Can be customized to build any query or command of your choosing.
  • Uses method chaining for ease of use.
  • Small: ~7 KB minified and gzipped
  • And much more, see the guide..

WARNING: Do not ever pass queries generated on the client side to your web server for execution. Such a configuration would make it trivial for a casual attacker to execute arbitrary queries—as with an SQL-injection vector, but much easier to exploit and practically impossible to protect against.

Note: See for latest changes


Install using npm:

$ npm install squel

Available files

  • squel.js - unminified version of Squel with the standard commands and all available non-standard commands added
  • squel.min.js - minified version of squel.js
  • squel-basic.js - unminified version of Squel with only the standard SQL commands
  • squel-basic.min.js - minified version of squel-basic.js


Before running the examples ensure you have squel installed and enabled at the top of your script:

var squel = require("squel");


// SELECT * FROM table
// SELECT, FROM table `t1` LEFT JOIN table2 `t2` ON ( = WHERE ( <> 'Mark') AND ( <> 'John') GROUP BY
    .from("table", "t1")
    .left_join("table2", "t2", " =")
    .where(" <> 'Mark'")
    .where(" <> 'John'")
// SELECT `t1`.`id`, `t1`.`name` as "My name", `t1`.`started` as "Date" FROM table `t1` WHERE age IN (RANGE(1, 1.2)) ORDER BY id ASC LIMIT 20{ autoQuoteFieldNames: true })
    .from("table", "t1")
    .field("", "My name")
    .field("t1.started", "Date")
    .where("age IN ?", squel.str('RANGE(?, ?)', 1, 1.2))

You can build parameterized queries:

    text: "SELECT `t1`.`id`, `t1`.`name` as "My name", `t1`.`started` as "Date" FROM table `t1` WHERE age IN (RANGE(?, ?)) ORDER BY id ASC LIMIT 20",
    values: [1, 1.2]
*/{ autoQuoteFieldNames: true })
    .from("table", "t1")
    .field("", "My name")
    .field("t1.started", "Date")
    .where("age IN ?", squel.str('RANGE(?, ?)', 1, 1.2))

You can use nested queries:

// SELECT FROM (SELECT * FROM students) `s` INNER JOIN (SELECT id FROM marks) `m` ON ( =
    .from('students'), 's' )
    .join('marks').field('id'), 'm', ' =' )


// UPDATE test SET f1 = 1
    .set("f1", 1)
// UPDATE test, test2, test3 AS `a` SET = 1, test2.val = 1.2, = "Ram", = NULL, a.count = a.count + 1
    .set("", 1)
    .set("test2.val", 1.2)
        "": "Ram",
        "": null,
        "a.count = a.count + 1": undefined


// INSERT INTO test (f1) VALUES (1)
    .set("f1", 1)
// INSERT INTO test (name, age) VALUES ('Thomas', 29), ('Jane', 31)
        { name: "Thomas", age: 29 },
        { name: "Jane", age: 31 }    


    .where(" = ?", 2)
    .order("id", false)

Paramterized queries

Use the useParam() method to obtain a parameterized query with a separate list of formatted parameter values:

// { text: "INSERT INTO test (f1, f2, f3, f4, f5) VALUES (?, ?, ?, ?, ?)", values: [1, 1.2, "TRUE", "blah", "NULL"] }
    .set("f1", 1)
    .set("f2", 1.2)
    .set("f3", true)
    .set("f4", "blah")
    .set("f5", null)

Expression builder

There is also an expression builder which allows you to build complex expressions for WHERE and ON clauses:

// test = 3 OR test = 4
    .or("test = 3")
    .or("test = 4")
// test = 3 AND (inner = 1 OR inner = 2) OR (inner = 3 AND inner = 4 OR (inner IN ('str1, 'str2', NULL)))
    .and("test = 3")
            .or("inner = 1")
            .or("inner = 2")
            .and("inner = ?", 3)
            .and("inner = ?", 4)
                    .and("inner IN ?", ['str1', 'str2', null])
// SELECT * FROM test INNER JOIN test2 ON ( = WHERE (test = 3 OR test = 4)
    .join( "test2", null, squel.expr().and(" =") )
    .where( squel.expr().or("test = 3").or("test = 4") )

Custom value types

By default Squel does not support the use of object instances as field values. Instead it lets you tell it how you want specific object types to be handled:

// handler for objects of type Date
squel.registerValueHandler(Date, function(date) {
  return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
  .set('start_date', new Date(2013, 5, 1))
// UPDATE students SET start_date = '2013/6/1'

Note that custom value handlers can be overridden on a per-instance basis (see the docs)

Custom queries

Squel allows you to override the built-in query builders with your own as well as create your own types of queries:

// ------------------------------------------------------
// Setup the PRAGMA query builder
// ------------------------------------------------------
var util = require('util');   // to use util.inherits() from node.js
var CommandBlock = function() {};
util.inherits(CommandBlock, squel.cls.Block);
// private method - will not get exposed within the query builder
CommandBlock.prototype._command = function(_command) {
  this._command = _command;
// public method - will get exposed within the query builder
CommandBlock.prototype.compress = function() {
CommandBlock.prototype.buildStr = function() {
  return this._command.toUpperCase();
// generic parameter block
var ParamBlock = function() {};
util.inherits(ParamBlock, squel.cls.Block);
ParamBlock.prototype.param = function(p) {
  this._p = p;
ParamBlock.prototype.buildStr = function() {
  return this._p;
// pragma query builder
var PragmaQuery = function(options) {, options, [
      new squel.cls.StringBlock(options, 'PRAGMA'),
      new CommandBlock(),
      new ParamBlock()
util.inherits(PragmaQuery, squel.cls.QueryBuilder);
// convenience method (we can override built-in squel methods this way too)
squel.pragma = function(options) {
  return new PragmaQuery(options)
// ------------------------------------------------------
// Build a PRAGMA query
// ------------------------------------------------------

Examples of custom queries in the wild:

Non-standard SQL

Squel supports the standard SQL commands and reserved words. However a number of database engines provide their own non-standard commands. To make things easy Squel allows for different 'flavours' of SQL to be loaded and used.

At the moment Squel provides mysql, mssql and postgres flavours which augment query builders with additional commands (e.g. INSERT ... RETURNING for use with Postgres).

To use this in node.js:

var squel = require('squel').useFlavour('postgres');

For the browser:

<script type="text/javascript" src=""></script>
<script type="text/javascript">
  squel = squel.useFlavour('postgres');

(Internally the flavour setup method simply utilizes the custom query mechanism to effect changes).

Read the the API docs to find out available commands. Flavours of SQL which get added to Squel in the future will be usable in the above manner.

Building it

To build the code and run the tests:

$ npm install
$ npm test <-- this will build the code and run the tests

Releasing it

Instructions for creating a new release of squel are in


Contributions are welcome! Please see

Ports to other languages


MIT - see