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-compose

v2.4.5

Published

A tool for safely building SQL queries using Javascript string interpolation.

Downloads

35

Readme

SQL Compose

A tool for safely building SQL queries using Javascript string interpolation.

Purpose of this library

When interfacing with the database, there are two main approaches one can use:

  1. Building queries by writing explicitly parameterized SQL templates: This allows for full control over the SQL but is verbose and hard to maintain - especially as the number of parameters grows.
  2. Building queries using an ORM: This results in succinct and easy to maintain code but at the cost of losing control of the SQL that runs - sometimes with significant performance implications.

The purpose of this library is therefore to provide a solution that results in terse code whilst also affording the user full control of the SQL that runs.

How does it work

The library exposes a single function that constructs a dynamic parameterized SQL query, along with its parameters: build :: Thunk => ( SQL, Params ).

A Thunk is an alias for a function of type: Wrapper => String, where Wrapper is a function provided by the library that can wrap values that are to be interpolated in the following ways:

  • If the value is itself a thunk, the Wrapper evaluates the thunk and returns the evaluated String. This allows you to compose SQL queries in a modular fashion.
  • Else, the value is assigned to a unique parameter and the Wrapper returns the parameter template string.

The auto utility

The library also exports a function: auto :: Wrapper => AutoWrapper. which takes a standard wrapper, and returns one that instead uses tagged template literals to automatically wrap interpolated values for us. Check the code snippet below for details on usage.

Parameter formatting

This library allows you to override how parameters are formatted. See the example below for details!

A quick example

const { build, config, auto } = require( "sql-compose" );

// Optionally override the default parameter formatting style of "$[param]".
config.formatFn = x => `%(${x}]s`;

const autoQuery = (opts) => (w) => auto(w) ` 
    val > ${ opts.min_val }
`;

const query = (opts) => (w) => `
    SELECT * FROM table
    WHERE id IN (${ ",".join( opts.ids.map( w ) ) })
    AND val < ${ w( opts.max_val ) }
    AND ${ w( autoQuery( opts ) ) }
`;

var opts = { ids : [ 1, 2, 3 ], max_val : 8, min_val : 5 };
var [ sql, params ] = build( query( opts ) );
console.log( sql, params );

Prints the following:

"SELECT * FROM table
WHERE id IN ( $[ param_0 ], $[ param_1 ], $[ param_2 ] )
AND val < $[ param_3 ]
AND val > $[ param_4 ]"

{ param_0 : 1, param_1 : 2, param_2 : 3, param_3 : 8, param_4 : 5 }