滴入后闪光SQL 用户
一个TypeScript库,为Drizzle ORM应用程序中PostgreSQL JSONB数据和Temperal类型的工作提供类型安全的公用设备.
快速启动
安装
npm install @denny-il/drizzle-pg-utils
查询示例( 选择 + 更新)
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))
特征
JSON 水电
- 访问嵌入 JSONB 路径
json(source)和完整的脚本推论. - 查询JSONB封装有打出路径的横道和全列索引方便SQL.
- 解析更新深分支
.$set(...), (中文(简体) ),.$default(...), (中文(简体) ),.$push(...), (中文(简体) ),.$merge(...),以及.$pipe(...)。 。 。 . - 构建,合并,并用已键入的 SQL 帮助器对数组进行修改.
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))
临时公用事业
- 将 PostgreSQL 日期和时间列直接映射到 Temperal 值 .
- 选择三个平等的切入点:
/temporal, (中文(简体) ),/temporal/global,或/temporal/polyfill。 。 。 . - 为
yearMonth财务报告和审定财务报表monthDay文本栏.
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')),
)
见 docs/temporal.md 用于设置选择,运行时间行为,驱动笔记,以及示例.
文档
代理技能
该存储库包括图书馆-消费者使用实例和排除故障的代理技能:
- 细雨- pg- utils - JSONB和临时助手指导,提供以查询为重点的实例
许可证
麻省理工学院执照 - 看 利比里亚 详细情况.