@denny-il/drizzle-pg-utils

中文文档
0.8.2发布于 2026/10/22MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 7.5万

Set of utilities for Drizzle ORM and PostgreSQL

原始使用文档
文档目录9 节
  1. Quick Start
  2. Installation
  3. Query Example (Select + Update)
  4. Features
  5. JSON Utilities
  6. Temporal Utilities
  7. Documentation
  8. Agent Skills
  9. License

Drizzle PostgreSQL Utils

A TypeScript library providing type-safe utilities for working with PostgreSQL JSONB data and Temporal types in Drizzle ORM applications.

Quick Start

Installation

npm install @denny-il/drizzle-pg-utils

Query Example (Select + Update)

import { json } from '@denny-il/drizzle-pg-utils'
import { eq, gte } from 'drizzle-orm'
import { jsonb, pgTable, serial, text } from 'drizzle-orm/pg-core'

type Profile = {
  user: {
    name: string
    preferences?: { theme: 'light' | 'dark'; tags?: string[] }
  }
}

const users = pgTable('users', {
  id: serial('id').primaryKey(),
  email: text('email').notNull(),
  profile: jsonb('profile').$type<Profile>().notNull(),
})

const profile = json(users.profile)

const [row] = await db
  .select({
    id: users.id,
    // This now works without having any runtime schemas,
    // and is fully type-safe
    theme: profile.user.preferences.theme.$value,
  })
  .from(users)
  .where(eq(profile.user.preferences.theme.$value, 'dark'))

await db
  .update(users)
  .set({
    // Update specific paths in JSONB column atomically in a single query,
    // without merging the entire object in application code.
    profile: json(users.profile).$pipe(
      // Initialize the optional branch before writing inside it.
      (s) => s.user.preferences.$default({ theme: 'light', tags: [] }),
      // Set theme to 'dark'.
      (s) => s.user.preferences.theme.$set('dark'),
      // Set first value in tags array to 'intro'.
      (s) => s.user.preferences.tags['0'].$set('intro'),
    ),
  })
  .where(eq(users.id, row!.id))

Features

JSON Utilities

  • Access nested JSONB paths with json(source) and full TypeScript inference.
  • Query JSONB containment with typed path traversal and full-column index-friendly SQL.
  • Update deep branches atomically with .$set(...), .$default(...), .$push(...), .$merge(...), and .$pipe(...).
  • Build, merge, coalesce, and modify arrays with typed SQL helpers.
import { json } from '@denny-il/drizzle-pg-utils'
import { eq } from 'drizzle-orm'

// Access nested properties with type safety
const profile = json(users.profile)
const rows = await db
  .select({
    id: users.id,
    theme: profile.user.preferences.theme.$value,
  })
  .from(users)
  .where(eq(profile.user.preferences.theme.$value, 'dark'))

// Query JSONB containment while keeping the predicate rooted at users.profile
const darkUsers = await db
  .select({ id: users.id })
  .from(users)
  .where(profile.user.preferences.$contains({ theme: 'dark' }))

// Update values at specific paths
await db
  .update(users)
  .set({
    profile: profile.user.name.$set('New Name'),
  })
  .where(eq(users.id, rows[0]!.id))

Temporal Utilities

  • Map PostgreSQL date and time columns directly to Temporal values.
  • Choose any of the three equal entrypoints: /temporal, /temporal/global, or /temporal/polyfill.
  • Add optional database constraints for yearMonth and monthDay text columns.
import {
  timestamp,
  timestampz,
} from '@denny-il/drizzle-pg-utils/temporal/global'

const events = pgTable('events', {
  id: serial('id').primaryKey(),
  scheduledAt: timestamp.column('scheduled_at'),
  createdAt: timestampz.column('created_at'),
})

await db.insert(events).values({
  scheduledAt: Temporal.PlainDateTime.from('2026-03-15T09:30:00'),
  createdAt: Temporal.Instant.from('2026-03-15T08:30:00Z'),
})

const upcoming = await db
  .select({ scheduledAt: events.scheduledAt })
  .from(events)
  .where(
    gte(events.scheduledAt, Temporal.PlainDateTime.from('2026-03-01T00:00:00')),
  )

See docs/temporal.md for setup choices, runtime behavior, driver notes, and examples.

Documentation

Agent Skills

This repository includes an agent skill for library-consumer usage examples and troubleshooting:

  • drizzle-pg-utils - JSONB and Temporal helper guidance with query-focused examples

License

MIT License - see LICENSE.md for details.

版本与发布记录

@denny-il/drizzle-pg-utils 共发布 22 个版本,首次发布于 2025/07/25;过去 12 个月发布了 13 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.2latest2026/10/2220 天前
0.8.12026/10/2221 天前
0.8.02026/10/2221 天前
0.7.02026/07/044 个月前
0.6.02026/06/194 个月前
0.5.02026/05/016 个月前

其他发布通道:beta → 0.5.0-beta.8。

关于 @denny-il/drizzle-pg-utils 的常见问题

@denny-il/drizzle-pg-utils 是什么?

Set of utilities for Drizzle ORM and PostgreSQL

如何安装 @denny-il/drizzle-pg-utils?

在项目目录运行 npm install @denny-il/drizzle-pg-utils,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:drizzle-orm(1.0.0-rc.5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@denny-il/drizzle-pg-utils 支持 TypeScript 吗?

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

@denny-il/drizzle-pg-utils 支持 ESM 还是 CommonJS?

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

@denny-il/drizzle-pg-utils 可以商用吗?

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

@denny-il/drizzle-pg-utils 的下载量有多少?

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

@denny-il/drizzle-pg-utils 还在维护吗?

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