
Company News
AWS Security Hub Adds Socket for Supply Chain Security
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.
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.
It supports the following conditionals:
{=key, optionalDefault} - Turns args into ? with an optional default{!key, optionalDefault} - Passed raw into the SQL{>path, optionalDataKey} - Require file from the templates{?key, replaceTruthy, replaceFalsey} - Ternary switch, the defaults are replaceTruthy/replaceFalsey === true/falseI 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.
npm i sql-stamp --save
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 = ?
* )
*/
}
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
Run the unit tests
npm test
Unit tests with code coverage
npm run coverage
And some really simple benchmarks
npm run benchmark
Thanks to Pearlshare for supporting development and Oliver Brooks for help with design.
FAQs
The tiny SQL templater
The npm package sql-stamp receives a total of 10 weekly downloads. As such, sql-stamp popularity was classified as not popular.
We found that sql-stamp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.

Research
/Security News
Popular npm packages keyv and cacheable compromised.

Security News
A misconfiguration gave three Anthropic models internet access, and one, believing it was in a simulation, shipped a credential-stealing package to PyPI.