Sqorn快速上手:5分钟安装@sqorn/pg连接Postgres,写出你的第一条SQL查询
Sqorn快速上手:5分钟安装@sqorn/pg连接Postgres,写出你的第一条SQL查询
本文带你 5 分钟搞定 sqorn 的 SQL 查询构建器:用
@sqorn/pg连接 Postgres,快速写出可执行、参数化的 SQL。适合新手和普通用户,无需大量代码即可理解核心用法。
Sqorn 是什么?为什么值得一试?
Sqorn 是一个用 JavaScript 构建 SQL 查询的开源库。它把晦涩的 SQL 字符串,变成了一行行可链式调用、可组合、不可变的现代 JS 代码。
它内置了三种互补的构建方式,各取所需:
| 机制 | 适用场景 | 特点 |
|---|---|---|
| SQL 查询构建器 | 常规 CRUD | 基于 SQL 子句的方法,直观简洁 |
| 手动查询构建器 | 高度定制 | 用模板字符串写原生 SQL,灵活最大 |
| 表达式构建器 | 复杂条件 | 流畅的类型化布尔/运算表达式 |
几个让你安心的特性:
- 🔒 安全:生成参数化查询,天生抵御 SQL 注入。
- ⚡ 快:官方基准测试显示,编译速度可达 Knex 的 10 倍、Squel 的 200 倍。
- 🧱 可组合:查询是不可变值,能用简单零件搭出复杂查询(链式、扩展、嵌入)。
- 📦 零依赖:没有外部依赖,轻量易维护。
📖 完整介绍见项目主页 README.md,概念详解见 docs/docs/about.md。
一键安装步骤
Sqorn 要求 Node 8 及以上版本。连接 Postgres 需要同时安装 Node 官方驱动 pg(Node Postgres)与 Sqorn 的 Postgres 适配器 @sqorn/pg。
打开终端,执行:
npm install --save pg @sqorn/pg
如果你只需要 Postgres,README 里也给出了最简安装:
npm install --save @sqorn/pg。 适配器源码在 packages/sqorn-pg/,内部封装了 Postgres 方言 packages/sqorn-dialect-postgres/。
连接 Postgres:创建查询构建器 sq
安装完成后,创建 Node Postgres 连接池,再把 pg 与 pool 一起传给 sqorn(),即可拿到查询构建器 sq:
const pg = require('pg')
const sqorn = require('@sqorn/pg')
const pool = new pg.Pool()
const sq = sqorn({ pg, pool })
sq 就是你的"瑞士军刀"——后面所有查询都由它发起。
写出你的第一条 SELECT 查询
最简单的查询长这样,一行搞定:
const children = await sq`person`
// 生成: select * from person
加上过滤条件,Sqorn 会自动做参数化,杜绝注入风险:
// 年龄小于 13 的 person
const kids = await sq`person```age < ${13}`
// 生成: select * from person where (age < $1) 参数: [13]
💡 技巧:
sq的第 1、2、3 次调用,等价于.from、.where、.return。详见 docs/docs/select_queries.md。
如果你不想立即执行,而是先拿到编译结果(SQL 文本 + 参数),加上 .query 即可:
sq.from`book`.query
// { text: 'select * from book', args: [] }
5 分钟搞定 CRUD 四连击
Sqorn 对常见的增删改查提供了极简语法,下面用 person 和 book 两张表演示:
1. 插入(INSERT)
await sq`person`.insert({ firstName: 'Rob' })
// 生成: insert into person (first_name) values ($1)
注意:输入的驼峰键 firstName 会被自动转成数据库习惯的 first_name。
2. 更新(UPDATE)
await sq`person`({ id: 23 }).set({ name: 'Rob' })
// 生成: update person set name = $1 where (id = $2)
3. 删除(DELETE)
await sq`book`.delete({ id: 7 })`title`
// 生成: delete from book where (id = $1) returning title
4. 查询(SELECT)
const rows = await sq`book`.where({ genre: 'Fantasy' })
更多子句(
from/where/join/groupBy/limit等)请查阅 docs/docs/select_queries.md 与 docs/docs/operations.md。
执行与编译:两种用法
拿到查询后,你有两种方式得到结果:
直接执行 —— 查询对象本身支持 await(thenable),.all 也是可选的:
const people = await sq`person` // 返回所有行
// 或
const people = await sq`person`.all()
手动编译 —— 用 .query 拿到 { text, args },再交给别的库执行:
const { text, args } = sq.from('book').query
pool.query(text, args)
⚠️ 安全提醒:
.unparameterized会生成无参数化的字符串,存在注入风险,不要用它拼接最终 SQL。详见 docs/docs/executing_queries.md。
用真实例子串起来:建表、填数据、查数据
仓库里有一个可直接运行的 Postgres 示例,它演示了"建库 → 建表 → 插入 → 查询"的完整流程,非常适合跟着读一遍:
核心片段——用 Sqorn 建表并插入:
// 建表
await sq.sql`create table book (
id serial primary key,
title text,
genre text,
publish_year integer
)`
// 批量插入
await sq`book`.insert(
{ title: 'The Way of Kings', genre: 'Fantasy', publishYear: 2010 },
{ title: 'The Eye of the World', genre: 'Fantasy', publishYear: 1990 }
)
跑通这段,你就已经用 Sqorn 完成了"连接 Postgres → 写 SQL"的全过程。🎉
几个新手友好的默认约定
了解这些小规则,能少走很多弯路:
- 🐍 输入键自动 snake_case:
firstName→first_name。 - 🐫 输出键自动 camelCase:查回的行,
first_name会变成firstName。 - ⏳ 可直接 await:查询即 thenable,省掉
.all()。 - 🔧 可自定义:通过
mapInputKeys、mapOutputKeys、thenable等配置调整上述行为,详见 docs/docs/Configuration.md。
下一步学什么
- 概念与三大构建器:docs/docs/about.md
- 安装与环境配置:docs/docs/setup.md
- 执行 / 编译查询:docs/docs/executing_queries.md
- 各类查询参考:docs/docs/select_queries.md、docs/docs/insert_queries.md、docs/docs/update_queries.md、docs/docs/delete_queries.md
- 表达式构建器:docs/docs/expressions.md
到这里,你已掌握 Sqorn 快速上手的核心:安装 @sqorn/pg、创建 sq、写出可执行的参数化 SQL。剩下的,就是用 examples/postgres/ 动手练起来吧。
