# @drizzle-adapter/postgres-js 中文文档

规范地址：https://npmcn.com/package/%40drizzle-adapter/postgres-js/

版本：1\.1\.1
npm 元数据更新时间：2026-03-25T23:49:52.635Z
中文译文更新时间：2026-11-12T20:17:42.729Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40drizzle-adapter/postgres-js
原文对照：https://npmcn.com/package/%40drizzle-adapter/postgres-js/?lang=original

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

- 包体积（解压后）：57.9 kB，50 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：3 个
- peer 依赖：postgres ^3\.4\.7
- 首次发布：2024-12-03T05:42:49.722Z
- 最新版本发布：2026-03-25T23:49:52.497Z
- 近 30 天下载量：17964（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：11（近 12 个月 1 个）

## 中文简介

PostgreSQL 应用postgres.js驱动程序执行 Drizzle 适配器生态系统.

## 安装

```sh
npm install @drizzle-adapter/postgres-js
```

也可以使用：`pnpm add @drizzle-adapter/postgres-js`、`yarn add @drizzle-adapter/postgres-js`、`bun add @drizzle-adapter/postgres-js`

## README

# @ 细雨适应器/ postgres-js

PostgreSQL 应用postgres.js驱动程序执行 Drizzle 适配器生态系统.

## 概览

这个 `@drizzle-adapter/postgres-js` 软件包为 Drizzle 适配器接口提供 PostgreSQL 执行,使用现代和轻量级 `postgres.js` 驾驶员。 虽然你不直接与此软件包互动( 您使用 `@drizzle-adapter/core` 相反,它使PostgreSQL能够在Drizzle适应器生态系统中提供支持.

## 为什么是邮递员?

Postgres.js 驱动程序提供了独特的优势:

-   **无依赖**: 无外部依赖关系的最小足迹
-   **现代设计**: 干净、基于承诺的API,没有遗留行李
-   **本地类型**: 第一等类型脚本支持
-   **内建池**: 高效的连接管理
-   **发言稿**: 自动编制报表
-   **业绩**: 为高吞吐量而优化
-   **轻巧**:小捆绑大小,适合无服务器
-   **简单 API**: 无复杂程度的直观界面

## 安装

```bash
# Install both the core package and the PostgreSQL adapter
pnpm install @drizzle-adapter/core @drizzle-adapter/postgres-js
```

## 重要:适配器注册

适配器要正确使用 DrizzleAdapterFactory 操作,必须导入它进行自我注册的副作用:

```typescript
// Import for side effects - adapter will self-register
import '@drizzle-adapter/postgres-js';

// Now you can use the factory
import { DrizzleAdapterFactory } from '@drizzle-adapter/core';
```

## 使用量

### 配置

```typescript
import { DrizzleAdapterFactory, TypeDrizzleDatabaseConfig } from '@drizzle-adapter/core';

const config: TypeDrizzleDatabaseConfig = {
  DATABASE_DRIVER: 'postgres-js',
  DATABASE_URL: 'postgresql://user:password@localhost:5432/database'
};

const factory = new DrizzleAdapterFactory();
const adapter = factory.create(config);
```

### Schema 定义

```typescript
const dataTypes = adapter.getDataTypes();

const users = dataTypes.dbTable('users', {
  id: dataTypes.dbSerial('id').primaryKey(),
  name: dataTypes.dbText('name').notNull(),
  email: dataTypes.dbText('email').notNull().unique(),
  profile: dataTypes.dbJsonb('profile'),
  searchVector: dataTypes.dbTsVector('search_vector'),
  createdAt: dataTypes.dbTimestampTz('created_at').defaultNow()
});

const posts = dataTypes.dbTable('posts', {
  id: dataTypes.dbSerial('id').primaryKey(),
  userId: dataTypes.dbInteger('user_id')
    .references(() => users.id),
  title: dataTypes.dbText('title').notNull(),
  content: dataTypes.dbText('content').notNull(),
  tags: dataTypes.dbArray('tags', { type: 'text' }),
  metadata: dataTypes.dbJsonb('metadata'),
  published: dataTypes.dbBoolean('published').default(false),
  createdAt: dataTypes.dbTimestampTz('created_at').defaultNow()
});
```

### 基本 CRUD 操作

```typescript
import { eq, and, or, desc, sql } from 'drizzle-orm';

const connection = await adapter.getConnection();
const client = connection.getClient();

try {
  // INSERT
  // Single insert with returning
  const [newUser] = await client
    .insert(users)
    .values({
      name: 'John Doe',
      email: 'john@example.com',
      profile: { bio: 'Hello!' }
    })
    .returning();

  // Bulk insert
  await client
    .insert(posts)
    .values([
      {
        userId: newUser.id,
        title: 'First Post',
        content: 'Hello, world!',
        tags: ['hello', 'first']
      },
      {
        userId: newUser.id,
        title: 'Second Post',
        content: 'Another post',
        tags: ['second']
      }
    ]);

  // SELECT
  // Select all
  const allUsers = await client
    .select()
    .from(users);

  // Select with conditions
  const user = await client
    .select()
    .from(users)
    .where(eq(users.email, 'john@example.com'));

  // Select with join and array operations
  const postsWithTags = await client
    .select({
      userName: users.name,
      postTitle: posts.title,
      tags: posts.tags
    })
    .from(posts)
    .leftJoin(users, eq(posts.userId, users.id))
    .where(sql`${posts.tags} && ARRAY['hello']::text[]`)
    .orderBy(desc(posts.createdAt));

  // Full-text search
  const searchResults = await client
    .select()
    .from(posts)
    .where(sql`to_tsvector('english', ${posts.title} || ' ' || ${posts.content}) @@ to_tsquery('english', 'hello')`)
    .orderBy(desc(posts.createdAt));

  // UPDATE
  // Update with JSONB operations
  await client
    .update(users)
    .set({
      profile: sql`${users.profile} || '{"title": "New Title"}'::jsonb`
    })
    .where(eq(users.id, newUser.id));

  // DELETE
  await client
    .delete(posts)
    .where(
      and(
        eq(posts.userId, newUser.id),
        eq(posts.published, false)
      )
    );

} finally {
  await connection.disconnect();
}
```

### 高级特性

#### 交易支持

```typescript
const connection = await adapter.getConnection();
const client = connection.getClient();

try {
  await client.transaction(async (tx) => {
    const [user] = await tx
      .insert(users)
      .values({
        name: 'John Doe',
        email: 'john@example.com'
      })
      .returning();

    await tx
      .insert(posts)
      .values({
        userId: user.id,
        title: 'My First Post',
        content: 'Hello, world!'
      });
  });
} finally {
  await connection.disconnect();
}
```

#### 流出结果

postgres.js 支持大型结果集的高效流 :

```typescript
const stream = client
  .select()
  .from(posts)
  .where(eq(posts.published, true))
  .stream();

for await (const post of stream) {
  // Process each post
  console.log(post.title);
}
```

## 最佳做法

1.  **连接管理**: 总是尝试/最后使用以确保连接被关闭
2.  **交易**: 相关业务使用交易
3.  **发言稿**: 自动准备发言
4.  **处理错误**: 执行适当的错误处理
5.  **查询优化**: 使用索引和适当的查询规划

## 捐款

我们欢迎捐款! 请随意提交拉力请求 对于重大变动,请先开出一个问题来讨论你想要改变什么.

## 相关软件包

-   [@ 细雨适应器/ 核心](https://npmcn.com/package/%40drizzle-adapter/core) - 核心接口和类型
-   [@ 细雨适应器/ pg- core](https://npmcn.com/package/%40drizzle-adapter/pg-core) - 共享 PostgreSQL 功能
-   [@ 细雨- 适应器/ 节点- 张贴](https://npmcn.com/package/%40drizzle-adapter/node-postgres) - 替代PostgreSQL适配器

## 常见问题

### @drizzle-adapter/postgres-js 是什么？

PostgreSQL 应用postgres\.js驱动程序执行 Drizzle 适配器生态系统\.

### 如何安装 @drizzle-adapter/postgres-js？

在项目目录运行 npm install @drizzle-adapter/postgres-js，或使用 pnpm add / yarn add / bun add。

### @drizzle-adapter/postgres-js 支持 TypeScript 吗？

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

### @drizzle-adapter/postgres-js 支持 ESM 还是 CommonJS？

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

### @drizzle-adapter/postgres-js 可以商用吗？

@drizzle-adapter/postgres-js 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @drizzle-adapter/postgres-js 的下载量有多少？

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

### @drizzle-adapter/postgres-js 还在维护吗？

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

