# @absolutejs/queue-postgres 中文文档

规范地址：https://npmcn.com/package/%40absolutejs/queue-postgres/

版本：0\.1\.9
npm 元数据更新时间：2026-10-26T11:28:58.072Z
中文译文更新时间：2026-11-06T17:57:49.809Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40absolutejs/queue-postgres
原文对照：https://npmcn.com/package/%40absolutejs/queue-postgres/?lang=original
项目主页：https://github.com/absolutejs/queue-adapters/tree/main/postgres
源码仓库：https://github.com/absolutejs/queue-adapters

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：623 kB，22 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：2 个
- peer 依赖：postgres \>=3\.4\.9 \<4，drizzle-orm \>=1\.0\.0-rc\.4 \<2，@absolutejs/queue \>=0\.8\.4 \<0\.11\.0，@neondatabase/serverless \>=1\.1\.0 \<2
- 首次发布：2026-05-23T14:48:33.757Z
- 最新版本发布：2026-10-26T11:28:57.572Z
- 近 30 天下载量：2524（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：17（近 12 个月 17 个）

## 中文简介

@absolutejs/queue 的 Postgres (Drizzle) 存储适配器 — postgres.js + Neon 服务器无

## 安装

```sh
npm install @absolutejs/queue-postgres
```

也可以使用：`pnpm add @absolutejs/queue-postgres`、`yarn add @absolutejs/queue-postgres`、`bun add @absolutejs/queue-postgres`

## README

# 校对:soup

Postgres 存储适配器 [`@absolutejs/queue`](https://www.npmjs.com/package/@absolutejs/queue)在Drizzle上兴建的 这个 生产 `JobStore`:耐用,原子多工者通过 `FOR UPDATE SKIP LOCKED`双方的船舶便利工厂 `postgres.js` 和 Neon 的 WebSocket 驱动程序(`@neondatabase/serverless`(b) 基础 `buildPostgresJobStore` 接受任何 Drizzle Postgres 数据库,所以其他 驱动程序(包括 Bun SQL 和 `node-postgres`工作也一样。 它的便携式JSONB 编码保存所有支持驱动器的有效载荷物体.

## 安装

```bash
# pick your driver
bun add @absolutejs/queue @absolutejs/queue-postgres drizzle-orm postgres
# …or
bun add @absolutejs/queue @absolutejs/queue-postgres drizzle-orm @neondatabase/serverless
```

`postgres` 财务报告和审定财务报表 `@neondatabase/serverless` 为: **选项** 相位分解 - 安装任何您要使用的.

## 用法为 `postgres.js`

```ts
import { createPostgresJobStore } from '@absolutejs/queue-postgres/postgres';
import { createJobRegistry, defineJobs, queue, t } from '@absolutejs/queue';
import postgres from 'postgres';

const jobs = defineJobs({
    'email.send': t.Object({ to: t.String(), subject: t.String() })
});
const registry = createJobRegistry(jobs).on(
    'email.send',
    async ({ to, subject }) => {}
);

// Share your app's existing postgres.js client (one pool)…
const client = postgres(process.env.DATABASE_URL, { prepare: false });
const store = createPostgresJobStore({ client, jobs });

// …or let the adapter open its own connection:
// const store = createPostgresJobStore({ connectionString: url, jobs });

app.use(queue({ registry, store }));
```

## 与尼永使用( L)`@neondatabase/serverless`(中文(简体) )

`createNeonJobStore` 镜像 `createPostgresJobStore` 但用的是尼永号 WebSocket Pool. (原始内容存档于2017-09-29). **重要**: 队列 `claimDue` 打开交易 选择 `FOR UPDATE SKIP LOCKED`. Neon 的 HTTP 驱动程序 (单位:千美元)`drizzle-orm/neon-http`)是单词,不能进行行级 锁 – 在此使用 WebSocket Pool 驱动程序. 您的应用程序的另一个代码可以 继续使用 HTTP 驱动程序; 它们是独立的 .

```ts
import {
    createNeonJobStore,
    neonConfig
} from '@absolutejs/queue-postgres/neon';
import { createJobRegistry, defineJobs, queue, t } from '@absolutejs/queue';
import { Pool } from '@neondatabase/serverless';

// Bun ships a global WebSocket; for node, polyfill once:
//   import ws from 'ws'; neonConfig.webSocketConstructor = ws;

const jobs = defineJobs({
    'email.send': t.Object({ to: t.String(), subject: t.String() })
});

// Share an existing Neon Pool…
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const store = createNeonJobStore({ jobs, pool });

// …or let the adapter open its own:
// const store = createNeonJobStore({ connectionString: url, jobs });
```

## 使用任何其他 Drizzle Postgres 适配器

两家工厂都是薄薄的包裹 `buildPostgresJobStore(db, jobs)`, (中文(简体) ), 接受任何 `PgDatabase<any, any, any>` 从Drizzle。 如果您使用 `drizzle-orm/node-postgres`,构建 `db` 自己传过来:

```ts
import { buildPostgresJobStore, queueSchema } from '@absolutejs/queue-postgres/drizzle';
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const db = drizzle(pool, { schema: queueSchema });
const store = buildPostgresJobStore(db, jobs);
```

### 移徙

在您的 Drizzle 计划中加入表格, 因此它包含在迁移中 :

```ts
export { queueJobsTable } from '@absolutejs/queue-postgres/drizzle';
```

礛 `drizzle-kit generate` 编号 : `push` 和往常一样.

## 如何要求工作

`claimDue` 在交易内运行 :

```sql
SELECT … FROM queue_jobs
WHERE status = 'pending' AND run_at <= $now
ORDER BY run_at LIMIT $n
FOR UPDATE SKIP LOCKED;        -- concurrent workers skip locked rows
-- then UPDATE those ids → status='claimed'
```

这保证了将一份工作交给一名工人,即使进行了许多工人投票. 倒闭工人由 `reapStuck` (租赁期满) `pending`) (中文(简体) ).

## 许可证

阿帕奇-2.0 — 见 [自由主义](https://npmcn.com/package/%40absolutejs/queue-postgres/LICENSE)。 。 。 .

## 原子能围栏

适配器现在指定 `claim_token` 与每项索赔和执行 `completeClaim` 编号 : `failClaim` 作为工作标识、符号和 `status = 'claimed'`。过期、被取消或被取代的尝试返回 `false` 不改变当前工作。 更新队列工人使用此能力 自动; `requireClaimFencing: true` 拒绝不兼容的商店.

应用此添加剂迁移 **在此之前** 启动更新的适配器(或生成) 从导出 `queueJobsTable` 使用您的应用程序的迁移工具 :

```sql
ALTER TABLE queue_jobs ADD COLUMN IF NOT EXISTS claim_token text;
```

推出期间排水老工人. 年长的二进制仍然没有栅栏 更新且无法提供此保证。 索赔围栏保护队列行; 处理器必须分别编写栅栏应用程序和外部副作用.

租期到期意味着一次尝试。 将耗尽的工作移到 `dead` 租期错误,所以工人的再生死亡无法避免 `maxAttempts`。 。 。 .

这个 `/drizzle` 切入点 (0.1.8+) 不装入可选的 `postgres` 或神经连接工厂。 与 Bun SQL 或已存在的 Drizzle 连接使用,包括 schema 生成. 现有的根出口仍然相容.

### 混合工人版本

用队列0.8.4,启用 `requireKindFiltering: true` 关于工人。 索赔为 仅限于适配器的工作定义和工人登记 在数据库应用其索赔限额之前,处理者。 空处理器索赔 没什么 在生产新产品之前,将每个工人升级到一个共享的表格上 工作类型;年长、没有过滤的工人仍然可以要求和拒绝这些工作.

## 常见问题

### @absolutejs/queue-postgres 是什么？

@absolutejs/queue 的 Postgres \(Drizzle\) 存储适配器 — postgres\.js \+ Neon 服务器无

### 如何安装 @absolutejs/queue-postgres？

在项目目录运行 npm install @absolutejs/queue-postgres，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：drizzle-orm（\>=1\.0\.0-rc\.4 \<2）、@absolutejs/queue（\>=0\.8\.4 \<0\.11\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @absolutejs/queue-postgres 支持 TypeScript 吗？

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

### @absolutejs/queue-postgres 支持 ESM 还是 CommonJS？

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

### @absolutejs/queue-postgres 可以商用吗？

@absolutejs/queue-postgres 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @absolutejs/queue-postgres 的下载量有多少？

根据 npm 官方下载统计，@absolutejs/queue-postgres 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,524 次，日均约 84 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @absolutejs/queue-postgres 还在维护吗？

最新版本 0\.1\.9 发布于 2026/10/26（12 天前）；至今共发布 17 个版本，其中过去 12 个月发布了 17 个；首次发布于 2026/05/23。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

