Skip to main content
The CreateTableBuilder class is used to construct CREATE TABLE queries. It provides a fluent API for defining columns, constraints, and table properties.

Type Parameters

  • TB extends string - The table name
  • C extends string - Union of column names that have been added

Methods

temporary

Adds the “temporary” modifier to create a temporary table.
CreateTableBuilder<TB, C>
A new builder with the temporary modifier

onCommit

Adds an “on commit” statement. This can be used in conjunction with temporary tables on supported databases like PostgreSQL.
OnCommitAction
required
The on commit action (e.g., preserve rows, delete rows, drop)
CreateTableBuilder<TB, C>
A new builder with the on commit action

ifNotExists

Adds the “if not exists” modifier. If the table already exists, no error is thrown when this method has been called.
CreateTableBuilder<TB, C>
A new builder with the if not exists modifier

addColumn

Adds a column to the table.
string
required
The name of the column
DataTypeExpression
required
The data type of the column (e.g., integer, varchar(50), text)
ColumnBuilderCallback
Optional callback to configure the column definition
CreateTableBuilder<TB, C | CN>
A new builder with the column added

Examples

For databases that don’t support foreign key constraints in column definitions (like MySQL 5.X):
PostgreSQL auto-incrementing column:

addPrimaryKeyConstraint

Adds a primary key constraint for one or more columns. The constraint name can be anything you want, but it must be unique across the whole database.
string
required
The name of the constraint
C[]
required
The columns to include in the primary key
PrimaryKeyConstraintBuilderCallback
Optional callback to configure the constraint
CreateTableBuilder<TB, C>
A new builder with the constraint added

Examples

addUniqueConstraint

Adds a unique constraint for one or more columns. The constraint name can be anything you want, but it must be unique across the whole database.
string
required
The name of the constraint
C[]
required
The columns to include in the unique constraint
UniqueConstraintNodeBuilderCallback
Optional callback to configure the constraint
CreateTableBuilder<TB, C>
A new builder with the constraint added

Examples

PostgreSQL with nulls not distinct:

addCheckConstraint

Adds a check constraint. The constraint name can be anything you want, but it must be unique across the whole database.
string
required
The name of the constraint
Expression<any>
required
The check expression
CheckConstraintBuilderCallback
Optional callback to configure the constraint
CreateTableBuilder<TB, C>
A new builder with the constraint added

Examples

addForeignKeyConstraint

Adds a foreign key constraint. The constraint name can be anything you want, but it must be unique across the whole database.
string
required
The name of the constraint
C[]
required
The columns in this table
string
required
The target table name
string[]
required
The columns in the target table
ForeignKeyConstraintBuilderCallback
Optional callback to configure the constraint
CreateTableBuilder<TB, C>
A new builder with the constraint added

Examples

Multiple columns:

modifyFront

Adds any additional SQL to the front of the query after the create keyword.
Expression<any>
required
The SQL expression to add
CreateTableBuilder<TB, C>
A new builder with the modifier added

Examples

Generated SQL (PostgreSQL):

modifyEnd

Adds any additional SQL to the end of the query.
Expression<any>
required
The SQL expression to add
CreateTableBuilder<TB, C>
A new builder with the modifier added

Examples

Generated SQL (MySQL):

as

Creates a table from a SELECT query.
Expression<unknown>
required
The SELECT query expression
CreateTableBuilder<TB, C>
A new builder with the SELECT query

Examples

Generated SQL (PostgreSQL):

$call

Calls the given function passing this as the only argument.
(qb: this) => T
required
A function that receives the builder and returns a value
T
The return value of the provided function

Examples

Creating reusable functions:

compile

Compiles the query to a CompiledQuery without executing it.
CompiledQuery
The compiled query object containing SQL and parameters

execute

Executes the query.
Promise<void>
A promise that resolves when the table is created