Skip to content

Schema Types

Type definitions for the schema objects used throughout pg-schemata. These interfaces are defined in src/schemaTypes.d.ts and provide IntelliSense in editors.

TableSchema

The top-level schema definition object.

ts
interface TableSchema {
  dbSchema: string;
  table: string;
  hasAuditFields?: boolean | AuditFieldsConfig;
  softDelete?: boolean;
  version?: string;
  columns: ColumnDefinition[];
  constraints?: Constraints;
}
PropertyTypeRequiredDescription
dbSchemastringYesPostgreSQL schema name (e.g. 'public')
tablestringYesTable name
columnsColumnDefinition[]YesArray of column definitions
constraintsConstraintsNoTable-level constraints
hasAuditFieldsboolean | AuditFieldsConfigNoEnable audit tracking columns
softDeletebooleanNoEnable soft delete via deactivated_at
versionstringNoSchema version string

ColumnDefinition

Defines the structure of a single column.

ts
interface ColumnDefinition {
  name: string;
  type: string;
  generated?: 'always' | 'by default';
  expression?: string;
  stored?: boolean;
  notNull?: boolean;
  default?: any;
  immutable?: boolean;
  colProps?: {
    mod?: string;
    skip?: (col: any) => boolean;
    cnd?: boolean;
    init?: (dto: any) => any;
    def?: string;
    validator?: any;
  };
}
PropertyTypeDescription
namestringColumn name
typestringPostgreSQL data type
generated'always' | 'by default'Generated column mode
expressionstringSQL expression for generated columns
storedbooleanWhether generated column is stored
notNullbooleanReject null values
defaultanyDefault value (SQL expression as string)
immutablebooleanExclude from update operations
colPropsobjectpg-promise column behavior modifiers

colProps detail

PropertyTypeDescription
modstringpg-promise format modifier (e.g. ':json')
skip(col) => booleanSkip column conditionally
cndbooleanUse in conditional update clause
init(dto) => anyCompute value dynamically
defstringOverride default value in ColumnSet
validatorZodSchemaCustom Zod validator for this column

Constraints

Container for all table-level constraints.

ts
interface Constraints {
  primaryKey?: string[];
  unique?: (string[] | UniqueConstraintDefinition)[];
  foreignKeys?: ConstraintDefinition[];
  checks?: ConstraintDefinition[];
  indexes?: ConstraintDefinition[];
}

ConstraintDefinition

A single constraint definition.

ts
interface ConstraintDefinition {
  type: 'PrimaryKey' | 'ForeignKey' | 'Unique' | 'Check' | 'Index';
  columns: string[];
  references?: { table: string; columns: string[] };
  onDelete?: string;
  expression?: string;
}

UniqueConstraintDefinition

Unique constraint with optional PostgreSQL 15+ modifiers.

ts
interface UniqueConstraintDefinition {
  columns: string[];
  nullsNotDistinct?: boolean;
  name?: string;
}
PropertyTypeDescription
columnsstring[]Column names for the unique constraint
nullsNotDistinctbooleanTreat NULLs as equal for uniqueness (PostgreSQL 15+)
namestringCustom constraint name (auto-generated if omitted)

AuditFieldsConfig

Configuration for audit fields in object format.

ts
interface AuditFieldsConfig {
  enabled: boolean;
  userFields?: {
    type?: string;
    nullable?: boolean;
    default?: any;
  };
}
PropertyTypeDefaultDescription
enabledbooleanWhether to include audit fields
userFields.typestring'varchar(50)'PostgreSQL type for created_by / updated_by
userFields.nullablebooleantrueAllow null for user fields
userFields.defaultanynullDefault value for user fields

A lightweight Postgres-first ORM layer.