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

sql-query-builder

v0.3.2

Published

Define models and associations and let your QB write your SQL.

Downloads

85

Readme

Query Builder

Yet another Javascript SQL generator for Node.
Built atop brianc/node-sql, thus supports postgres, mysql, and sqlite.
Here is an example UI for a BI webapp that uses Qb as an api.

Query Builder gets you, man.

When you create a new Qb() you pass in an object that defines your tables and how they relate. And because QB "gets" you, it handles the boring stuff. So that you can focus on you.

Instead of method chaining something like,

query().select([users.id.as('User'), users.id.count(1)])
    .from(users).join(posts)
    .on(posts.user_id).equals(users.id)
    .groupBy(users.id);

Qb accepts an object. Like,

{ select: ['id', {function: 'count', value: 1}], join: ['users', 'posts'] }

Anything you leave out of the object will be filled in with smart defaults based on your model definitions.

Examples

See examples.js to get started or dive into the test directory for something more detailed. Here's an abbreviated reference:

var definitions = require('./example-definitions');
var qb = new Qb(definitions, 'postgres');
var spec, query;

// SELECT a single field.
spec  = { select: 'id', from: 'users' };
query = qb.query(spec);

// SELECT "Users"."id" AS "User ID" 
// FROM "users" AS "Users"

// Use the COUNT function.
spec  = { select: { name: 'id', functions: 'count' }, from: 'posts' };
query = qb.query(spec);

// SELECT COUNT("Blog Posts"."id") AS "Post ID_count" 
// FROM "posts" AS "Blog Posts"

// JOIN another table.
spec  = { select: 'id', from: 'users', join: 'posts' };
query = qb.query(spec);

// SELECT "Users"."id" AS "User ID" 
// FROM "users" AS "Users" 
// INNER JOIN "posts" AS "Blog Posts" 
// ON ("Users"."id" = "Blog Posts"."user_id")

// Filter with a WHERE clause.
spec  = { select: 'id', from: 'users', where: { field: 'id', match: { value: 1 } }};
query = qb.query(spec);

// SELECT "Users"."id" AS "User ID" 
// FROM "users" AS "Users" 
// WHERE ("Users"."id" = 1)

// Aggregate with a GROUP BY clause.
spec  = { select: { name: 'id', groupBy: true }, from: 'users' };
query = qb.query(spec);

// SELECT "Users"."id" AS "User ID"
// FROM "users" AS "Users"
// GROUP BY "Users"."id"

Query Builder for BI Applications

Wouldn't it be great if nonengineers could query your database without having to learn SQL or the nuances of your particular schema? Query builder was written with this in mind.

Qb is ready for your API.
Because it already accepts JSON, you can plug a POST body directly into Qb.query.

Qb only selects from tables/columns you have defined.
Your "unhashed_passwords" table is safe, at least from us.

Qb has context builtin
Tables may have a different default alias depending on what table they are joined on. For example your "users" table might always be aliased "Vendors" when joined from the "inventory" table, but called "Customers" when joined from the "orders" table.

Qb does complex joins automatically
Once you have defined a schema, Qb knows that "orders" is joined to "users" by the orders.user_id column and that "orders" is joined to "categories" via an "orders_categories" intermediate table. So all you have to type is { join: ['users', 'orders'] } in the first case or { join: ['users', 'orders_categories'] } in the second. You could even perform a four part join from users to orders_categories with { join: ['users', 'orders_categories'] }.

Qb is secure
You can define a table to Qb that is hidden to users. So you can still join through "unhashed_passwords" without revealing the table in qb.schema. And of course query output is parameterized.

Contributing

Please do. Any reasonable PR will be merged so long as it has test coverage.