@drizzle-adapter/pg-core
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

@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

Related Packages

Readme

Keywords

none

Package Sidebar

Install

npm i @drizzle-adapter/pg-core

Weekly Downloads

7

Version

1.0.9

License

none

Unpacked Size

46.6 kB

Total Files

16

Last publish

Collaborators

  • dmitryrechkin