Postgres Enums

Postgres AST Enum Definitions

TypeScript enum definitions for Postgres AST nodes, providing type-safe usage of Postgres enums and constants. Essential for safe and ergonomic parsing logic with comprehensive multi-version support.

Type-Safe Enums

Complete TypeScript enum definitions for Postgres

Multi-Version Support

Supports Postgres versions 13 through 18

AST Integration

Designed for use with pgsql-parser and AST manipulation

Postgres Enums Features

Type-safe Postgres enum handling for robust AST manipulation

Complete Type Safety

Comprehensive TypeScript enum definitions for all Postgres constants, enabling type-safe usage throughout your application.

Multi-Version Postgres Support

Supports Postgres versions 13 through 18 with version-specific enum definitions and npm dist-tags.

AST Parser Integration

Designed specifically for use with pgsql-parser and Postgres AST manipulation, providing seamless integration.

Bidirectional Conversion

Convert between enum names and numeric values with full type safety and IntelliSense support.

Comprehensive Coverage

Covers all Postgres enum types including ObjectType, NodeTag, JoinType, and many more for complete AST support.

Zero Runtime Overhead

Pure TypeScript enums with no runtime dependencies, ensuring optimal performance and bundle size.

typescript
import { 
  ObjectType, 
  NodeTag, 
  JoinType, 
  SetOperation,
  SortByDir,
  LimitOption 
} from '@pgsql/enums';

// Basic enum usage for type-safe comparisons
function analyzeObjectType(objectType: number) {
  switch (objectType) {
    case ObjectType.OBJECT_TABLE:
      return 'This is a table object';
    case ObjectType.OBJECT_INDEX:
      return 'This is an index object';
    case ObjectType.OBJECT_FUNCTION:
      return 'This is a function object';
    default:
      return 'Unknown object type';
  }
}

// Get numeric value from enum
const tableValue = ObjectType.OBJECT_TABLE;
console.log('Table object type value:', tableValue); // 41

// Convert from value back to enum name
const enumName = ObjectType[41];
console.log('Enum name for value 41:', enumName); // "OBJECT_TABLE"

Basic Enum Usage

Use Postgres enums for type-safe AST manipulation with automatic value conversion and validation.

typescript
import { 
  NodeTag, 
  ObjectType, 
  JoinType, 
  A_Expr_Kind,
  BoolExprType 
} from '@pgsql/enums';

// Type-safe AST node checking
function isSelectStatement(node: any): boolean {
  return node.nodeTag === NodeTag.T_SelectStmt;
}

function getJoinTypeName(joinType: number): string {
  switch (joinType) {
    case JoinType.JOIN_INNER:
      return 'INNER JOIN';
    case JoinType.JOIN_LEFT:
      return 'LEFT JOIN';
    case JoinType.JOIN_RIGHT:
      return 'RIGHT JOIN';
    case JoinType.JOIN_FULL:
      return 'FULL JOIN';
    default:
      return 'UNKNOWN JOIN';
  }
}

// Expression type checking
function analyzeExpression(expr: any) {
  if (expr.kind === A_Expr_Kind.AEXPR_OP) {
    return { type: 'operator', operator: expr.name[0].String.sval };
  }
  return { type: 'unknown' };
}

AST Node Type Checking

Leverage enums for robust AST node type checking and validation in Postgres query analysis.

typescript
import { ObjectType } from '@pgsql/enums';

// Version-specific enum handling
class PostgresVersionHandler {
  constructor(private version: number) {}

  getObjectTypeName(objectType: number): string {
    // Handle version-specific enum differences
    if (this.version >= 17) {
      return this.getV17ObjectTypeName(objectType);
    } else if (this.version >= 16) {
      return this.getV16ObjectTypeName(objectType);
    }
    return this.getLegacyObjectTypeName(objectType);
  }

  private getV17ObjectTypeName(objectType: number): string {
    return ObjectType[objectType] || 'UNKNOWN';
  }

  private getV16ObjectTypeName(objectType: number): string {
    // Handle PG16 specific mappings
    return ObjectType[objectType] || 'UNKNOWN';
  }

  private getLegacyObjectTypeName(objectType: number): string {
    // Handle legacy versions
    return ObjectType[objectType] || 'UNKNOWN';
  }
}

// Usage
const handler = new PostgresVersionHandler(17);
console.log(handler.getObjectTypeName(ObjectType.OBJECT_TABLE));

Multi-Version Enum Handling

Handle different Postgres versions with version-specific enum definitions and compatibility layers.