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

sqlify

v2.5.2

Published

Yet another SQL query builder for Node.js

Downloads

85

Readme

Sqlify

Yet another SQL query builder.

npm Build Status T npm Greenkeeper badge

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

Install

npm install --save sqlify

Why

  • This package is a wrapper around squel module to make it more friendly. (Check that package to know its maintenance status)

  • 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:

    const resource = {
      set: req.body
      where: {
        id: 5
      }
    }
    
    sqlify(chain, resource); // done!

Warning ⚠️: Do not ever pass queries generated on the client side to your web server for execution. The above example is only a use case. Do NOT copy paste as such.

Examples

SELECT

const { squel, sqlify } = require('sqlify');

const resource = {
  field: ['name', 'age', 'address'],
  where: {
    name: 'Swat',
    age: 22,
  },
};

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

sqlify(chain, resource);

chain.toString();
// => SELECT name, age, address FROM users WHERE (name=Swat) AND (age=22)
Starter Guide For TypeScript
import { squel, sqlify, Resource } from 'sqlify'

// `Resource` is type.
const resource :Resource = {
  field: ['name', 'age', 'address'],
  where: {
    name: 'Swat',
    age: 22,
  },
};

// ...

SELECT with a simple JOIN

// ...

const resource = {
  field: ['user.*', 'hobbies.hobby', 'colors.favorite'],
  where: {
    name: 'Swat',
    age: 22,
  },
  join: [
    ['hobbies', null, 'hobbies.id = user.id'],
    ['colors', null, 'colors.user_id = user.id'],
  ];
}
const chain = squel.select().from('Hero');

sqlify(chain, resource);

chain.toString();

/*
SELECT 
  user.*,
  hobbies.hobby,
  colors.favorite 
FROM Hero 
  INNER JOIN hobbies 
    ON (hobbies.id = user.id) 
  INNER JOIN colors 
    ON (colors.user_id = user.id) 
WHERE (name='Swat') AND (age=22)
*/

Read the JOIN section of squel docs for more.

INSERT

const { squel, sqlify } = require('sqlify');

const resource = {
  set: {
    name: 'Swat',
    age: 22,
  },
};

const chain = sql.insert().into('users');
sqlify(chain, resource);

chain.toString();
// => INSERT INTO users (name, age) VALUES ('Swat', 22)

How?

sqlify exposes a function, module (squel) and a Resource type (for using with TypeScript).

The function receives 2 arguments. They are:

  • chain
  • resource

Step 1: Require the package

const { squel, sqlify } = require('sqlify');

Step 2: Initialize chain and resource

chain is an instance of squel. For example,

// ...

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

// ...

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

Example:

// ...

const 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. There are more. Refer their docs and use them accordingly.

When used with TypeScript, you should mark type of resource with the imported Resource class. Like const resource:Resource = {...}.

Step 3: Sqlify

// ...

sqlify(chain, resource);

// ...

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

Step 4: Watch stuff

// ...

// parse query
const query = chain.toString();
// see it
console.log(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.

Example:

// ...

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

sqlify(chain, resource);

chain.limit(10);

chain.toString(); // Voila!

Supported Squel Functions

The following fields can be used inside the resource object. Logic behind the usage of these functions can be found at squel docs.

| | | | | | | ---------- | ---------- | ---- | --------- | ---------- | | 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, squel } = require('sqlify');
    // ...
  • change in function name: change fields:[] to field:[] in the resource object. Oh yes! it's that simple.

Change log

  • v2.5.0, v2.5.1, v2.5.2
    • Security Update
  • v2.4.0
    • TypeScript support and definitions
    • Better docs
  • 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