Gathering detailed insights and metrics for sql-template-tag
Gathering detailed insights and metrics for sql-template-tag
Gathering detailed insights and metrics for sql-template-tag
Gathering detailed insights and metrics for sql-template-tag
npm install sql-template-tag
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
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
351 Stars
88 Commits
19 Forks
5 Watching
4 Branches
7 Contributors
Updated on 21 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
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
ES2015 tagged template string for preparing SQL statements.
npm install sql-template-tag --save
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}`;
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 ?"
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.
Simple placeholder value for an empty SQL string. Equivalent to raw("")
.
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"]
This package "just works" with pg
, mysql
, sqlite
and oracledb
.
1mssql.query(query.strings, ...query.values);
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}
Some other modules exist that do something similar:
sql-template-strings
: promotes mutation via chained methods and lacks nesting SQL statements. The idea to support sql
and text
properties for dual mysql
and pg
compatibility came from here.pg-template-tag
: missing TypeScript and MySQL support. This is the API I envisioned before writing this library, and by supporting pg
only it has the ability to dedupe values
.MIT
No vulnerabilities found.
Reason
security policy file detected
Details
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
6 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
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
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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