node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »


build status sqlish

A SQL generator for for NodeJS, Mongo Shell and web browsers.


JavaScript's ability to chaining function calls provides an nice semantic for generating SQL statements. sqlish (Californian pronunciation: sk wil' ish) takes this approach and attempts to be portable as well as simple to use.


There are several advantages two taking this approach

  • lower cognitive noise (you're programming in one language not two)
  • your JS tools (e.g. jslint) now cover your assembly of SQL statements
  • assembling complex SQL is just a matter of chaining functions
  • MongoDB like object literals for expressions
  • sqlish is very small without dependancies
  • it works as a NodeJS module
  • it works as a loadable script in MongoDB's shell
  • it works should work web browsers supporting SQLite


To use with NodeJS install via npm.

    npm install sqlish

A NodeJS example

    var sqlish = require("sqlish"),
        dialect = sqlish.Dialect,
        Sql = new sqlish.Sqlish("MySQL 5.5"),
        message = {
            id: 0,
            name: "fred",
            email: "",
            messages: 'He Said, "Hello World"',
            sent: new Date("09/01/2012 14:15:00")
    Sql.use_UTC = false;
    // Output:
    // INSERT INTO messages (name, email, msg, sent) VALUES (
    // "fred", "", "He siad, \"Hello World\"", 
    // "2012-09-01 14:15:00");
    console.log(Sql.insert("test", message).toString());
    // Output:
    // SELECT id, name, email, msg, sent FROM messages 
    // WHERE email LIKE "";
            .where({email: {$like: ""}}).toString());
    // Output:
    // REPLACE INTO messages (id, name, email, msg, sent) VALUES (
    // 10123, "George", "", "He siad, \"Hello World\"", 
    // "2012-07-01"); = 10123; = "George"; = "";
    message.sent = new Date("07/01/2012");
    console.log(Sql.replace("test", message).toString());

a word about sqlish's toString()

Normally you want SQL statements to end with a semi-colon and by default this is what the toString() at the end of the function chain will do. However their are cases where you may want to render complex SQL statements by parts. In that case you can overwrite the default semi-colon by passing the terminating string (including the empty string) as a paramater to toString().

    var sqlish = require("sqlish"),
        Sql = new sqlish.Sqlish();
    // No trailing semi-colon
    // Trailing semi-colon

Running the above will yeald something like-

    > console.log("count()").toString(""));
    'SELECT count()'
    > console.log("count()").toString());
    'SELECT count();'

If you would like to have some other delimiter used as the end of statement marker you can do so when overwriting Sql.eol at time of object creation or before calling toString().

    var sqlish = require("sqlish"),
        Sql = new sqlish.Sqlish();
    Sql.eol = ";\n\n";
    // No trailing semi-colon
    // Trailing semi-colon with two new lines

Would yeild something like-

    > console.log("count()").toString(""));
    'SELECT count()'
    > console.log("count()").toString());
    'SELECT count();'

MongoDB shell example

Copy sqlish.js and load-sqlish.js to your working directory. Then use MongoDB's shell's load() function to include it. See mongo-example.js:

    sql = new Sql();
    // Output should look like: 
    //  SELECT id, name, email, modified FROM myTable; 
    print(["id", "name", "email", "modified"]).from("myTable").toString());
    item = {id:1, name: "fred", email: "", modified: new Date("09/14/2012 10:21:14")};
    // Output should look like: 
    //  REPLACE INTO myTable (id, name, email, modified) VALUES ("fred", "", "2012-09-14 10:21:14"); 
    print(sql.replace("myTable", item).toString());

Save the above example code as mongo-example.js. Run under MongoDB's shell-

    mongo mongo-example.js

Output should look something like-

    MongoDB shell version: 2.2.0
    connecting to: test
    SELECT id, name, email, modified FROM myTable;
    REPLACE INTO myTable (id, name, email, modified) VALUES (1, "fred""""2012-09-14 10:21:14");


If you don't need web browser or MongoDB shell support two SQL generators at look very promising-