Installations
npm install sql-template-tag
Releases
Documentation: Native OracleDB support
Published on 20 Apr 2024
OracleDB support
Published on 28 Nov 2023
Bulk method and readonly input types
Published on 04 Sept 2023
Set `Value` type to `unknown`
Published on 27 May 2022
Performance improvements
Published on 13 May 2022
Allow `Date` as value type
Published on 31 Jan 2022
Developer
blakeembrey
Developer Guide
Module System
ESM
Min. Node Version
>=14
Typescript Support
No
Node Version
20.12.1
NPM Version
10.5.0
Statistics
351 Stars
88 Commits
19 Forks
5 Watching
4 Branches
7 Contributors
Updated on 21 Nov 2024
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
2,229,579
Last day
33.5%
5,242
Compared to previous day
Last week
14.1%
26,817
Compared to previous week
Last month
2.3%
105,637
Compared to previous month
Last year
59.4%
961,205
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
SQL Template Tag
ES2015 tagged template string for preparing SQL statements.
Installation
npm install sql-template-tag --save
Usage
1import sql, { empty, join, raw } from "sql-template-tag"; 2 3const query = sql`SELECT * FROM books WHERE id = ${id}`; 4 5query.sql; //=> "SELECT * FROM books WHERE id = ?" 6query.text; //=> "SELECT * FROM books WHERE id = $1" 7query.statement; //=> "SELECT * FROM books WHERE id = :1" 8query.values; //=> [id] 9 10pg.query(query); // Uses `text` and `values`. 11mysql.query(query); // Uses `sql` and `values`. 12oracledb.execute(query); // Uses `statement` and `values`. 13 14// Embed SQL instances inside SQL instances. 15const nested = sql`SELECT id FROM authors WHERE name = ${"Blake"}`; 16const query = sql`SELECT * FROM books WHERE author_id IN (${nested})`; 17 18// Join and "empty" helpers (useful for nested queries). 19sql`SELECT * FROM books ${hasIds ? sql`WHERE ids IN (${join(ids)})` : empty}`;
Join
Accepts an array of values or SQL, and returns SQL with the values joined together using the separator.
1const query = join([1, 2, 3]); 2 3query.sql; //=> "?,?,?" 4query.values; //=> [1, 2, 3]
Tip: You can set the second argument to change the join separator, for example:
1join( 2 [sql`first_name LIKE ${firstName}`, sql`last_name LIKE ${lastName}`], 3 " AND ", 4); // => "first_name LIKE ? AND last_name LIKE ?"
Raw
Accepts a string and returns a SQL instance, useful if you want some part of the SQL to be dynamic.
1raw("SELECT"); // == sql`SELECT`
Do not accept user input to raw
, this will create a SQL injection vulnerability.
Empty
Simple placeholder value for an empty SQL string. Equivalent to raw("")
.
Bulk
Accepts an array of arrays, and returns the SQL with the values joined together in a format useful for bulk inserts.
1const query = sql`INSERT INTO users (name) VALUES ${bulk([ 2 ["Blake"], 3 ["Bob"], 4 ["Joe"], 5])}`; 6 7query.sql; //=> "INSERT INTO users (name) VALUES (?),(?),(?)" 8query.values; //=> ["Blake", "Bob", "Joe"]
Recipes
This package "just works" with pg
, mysql
, sqlite
and oracledb
.
MSSQL
1mssql.query(query.strings, ...query.values);
Stricter TypeScript
The default value is unknown
to support every possible input. If you want stricter TypeScript values you can create a new sql
template tag function.
1import { Sql } from "sql-template-tag";
2
3type SupportedValue =
4 | string
5 | number
6 | SupportedValue[]
7 | { [key: string]: SupportedValue };
8
9function sql(
10 strings: ReadonlyArray<string>,
11 ...values: Array<SupportedValue | Sql>
12) {
13 return new Sql(strings, values);
14}
Related
Some other modules exist that do something similar:
sql-template-strings
: promotes mutation via chained methods and lacks nesting SQL statements. The idea to supportsql
andtext
properties for dualmysql
andpg
compatibility came from here.pg-template-tag
: missing TypeScript and MySQL support. This is the API I envisioned before writing this library, and by supportingpg
only it has the ability to dedupevalues
.
License
MIT
No vulnerabilities found.
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/sql-template-tag/ci.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/sql-template-tag/ci.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/sql-template-tag/ci.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/sql-template-tag/ci.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:26
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 1 out of 2 npmCommand dependencies pinned
Reason
Found 3/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 3 are checked with a SAST tool
Score
3.7
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More