Shared PostgreSQL functionality for the Drizzle Adapter ecosystem.
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:
- @drizzle-adapter/node-postgres
- @drizzle-adapter/postgres-js
- @drizzle-adapter/neon-http
- @drizzle-adapter/pglite
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
# This package is typically not installed directly but as a dependency of PostgreSQL adapters
pnpm install @drizzle-adapter/pg-core
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
}
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' })
});
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);
}
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'
});
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);
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);
This package is maintained as part of the Drizzle Adapter ecosystem. If you'd like to contribute:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- @drizzle-adapter/core - Core interfaces and types
- @drizzle-adapter/node-postgres - PostgreSQL adapter using node-postgres
- @drizzle-adapter/postgres-js - PostgreSQL adapter using postgres.js
- @drizzle-adapter/neon-http - Neon serverless PostgreSQL adapter
- @drizzle-adapter/pglite - Lightweight PostgreSQL adapter