@drizzle-adapter/pg-core

中文文档
1.1.1发布于 2026/03/26MITTypeScriptES Module近 30 天下载 2万

Shared PostgreSQL functionality for the Drizzle Adapter ecosystem.

原始使用文档
文档目录11 节
  1. Overview
  2. Purpose
  3. Installation
  4. For Adapter Developers
  5. Data Types
  6. Error Handling
  7. Query Building
  8. Type Mapping
  9. PostgreSQL-Specific Features
  10. Contributing
  11. Related Packages

@drizzle-adapter/pg-core

Shared PostgreSQL functionality for the Drizzle Adapter ecosystem.

Overview

The @drizzle-adapter/pg-core package provides shared PostgreSQL functionality used by PostgreSQL-compatible adapters in the Drizzle Adapter ecosystem. This package is an internal dependency used by:

Purpose

This package standardizes PostgreSQL-specific functionality across different PostgreSQL-compatible adapters:

  • Common PostgreSQL data type definitions
  • Shared PostgreSQL query building logic
  • Unified PostgreSQL error handling
  • Common PostgreSQL utility functions

Installation

# This package is typically not installed directly but as a dependency of PostgreSQL adapters
pnpm install @drizzle-adapter/pg-core

For Adapter Developers

If you're developing a new PostgreSQL-compatible adapter for the Drizzle Adapter ecosystem, you should use this package to ensure consistency with other PostgreSQL adapters:

import { PostgresDrizzleDataTypes } from '@drizzle-adapter/pg-core';

export class YourPostgresAdapter implements DrizzleAdapterInterface {
  public getDataTypes(): PostgresDrizzleDataTypes {
    return new PostgresDrizzleDataTypes();
  }
  
  // ... rest of your adapter implementation
}

Data Types

The package provides standardized PostgreSQL data type definitions:

const dataTypes = new PostgresDrizzleDataTypes();

const table = dataTypes.dbTable('example', {
  // Numeric types
  id: dataTypes.dbSerial('id').primaryKey(),
  count: dataTypes.dbInteger('count'),
  price: dataTypes.dbDecimal('price', { precision: 10, scale: 2 }),
  
  // String types
  name: dataTypes.dbText('name'),
  description: dataTypes.dbText('description'),
  
  // Date/Time types
  createdAt: dataTypes.dbTimestampTz('created_at').defaultNow(),
  updatedAt: dataTypes.dbTimestampTz('updated_at').onUpdateNow(),
  
  // PostgreSQL-specific types
  tags: dataTypes.dbArray('tags', { type: 'text' }),
  metadata: dataTypes.dbJsonb('metadata'),
  searchVector: dataTypes.dbTsVector('search_vector'),
  status: dataTypes.dbEnum('status', ['active', 'inactive']),
  point: dataTypes.dbPoint('location'),
  range: dataTypes.dbRange('valid_period', { type: 'timestamp' })
});

Error Handling

The package provides unified error handling for PostgreSQL-specific errors:

import { handlePostgresError } from '@drizzle-adapter/pg-core';

try {
  // Your database operation
} catch (error) {
  throw handlePostgresError(error);
}

Query Building

The package includes utilities for building PostgreSQL-specific queries:

import { 
  buildInsertQuery,
  buildUpdateQuery,
  buildDeleteQuery,
  buildSelectQuery,
  buildFullTextSearchQuery
} from '@drizzle-adapter/pg-core';

// Build INSERT query
const insertQuery = buildInsertQuery(table, data);

// Build UPDATE query with JSONB operations
const updateQuery = buildUpdateQuery(table, {
  metadata: sql`${table.metadata} || '{"key": "value"}'::jsonb`
});

// Build DELETE query with conditions
const deleteQuery = buildDeleteQuery(table, conditions);

// Build SELECT query with joins
const selectQuery = buildSelectQuery({
  table,
  joins: [...],
  conditions: [...],
  orderBy: [...]
});

// Build full-text search query
const searchQuery = buildFullTextSearchQuery({
  table,
  columns: ['title', 'content'],
  searchTerm: 'search term',
  language: 'english'
});

Type Mapping

The package handles PostgreSQL-specific type mapping:

import { mapPostgresType } from '@drizzle-adapter/pg-core';

// Map JavaScript types to PostgreSQL types
const pgType = mapPostgresType(value);

// Map PostgreSQL types to JavaScript types
const jsValue = mapToJavaScript(pgValue, pgType);

// Handle array types
const arrayType = mapArrayType(elementType);

// Handle range types
const rangeType = mapRangeType(boundType);

PostgreSQL-Specific Features

import { 
  createTsVector,
  createGinIndex,
  handleArrayOperations,
  handleJsonbOperations
} from '@drizzle-adapter/pg-core';

// Create tsvector column
const tsVectorSQL = createTsVector(['title', 'content'], 'english');

// Create GIN index for full-text search
const indexSQL = createGinIndex('posts_search_idx', 'posts', 'search_vector');

// Handle array operations
const arrayCondition = handleArrayOperations(column, operator, values);

// Handle JSONB operations
const jsonbCondition = handleJsonbOperations(column, path, value);

Contributing

This package is maintained as part of the Drizzle Adapter ecosystem. If you'd like to contribute:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

版本与发布记录

@drizzle-adapter/pg-core 共发布 12 个版本,首次发布于 2024/12/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.1.1latest2026/03/267 个月前
1.0.152025/07/271.3 年前
1.0.142025/07/171.3 年前
1.0.122025/07/121.3 年前
1.0.112025/07/121.3 年前
1.0.92025/04/201.6 年前

关于 @drizzle-adapter/pg-core 的常见问题

@drizzle-adapter/pg-core 是什么?

Shared PostgreSQL functionality for the Drizzle Adapter ecosystem.

如何安装 @drizzle-adapter/pg-core?

在项目目录运行 npm install @drizzle-adapter/pg-core,或使用 pnpm add / yarn add / bun add。

@drizzle-adapter/pg-core 支持 TypeScript 吗?

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

@drizzle-adapter/pg-core 支持 ESM 还是 CommonJS?

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

@drizzle-adapter/pg-core 可以商用吗?

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

@drizzle-adapter/pg-core 的下载量有多少?

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

@drizzle-adapter/pg-core 还在维护吗?

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