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

v0.8.0

Published

The tiny SQL templater

Downloads

12

Readme

sql-stamp

The tiny SQL templating library, with the aim to be as simple as possible so to not get in the way of you writing SQL.

Build Status Test Coverage Code Climate Dependency Status Dev Dependency Status

It supports the following conditionals:

  • {=key, optionalDefault} - Turns args into ? with an optional default
  • {!key, optionalDefault} - Passed raw into the SQL
  • {?key, replaceTruthy, replaceFalsey} - Ternary switch, the defaults are replaceTruthy/replaceFalsey === true/false
  • {>path, optionalDataKeys*} - Require file from the templates, the data keys can take on the format
    • {>path id:user.id} - pass user.id as id
    • {>path user.id} - pass user.id as id
    • {>path ...user} - object spread, in the same way as javascript ES6 spread operator

Why?

I guess the best way to answer that is why not a HTML templating language like mustache? Basically these support loops and conditionals sql-stamp doesn't because I want the SQL to be kept clean and readable. It does however support a limited ternary switch for feature switches. Also it'll add in ? as params so you can use your SQL libraries injection protection.

Install

npm i sql-stamp --save

API

The API is as follows

var sqlStamp = require("sql-stamp");
var tmpl = sqlStamp([
  /* Pass a list of SQL templates */
  __dirname+"/friends.sql",
  __dirname+"/example.sql"
]).then(function(sql) {
  // 'sql' call with 'sql(pathToFile, args)' to exec the template
});

var files = glob.sync("./sql/**/*.sql")
sqlStamp(files).then(function(sql) {
  sql(__dirname+"../lib/sql/foo.sql", {foo: "bar"});
  // => {sql: "select...", args: ["bar"]}
});

So for example given the following SQL file which selects all friend requests you've accepted

/* ./friends.sql */
select * from friends where status = "accepted"

The following file can require this as a CTE (http://www.postgresql.org/docs/9.1/static/queries-with.html) via a require directive {> ./file/path.sql}

/* ./example.sql */
WITH friend AS (
  {> ./friends.sql}
)
select
  *
from
  account
where
  account.id = friend.toId
  AND friend.fromId = {accountId}
  AND (
    {?filterDisabled} OR {!filterKey} = {filterVal}
  )

When we run the following

var out = tmpl(__dirname+"/example.sql", {
  accountId: 1,
  filterDisabled: false,
  filterKey: "role",
  filterVal: "dev"
});

The following will be returned

{
  args: [1, "dev"],
  sql: /* SQL in comment below */
  /**
   * WITH friend AS (
   *   select * from friends where status = "active"
   * )
   * select
   *   *
   * from
   *   account
   * where
   *   account.id = friend.toId
   *   AND friend.fromId = ?
   *   AND (
   *     /*feature:filterDisabled*/ false AND role = ?
   *   )
   */
}

Errors

You'll get more descriptive errors about where the error happened in your source SQL. This can be disabled with {prettyErrors: false}

SQLError: Too many args

select
  *
from
  account
where
  foo = {too, many, args}
--------^

You can see some more examples in the tests here here

Test

Run the unit tests

npm test

Unit tests with code coverage

npm run coverage

And some really simple benchmarks

npm run benchmark

Thanks

Thanks to Pearlshare for supporting development and Oliver Brooks for help with design.

License

MIT