@kequtech/pgclear

中文文档
0.3.4发布于 2026/11/01ISCTypeScriptES Module零依赖

A standalone TypeScript PostgreSQL driver with explicit session ownership

原始使用文档
文档目录4 节
  1. Install and run a query
  2. Keep related work on one connection
  3. Know your values
  4. Documentation

pgclear

A PostgreSQL driver for JavaScript and TypeScript on Node.js 24+. ESM only, with TypeScript declarations and no runtime dependencies. Tested with PostgreSQL 16 and 18.

pgclear sends your SQL to PostgreSQL, binds values separately, and decodes the results. It provides connection pools, transactions, prepared statements, row streams, COPY, and notifications. Each connection runs one operation at a time; a pool lets independent operations use separate connections.

Install and run a query

npm install @kequtech/pgclear

Set DATABASE_URL to your PostgreSQL connection URI. Save this as example.mjs and run it with node example.mjs:

import { createPool, sql } from '@kequtech/pgclear';

const connectionString = process.env.DATABASE_URL;
if (!connectionString) throw new Error('Set DATABASE_URL before running this example.');

const pool = createPool(connectionString);
try {
  const message = 'Hello, PostgreSQL!';
  const result = await pool.query(sql`SELECT ${message} AS message`);
  console.log(result.rows[0]?.message); // Hello, PostgreSQL!
} finally {
  await pool.close();
}

The sql tag turns ${message} into a $1 placeholder and a separate bound value. It does not insert the value into the SQL text. For existing SQL with numbered placeholders, use queryClassic(text, values):

const result = await pool.queryClassic<{ message: string }>('SELECT $1 AS message', [
  'Hello, PostgreSQL!',
]);

Both methods return rows, fields, command, commandTag, and rowCount. TypeScript row annotations describe what you expect; pgclear does not validate them against your SQL.

The pool opens connections on demand. In an application, create one pool, reuse it, and close it at shutdown. The application must read environment variables and pass them to pgclear; the driver does not load DATABASE_URL or .env automatically.

Separate pool queries can use different connections. A transaction callback holds one connection, commits when the callback succeeds, and rolls back when it throws:

await pool.transaction(async (tx) => {
  await tx.queryClassic('UPDATE accounts SET balance = balance - $1 WHERE id = $2', [10, 1]);
  await tx.queryClassic('UPDATE accounts SET balance = balance + $1 WHERE id = $2', [10, 2]);
});

This example assumes an accounts table. Use the supplied tx handle for all work in the transaction. Use pool.withSession when you need one connection without starting a transaction. Prepared statements, streams, and COPY also run through a held session.

Know your values

  • int8/bigserial and numeric/decimal results are strings to preserve precision. That includes count(*). Command metadata such as rowCount is a number | null.
  • Dates and times are strings by default. Date, bigint, and supported Temporal values can be bound directly; reading them as Date, bigint, or Temporal requires an explicit codec choice.
  • Use json(value) for JSON parameters and param(array, codec) for PostgreSQL arrays. Plain objects, arrays, and undefined are rejected as binds.
  • Query values are parameters. Table names, column names, SQL fragments, and lists cannot be inserted with sql interpolations.

See values and codecs for examples and the full output mapping.

Documentation

Start with the documentation index or choose a guide:

Schema generation and query checking live in Tenon. SQL composition and migrations belong to the application or a higher-level library.

For contributors: development and testing, architecture, benchmarks, and release notes.

版本与发布记录

@kequtech/pgclear 共发布 9 个版本,首次发布于 2026/10/29;过去 12 个月发布了 9 个版本。

版本发布日期距今
0.3.4latest2026/11/012 天前
0.3.32026/10/313 天前
0.3.22026/10/304 天前
0.3.12026/10/304 天前
0.3.02026/10/304 天前
0.2.02026/10/294 天前

关于 @kequtech/pgclear 的常见问题

@kequtech/pgclear 是什么?

一个带有显式会话所有权的独立TypeScript PostgreSQL驱动程序

如何安装 @kequtech/pgclear?

在项目目录运行 npm install @kequtech/pgclear,或使用 pnpm add / yarn add / bun add。

@kequtech/pgclear 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@kequtech/pgclear 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@kequtech/pgclear 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@kequtech/pgclear 可以商用吗?

@kequtech/pgclear 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@kequtech/pgclear 还在维护吗?

最新版本 0.3.4 发布于 2026/11/01(2 天前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/10/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。