百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

sql-template-tag

> 数据库
开源

ES2015 标记的模板字符串用于准备 SQL 语句,可与 `pg`,`MySQL`,`sqlite` 和 `oracledb` 兼容

392 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

ES2015 标记的模板字符串用于准备 SQL 语句,可与 `pg`,`MySQL`,`sqlite` 和 `oracledb` 兼容

SQL Template Tag

ES2015 tagged template string for preparing SQL statements.

Installation

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.

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:

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.

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.

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

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.

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptmysqloracle-dbpgpostgresql

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类数据库
定价开源

> 相关工具

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