Skip to main content

Schema Utilities

getTableConfig()

Extracts complete table configuration including columns, indexes, and constraints.
PgTable
required
The table to extract configuration from
PgColumn[]
Array of column objects
Index[]
Array of index definitions
ForeignKey[]
Array of foreign key constraints
Check[]
Array of check constraints
PrimaryKey[]
Array of primary key definitions
UniqueConstraint[]
Array of unique constraints
string
Table name
string | undefined
Schema name (undefined for public schema)
PgPolicy[]
Array of Row Level Security policies
boolean
Whether Row Level Security is enabled

getViewConfig()

Extracts view configuration.
PgView
required
The view to extract configuration from

getMaterializedViewConfig()

Extracts materialized view configuration.
PgMaterializedView
required
The materialized view to extract configuration from

Constraint Builders

index()

Creates an index definition.
string
required
Index name
Methods:
  • .on(...columns): Specify columns to index
  • .using(method, ...columns): Specify index method (btree, hash, gist, gin, etc.)
  • .where(condition): Create partial index
  • .asc() / .desc(): Sort order (for btree indexes)
  • .nullsFirst() / .nullsLast(): NULL ordering

uniqueIndex()

Creates a unique index.
Same API as index() but creates a unique index.

primaryKey()

Defines a primary key constraint (for composite keys).
PgColumn[]
required
Columns that form the primary key
string
Constraint name

foreignKey()

Defines a foreign key constraint.
PgColumn[]
required
Local columns
PgColumn[]
required
Referenced columns in foreign table
string
Constraint name
Methods:
  • .onDelete(action): 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action'
  • .onUpdate(action): Same options as onDelete

unique()

Defines a unique constraint.
string
Constraint name
Methods:
  • .on(...columns): Columns that must be unique together

check()

Defines a check constraint.
string
required
Constraint name
SQL
required
SQL condition that must be true

Row Level Security

pgPolicy()

Defines a Row Level Security policy.
string
required
Policy name
'all' | 'select' | 'insert' | 'update' | 'delete'
Which operations the policy applies to
PgRole | PgRole[]
Which roles the policy applies to
SQL
USING clause (which rows are visible)
SQL
WITH CHECK clause (which rows can be modified)

pgRole()

Defines a database role for use with RLS.
string
required
Role name in the database

Sequences

pgSequence()

Defines a PostgreSQL sequence.
string
required
Sequence name
object
Sequence configuration:
  • startWith: Starting value
  • increment: Increment amount
  • minValue: Minimum value
  • maxValue: Maximum value
  • cache: Number of values to cache
  • cycle: Whether to cycle when reaching limits

Views

pgView()

Defines a PostgreSQL view.
string
required
View name
Methods:
  • .as(query): Define the view’s query
  • .existing(): Reference an existing view without creating it

pgMaterializedView()

Defines a PostgreSQL materialized view.
string
required
Materialized view name
Methods:
  • .as(query): Define the view’s query
  • .existing(): Reference an existing materialized view

Array Utilities

arrayOverlaps()

Checks if PostgreSQL arrays have overlapping elements.

arrayContains()

Checks if a PostgreSQL array contains all specified elements.

arrayContained()

Checks if a PostgreSQL array is contained by another array.

Type Inference Helpers

InferSelectModel

Infers the TypeScript type for selected rows.

InferInsertModel

Infers the TypeScript type for insert operations.

Migration Utilities

sql.empty()

Creates an empty SQL object.

sql.join()

Joins multiple SQL fragments.

sql.raw()

Creates SQL from a raw string (use with caution - no escaping).
Warning: sql.raw() does not escape values. Use parameterized queries instead: