Skip to content

vajahath/sqlify

Repository files navigation

Sqlify

Yet another SQL query builder

npm Build Status Known Vulnerabilities Gitter chat npm

There are many sql query builders out there. But this one makes more sense to me 😉.

Sqlify v2.3 is out! What's new?

  • better error handling
  • more squel functions (order, group)
  • (migrating from v1 to v2 is easier than you think. It just requires 2 mins, you bet!)
    migration guide

🎏 This package is bound to strictly follow Semantic Versioning.

Greenkeeper badge

Install

npm install --save sqlify

Read squel's documentation along with this. This package is a wrapper around squel

Why?

  • Helps you to build dynamic sql queries.

  • Example use case: suppose, you are getting a POST request to insert some data to your SQL database. You'll get the data in req.body as {name: "Swat", age: 22, address: "ND"}. Now make the query like:

    let resource = { set: req.body } sqlify(chain, resource); // done!

Examples

SELECT

const sql = require('sqlify').squel; const sqlify = require('sqlify').sqlify; let resource = { field: ['name', 'age', 'address'], where: { name: 'Swat', age: 22 } }; let chain = sql.select().from('users'); sqlify(chain, resource); chain.toString() // => SELECT name, age, address FROM users WHERE (name=Swat) AND (age=22)

INSERT

const sql = require('sqlify').squel; const sqlify = require('sqlify').sqlify; let resource = { set: { name: 'Swat', age: 22 } }; let chain = sql.insert().into('users'); sqlify(chain, resource); chain.toString() // => INSERT INTO users (name, age) VALUES ('Swat', 22)

How?

sqlify exposes a function and a module (squel).

The function receives 2 arguments. They are:

  • chain
  • resource

Step 1: Require the package

const sql = require('sqlify').squel; const sqlify = require('sqlify').sqlify; const lme = require('lme') // not necessary. For printing things out.. ...

Step 2: Initialize chain and resource

chain is an instance of squel. For example,

... let chain = squel.select().from('users'); ...

resource is an object which contains the data to build the query. Example:

... let resource = { field: ['name', 'age', 'address'], where: { name: 'Swa', age: 22 } }; ...

Where, the properties of resource object (in the above case, field and where) are taken from the chain function names of the squel. Use them accordingly.

Step 3: Sqlify

... sqlify(chain, resource); ...

sqlify function wont return anything. It simply do things in in-place.

Step 4: Watch stuff...

... // parse query let query = chain.toString(); // see it lme.s(query); // => SELECT name, age, address FROM users WHERE (name='Swa') AND (age=22) ...
Unclear about something here? Feel free to rise an issue..

Also,

Since sqlify takes in and out chain functions, you can modify it even after sqlifying it.


💚 Find some time to contribute ⭐ to accommodate other functionalities from squel.

supported functions

cross-join field join left-join outer-join
returning right-join set where group
order

Contributors

v1 to v2 migration guide

  • change the way you require the package:
    • in v1, you required sqlify along with squel as:
      const sqlify = require('sqlify'); const squel = require('squel'); ...
    • in v2 you've to change that code into:
      const sqlify = require('sqlify').sqlify; const squel = require('sqlify').squel; ...
  • change in function name: change fields:[] to field:[] in the resource object.

    Oh yes! it's that simple.

    Just in case if you liked this package,   PayPal

Change log

  • v2.3.1

    • enabling Greeenkeeper, better docs
  • v2.3.0

    • adds better error handling: (if an unsupported method is used, sqlify throws an err)
  • v2.2.0

  • v2.1.1

  • v2.0.0

    • fixing #5 and #2.
    • more squel functions
  • v1.0.4

    • bug fix with 's in select queries
  • v1.0.1, 1.0.2, 1.0.3

    • bug fix (in package.json)
    • better docs
  • v1.0.0

    • initial release

Licence

MIT © Vajahath Ahmed