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 nameC extends string- Union of column names that have been added
Methods
temporary
Adds the “temporary” modifier to create a temporary table.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.The on commit action (e.g.,
preserve rows, delete rows, drop)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.A new builder with the if not exists modifier
addColumn
Adds a column to the table.The name of the column
The data type of the column (e.g.,
integer, varchar(50), text)Optional callback to configure the column definition
A new builder with the column added
Examples
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.The name of the constraint
The columns to include in the primary key
Optional callback to configure the constraint
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.The name of the constraint
The columns to include in the unique constraint
Optional callback to configure the constraint
A new builder with the constraint added
Examples
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.The name of the constraint
The check expression
Optional callback to configure the constraint
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.The name of the constraint
The columns in this table
The target table name
The columns in the target table
Optional callback to configure the constraint
A new builder with the constraint added
Examples
modifyFront
Adds any additional SQL to the front of the query after thecreate keyword.
The SQL expression to add
A new builder with the modifier added
Examples
modifyEnd
Adds any additional SQL to the end of the query.The SQL expression to add
A new builder with the modifier added
Examples
as
Creates a table from aSELECT query.
The SELECT query expression
A new builder with the SELECT query
Examples
$call
Calls the given function passingthis as the only argument.
A function that receives the builder and returns a value
The return value of the provided function
Examples
compile
Compiles the query to aCompiledQuery without executing it.
The compiled query object containing SQL and parameters
execute
Executes the query.A promise that resolves when the table is created