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

sqlbars

v0.11.0

Published

A customized Handlebars instance for generating SQL

Downloads

15

Readme

sqlbars

It's Handlebars! But for SQL!

Why?

Sometimes, you have to write some SQL that just doesn't map well to an ORM.

With this wrapper around Handlebars, you can now do so with a little more safety and convenience.

helpers

{{sql}}

  {{sql null}} // result is NULL
  {{sql val}} // with data { val: null }, result is NULL
  {{sql val}} // with data { val: undefined }, result is NULL

  {{sql true}} // result is TRUE
  {{sql false}} // result is FALSE
  {{sql val}} // with data { val: true }, result is TRUE
  {{sql val}} // with data { val: false }, result is FALSE

  {{sql 1.0}} // result is 1.0
  {{sql val}} // with data { val: 1.0 }, result is 1.0

  {{sql "foo"}} // result is 'foo'
  {{sql val}} // with data { val: "foo" }, result is 'foo'

  {{sql "'foo'"}} // result is '\'foo\''
  {{sql val}} // with data { val: "'foo'" }, result is '\'foo\''

  {{sql 1 2 3}} // result is (1, 2, 3)
  {{sql val}} // with data { val: [1, 2, 3] }, result is (1, 2, 3)

{{sql}} converts all values passed in to SQL-compatible types, and escapes any special characters as needed

{{sql-id}}

  {{sql-id "foo"}} // result is `foo`
  {{sql-id "`foo`"}} // result is ```foo```

  {{sql-id "foo" "bar"}} // result is `foo`.`bar`
  {{sql val}} // with data { val: [ 'foo', 'bar' ] }, result is `foo`.`bar`

{{sql-id}} is a utility to create a safely escaped SQL identifier (a database, table, or column name). When given multiple arguments, or a single argument that is an array, the identifiers are joined together with '.'

{{sql-list}}

{{sql-list null}} // result is NULL
{{sql-list val}} // with data { val: null }, result is NULL
{{sql-list val}} // with data { val: undefined }, result is NULL

{{sql-list true}} // result is TRUE
{{sql-list false}} // result is FALSE
{{sql-list val}} // with data { val: true }, result is TRUE
{{sql-list val}} // with data { val: false }, result is FALSE

{{sql-list 1.0}} // result is 1.0
{{sql-list val}} // with data { val: 1.0 }, result is 1.0

{{sql-list "foo"}} // result is 'foo'
{{sql-list val}} // with data { val: "foo" }, result is 'foo'

{{sql-list "'foo'"}} // result is '\'foo\''
{{sql-list val}} // with data { val: "'foo'" }, result is '\'foo\''

{{sql-list 1 2 3}} // result is 1, 2, 3
{{sql-list val}} // with data { val: [1, 2, 3] }, result is 1, 2, 3

{{sql-list}} converts all values passed in to SQL-compatible types in an almost identical way as {{sql}}, with the exception of not adding the outermost set of parentheses to lists of results. This is useful for building lists for certain sql statements, such as INSERT VALUES ...;

{{sql-limit}}

  {{sql-limit 1}} // result is 1
  {{sql-limit '1'}} // result is 1

  {{sql-limit 1.5}} // result is 1
  {{sql-limit '1.5'}} // result is 1

  {{sql-limit -1}} // result is value of MAX_LIMIT (999999999999)
  {{sql-limit '-1'}} // result is value of MAX_LIMIT (999999999999)

  {{sql-limit}} // result is value of MAX_LIMIT (999999999999)

{{sql-limit}} is a utility to safely parse its argument into a limit value, that will always be a non-negative integer. {{sql-limit}} does not generate the LIMIT keyword, just the numberical value. This is so you can use it as a number in checks, or set variable values. If {{sql-limit}} is given a negative number, it assumes that means no limit, and outputs the MAX_LIMIT value.

{{sql-offset}}

  {{sql-offset 1}} // result is 1
  {{sql-offset '1'}} // result is 1

  {{sql-offset 1.5}} // result is 1
  {{sql-offset '1.5'}} // result is 1

  {{sql-offset -1}} // throws an Error
  {{sql-offset -1}} // throws an Error

  {{sql-offset}} // result is 0

{{sql-offset}} is a utility to safely parse its argument into a offset value, that will always be a non-negative integer. {{sql-offset}} does not generate the OFFSET keyword, just the numberical value. This is so you can use it as a number in checks, or set variable values. If {{sql-offset}} is given a negative number, it throws an error.

{{sql-order}}

  {{sql-order "foo"}} // result is `foo` ASC
  {{sql-order "+foo"}} // result is `foo` ASC
  {{sql-order "-foo"}} // result is `foo` DESC

  {{sql-order "+foo" "-bar"}} // result is `foo` ASC, `bar` DESC

  {{sql-order val}} // if val is ['+foo', '-bar'], result is `foo` ASC, `bar` DESC

  {{sql-order val "cab"}} // if val is ['+foo', '-bar'], result is `foo` ASC, `bar` DESC, `cab` ASC

{{sql-order}} is a utility to generate a sequence of properties to order by. {{sql-order}} does not generate the ORDER BY keywords, just the ordering sequence. {{sql-order}} accepts any number of strings or arrays, and flattens them out into a single ordering sequence

{{and}}

  {{#if (and a b)}}foo{{/if}} // with data { a: true, b: true } result is foo
  {{#if (and a b)}}foo{{/if}} // with data { a: true, b: false } result is empty string
  {{#if (and a b)}}foo{{/if}} // with data { a: false, b: true } result is empty string
  {{#if (and a b)}}foo{{/if}} // with data { a: false, b: false } result is empty string

{{and}} is a logical operator that takes any number of arguments, coerces them into booleans, and returns the logical and of all of them.

{{or}}

  {{#if (or a b)}}foo{{/if}} // with data { a: true, b: true } result is foo
  {{#if (or a b)}}foo{{/if}} // with data { a: true, b: false } result is foo
  {{#if (or a b)}}foo{{/if}} // with data { a: false, b: true } result is foo
  {{#if (or a b)}}foo{{/if}} // with data { a: false, b: false } result is empty string

{{or}} is a logical operator that takes any number of arguments, coerces them into booleans, and returns the logical or of all of them.

{{not}}

  {{#if (not a)}}foo{{/if}} // with data { a: true } result is empty string
  {{#if (not a)}}foo{{/if}} // with data { a: false } result is foo

{{not}} is a logical operator that takes one argument, coerces it into a boolean, and returns the logical not of it.