kysely
Version:
Type safe SQL query builder
253 lines (252 loc) • 8 kB
TypeScript
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
import { OnModifyForeignAction } from '../operation-node/references-node.js';
import { ColumnDefinitionNode } from '../operation-node/column-definition-node.js';
import { DefaultValueExpression } from '../parser/default-value-parser.js';
import { Expression } from '../expression/expression.js';
export declare class ColumnDefinitionBuilder implements OperationNodeSource {
#private;
constructor(node: ColumnDefinitionNode);
/**
* Adds `auto_increment` or `autoincrement` to the column definition
* depending on the dialect.
*
* Some dialects like PostgreSQL don't support this. On PostgreSQL
* you can use the `serial` or `bigserial` data type instead.
*/
autoIncrement(): ColumnDefinitionBuilder;
/**
* Makes the column an identity column.
*
* This only works on some dialects like MS SQL Server (MSSQL).
*
* For PostgreSQL's `generated always as identity` use {@link generatedAlwaysAsIdentity}.
*/
identity(): ColumnDefinitionBuilder;
/**
* Makes the column the primary key.
*
* If you want to specify a composite primary key use the
* {@link CreateTableBuilder.addPrimaryKeyConstraint} method.
*/
primaryKey(): ColumnDefinitionBuilder;
/**
* Adds a foreign key constraint for the column.
*
* If your database engine doesn't support foreign key constraints in the
* column definition (like MySQL 5) you need to call the table level
* {@link CreateTableBuilder.addForeignKeyConstraint} method instead.
*
* ### Examples
*
* ```ts
* col.references('person.id')
* ```
*/
references(ref: string): ColumnDefinitionBuilder;
/**
* Adds an `on delete` constraint for the foreign key column.
*
* If your database engine doesn't support foreign key constraints in the
* column definition (like MySQL 5) you need to call the table level
* {@link CreateTableBuilder.addForeignKeyConstraint} method instead.
*
* ### Examples
*
* ```ts
* col.references('person.id').onDelete('cascade')
* ```
*/
onDelete(onDelete: OnModifyForeignAction): ColumnDefinitionBuilder;
/**
* Adds an `on update` constraint for the foreign key column.
*
* ### Examples
*
* ```ts
* col.references('person.id').onUpdate('cascade')
* ```
*/
onUpdate(onUpdate: OnModifyForeignAction): ColumnDefinitionBuilder;
/**
* Adds a unique constraint for the column.
*/
unique(): ColumnDefinitionBuilder;
/**
* Adds a `not null` constraint for the column.
*/
notNull(): ColumnDefinitionBuilder;
/**
* Adds a `unsigned` modifier for the column.
*
* This only works on some dialects like MySQL.
*/
unsigned(): ColumnDefinitionBuilder;
/**
* Adds a default value constraint for the column.
*
* ### Examples
*
* ```ts
* db.schema
* .createTable('pet')
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
* .execute()
* ```
*
* Values passed to `defaultTo` are interpreted as value literals by default. You can define
* an arbitrary SQL expression using the {@link sql} template tag:
*
* ```ts
* import { sql } from 'kysely'
*
* db.schema
* .createTable('pet')
* .addColumn(
* 'number_of_legs',
* 'integer',
* (col) => col.defaultTo(sql`any SQL here`)
* )
* .execute()
* ```
*/
defaultTo(value: DefaultValueExpression): ColumnDefinitionBuilder;
/**
* Adds a check constraint for the column.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* db.schema
* .createTable('pet')
* .addColumn('number_of_legs', 'integer', (col) =>
* col.check(sql`number_of_legs < 5`)
* )
* .execute()
* ```
*/
check(expression: Expression<any>): ColumnDefinitionBuilder;
/**
* Makes the column a generated column using a `generated always as` statement.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* db.schema
* .createTable('person')
* .addColumn('full_name', 'varchar(255)',
* (col) => col.generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
* )
* .execute()
* ```
*/
generatedAlwaysAs(expression: Expression<any>): ColumnDefinitionBuilder;
/**
* Adds the `generated always as identity` specifier.
*
* This only works on some dialects like PostgreSQL.
*
* For MS SQL Server (MSSQL)'s identity column use {@link identity}.
*/
generatedAlwaysAsIdentity(): ColumnDefinitionBuilder;
/**
* Adds the `generated by default as identity` specifier on supported dialects.
*/
generatedByDefaultAsIdentity(): ColumnDefinitionBuilder;
/**
* Makes a generated column stored instead of virtual. This method can only
* be used with {@link generatedAlwaysAs}
*
* ### Examples
*
* ```ts
* db.schema
* .createTable('person')
* .addColumn('full_name', 'varchar(255)', (col) => col
* .generatedAlwaysAs("concat(first_name, ' ', last_name)")
* .stored()
* )
* .execute()
* ```
*/
stored(): ColumnDefinitionBuilder;
/**
* This can be used to add any additional SQL right after the column's data type.
*
* ### Examples
*
* ```ts
* db.schema.createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey())
* .addColumn('first_name', 'varchar(36)', col => col.modifyFront(sql`collate utf8mb4_general_ci`).notNull())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key,
* `first_name` varchar(36) collate utf8mb4_general_ci not null
* )
* ```
*/
modifyFront(modifier: Expression<any>): ColumnDefinitionBuilder;
/**
* Adds `nulls not distinct` specifier.
* Should be used with `unique` constraint.
*
* This only works on some dialects like PostgreSQL.
*
* ### Examples
*
* ```ts
* db.schema.createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey())
* .addColumn('first_name', 'varchar(30)', col => col.unique().nullsNotDistinct())
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "person" (
* "id" integer primary key,
* "first_name" varchar(30) unique nulls not distinct
* )
* ```
*/
nullsNotDistinct(): ColumnDefinitionBuilder;
/**
* This can be used to add any additional SQL to the end of the column definition.
*
* ### Examples
*
* ```ts
* db.schema.createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey())
* .addColumn('age', 'integer', col => col.unsigned().notNull().modifyEnd(sql`comment ${sql.lit('it is not polite to ask a woman her age')}`))
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key,
* `age` integer unsigned not null comment 'it is not polite to ask a woman her age'
* )
* ```
*/
modifyEnd(modifier: Expression<any>): ColumnDefinitionBuilder;
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call<T>(func: (qb: this) => T): T;
toOperationNode(): ColumnDefinitionNode;
}
export type ColumnDefinitionBuilderCallback = (builder: ColumnDefinitionBuilder) => ColumnDefinitionBuilder;