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

node-jsql

v1.1.1-0

Published

turn SQL queries into JS functions

Downloads

6

Readme

jsql

  • Turn SQL queries into javascript functions
  • Allow named parameters
  • Compatible with mysql, postgres & sqlite any-db adapters

Usage

  • jsql.loadSync(str[, opts]): load a query or queryfile synchronously
  • jsql.load(str[, opts], callback): load a query or queryfile asynchronously

parameters

  • str: a single SQL query, or a path to a query file
  • opts:
    • db: an any-db adapter
    • promisify: a promisifier
    • postgres: set to true to enable postgres style parameters

Examples

Simple (no parameters, no promise or event)

var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db};
var query = jsql.loadSync('SELECT * FROM foobar', opts);
query(function(err, result) { /* ... */ });

With named parameters

var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db};
var query = jsql.loadSync('SELECT * FROM foobar WHERE id = :foo', opts);
query({foo: 123}, function(err, result) { /* ... */ });

With promises

var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db, promisify: Bluebird.promisify};
var query = jsql.loadSync('SELECT * FROM foobar', opts);
query().then(function(result) { /* ... */ });

With events

var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db};
var query = jsql.loadSync('SELECT * FROM foobar', opts);
query().on('data', function() { /* ... */ })

(events are defined by the underlying any-db adapter)

With several db adapters

var dbOne = require('any-db').createConnection(/* DSN1 */);
var dbTwo = require('any-db').createConnection(/* DSN2 */);
var query = jsql.loadSync('SELECT * FROM foobar', {db: dbOne});
query(function(err, result) { /*   */ }); // uses default db, dbOne
query(dbTwo, function(err, result) { /* ... */ }); // uses dbTwo

Named parameters

Named parameters (such as :foobar) are turned into positional parameters (?) or numbered parameters ($1,$2,...) depending on db type. Parameters can they be passed as a javascript object at query time. Since named parameters are merely positional/numbered parameters they cannot be used on column or table names.

Query files

Queries can also be loaded from a file.

Single query

var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db};
var query = jsql.loadSync('file://my-query.sql', opts);
query(opts)

Multiple queries

Multiple queries can be added to the same query file. Queries must be named using valid javascript names ([a-z_$][a-z0-9_$]*) and delimited using a special delimiter: /*: valid_javascript_name */.

/*: my_first_query */
SELECT * FROM foo WHERE id = :id
/*: my_second_query */
SELECT * FROM bar WHERE id = :id
var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db, promisify: Bluebird.promisify};
var queries = jsql.loadSync('file://my-queries.sql', opts);
queries.my_first_query({id: 1234}).then(/* ... */);
queries.my_second_query({id: 1234}).then(/* ... */);

Nested query names

If the query name contains a dot, it will be nested in the resulting object

/*: foo.one */
SELECT * FROM foo
/*: foo.two */
SELECT * FROM bar
var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db, promisify: Bluebird.promisify};
var queries = jsql.loadSync('file://my-queries.sql', opts);
queries.foo.one().then(/* ... */);
queries.foo.two().then(/* ... */);

Load a query file asynchronously

var db = require('any-db').createConnection(/* DSN */);
var opts = {db: db};
jsql.load('file://my-queries.sql', function(err, queries) {
  /* ... */
});

postgres support

var db = require('any-db').createConnection(/* DSN */);
jsql.loadSync('file://my-queries', {postgres: true});
/* ... */

postgres & non-postgres queries can be contained in the same query file, but adapters should either be omitted at load time or overloaded at call time.