Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sql-template-tag

> 数据库
Open source

ES2015 tagged template string for preparing SQL statements, works with `pg`, `mysql`, `sqlite` and `oracledb`

392 stars0 likes3 views
WebsiteGitHub

About

ES2015 tagged template string for preparing SQL statements, works with `pg`, `mysql`, `sqlite` and `oracledb`

SQL Template Tag

NPM version NPM downloads Build status Build coverage

ES2015 tagged template string for preparing SQL statements.

Installation

bash
npm install sql-template-tag --save

Usage

…

Join

Accepts an array of values or SQL, and returns SQL with the values joined together using the separator.

javascript
const query = join([1, 2, 3]);

query.sql; //=> "?,?,?"
query.values; //=> [1, 2, 3]

Tip: You can set the second argument to change the join separator, for example:

javascript
join(
  [sql`first_name LIKE ${firstName}`, sql`last_name LIKE ${lastName}`],
  " AND ",
); // => "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.

javascript
raw("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.

javascript
const query = sql`INSERT INTO users (name) VALUES ${bulk([
  ["Blake"],
  ["Bob"],
  ["Joe"],
])}`;

query.sql; //=> "INSERT INTO users (name) VALUES (?),(?),(?)"
query.values; //=> ["Blake", "Bob", "Joe"]

Recipes

This package "just works" with pg, mysql, sqlite and oracledb.

MSSQL

javascript
mssql.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.

typescript
import { Sql } from "sql-template-tag";

type SupportedValue =
  | string
  | number
  | SupportedValue[]
  | { [key: string]: SupportedValue };

function sql(
  strings: ReadonlyArray,
  ...values: Array
) {
  return new Sql(strings, values);
}

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

License

MIT

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptmysqloracle-dbpgpostgresql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库