Initial commit - Event Planner application

This commit is contained in:
mberlin
2026-03-18 14:55:56 -03:00
commit 86d779eb4d
7548 changed files with 1006324 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import { AlterColumnNode } from '../operation-node/alter-column-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import { type DataTypeExpression } from '../parser/data-type-parser.js';
import { type DefaultValueExpression } from '../parser/default-value-parser.js';
export declare class AlterColumnBuilder {
#private;
constructor(column: string);
setDataType(dataType: DataTypeExpression): AlteredColumnBuilder;
setDefault(value: DefaultValueExpression): AlteredColumnBuilder;
dropDefault(): AlteredColumnBuilder;
setNotNull(): AlteredColumnBuilder;
dropNotNull(): AlteredColumnBuilder;
/**
* 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;
}
/**
* Allows us to force consumers to do exactly one alteration to a column.
*
* One cannot do no alterations:
*
* ```ts
* await db.schema
* .alterTable('person')
* // .execute() // Property 'execute' does not exist on type 'AlteredColumnBuilder'.
* ```
*
* ```ts
* await db.schema
* .alterTable('person')
* // .alterColumn('age', (ac) => ac) // Type 'AlterColumnBuilder' is not assignable to type 'AlteredColumnBuilder'.
* // .execute()
* ```
*
* One cannot do multiple alterations:
*
* ```ts
* await db.schema
* .alterTable('person')
* // .alterColumn('age', (ac) => ac.dropNotNull().setNotNull()) // Property 'setNotNull' does not exist on type 'AlteredColumnBuilder'.
* // .execute()
* ```
*
* Which would now throw a compilation error, instead of a runtime error.
*/
export declare class AlteredColumnBuilder implements OperationNodeSource {
#private;
constructor(alterColumnNode: AlterColumnNode);
toOperationNode(): AlterColumnNode;
}
export type AlterColumnBuilderCallback = (builder: AlterColumnBuilder) => AlteredColumnBuilder;

View File

@@ -0,0 +1,74 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlteredColumnBuilder = exports.AlterColumnBuilder = void 0;
const alter_column_node_js_1 = require("../operation-node/alter-column-node.js");
const data_type_parser_js_1 = require("../parser/data-type-parser.js");
const default_value_parser_js_1 = require("../parser/default-value-parser.js");
class AlterColumnBuilder {
#column;
constructor(column) {
this.#column = column;
}
setDataType(dataType) {
return new AlteredColumnBuilder(alter_column_node_js_1.AlterColumnNode.create(this.#column, 'dataType', (0, data_type_parser_js_1.parseDataTypeExpression)(dataType)));
}
setDefault(value) {
return new AlteredColumnBuilder(alter_column_node_js_1.AlterColumnNode.create(this.#column, 'setDefault', (0, default_value_parser_js_1.parseDefaultValueExpression)(value)));
}
dropDefault() {
return new AlteredColumnBuilder(alter_column_node_js_1.AlterColumnNode.create(this.#column, 'dropDefault', true));
}
setNotNull() {
return new AlteredColumnBuilder(alter_column_node_js_1.AlterColumnNode.create(this.#column, 'setNotNull', true));
}
dropNotNull() {
return new AlteredColumnBuilder(alter_column_node_js_1.AlterColumnNode.create(this.#column, 'dropNotNull', true));
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
}
exports.AlterColumnBuilder = AlterColumnBuilder;
/**
* Allows us to force consumers to do exactly one alteration to a column.
*
* One cannot do no alterations:
*
* ```ts
* await db.schema
* .alterTable('person')
* // .execute() // Property 'execute' does not exist on type 'AlteredColumnBuilder'.
* ```
*
* ```ts
* await db.schema
* .alterTable('person')
* // .alterColumn('age', (ac) => ac) // Type 'AlterColumnBuilder' is not assignable to type 'AlteredColumnBuilder'.
* // .execute()
* ```
*
* One cannot do multiple alterations:
*
* ```ts
* await db.schema
* .alterTable('person')
* // .alterColumn('age', (ac) => ac.dropNotNull().setNotNull()) // Property 'setNotNull' does not exist on type 'AlteredColumnBuilder'.
* // .execute()
* ```
*
* Which would now throw a compilation error, instead of a runtime error.
*/
class AlteredColumnBuilder {
#alterColumnNode;
constructor(alterColumnNode) {
this.#alterColumnNode = alterColumnNode;
}
toOperationNode() {
return this.#alterColumnNode;
}
}
exports.AlteredColumnBuilder = AlteredColumnBuilder;

View File

@@ -0,0 +1,32 @@
import { AlterTableNode } from '../operation-node/alter-table-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { OnModifyForeignAction } from '../operation-node/references-node.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryId } from '../util/query-id.js';
import type { ForeignKeyConstraintBuilder, ForeignKeyConstraintBuilderInterface } from './foreign-key-constraint-builder.js';
export declare class AlterTableAddForeignKeyConstraintBuilder implements ForeignKeyConstraintBuilderInterface<AlterTableAddForeignKeyConstraintBuilder>, OperationNodeSource, Compilable {
#private;
constructor(props: AlterTableAddForeignKeyConstraintBuilderProps);
onDelete(onDelete: OnModifyForeignAction): AlterTableAddForeignKeyConstraintBuilder;
onUpdate(onUpdate: OnModifyForeignAction): AlterTableAddForeignKeyConstraintBuilder;
deferrable(): AlterTableAddForeignKeyConstraintBuilder;
notDeferrable(): AlterTableAddForeignKeyConstraintBuilder;
initiallyDeferred(): AlterTableAddForeignKeyConstraintBuilder;
initiallyImmediate(): AlterTableAddForeignKeyConstraintBuilder;
/**
* 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(): AlterTableNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface AlterTableAddForeignKeyConstraintBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: AlterTableNode;
readonly constraintBuilder: ForeignKeyConstraintBuilder;
}

View File

@@ -0,0 +1,67 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlterTableAddForeignKeyConstraintBuilder = void 0;
const add_constraint_node_js_1 = require("../operation-node/add-constraint-node.js");
const alter_table_node_js_1 = require("../operation-node/alter-table-node.js");
const object_utils_js_1 = require("../util/object-utils.js");
class AlterTableAddForeignKeyConstraintBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
onDelete(onDelete) {
return new AlterTableAddForeignKeyConstraintBuilder({
...this.#props,
constraintBuilder: this.#props.constraintBuilder.onDelete(onDelete),
});
}
onUpdate(onUpdate) {
return new AlterTableAddForeignKeyConstraintBuilder({
...this.#props,
constraintBuilder: this.#props.constraintBuilder.onUpdate(onUpdate),
});
}
deferrable() {
return new AlterTableAddForeignKeyConstraintBuilder({
...this.#props,
constraintBuilder: this.#props.constraintBuilder.deferrable(),
});
}
notDeferrable() {
return new AlterTableAddForeignKeyConstraintBuilder({
...this.#props,
constraintBuilder: this.#props.constraintBuilder.notDeferrable(),
});
}
initiallyDeferred() {
return new AlterTableAddForeignKeyConstraintBuilder({
...this.#props,
constraintBuilder: this.#props.constraintBuilder.initiallyDeferred(),
});
}
initiallyImmediate() {
return new AlterTableAddForeignKeyConstraintBuilder({
...this.#props,
constraintBuilder: this.#props.constraintBuilder.initiallyImmediate(),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addConstraint: add_constraint_node_js_1.AddConstraintNode.create(this.#props.constraintBuilder.toOperationNode()),
}), this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.AlterTableAddForeignKeyConstraintBuilder = AlterTableAddForeignKeyConstraintBuilder;

View File

@@ -0,0 +1,138 @@
import type { Expression } from '../expression/expression.js';
import { AlterTableNode } from '../operation-node/alter-table-node.js';
import type { IndexType } from '../operation-node/create-index-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import { type OrderedColumnName } from '../parser/reference-parser.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryId } from '../util/query-id.js';
export declare class AlterTableAddIndexBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: AlterTableAddIndexBuilderProps);
/**
* Makes the index unique.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_index')
* .unique()
* .column('email')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add unique index `person_first_name_index` (`email`)
* ```
*/
unique(): AlterTableAddIndexBuilder;
/**
* Adds a column to the index.
*
* Also see {@link columns} for adding multiple columns at once or {@link expression}
* for specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_and_age_index')
* .column('first_name')
* .column('age desc')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add index `person_first_name_and_age_index` (`first_name`, `age` desc)
* ```
*/
column<CL extends string>(column: OrderedColumnName<CL>): AlterTableAddIndexBuilder;
/**
* Specifies a list of columns for the index.
*
* Also see {@link column} for adding a single column or {@link expression} for
* specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_and_age_index')
* .columns(['first_name', 'age desc'])
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add index `person_first_name_and_age_index` (`first_name`, `age` desc)
* ```
*/
columns<CL extends string>(columns: OrderedColumnName<CL>[]): AlterTableAddIndexBuilder;
/**
* Specifies an arbitrary expression for the index.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_index')
* .expression(sql<boolean>`(first_name < 'Sami')`)
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add index `person_first_name_index` ((first_name < 'Sami'))
* ```
*/
expression(expression: Expression<any>): AlterTableAddIndexBuilder;
/**
* Specifies the index type.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_index')
* .column('first_name')
* .using('hash')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add index `person_first_name_index` (`first_name`) using hash
* ```
*/
using(indexType: IndexType): AlterTableAddIndexBuilder;
using(indexType: string): AlterTableAddIndexBuilder;
/**
* 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(): AlterTableNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface AlterTableAddIndexBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: AlterTableNode;
}

View File

@@ -0,0 +1,165 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlterTableAddIndexBuilder = void 0;
const add_index_node_js_1 = require("../operation-node/add-index-node.js");
const alter_table_node_js_1 = require("../operation-node/alter-table-node.js");
const raw_node_js_1 = require("../operation-node/raw-node.js");
const reference_parser_js_1 = require("../parser/reference-parser.js");
const object_utils_js_1 = require("../util/object-utils.js");
class AlterTableAddIndexBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
/**
* Makes the index unique.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_index')
* .unique()
* .column('email')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add unique index `person_first_name_index` (`email`)
* ```
*/
unique() {
return new AlterTableAddIndexBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addIndex: add_index_node_js_1.AddIndexNode.cloneWith(this.#props.node.addIndex, {
unique: true,
}),
}),
});
}
/**
* Adds a column to the index.
*
* Also see {@link columns} for adding multiple columns at once or {@link expression}
* for specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_and_age_index')
* .column('first_name')
* .column('age desc')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add index `person_first_name_and_age_index` (`first_name`, `age` desc)
* ```
*/
column(column) {
return new AlterTableAddIndexBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addIndex: add_index_node_js_1.AddIndexNode.cloneWithColumns(this.#props.node.addIndex, [
(0, reference_parser_js_1.parseOrderedColumnName)(column),
]),
}),
});
}
/**
* Specifies a list of columns for the index.
*
* Also see {@link column} for adding a single column or {@link expression} for
* specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_and_age_index')
* .columns(['first_name', 'age desc'])
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add index `person_first_name_and_age_index` (`first_name`, `age` desc)
* ```
*/
columns(columns) {
return new AlterTableAddIndexBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addIndex: add_index_node_js_1.AddIndexNode.cloneWithColumns(this.#props.node.addIndex, columns.map(reference_parser_js_1.parseOrderedColumnName)),
}),
});
}
/**
* Specifies an arbitrary expression for the index.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .alterTable('person')
* .addIndex('person_first_name_index')
* .expression(sql<boolean>`(first_name < 'Sami')`)
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add index `person_first_name_index` ((first_name < 'Sami'))
* ```
*/
expression(expression) {
return new AlterTableAddIndexBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addIndex: add_index_node_js_1.AddIndexNode.cloneWithColumns(this.#props.node.addIndex, [
expression.toOperationNode(),
]),
}),
});
}
using(indexType) {
return new AlterTableAddIndexBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addIndex: add_index_node_js_1.AddIndexNode.cloneWith(this.#props.node.addIndex, {
using: raw_node_js_1.RawNode.createWithSql(indexType),
}),
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.AlterTableAddIndexBuilder = AlterTableAddIndexBuilder;

View File

@@ -0,0 +1,148 @@
import { AlterTableNode } from '../operation-node/alter-table-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import { type ColumnDefinitionBuilderCallback } from './column-definition-builder.js';
import type { QueryId } from '../util/query-id.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import { type DataTypeExpression } from '../parser/data-type-parser.js';
import { type ForeignKeyConstraintBuilderCallback } from './foreign-key-constraint-builder.js';
import type { Expression } from '../expression/expression.js';
import { type AlterColumnBuilderCallback } from './alter-column-builder.js';
import { AlterTableExecutor } from './alter-table-executor.js';
import { AlterTableAddForeignKeyConstraintBuilder } from './alter-table-add-foreign-key-constraint-builder.js';
import { AlterTableDropConstraintBuilder } from './alter-table-drop-constraint-builder.js';
import { AlterTableAddIndexBuilder } from './alter-table-add-index-builder.js';
import { type UniqueConstraintNodeBuilderCallback } from './unique-constraint-builder.js';
import { type PrimaryKeyConstraintBuilderCallback } from './primary-key-constraint-builder.js';
import { type CheckConstraintBuilderCallback } from './check-constraint-builder.js';
/**
* This builder can be used to create a `alter table` query.
*/
export declare class AlterTableBuilder implements ColumnAlteringInterface {
#private;
constructor(props: AlterTableBuilderProps);
renameTo(newTableName: string): AlterTableExecutor;
setSchema(newSchema: string): AlterTableExecutor;
alterColumn(column: string, alteration: AlterColumnBuilderCallback): AlterTableColumnAlteringBuilder;
dropColumn(column: string): AlterTableColumnAlteringBuilder;
renameColumn(column: string, newColumn: string): AlterTableColumnAlteringBuilder;
/**
* See {@link CreateTableBuilder.addColumn}
*/
addColumn(columnName: string, dataType: DataTypeExpression, build?: ColumnDefinitionBuilderCallback): AlterTableColumnAlteringBuilder;
/**
* Creates an `alter table modify column` query. The `modify column` statement
* is only implemeted by MySQL and oracle AFAIK. On other databases you
* should use the `alterColumn` method.
*/
modifyColumn(columnName: string, dataType: DataTypeExpression, build?: ColumnDefinitionBuilderCallback): AlterTableColumnAlteringBuilder;
/**
* See {@link CreateTableBuilder.addUniqueConstraint}
*/
addUniqueConstraint(constraintName: string, columns: string[], build?: UniqueConstraintNodeBuilderCallback): AlterTableExecutor;
/**
* See {@link CreateTableBuilder.addCheckConstraint}
*/
addCheckConstraint(constraintName: string, checkExpression: Expression<any>, build?: CheckConstraintBuilderCallback): AlterTableExecutor;
/**
* See {@link CreateTableBuilder.addForeignKeyConstraint}
*
* Unlike {@link CreateTableBuilder.addForeignKeyConstraint} this method returns
* the constraint builder and doesn't take a callback as the last argument. This
* is because you can only add one column per `ALTER TABLE` query.
*/
addForeignKeyConstraint(constraintName: string, columns: string[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): AlterTableAddForeignKeyConstraintBuilder;
/**
* See {@link CreateTableBuilder.addPrimaryKeyConstraint}
*/
addPrimaryKeyConstraint(constraintName: string, columns: string[], build?: PrimaryKeyConstraintBuilderCallback): AlterTableExecutor;
dropConstraint(constraintName: string): AlterTableDropConstraintBuilder;
renameConstraint(oldName: string, newName: string): AlterTableDropConstraintBuilder;
/**
* This can be used to add index to table.
*
* ### Examples
*
* ```ts
* db.schema.alterTable('person')
* .addIndex('person_email_index')
* .column('email')
* .unique()
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add unique index `person_email_index` (`email`)
* ```
*/
addIndex(indexName: string): AlterTableAddIndexBuilder;
/**
* This can be used to drop index from table.
*
* ### Examples
*
* ```ts
* db.schema.alterTable('person')
* .dropIndex('person_email_index')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` drop index `test_first_name_index`
* ```
*/
dropIndex(indexName: string): AlterTableExecutor;
/**
* Calls the given function passing `this` as the only argument.
*
* See {@link CreateTableBuilder.$call}
*/
$call<T>(func: (qb: this) => T): T;
}
export interface AlterTableBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: AlterTableNode;
}
export interface ColumnAlteringInterface {
alterColumn(column: string, alteration: AlterColumnBuilderCallback): ColumnAlteringInterface;
dropColumn(column: string): ColumnAlteringInterface;
renameColumn(column: string, newColumn: string): ColumnAlteringInterface;
/**
* See {@link CreateTableBuilder.addColumn}
*/
addColumn(columnName: string, dataType: DataTypeExpression, build?: ColumnDefinitionBuilderCallback): ColumnAlteringInterface;
/**
* Creates an `alter table modify column` query. The `modify column` statement
* is only implemeted by MySQL and oracle AFAIK. On other databases you
* should use the `alterColumn` method.
*/
modifyColumn(columnName: string, dataType: DataTypeExpression, build: ColumnDefinitionBuilderCallback): ColumnAlteringInterface;
}
export declare class AlterTableColumnAlteringBuilder implements ColumnAlteringInterface, OperationNodeSource, Compilable {
#private;
constructor(props: AlterTableColumnAlteringBuilderProps);
alterColumn(column: string, alteration: AlterColumnBuilderCallback): AlterTableColumnAlteringBuilder;
dropColumn(column: string): AlterTableColumnAlteringBuilder;
renameColumn(column: string, newColumn: string): AlterTableColumnAlteringBuilder;
/**
* See {@link CreateTableBuilder.addColumn}
*/
addColumn(columnName: string, dataType: DataTypeExpression, build?: ColumnDefinitionBuilderCallback): AlterTableColumnAlteringBuilder;
/**
* Creates an `alter table modify column` query. The `modify column` statement
* is only implemeted by MySQL and oracle AFAIK. On other databases you
* should use the `alterColumn` method.
*/
modifyColumn(columnName: string, dataType: DataTypeExpression, build?: ColumnDefinitionBuilderCallback): AlterTableColumnAlteringBuilder;
toOperationNode(): AlterTableNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface AlterTableColumnAlteringBuilderProps extends AlterTableBuilderProps {
}

View File

@@ -0,0 +1,267 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlterTableColumnAlteringBuilder = exports.AlterTableBuilder = void 0;
const add_column_node_js_1 = require("../operation-node/add-column-node.js");
const alter_table_node_js_1 = require("../operation-node/alter-table-node.js");
const column_definition_node_js_1 = require("../operation-node/column-definition-node.js");
const drop_column_node_js_1 = require("../operation-node/drop-column-node.js");
const identifier_node_js_1 = require("../operation-node/identifier-node.js");
const rename_column_node_js_1 = require("../operation-node/rename-column-node.js");
const object_utils_js_1 = require("../util/object-utils.js");
const column_definition_builder_js_1 = require("./column-definition-builder.js");
const modify_column_node_js_1 = require("../operation-node/modify-column-node.js");
const data_type_parser_js_1 = require("../parser/data-type-parser.js");
const foreign_key_constraint_builder_js_1 = require("./foreign-key-constraint-builder.js");
const add_constraint_node_js_1 = require("../operation-node/add-constraint-node.js");
const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
const column_node_js_1 = require("../operation-node/column-node.js");
const table_parser_js_1 = require("../parser/table-parser.js");
const drop_constraint_node_js_1 = require("../operation-node/drop-constraint-node.js");
const alter_column_builder_js_1 = require("./alter-column-builder.js");
const alter_table_executor_js_1 = require("./alter-table-executor.js");
const alter_table_add_foreign_key_constraint_builder_js_1 = require("./alter-table-add-foreign-key-constraint-builder.js");
const alter_table_drop_constraint_builder_js_1 = require("./alter-table-drop-constraint-builder.js");
const primary_key_constraint_node_js_1 = require("../operation-node/primary-key-constraint-node.js");
const drop_index_node_js_1 = require("../operation-node/drop-index-node.js");
const add_index_node_js_1 = require("../operation-node/add-index-node.js");
const alter_table_add_index_builder_js_1 = require("./alter-table-add-index-builder.js");
const unique_constraint_builder_js_1 = require("./unique-constraint-builder.js");
const primary_key_constraint_builder_js_1 = require("./primary-key-constraint-builder.js");
const check_constraint_builder_js_1 = require("./check-constraint-builder.js");
const rename_constraint_node_js_1 = require("../operation-node/rename-constraint-node.js");
/**
* This builder can be used to create a `alter table` query.
*/
class AlterTableBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
renameTo(newTableName) {
return new alter_table_executor_js_1.AlterTableExecutor({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
renameTo: (0, table_parser_js_1.parseTable)(newTableName),
}),
});
}
setSchema(newSchema) {
return new alter_table_executor_js_1.AlterTableExecutor({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
setSchema: identifier_node_js_1.IdentifierNode.create(newSchema),
}),
});
}
alterColumn(column, alteration) {
const builder = alteration(new alter_column_builder_js_1.AlterColumnBuilder(column));
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, builder.toOperationNode()),
});
}
dropColumn(column) {
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, drop_column_node_js_1.DropColumnNode.create(column)),
});
}
renameColumn(column, newColumn) {
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, rename_column_node_js_1.RenameColumnNode.create(column, newColumn)),
});
}
addColumn(columnName, dataType, build = object_utils_js_1.noop) {
const builder = build(new column_definition_builder_js_1.ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.create(columnName, (0, data_type_parser_js_1.parseDataTypeExpression)(dataType))));
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, add_column_node_js_1.AddColumnNode.create(builder.toOperationNode())),
});
}
modifyColumn(columnName, dataType, build = object_utils_js_1.noop) {
const builder = build(new column_definition_builder_js_1.ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.create(columnName, (0, data_type_parser_js_1.parseDataTypeExpression)(dataType))));
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, modify_column_node_js_1.ModifyColumnNode.create(builder.toOperationNode())),
});
}
/**
* See {@link CreateTableBuilder.addUniqueConstraint}
*/
addUniqueConstraint(constraintName, columns, build = object_utils_js_1.noop) {
const uniqueConstraintBuilder = build(new unique_constraint_builder_js_1.UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.create(columns, constraintName)));
return new alter_table_executor_js_1.AlterTableExecutor({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addConstraint: add_constraint_node_js_1.AddConstraintNode.create(uniqueConstraintBuilder.toOperationNode()),
}),
});
}
/**
* See {@link CreateTableBuilder.addCheckConstraint}
*/
addCheckConstraint(constraintName, checkExpression, build = object_utils_js_1.noop) {
const constraintBuilder = build(new check_constraint_builder_js_1.CheckConstraintBuilder(check_constraint_node_js_1.CheckConstraintNode.create(checkExpression.toOperationNode(), constraintName)));
return new alter_table_executor_js_1.AlterTableExecutor({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addConstraint: add_constraint_node_js_1.AddConstraintNode.create(constraintBuilder.toOperationNode()),
}),
});
}
/**
* See {@link CreateTableBuilder.addForeignKeyConstraint}
*
* Unlike {@link CreateTableBuilder.addForeignKeyConstraint} this method returns
* the constraint builder and doesn't take a callback as the last argument. This
* is because you can only add one column per `ALTER TABLE` query.
*/
addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build = object_utils_js_1.noop) {
const constraintBuilder = build(new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), (0, table_parser_js_1.parseTable)(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
return new alter_table_add_foreign_key_constraint_builder_js_1.AlterTableAddForeignKeyConstraintBuilder({
...this.#props,
constraintBuilder,
});
}
/**
* See {@link CreateTableBuilder.addPrimaryKeyConstraint}
*/
addPrimaryKeyConstraint(constraintName, columns, build = object_utils_js_1.noop) {
const constraintBuilder = build(new primary_key_constraint_builder_js_1.PrimaryKeyConstraintBuilder(primary_key_constraint_node_js_1.PrimaryKeyConstraintNode.create(columns, constraintName)));
return new alter_table_executor_js_1.AlterTableExecutor({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addConstraint: add_constraint_node_js_1.AddConstraintNode.create(constraintBuilder.toOperationNode()),
}),
});
}
dropConstraint(constraintName) {
return new alter_table_drop_constraint_builder_js_1.AlterTableDropConstraintBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
dropConstraint: drop_constraint_node_js_1.DropConstraintNode.create(constraintName),
}),
});
}
renameConstraint(oldName, newName) {
return new alter_table_drop_constraint_builder_js_1.AlterTableDropConstraintBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
renameConstraint: rename_constraint_node_js_1.RenameConstraintNode.create(oldName, newName),
}),
});
}
/**
* This can be used to add index to table.
*
* ### Examples
*
* ```ts
* db.schema.alterTable('person')
* .addIndex('person_email_index')
* .column('email')
* .unique()
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` add unique index `person_email_index` (`email`)
* ```
*/
addIndex(indexName) {
return new alter_table_add_index_builder_js_1.AlterTableAddIndexBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
addIndex: add_index_node_js_1.AddIndexNode.create(indexName),
}),
});
}
/**
* This can be used to drop index from table.
*
* ### Examples
*
* ```ts
* db.schema.alterTable('person')
* .dropIndex('person_email_index')
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* alter table `person` drop index `test_first_name_index`
* ```
*/
dropIndex(indexName) {
return new alter_table_executor_js_1.AlterTableExecutor({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
dropIndex: drop_index_node_js_1.DropIndexNode.create(indexName),
}),
});
}
/**
* Calls the given function passing `this` as the only argument.
*
* See {@link CreateTableBuilder.$call}
*/
$call(func) {
return func(this);
}
}
exports.AlterTableBuilder = AlterTableBuilder;
class AlterTableColumnAlteringBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
alterColumn(column, alteration) {
const builder = alteration(new alter_column_builder_js_1.AlterColumnBuilder(column));
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, builder.toOperationNode()),
});
}
dropColumn(column) {
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, drop_column_node_js_1.DropColumnNode.create(column)),
});
}
renameColumn(column, newColumn) {
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, rename_column_node_js_1.RenameColumnNode.create(column, newColumn)),
});
}
addColumn(columnName, dataType, build = object_utils_js_1.noop) {
const builder = build(new column_definition_builder_js_1.ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.create(columnName, (0, data_type_parser_js_1.parseDataTypeExpression)(dataType))));
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, add_column_node_js_1.AddColumnNode.create(builder.toOperationNode())),
});
}
modifyColumn(columnName, dataType, build = object_utils_js_1.noop) {
const builder = build(new column_definition_builder_js_1.ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.create(columnName, (0, data_type_parser_js_1.parseDataTypeExpression)(dataType))));
return new AlterTableColumnAlteringBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithColumnAlteration(this.#props.node, modify_column_node_js_1.ModifyColumnNode.create(builder.toOperationNode())),
});
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.AlterTableColumnAlteringBuilder = AlterTableColumnAlteringBuilder;

View File

@@ -0,0 +1,26 @@
import { AlterTableNode } from '../operation-node/alter-table-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryId } from '../util/query-id.js';
export declare class AlterTableDropConstraintBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: AlterTableDropConstraintBuilderProps);
ifExists(): AlterTableDropConstraintBuilder;
cascade(): AlterTableDropConstraintBuilder;
restrict(): AlterTableDropConstraintBuilder;
/**
* 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(): AlterTableNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface AlterTableDropConstraintBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: AlterTableNode;
}

View File

@@ -0,0 +1,59 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlterTableDropConstraintBuilder = void 0;
const alter_table_node_js_1 = require("../operation-node/alter-table-node.js");
const drop_constraint_node_js_1 = require("../operation-node/drop-constraint-node.js");
const object_utils_js_1 = require("../util/object-utils.js");
class AlterTableDropConstraintBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
ifExists() {
return new AlterTableDropConstraintBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
dropConstraint: drop_constraint_node_js_1.DropConstraintNode.cloneWith(this.#props.node.dropConstraint, {
ifExists: true,
}),
}),
});
}
cascade() {
return new AlterTableDropConstraintBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
dropConstraint: drop_constraint_node_js_1.DropConstraintNode.cloneWith(this.#props.node.dropConstraint, {
modifier: 'cascade',
}),
}),
});
}
restrict() {
return new AlterTableDropConstraintBuilder({
...this.#props,
node: alter_table_node_js_1.AlterTableNode.cloneWithTableProps(this.#props.node, {
dropConstraint: drop_constraint_node_js_1.DropConstraintNode.cloneWith(this.#props.node.dropConstraint, {
modifier: 'restrict',
}),
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.AlterTableDropConstraintBuilder = AlterTableDropConstraintBuilder;

View File

@@ -0,0 +1,18 @@
import type { AlterTableNode } from '../operation-node/alter-table-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryId } from '../util/query-id.js';
export declare class AlterTableExecutor implements OperationNodeSource, Compilable {
#private;
constructor(props: AlterTableExecutorProps);
toOperationNode(): AlterTableNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface AlterTableExecutorProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: AlterTableNode;
}

View File

@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlterTableExecutor = void 0;
const object_utils_js_1 = require("../util/object-utils.js");
class AlterTableExecutor {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.AlterTableExecutor = AlterTableExecutor;

View File

@@ -0,0 +1,13 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CheckConstraintNode } from '../operation-node/check-constraint-node.js';
export declare class CheckConstraintBuilder implements OperationNodeSource {
#private;
constructor(node: CheckConstraintNode);
/**
* 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(): CheckConstraintNode;
}
export type CheckConstraintBuilderCallback = (builder: CheckConstraintBuilder) => CheckConstraintBuilder;

View File

@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckConstraintBuilder = void 0;
class CheckConstraintBuilder {
#node;
constructor(node) {
this.#node = node;
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#node;
}
}
exports.CheckConstraintBuilder = CheckConstraintBuilder;

View File

@@ -0,0 +1,514 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import { type OnModifyForeignAction } from '../operation-node/references-node.js';
import { ColumnDefinitionNode } from '../operation-node/column-definition-node.js';
import { type DefaultValueExpression } from '../parser/default-value-parser.js';
import type { 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.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.autoIncrement().primaryKey())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key auto_increment
* )
* ```
*/
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}.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.identity().primaryKey())
* .execute()
* ```
*
* The generated SQL (MSSQL):
*
* ```sql
* create table "person" (
* "id" integer identity primary key
* )
* ```
*/
identity(): ColumnDefinitionBuilder;
/**
* Makes the column the primary key.
*
* If you want to specify a composite primary key use the
* {@link CreateTableBuilder.addPrimaryKeyConstraint} method.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key
* )
*/
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
* await db.schema
* .createTable('pet')
* .addColumn('owner_id', 'integer', (col) => col.references('person.id'))
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "pet" (
* "owner_id" integer 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
* await db.schema
* .createTable('pet')
* .addColumn(
* 'owner_id',
* 'integer',
* (col) => col.references('person.id').onDelete('cascade')
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "pet" (
* "owner_id" integer references "person" ("id") on delete cascade
* )
* ```
*/
onDelete(onDelete: OnModifyForeignAction): ColumnDefinitionBuilder;
/**
* Adds an `on update` 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
* await db.schema
* .createTable('pet')
* .addColumn(
* 'owner_id',
* 'integer',
* (col) => col.references('person.id').onUpdate('cascade')
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "pet" (
* "owner_id" integer references "person" ("id") on update cascade
* )
* ```
*/
onUpdate(onUpdate: OnModifyForeignAction): ColumnDefinitionBuilder;
/**
* Adds a unique constraint for the column.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('email', 'varchar(255)', col => col.unique())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `email` varchar(255) unique
* )
* ```
*/
unique(): ColumnDefinitionBuilder;
/**
* Adds a `not null` constraint for the column.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(255)', col => col.notNull())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `first_name` varchar(255) not null
* )
* ```
*/
notNull(): ColumnDefinitionBuilder;
/**
* Adds a `unsigned` modifier for the column.
*
* This only works on some dialects like MySQL.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('age', 'integer', col => col.unsigned())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `age` integer unsigned
* )
* ```
*/
unsigned(): ColumnDefinitionBuilder;
/**
* Adds a default value constraint for the column.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `pet` (
* `number_of_legs` integer default 4
* )
* ```
*
* 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'
*
* await db.schema
* .createTable('pet')
* .addColumn(
* 'created_at',
* 'timestamp',
* (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`)
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `pet` (
* `created_at` timestamp default CURRENT_TIMESTAMP
* )
* ```
*/
defaultTo(value: DefaultValueExpression): ColumnDefinitionBuilder;
/**
* Adds a check constraint for the column.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('pet')
* .addColumn('number_of_legs', 'integer', (col) =>
* col.check(sql`number_of_legs < 5`)
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `pet` (
* `number_of_legs` integer check (number_of_legs < 5)
* )
* ```
*/
check(expression: Expression<any>): ColumnDefinitionBuilder;
/**
* Makes the column a generated column using a `generated always as` statement.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('full_name', 'varchar(255)',
* (col) => col.generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name))
* )
* ```
*/
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}.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.generatedAlwaysAsIdentity().primaryKey())
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "person" (
* "id" integer generated always as identity primary key
* )
* ```
*/
generatedAlwaysAsIdentity(): ColumnDefinitionBuilder;
/**
* Adds the `generated by default as identity` specifier on supported dialects.
*
* This only works on some dialects like PostgreSQL.
*
* For MS SQL Server (MSSQL)'s identity column use {@link identity}.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.generatedByDefaultAsIdentity().primaryKey())
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "person" (
* "id" integer generated by default as identity primary key
* )
* ```
*/
generatedByDefaultAsIdentity(): ColumnDefinitionBuilder;
/**
* Makes a generated column stored instead of virtual. This method can only
* be used with {@link generatedAlwaysAs}
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('full_name', 'varchar(255)', (col) => col
* .generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
* .stored()
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name)) stored
* )
* ```
*/
stored(): ColumnDefinitionBuilder;
/**
* This can be used to add any additional SQL right after the column's data type.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await 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;
/**
* Adds `if not exists` specifier. This only works for PostgreSQL.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addColumn('email', 'varchar(255)', col => col.unique().ifNotExists())
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* alter table "person" add column if not exists "email" varchar(255) unique
* ```
*/
ifNotExists(): ColumnDefinitionBuilder;
/**
* This can be used to add any additional SQL to the end of the column definition.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await 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;

View File

@@ -0,0 +1,600 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnDefinitionBuilder = void 0;
const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
const references_node_js_1 = require("../operation-node/references-node.js");
const select_all_node_js_1 = require("../operation-node/select-all-node.js");
const reference_parser_js_1 = require("../parser/reference-parser.js");
const column_definition_node_js_1 = require("../operation-node/column-definition-node.js");
const default_value_parser_js_1 = require("../parser/default-value-parser.js");
const generated_node_js_1 = require("../operation-node/generated-node.js");
const default_value_node_js_1 = require("../operation-node/default-value-node.js");
const on_modify_action_parser_js_1 = require("../parser/on-modify-action-parser.js");
class ColumnDefinitionBuilder {
#node;
constructor(node) {
this.#node = node;
}
/**
* 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.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.autoIncrement().primaryKey())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key auto_increment
* )
* ```
*/
autoIncrement() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { autoIncrement: true }));
}
/**
* 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}.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.identity().primaryKey())
* .execute()
* ```
*
* The generated SQL (MSSQL):
*
* ```sql
* create table "person" (
* "id" integer identity primary key
* )
* ```
*/
identity() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { identity: true }));
}
/**
* Makes the column the primary key.
*
* If you want to specify a composite primary key use the
* {@link CreateTableBuilder.addPrimaryKeyConstraint} method.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key
* )
*/
primaryKey() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { primaryKey: true }));
}
/**
* 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
* await db.schema
* .createTable('pet')
* .addColumn('owner_id', 'integer', (col) => col.references('person.id'))
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "pet" (
* "owner_id" integer references "person" ("id")
* )
* ```
*/
references(ref) {
const references = (0, reference_parser_js_1.parseStringReference)(ref);
if (!references.table || select_all_node_js_1.SelectAllNode.is(references.column)) {
throw new Error(`invalid call references('${ref}'). The reference must have format table.column or schema.table.column`);
}
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
references: references_node_js_1.ReferencesNode.create(references.table, [
references.column,
]),
}));
}
/**
* 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
* await db.schema
* .createTable('pet')
* .addColumn(
* 'owner_id',
* 'integer',
* (col) => col.references('person.id').onDelete('cascade')
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "pet" (
* "owner_id" integer references "person" ("id") on delete cascade
* )
* ```
*/
onDelete(onDelete) {
if (!this.#node.references) {
throw new Error('on delete constraint can only be added for foreign keys');
}
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
references: references_node_js_1.ReferencesNode.cloneWithOnDelete(this.#node.references, (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onDelete)),
}));
}
/**
* Adds an `on update` 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
* await db.schema
* .createTable('pet')
* .addColumn(
* 'owner_id',
* 'integer',
* (col) => col.references('person.id').onUpdate('cascade')
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "pet" (
* "owner_id" integer references "person" ("id") on update cascade
* )
* ```
*/
onUpdate(onUpdate) {
if (!this.#node.references) {
throw new Error('on update constraint can only be added for foreign keys');
}
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
references: references_node_js_1.ReferencesNode.cloneWithOnUpdate(this.#node.references, (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onUpdate)),
}));
}
/**
* Adds a unique constraint for the column.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('email', 'varchar(255)', col => col.unique())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `email` varchar(255) unique
* )
* ```
*/
unique() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { unique: true }));
}
/**
* Adds a `not null` constraint for the column.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(255)', col => col.notNull())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `first_name` varchar(255) not null
* )
* ```
*/
notNull() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { notNull: true }));
}
/**
* Adds a `unsigned` modifier for the column.
*
* This only works on some dialects like MySQL.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('age', 'integer', col => col.unsigned())
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `age` integer unsigned
* )
* ```
*/
unsigned() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { unsigned: true }));
}
/**
* Adds a default value constraint for the column.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `pet` (
* `number_of_legs` integer default 4
* )
* ```
*
* 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'
*
* await db.schema
* .createTable('pet')
* .addColumn(
* 'created_at',
* 'timestamp',
* (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`)
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `pet` (
* `created_at` timestamp default CURRENT_TIMESTAMP
* )
* ```
*/
defaultTo(value) {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
defaultTo: default_value_node_js_1.DefaultValueNode.create((0, default_value_parser_js_1.parseDefaultValueExpression)(value)),
}));
}
/**
* Adds a check constraint for the column.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('pet')
* .addColumn('number_of_legs', 'integer', (col) =>
* col.check(sql`number_of_legs < 5`)
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `pet` (
* `number_of_legs` integer check (number_of_legs < 5)
* )
* ```
*/
check(expression) {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
check: check_constraint_node_js_1.CheckConstraintNode.create(expression.toOperationNode()),
}));
}
/**
* Makes the column a generated column using a `generated always as` statement.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('full_name', 'varchar(255)',
* (col) => col.generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name))
* )
* ```
*/
generatedAlwaysAs(expression) {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
generated: generated_node_js_1.GeneratedNode.createWithExpression(expression.toOperationNode()),
}));
}
/**
* 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}.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.generatedAlwaysAsIdentity().primaryKey())
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "person" (
* "id" integer generated always as identity primary key
* )
* ```
*/
generatedAlwaysAsIdentity() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
generated: generated_node_js_1.GeneratedNode.create({ identity: true, always: true }),
}));
}
/**
* Adds the `generated by default as identity` specifier on supported dialects.
*
* This only works on some dialects like PostgreSQL.
*
* For MS SQL Server (MSSQL)'s identity column use {@link identity}.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.generatedByDefaultAsIdentity().primaryKey())
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create table "person" (
* "id" integer generated by default as identity primary key
* )
* ```
*/
generatedByDefaultAsIdentity() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
generated: generated_node_js_1.GeneratedNode.create({ identity: true, byDefault: true }),
}));
}
/**
* Makes a generated column stored instead of virtual. This method can only
* be used with {@link generatedAlwaysAs}
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('full_name', 'varchar(255)', (col) => col
* .generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
* .stored()
* )
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name)) stored
* )
* ```
*/
stored() {
if (!this.#node.generated) {
throw new Error('stored() can only be called after generatedAlwaysAs');
}
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
generated: generated_node_js_1.GeneratedNode.cloneWith(this.#node.generated, {
stored: true,
}),
}));
}
/**
* This can be used to add any additional SQL right after the column's data type.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await 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) {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWithFrontModifier(this.#node, modifier.toOperationNode()));
}
/**
* 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() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { nullsNotDistinct: true }));
}
/**
* Adds `if not exists` specifier. This only works for PostgreSQL.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .addColumn('email', 'varchar(255)', col => col.unique().ifNotExists())
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* alter table "person" add column if not exists "email" varchar(255) unique
* ```
*/
ifNotExists() {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { ifNotExists: true }));
}
/**
* This can be used to add any additional SQL to the end of the column definition.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await 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) {
return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWithEndModifier(this.#node, modifier.toOperationNode()));
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#node;
}
}
exports.ColumnDefinitionBuilder = ColumnDefinitionBuilder;

View File

@@ -0,0 +1,173 @@
import { CreateIndexNode, type IndexType } from '../operation-node/create-index-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import { type ExtractColumnNameFromOrderedColumnName, type OrderedColumnName } from '../parser/reference-parser.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
import type { Expression } from '../expression/expression.js';
import { type ComparisonOperatorExpression } from '../parser/binary-operation-parser.js';
import type { ExpressionBuilder } from '../expression/expression-builder.js';
import type { ShallowRecord, SqlBool } from '../util/type-utils.js';
export declare class CreateIndexBuilder<C = never> implements OperationNodeSource, Compilable {
#private;
constructor(props: CreateIndexBuilderProps);
/**
* Adds the "if not exists" modifier.
*
* If the index already exists, no error is thrown if this method has been called.
*/
ifNotExists(): CreateIndexBuilder<C>;
/**
* Makes the index unique.
*/
unique(): CreateIndexBuilder<C>;
/**
* Adds `nulls not distinct` specifier to index.
* This only works on some dialects like PostgreSQL.
*
* ### Examples
*
* ```ts
* db.schema.createIndex('person_first_name_index')
* .on('person')
* .column('first_name')
* .nullsNotDistinct()
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_index"
* on "test" ("first_name")
* nulls not distinct;
* ```
*/
nullsNotDistinct(): CreateIndexBuilder<C>;
/**
* Specifies the table for the index.
*/
on(table: string): CreateIndexBuilder<C>;
/**
* Adds a column to the index.
*
* Also see {@link columns} for adding multiple columns at once or {@link expression}
* for specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .createIndex('person_first_name_and_age_index')
* .on('person')
* .column('first_name')
* .column('age desc')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
* ```
*/
column<CL extends string>(column: OrderedColumnName<CL>): CreateIndexBuilder<C | ExtractColumnNameFromOrderedColumnName<CL>>;
/**
* Specifies a list of columns for the index.
*
* Also see {@link column} for adding a single column or {@link expression} for
* specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .createIndex('person_first_name_and_age_index')
* .on('person')
* .columns(['first_name', 'age desc'])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
* ```
*/
columns<CL extends string>(columns: OrderedColumnName<CL>[]): CreateIndexBuilder<C | ExtractColumnNameFromOrderedColumnName<CL>>;
/**
* Specifies an arbitrary expression for the index.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createIndex('person_first_name_index')
* .on('person')
* .expression(sql`first_name COLLATE "fi_FI"`)
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_index" on "person" (first_name COLLATE "fi_FI")
* ```
*/
expression(expression: Expression<any>): CreateIndexBuilder<C>;
/**
* Specifies the index type.
*/
using(indexType: IndexType): CreateIndexBuilder<C>;
using(indexType: string): CreateIndexBuilder<C>;
/**
* Adds a where clause to the query. This Effectively turns the index partial.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createIndex('orders_unbilled_index')
* .on('orders')
* .column('order_nr')
* .where(sql.ref('billed'), 'is not', true)
* .where('order_nr', 'like', '123%')
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "orders_unbilled_index" on "orders" ("order_nr") where "billed" is not true and "order_nr" like '123%'
* ```
*
* Column names specified in {@link column} or {@link columns} are known at compile-time
* and can be referred to in the current query and context.
*
* Sometimes you may want to refer to columns that exist in the table but are not
* part of the current index. In that case you can refer to them using {@link sql}
* expressions.
*
* Parameters are always sent as literals due to database restrictions.
*/
where(lhs: C | Expression<any>, op: ComparisonOperatorExpression, rhs: unknown): CreateIndexBuilder<C>;
where(factory: (qb: ExpressionBuilder<ShallowRecord<string, ShallowRecord<C & string, any>>, string>) => Expression<SqlBool>): CreateIndexBuilder<C>;
where(expression: Expression<SqlBool>): CreateIndexBuilder<C>;
/**
* 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(): CreateIndexNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface CreateIndexBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: CreateIndexNode;
}

View File

@@ -0,0 +1,202 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateIndexBuilder = void 0;
const create_index_node_js_1 = require("../operation-node/create-index-node.js");
const raw_node_js_1 = require("../operation-node/raw-node.js");
const reference_parser_js_1 = require("../parser/reference-parser.js");
const table_parser_js_1 = require("../parser/table-parser.js");
const object_utils_js_1 = require("../util/object-utils.js");
const binary_operation_parser_js_1 = require("../parser/binary-operation-parser.js");
const query_node_js_1 = require("../operation-node/query-node.js");
const immediate_value_transformer_js_1 = require("../plugin/immediate-value/immediate-value-transformer.js");
class CreateIndexBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
/**
* Adds the "if not exists" modifier.
*
* If the index already exists, no error is thrown if this method has been called.
*/
ifNotExists() {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWith(this.#props.node, {
ifNotExists: true,
}),
});
}
/**
* Makes the index unique.
*/
unique() {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWith(this.#props.node, {
unique: true,
}),
});
}
/**
* Adds `nulls not distinct` specifier to index.
* This only works on some dialects like PostgreSQL.
*
* ### Examples
*
* ```ts
* db.schema.createIndex('person_first_name_index')
* .on('person')
* .column('first_name')
* .nullsNotDistinct()
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_index"
* on "test" ("first_name")
* nulls not distinct;
* ```
*/
nullsNotDistinct() {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWith(this.#props.node, {
nullsNotDistinct: true,
}),
});
}
/**
* Specifies the table for the index.
*/
on(table) {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWith(this.#props.node, {
table: (0, table_parser_js_1.parseTable)(table),
}),
});
}
/**
* Adds a column to the index.
*
* Also see {@link columns} for adding multiple columns at once or {@link expression}
* for specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .createIndex('person_first_name_and_age_index')
* .on('person')
* .column('first_name')
* .column('age desc')
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
* ```
*/
column(column) {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWithColumns(this.#props.node, [
(0, reference_parser_js_1.parseOrderedColumnName)(column),
]),
});
}
/**
* Specifies a list of columns for the index.
*
* Also see {@link column} for adding a single column or {@link expression} for
* specifying an arbitrary expression.
*
* ### Examples
*
* ```ts
* await db.schema
* .createIndex('person_first_name_and_age_index')
* .on('person')
* .columns(['first_name', 'age desc'])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
* ```
*/
columns(columns) {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWithColumns(this.#props.node, columns.map(reference_parser_js_1.parseOrderedColumnName)),
});
}
/**
* Specifies an arbitrary expression for the index.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createIndex('person_first_name_index')
* .on('person')
* .expression(sql`first_name COLLATE "fi_FI"`)
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create index "person_first_name_index" on "person" (first_name COLLATE "fi_FI")
* ```
*/
expression(expression) {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWithColumns(this.#props.node, [
expression.toOperationNode(),
]),
});
}
using(indexType) {
return new CreateIndexBuilder({
...this.#props,
node: create_index_node_js_1.CreateIndexNode.cloneWith(this.#props.node, {
using: raw_node_js_1.RawNode.createWithSql(indexType),
}),
});
}
where(...args) {
const transformer = new immediate_value_transformer_js_1.ImmediateValueTransformer();
return new CreateIndexBuilder({
...this.#props,
node: query_node_js_1.QueryNode.cloneWithWhere(this.#props.node, transformer.transformNode((0, binary_operation_parser_js_1.parseValueBinaryOperationOrExpression)(args), this.#props.queryId)),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.CreateIndexBuilder = CreateIndexBuilder;

View File

@@ -0,0 +1,24 @@
import { CreateSchemaNode } from '../operation-node/create-schema-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
export declare class CreateSchemaBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: CreateSchemaBuilderProps);
ifNotExists(): CreateSchemaBuilder;
/**
* 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(): CreateSchemaNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface CreateSchemaBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: CreateSchemaNode;
}

View File

@@ -0,0 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateSchemaBuilder = void 0;
const create_schema_node_js_1 = require("../operation-node/create-schema-node.js");
const object_utils_js_1 = require("../util/object-utils.js");
class CreateSchemaBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
ifNotExists() {
return new CreateSchemaBuilder({
...this.#props,
node: create_schema_node_js_1.CreateSchemaNode.cloneWith(this.#props.node, { ifNotExists: true }),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.CreateSchemaBuilder = CreateSchemaBuilder;

View File

@@ -0,0 +1,331 @@
import { CreateTableNode, type OnCommitAction } from '../operation-node/create-table-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import { ColumnDefinitionBuilder } from './column-definition-builder.js';
import type { QueryId } from '../util/query-id.js';
import { type ForeignKeyConstraintBuilderCallback } from './foreign-key-constraint-builder.js';
import { type DataTypeExpression } from '../parser/data-type-parser.js';
import type { Expression } from '../expression/expression.js';
import { type UniqueConstraintNodeBuilderCallback } from './unique-constraint-builder.js';
import { type PrimaryKeyConstraintBuilderCallback } from './primary-key-constraint-builder.js';
import { type CheckConstraintBuilderCallback } from './check-constraint-builder.js';
/**
* This builder can be used to create a `create table` query.
*/
export declare class CreateTableBuilder<TB extends string, C extends string = never> implements OperationNodeSource, Compilable {
#private;
constructor(props: CreateTableBuilderProps);
/**
* Adds the "temporary" modifier.
*
* Use this to create a temporary table.
*/
temporary(): CreateTableBuilder<TB, C>;
/**
* Adds an "on commit" statement.
*
* This can be used in conjunction with temporary tables on supported databases
* like PostgreSQL.
*/
onCommit(onCommit: OnCommitAction): CreateTableBuilder<TB, C>;
/**
* Adds the "if not exists" modifier.
*
* If the table already exists, no error is thrown if this method has been called.
*/
ifNotExists(): CreateTableBuilder<TB, C>;
/**
* Adds a column to the table.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', (col) => col.autoIncrement().primaryKey())
* .addColumn('first_name', 'varchar(50)', (col) => col.notNull())
* .addColumn('last_name', 'varchar(255)')
* .addColumn('bank_balance', 'numeric(8, 2)')
* // You can specify any data type using the `sql` tag if the types
* // don't include it.
* .addColumn('data', sql`any_type_here`)
* .addColumn('parent_id', 'integer', (col) =>
* col.references('person.id').onDelete('cascade')
* )
* ```
*
* With this method, it's once again good to remember that Kysely just builds the
* query and doesn't provide the same API for all databases. For example, some
* databases like older MySQL don't support the `references` statement in the
* column definition. Instead foreign key constraints need to be defined in the
* `create table` query. See the next example:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', (col) => col.primaryKey())
* .addColumn('parent_id', 'integer')
* .addForeignKeyConstraint(
* 'person_parent_id_fk',
* ['parent_id'],
* 'person',
* ['id'],
* (cb) => cb.onDelete('cascade')
* )
* .execute()
* ```
*
* Another good example is that PostgreSQL doesn't support the `auto_increment`
* keyword and you need to define an autoincrementing column for example using
* `serial`:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'serial', (col) => col.primaryKey())
* .execute()
* ```
*/
addColumn<CN extends string>(columnName: CN, dataType: DataTypeExpression, build?: ColumnBuilderCallback): CreateTableBuilder<TB, C | CN>;
/**
* 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.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(64)')
* .addColumn('last_name', 'varchar(64)')
* .addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
* .execute()
* ```
*/
addPrimaryKeyConstraint(constraintName: string, columns: C[], build?: PrimaryKeyConstraintBuilderCallback): CreateTableBuilder<TB, C>;
/**
* 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.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(64)')
* .addColumn('last_name', 'varchar(64)')
* .addUniqueConstraint(
* 'first_name_last_name_unique',
* ['first_name', 'last_name']
* )
* .execute()
* ```
*
* In dialects such as PostgreSQL you can specify `nulls not distinct` as follows:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(64)')
* .addColumn('last_name', 'varchar(64)')
* .addUniqueConstraint(
* 'first_name_last_name_unique',
* ['first_name', 'last_name'],
* (cb) => cb.nullsNotDistinct()
* )
* .execute()
* ```
*/
addUniqueConstraint(constraintName: string, columns: C[], build?: UniqueConstraintNodeBuilderCallback): CreateTableBuilder<TB, C>;
/**
* Adds a check constraint.
*
* The constraint name can be anything you want, but it must be unique
* across the whole database.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('animal')
* .addColumn('number_of_legs', 'integer')
* .addCheckConstraint('check_legs', sql`number_of_legs < 5`)
* .execute()
* ```
*/
addCheckConstraint(constraintName: string, checkExpression: Expression<any>, build?: CheckConstraintBuilderCallback): CreateTableBuilder<TB, C>;
/**
* Adds a foreign key constraint.
*
* The constraint name can be anything you want, but it must be unique
* across the whole database.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('owner_id', 'integer')
* .addForeignKeyConstraint(
* 'owner_id_foreign',
* ['owner_id'],
* 'person',
* ['id'],
* )
* .execute()
* ```
*
* Add constraint for multiple columns:
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('owner_id1', 'integer')
* .addColumn('owner_id2', 'integer')
* .addForeignKeyConstraint(
* 'owner_id_foreign',
* ['owner_id1', 'owner_id2'],
* 'person',
* ['id1', 'id2'],
* (cb) => cb.onDelete('cascade')
* )
* .execute()
* ```
*/
addForeignKeyConstraint(constraintName: string, columns: C[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): CreateTableBuilder<TB, C>;
/**
* This can be used to add any additional SQL to the front of the query __after__ the `create` keyword.
*
* Also see {@link temporary}.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .modifyFront(sql`global temporary`)
* .addColumn('id', 'integer', col => col.primaryKey())
* .addColumn('first_name', 'varchar(64)', col => col.notNull())
* .addColumn('last_name', 'varchar(64)', col => col.notNull())
* .execute()
* ```
*
* The generated SQL (Postgres):
*
* ```sql
* create global temporary table "person" (
* "id" integer primary key,
* "first_name" varchar(64) not null,
* "last_name" varchar(64) not null
* )
* ```
*/
modifyFront(modifier: Expression<any>): CreateTableBuilder<TB, C>;
/**
* This can be used to add any additional SQL to the end of the query.
*
* Also see {@link onCommit}.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey())
* .addColumn('first_name', 'varchar(64)', col => col.notNull())
* .addColumn('last_name', 'varchar(64)', col => col.notNull())
* .modifyEnd(sql`collate utf8_unicode_ci`)
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key,
* `first_name` varchar(64) not null,
* `last_name` varchar(64) not null
* ) collate utf8_unicode_ci
* ```
*/
modifyEnd(modifier: Expression<any>): CreateTableBuilder<TB, C>;
/**
* Allows to create table from `select` query.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('copy')
* .temporary()
* .as(db.selectFrom('person').select(['first_name', 'last_name']))
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create temporary table "copy" as
* select "first_name", "last_name" from "person"
* ```
*/
as(expression: Expression<unknown>): CreateTableBuilder<TB, C>;
/**
* Calls the given function passing `this` as the only argument.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('test')
* .$call((builder) => builder.addColumn('id', 'integer'))
* .execute()
* ```
*
* This is useful for creating reusable functions that can be called with a builder.
*
* ```ts
* import { type CreateTableBuilder, sql } from 'kysely'
*
* const addDefaultColumns = (ctb: CreateTableBuilder<any, any>) => {
* return ctb
* .addColumn('id', 'integer', (col) => col.notNull())
* .addColumn('created_at', 'date', (col) =>
* col.notNull().defaultTo(sql`now()`)
* )
* .addColumn('updated_at', 'date', (col) =>
* col.notNull().defaultTo(sql`now()`)
* )
* }
*
* await db.schema
* .createTable('test')
* .$call(addDefaultColumns)
* .execute()
* ```
*/
$call<T>(func: (qb: this) => T): T;
toOperationNode(): CreateTableNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface CreateTableBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: CreateTableNode;
}
export type ColumnBuilderCallback = (builder: ColumnDefinitionBuilder) => ColumnDefinitionBuilder;

View File

@@ -0,0 +1,411 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateTableBuilder = void 0;
const column_definition_node_js_1 = require("../operation-node/column-definition-node.js");
const create_table_node_js_1 = require("../operation-node/create-table-node.js");
const column_definition_builder_js_1 = require("./column-definition-builder.js");
const object_utils_js_1 = require("../util/object-utils.js");
const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
const column_node_js_1 = require("../operation-node/column-node.js");
const foreign_key_constraint_builder_js_1 = require("./foreign-key-constraint-builder.js");
const data_type_parser_js_1 = require("../parser/data-type-parser.js");
const primary_key_constraint_node_js_1 = require("../operation-node/primary-key-constraint-node.js");
const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
const table_parser_js_1 = require("../parser/table-parser.js");
const on_commit_action_parse_js_1 = require("../parser/on-commit-action-parse.js");
const unique_constraint_builder_js_1 = require("./unique-constraint-builder.js");
const expression_parser_js_1 = require("../parser/expression-parser.js");
const primary_key_constraint_builder_js_1 = require("./primary-key-constraint-builder.js");
const check_constraint_builder_js_1 = require("./check-constraint-builder.js");
/**
* This builder can be used to create a `create table` query.
*/
class CreateTableBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
/**
* Adds the "temporary" modifier.
*
* Use this to create a temporary table.
*/
temporary() {
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWith(this.#props.node, {
temporary: true,
}),
});
}
/**
* Adds an "on commit" statement.
*
* This can be used in conjunction with temporary tables on supported databases
* like PostgreSQL.
*/
onCommit(onCommit) {
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWith(this.#props.node, {
onCommit: (0, on_commit_action_parse_js_1.parseOnCommitAction)(onCommit),
}),
});
}
/**
* Adds the "if not exists" modifier.
*
* If the table already exists, no error is thrown if this method has been called.
*/
ifNotExists() {
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWith(this.#props.node, {
ifNotExists: true,
}),
});
}
/**
* Adds a column to the table.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', (col) => col.autoIncrement().primaryKey())
* .addColumn('first_name', 'varchar(50)', (col) => col.notNull())
* .addColumn('last_name', 'varchar(255)')
* .addColumn('bank_balance', 'numeric(8, 2)')
* // You can specify any data type using the `sql` tag if the types
* // don't include it.
* .addColumn('data', sql`any_type_here`)
* .addColumn('parent_id', 'integer', (col) =>
* col.references('person.id').onDelete('cascade')
* )
* ```
*
* With this method, it's once again good to remember that Kysely just builds the
* query and doesn't provide the same API for all databases. For example, some
* databases like older MySQL don't support the `references` statement in the
* column definition. Instead foreign key constraints need to be defined in the
* `create table` query. See the next example:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', (col) => col.primaryKey())
* .addColumn('parent_id', 'integer')
* .addForeignKeyConstraint(
* 'person_parent_id_fk',
* ['parent_id'],
* 'person',
* ['id'],
* (cb) => cb.onDelete('cascade')
* )
* .execute()
* ```
*
* Another good example is that PostgreSQL doesn't support the `auto_increment`
* keyword and you need to define an autoincrementing column for example using
* `serial`:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'serial', (col) => col.primaryKey())
* .execute()
* ```
*/
addColumn(columnName, dataType, build = object_utils_js_1.noop) {
const columnBuilder = build(new column_definition_builder_js_1.ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.create(columnName, (0, data_type_parser_js_1.parseDataTypeExpression)(dataType))));
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWithColumn(this.#props.node, columnBuilder.toOperationNode()),
});
}
/**
* 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.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(64)')
* .addColumn('last_name', 'varchar(64)')
* .addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
* .execute()
* ```
*/
addPrimaryKeyConstraint(constraintName, columns, build = object_utils_js_1.noop) {
const constraintBuilder = build(new primary_key_constraint_builder_js_1.PrimaryKeyConstraintBuilder(primary_key_constraint_node_js_1.PrimaryKeyConstraintNode.create(columns, constraintName)));
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.node, constraintBuilder.toOperationNode()),
});
}
/**
* 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.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(64)')
* .addColumn('last_name', 'varchar(64)')
* .addUniqueConstraint(
* 'first_name_last_name_unique',
* ['first_name', 'last_name']
* )
* .execute()
* ```
*
* In dialects such as PostgreSQL you can specify `nulls not distinct` as follows:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('first_name', 'varchar(64)')
* .addColumn('last_name', 'varchar(64)')
* .addUniqueConstraint(
* 'first_name_last_name_unique',
* ['first_name', 'last_name'],
* (cb) => cb.nullsNotDistinct()
* )
* .execute()
* ```
*/
addUniqueConstraint(constraintName, columns, build = object_utils_js_1.noop) {
const uniqueConstraintBuilder = build(new unique_constraint_builder_js_1.UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.create(columns, constraintName)));
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.node, uniqueConstraintBuilder.toOperationNode()),
});
}
/**
* Adds a check constraint.
*
* The constraint name can be anything you want, but it must be unique
* across the whole database.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('animal')
* .addColumn('number_of_legs', 'integer')
* .addCheckConstraint('check_legs', sql`number_of_legs < 5`)
* .execute()
* ```
*/
addCheckConstraint(constraintName, checkExpression, build = object_utils_js_1.noop) {
const constraintBuilder = build(new check_constraint_builder_js_1.CheckConstraintBuilder(check_constraint_node_js_1.CheckConstraintNode.create(checkExpression.toOperationNode(), constraintName)));
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.node, constraintBuilder.toOperationNode()),
});
}
/**
* Adds a foreign key constraint.
*
* The constraint name can be anything you want, but it must be unique
* across the whole database.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('owner_id', 'integer')
* .addForeignKeyConstraint(
* 'owner_id_foreign',
* ['owner_id'],
* 'person',
* ['id'],
* )
* .execute()
* ```
*
* Add constraint for multiple columns:
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('owner_id1', 'integer')
* .addColumn('owner_id2', 'integer')
* .addForeignKeyConstraint(
* 'owner_id_foreign',
* ['owner_id1', 'owner_id2'],
* 'person',
* ['id1', 'id2'],
* (cb) => cb.onDelete('cascade')
* )
* .execute()
* ```
*/
addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build = object_utils_js_1.noop) {
const builder = build(new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), (0, table_parser_js_1.parseTable)(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.node, builder.toOperationNode()),
});
}
/**
* This can be used to add any additional SQL to the front of the query __after__ the `create` keyword.
*
* Also see {@link temporary}.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .modifyFront(sql`global temporary`)
* .addColumn('id', 'integer', col => col.primaryKey())
* .addColumn('first_name', 'varchar(64)', col => col.notNull())
* .addColumn('last_name', 'varchar(64)', col => col.notNull())
* .execute()
* ```
*
* The generated SQL (Postgres):
*
* ```sql
* create global temporary table "person" (
* "id" integer primary key,
* "first_name" varchar(64) not null,
* "last_name" varchar(64) not null
* )
* ```
*/
modifyFront(modifier) {
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWithFrontModifier(this.#props.node, modifier.toOperationNode()),
});
}
/**
* This can be used to add any additional SQL to the end of the query.
*
* Also see {@link onCommit}.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey())
* .addColumn('first_name', 'varchar(64)', col => col.notNull())
* .addColumn('last_name', 'varchar(64)', col => col.notNull())
* .modifyEnd(sql`collate utf8_unicode_ci`)
* .execute()
* ```
*
* The generated SQL (MySQL):
*
* ```sql
* create table `person` (
* `id` integer primary key,
* `first_name` varchar(64) not null,
* `last_name` varchar(64) not null
* ) collate utf8_unicode_ci
* ```
*/
modifyEnd(modifier) {
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWithEndModifier(this.#props.node, modifier.toOperationNode()),
});
}
/**
* Allows to create table from `select` query.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('copy')
* .temporary()
* .as(db.selectFrom('person').select(['first_name', 'last_name']))
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* create temporary table "copy" as
* select "first_name", "last_name" from "person"
* ```
*/
as(expression) {
return new CreateTableBuilder({
...this.#props,
node: create_table_node_js_1.CreateTableNode.cloneWith(this.#props.node, {
selectQuery: (0, expression_parser_js_1.parseExpression)(expression),
}),
});
}
/**
* Calls the given function passing `this` as the only argument.
*
* ### Examples
*
* ```ts
* await db.schema
* .createTable('test')
* .$call((builder) => builder.addColumn('id', 'integer'))
* .execute()
* ```
*
* This is useful for creating reusable functions that can be called with a builder.
*
* ```ts
* import { type CreateTableBuilder, sql } from 'kysely'
*
* const addDefaultColumns = (ctb: CreateTableBuilder<any, any>) => {
* return ctb
* .addColumn('id', 'integer', (col) => col.notNull())
* .addColumn('created_at', 'date', (col) =>
* col.notNull().defaultTo(sql`now()`)
* )
* .addColumn('updated_at', 'date', (col) =>
* col.notNull().defaultTo(sql`now()`)
* )
* }
*
* await db.schema
* .createTable('test')
* .$call(addDefaultColumns)
* .execute()
* ```
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.CreateTableBuilder = CreateTableBuilder;

View File

@@ -0,0 +1,33 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
import { CreateTypeNode } from '../operation-node/create-type-node.js';
export declare class CreateTypeBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: CreateTypeBuilderProps);
toOperationNode(): CreateTypeNode;
/**
* Creates an anum type.
*
* ### Examples
*
* ```ts
* db.schema.createType('species').asEnum(['cat', 'dog', 'frog'])
* ```
*/
asEnum(values: readonly string[]): CreateTypeBuilder;
/**
* 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;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface CreateTypeBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: CreateTypeNode;
}

View File

@@ -0,0 +1,43 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateTypeBuilder = void 0;
const object_utils_js_1 = require("../util/object-utils.js");
const create_type_node_js_1 = require("../operation-node/create-type-node.js");
class CreateTypeBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
/**
* Creates an anum type.
*
* ### Examples
*
* ```ts
* db.schema.createType('species').asEnum(['cat', 'dog', 'frog'])
* ```
*/
asEnum(values) {
return new CreateTypeBuilder({
...this.#props,
node: create_type_node_js_1.CreateTypeNode.cloneWithEnum(this.#props.node, values),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.CreateTypeBuilder = CreateTypeBuilder;

View File

@@ -0,0 +1,48 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
import { CreateViewNode } from '../operation-node/create-view-node.js';
import type { RawBuilder } from '../raw-builder/raw-builder.js';
import type { SelectQueryBuilder } from '../query-builder/select-query-builder.js';
export declare class CreateViewBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: CreateViewBuilderProps);
/**
* Adds the "temporary" modifier.
*
* Use this to create a temporary view.
*/
temporary(): CreateViewBuilder;
materialized(): CreateViewBuilder;
/**
* Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
*/
ifNotExists(): CreateViewBuilder;
orReplace(): CreateViewBuilder;
columns(columns: string[]): CreateViewBuilder;
/**
* Sets the select query or a `values` statement that creates the view.
*
* WARNING!
* Some dialects don't support parameterized queries in DDL statements and therefore
* the query or raw {@link sql } expression passed here is interpolated into a single
* string opening an SQL injection vulnerability. DO NOT pass unchecked user input
* into the query or raw expression passed to this method!
*/
as(query: SelectQueryBuilder<any, any, any> | RawBuilder<any>): CreateViewBuilder;
/**
* 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(): CreateViewNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface CreateViewBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: CreateViewNode;
}

View File

@@ -0,0 +1,98 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateViewBuilder = void 0;
const object_utils_js_1 = require("../util/object-utils.js");
const create_view_node_js_1 = require("../operation-node/create-view-node.js");
const reference_parser_js_1 = require("../parser/reference-parser.js");
const immediate_value_plugin_js_1 = require("../plugin/immediate-value/immediate-value-plugin.js");
class CreateViewBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
/**
* Adds the "temporary" modifier.
*
* Use this to create a temporary view.
*/
temporary() {
return new CreateViewBuilder({
...this.#props,
node: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.node, {
temporary: true,
}),
});
}
materialized() {
return new CreateViewBuilder({
...this.#props,
node: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.node, {
materialized: true,
}),
});
}
/**
* Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
*/
ifNotExists() {
return new CreateViewBuilder({
...this.#props,
node: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.node, {
ifNotExists: true,
}),
});
}
orReplace() {
return new CreateViewBuilder({
...this.#props,
node: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.node, {
orReplace: true,
}),
});
}
columns(columns) {
return new CreateViewBuilder({
...this.#props,
node: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.node, {
columns: columns.map(reference_parser_js_1.parseColumnName),
}),
});
}
/**
* Sets the select query or a `values` statement that creates the view.
*
* WARNING!
* Some dialects don't support parameterized queries in DDL statements and therefore
* the query or raw {@link sql } expression passed here is interpolated into a single
* string opening an SQL injection vulnerability. DO NOT pass unchecked user input
* into the query or raw expression passed to this method!
*/
as(query) {
const queryNode = query
.withPlugin(new immediate_value_plugin_js_1.ImmediateValuePlugin())
.toOperationNode();
return new CreateViewBuilder({
...this.#props,
node: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.node, {
as: queryNode,
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.CreateViewBuilder = CreateViewBuilder;

View File

@@ -0,0 +1,30 @@
import { DropIndexNode } from '../operation-node/drop-index-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
export declare class DropIndexBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: DropIndexBuilderProps);
/**
* Specifies the table the index was created for. This is not needed
* in all dialects.
*/
on(table: string): DropIndexBuilder;
ifExists(): DropIndexBuilder;
cascade(): DropIndexBuilder;
/**
* 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(): DropIndexNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface DropIndexBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: DropIndexNode;
}

View File

@@ -0,0 +1,57 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropIndexBuilder = void 0;
const drop_index_node_js_1 = require("../operation-node/drop-index-node.js");
const table_parser_js_1 = require("../parser/table-parser.js");
const object_utils_js_1 = require("../util/object-utils.js");
class DropIndexBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
/**
* Specifies the table the index was created for. This is not needed
* in all dialects.
*/
on(table) {
return new DropIndexBuilder({
...this.#props,
node: drop_index_node_js_1.DropIndexNode.cloneWith(this.#props.node, {
table: (0, table_parser_js_1.parseTable)(table),
}),
});
}
ifExists() {
return new DropIndexBuilder({
...this.#props,
node: drop_index_node_js_1.DropIndexNode.cloneWith(this.#props.node, {
ifExists: true,
}),
});
}
cascade() {
return new DropIndexBuilder({
...this.#props,
node: drop_index_node_js_1.DropIndexNode.cloneWith(this.#props.node, {
cascade: true,
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.DropIndexBuilder = DropIndexBuilder;

View File

@@ -0,0 +1,25 @@
import { DropSchemaNode } from '../operation-node/drop-schema-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
export declare class DropSchemaBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: DropSchemaBuilderProps);
ifExists(): DropSchemaBuilder;
cascade(): DropSchemaBuilder;
/**
* 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(): DropSchemaNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface DropSchemaBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: DropSchemaNode;
}

View File

@@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropSchemaBuilder = void 0;
const drop_schema_node_js_1 = require("../operation-node/drop-schema-node.js");
const object_utils_js_1 = require("../util/object-utils.js");
class DropSchemaBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
ifExists() {
return new DropSchemaBuilder({
...this.#props,
node: drop_schema_node_js_1.DropSchemaNode.cloneWith(this.#props.node, {
ifExists: true,
}),
});
}
cascade() {
return new DropSchemaBuilder({
...this.#props,
node: drop_schema_node_js_1.DropSchemaNode.cloneWith(this.#props.node, {
cascade: true,
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.DropSchemaBuilder = DropSchemaBuilder;

View File

@@ -0,0 +1,25 @@
import { DropTableNode } from '../operation-node/drop-table-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
export declare class DropTableBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: DropTableBuilderProps);
ifExists(): DropTableBuilder;
cascade(): DropTableBuilder;
/**
* 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(): DropTableNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface DropTableBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: DropTableNode;
}

View File

@@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropTableBuilder = void 0;
const drop_table_node_js_1 = require("../operation-node/drop-table-node.js");
const object_utils_js_1 = require("../util/object-utils.js");
class DropTableBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
ifExists() {
return new DropTableBuilder({
...this.#props,
node: drop_table_node_js_1.DropTableNode.cloneWith(this.#props.node, {
ifExists: true,
}),
});
}
cascade() {
return new DropTableBuilder({
...this.#props,
node: drop_table_node_js_1.DropTableNode.cloneWith(this.#props.node, {
cascade: true,
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.DropTableBuilder = DropTableBuilder;

View File

@@ -0,0 +1,24 @@
import { DropTypeNode } from '../operation-node/drop-type-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
export declare class DropTypeBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: DropTypeBuilderProps);
ifExists(): DropTypeBuilder;
/**
* 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(): DropTypeNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface DropTypeBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: DropTypeNode;
}

View File

@@ -0,0 +1,36 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropTypeBuilder = void 0;
const drop_type_node_js_1 = require("../operation-node/drop-type-node.js");
const object_utils_js_1 = require("../util/object-utils.js");
class DropTypeBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
ifExists() {
return new DropTypeBuilder({
...this.#props,
node: drop_type_node_js_1.DropTypeNode.cloneWith(this.#props.node, {
ifExists: true,
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.DropTypeBuilder = DropTypeBuilder;

View File

@@ -0,0 +1,26 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
import { DropViewNode } from '../operation-node/drop-view-node.js';
export declare class DropViewBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: DropViewBuilderProps);
materialized(): DropViewBuilder;
ifExists(): DropViewBuilder;
cascade(): DropViewBuilder;
/**
* 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(): DropViewNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface DropViewBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: DropViewNode;
}

View File

@@ -0,0 +1,52 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropViewBuilder = void 0;
const object_utils_js_1 = require("../util/object-utils.js");
const drop_view_node_js_1 = require("../operation-node/drop-view-node.js");
class DropViewBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
materialized() {
return new DropViewBuilder({
...this.#props,
node: drop_view_node_js_1.DropViewNode.cloneWith(this.#props.node, {
materialized: true,
}),
});
}
ifExists() {
return new DropViewBuilder({
...this.#props,
node: drop_view_node_js_1.DropViewNode.cloneWith(this.#props.node, {
ifExists: true,
}),
});
}
cascade() {
return new DropViewBuilder({
...this.#props,
node: drop_view_node_js_1.DropViewNode.cloneWith(this.#props.node, {
cascade: true,
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.DropViewBuilder = DropViewBuilder;

View File

@@ -0,0 +1,28 @@
import { ForeignKeyConstraintNode } from '../operation-node/foreign-key-constraint-node.js';
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { OnModifyForeignAction } from '../operation-node/references-node.js';
export interface ForeignKeyConstraintBuilderInterface<R> {
onDelete(onDelete: OnModifyForeignAction): R;
onUpdate(onUpdate: OnModifyForeignAction): R;
deferrable(): R;
notDeferrable(): R;
initiallyDeferred(): R;
initiallyImmediate(): R;
}
export declare class ForeignKeyConstraintBuilder implements ForeignKeyConstraintBuilderInterface<ForeignKeyConstraintBuilder>, OperationNodeSource {
#private;
constructor(node: ForeignKeyConstraintNode);
onDelete(onDelete: OnModifyForeignAction): ForeignKeyConstraintBuilder;
onUpdate(onUpdate: OnModifyForeignAction): ForeignKeyConstraintBuilder;
deferrable(): ForeignKeyConstraintBuilder;
notDeferrable(): ForeignKeyConstraintBuilder;
initiallyDeferred(): ForeignKeyConstraintBuilder;
initiallyImmediate(): ForeignKeyConstraintBuilder;
/**
* 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(): ForeignKeyConstraintNode;
}
export type ForeignKeyConstraintBuilderCallback = (builder: ForeignKeyConstraintBuilder) => ForeignKeyConstraintBuilder;

View File

@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ForeignKeyConstraintBuilder = void 0;
const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
const on_modify_action_parser_js_1 = require("../parser/on-modify-action-parser.js");
class ForeignKeyConstraintBuilder {
#node;
constructor(node) {
this.#node = node;
}
onDelete(onDelete) {
return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, {
onDelete: (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onDelete),
}));
}
onUpdate(onUpdate) {
return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, {
onUpdate: (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onUpdate),
}));
}
deferrable() {
return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, { deferrable: true }));
}
notDeferrable() {
return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, { deferrable: false }));
}
initiallyDeferred() {
return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, {
initiallyDeferred: true,
}));
}
initiallyImmediate() {
return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, {
initiallyDeferred: false,
}));
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#node;
}
}
exports.ForeignKeyConstraintBuilder = ForeignKeyConstraintBuilder;

View File

@@ -0,0 +1,17 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import { PrimaryKeyConstraintNode } from '../operation-node/primary-key-constraint-node.js';
export declare class PrimaryKeyConstraintBuilder implements OperationNodeSource {
#private;
constructor(node: PrimaryKeyConstraintNode);
deferrable(): PrimaryKeyConstraintBuilder;
notDeferrable(): PrimaryKeyConstraintBuilder;
initiallyDeferred(): PrimaryKeyConstraintBuilder;
initiallyImmediate(): PrimaryKeyConstraintBuilder;
/**
* 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(): PrimaryKeyConstraintNode;
}
export type PrimaryKeyConstraintBuilderCallback = (builder: PrimaryKeyConstraintBuilder) => PrimaryKeyConstraintBuilder;

View File

@@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrimaryKeyConstraintBuilder = void 0;
const primary_key_constraint_node_js_1 = require("../operation-node/primary-key-constraint-node.js");
class PrimaryKeyConstraintBuilder {
#node;
constructor(node) {
this.#node = node;
}
deferrable() {
return new PrimaryKeyConstraintBuilder(primary_key_constraint_node_js_1.PrimaryKeyConstraintNode.cloneWith(this.#node, { deferrable: true }));
}
notDeferrable() {
return new PrimaryKeyConstraintBuilder(primary_key_constraint_node_js_1.PrimaryKeyConstraintNode.cloneWith(this.#node, { deferrable: false }));
}
initiallyDeferred() {
return new PrimaryKeyConstraintBuilder(primary_key_constraint_node_js_1.PrimaryKeyConstraintNode.cloneWith(this.#node, {
initiallyDeferred: true,
}));
}
initiallyImmediate() {
return new PrimaryKeyConstraintBuilder(primary_key_constraint_node_js_1.PrimaryKeyConstraintNode.cloneWith(this.#node, {
initiallyDeferred: false,
}));
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#node;
}
}
exports.PrimaryKeyConstraintBuilder = PrimaryKeyConstraintBuilder;

View File

@@ -0,0 +1,47 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { Compilable } from '../util/compilable.js';
import type { QueryExecutor } from '../query-executor/query-executor.js';
import type { QueryId } from '../util/query-id.js';
import { RefreshMaterializedViewNode } from '../operation-node/refresh-materialized-view-node.js';
export declare class RefreshMaterializedViewBuilder implements OperationNodeSource, Compilable {
#private;
constructor(props: RefreshMaterializedViewBuilderProps);
/**
* Adds the "concurrently" modifier.
*
* Use this to refresh the view without locking out concurrent selects on the materialized view.
*
* WARNING!
* This cannot be used with the "with no data" modifier.
*/
concurrently(): RefreshMaterializedViewBuilder;
/**
* Adds the "with data" modifier.
*
* If specified (or defaults) the backing query is executed to provide the new data, and the materialized view is left in a scannable state
*/
withData(): RefreshMaterializedViewBuilder;
/**
* Adds the "with no data" modifier.
*
* If specified, no new data is generated and the materialized view is left in an unscannable state.
*
* WARNING!
* This cannot be used with the "concurrently" modifier.
*/
withNoData(): RefreshMaterializedViewBuilder;
/**
* 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(): RefreshMaterializedViewNode;
compile(): CompiledQuery;
execute(): Promise<void>;
}
export interface RefreshMaterializedViewBuilderProps {
readonly queryId: QueryId;
readonly executor: QueryExecutor;
readonly node: RefreshMaterializedViewNode;
}

View File

@@ -0,0 +1,75 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RefreshMaterializedViewBuilder = void 0;
const object_utils_js_1 = require("../util/object-utils.js");
const refresh_materialized_view_node_js_1 = require("../operation-node/refresh-materialized-view-node.js");
class RefreshMaterializedViewBuilder {
#props;
constructor(props) {
this.#props = (0, object_utils_js_1.freeze)(props);
}
/**
* Adds the "concurrently" modifier.
*
* Use this to refresh the view without locking out concurrent selects on the materialized view.
*
* WARNING!
* This cannot be used with the "with no data" modifier.
*/
concurrently() {
return new RefreshMaterializedViewBuilder({
...this.#props,
node: refresh_materialized_view_node_js_1.RefreshMaterializedViewNode.cloneWith(this.#props.node, {
concurrently: true,
withNoData: false,
}),
});
}
/**
* Adds the "with data" modifier.
*
* If specified (or defaults) the backing query is executed to provide the new data, and the materialized view is left in a scannable state
*/
withData() {
return new RefreshMaterializedViewBuilder({
...this.#props,
node: refresh_materialized_view_node_js_1.RefreshMaterializedViewNode.cloneWith(this.#props.node, {
withNoData: false,
}),
});
}
/**
* Adds the "with no data" modifier.
*
* If specified, no new data is generated and the materialized view is left in an unscannable state.
*
* WARNING!
* This cannot be used with the "concurrently" modifier.
*/
withNoData() {
return new RefreshMaterializedViewBuilder({
...this.#props,
node: refresh_materialized_view_node_js_1.RefreshMaterializedViewNode.cloneWith(this.#props.node, {
withNoData: true,
concurrently: false,
}),
});
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
}
compile() {
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
}
async execute() {
await this.#props.executor.executeQuery(this.compile());
}
}
exports.RefreshMaterializedViewBuilder = RefreshMaterializedViewBuilder;

230
node_modules/kysely/dist/cjs/schema/schema.d.ts generated vendored Normal file
View File

@@ -0,0 +1,230 @@
import type { QueryExecutor } from '../query-executor/query-executor.js';
import { AlterTableBuilder } from './alter-table-builder.js';
import { CreateIndexBuilder } from './create-index-builder.js';
import { CreateSchemaBuilder } from './create-schema-builder.js';
import { CreateTableBuilder } from './create-table-builder.js';
import { DropIndexBuilder } from './drop-index-builder.js';
import { DropSchemaBuilder } from './drop-schema-builder.js';
import { DropTableBuilder } from './drop-table-builder.js';
import { CreateViewBuilder } from './create-view-builder.js';
import { DropViewBuilder } from './drop-view-builder.js';
import type { KyselyPlugin } from '../plugin/kysely-plugin.js';
import { CreateTypeBuilder } from './create-type-builder.js';
import { DropTypeBuilder } from './drop-type-builder.js';
import { RefreshMaterializedViewBuilder } from './refresh-materialized-view-builder.js';
/**
* Provides methods for building database schema.
*/
export declare class SchemaModule {
#private;
constructor(executor: QueryExecutor);
/**
* Create a new table.
*
* ### Examples
*
* This example creates a new table with columns `id`, `first_name`,
* `last_name` and `gender`:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
* .addColumn('first_name', 'varchar', col => col.notNull())
* .addColumn('last_name', 'varchar', col => col.notNull())
* .addColumn('gender', 'varchar')
* .execute()
* ```
*
* This example creates a table with a foreign key. Not all database
* engines support column-level foreign key constraint definitions.
* For example if you are using MySQL 5.X see the next example after
* this one.
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
* .addColumn('owner_id', 'integer', col => col
* .references('person.id')
* .onDelete('cascade')
* )
* .execute()
* ```
*
* This example adds a foreign key constraint for a columns just
* like the previous example, but using a table-level statement.
* On MySQL 5.X you need to define foreign key constraints like
* this:
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
* .addColumn('owner_id', 'integer')
* .addForeignKeyConstraint(
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
* (constraint) => constraint.onDelete('cascade')
* )
* .execute()
* ```
*/
createTable<TB extends string>(table: TB): CreateTableBuilder<TB, never>;
/**
* Drop a table.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropTable('person')
* .execute()
* ```
*/
dropTable(table: string): DropTableBuilder;
/**
* Create a new index.
*
* ### Examples
*
* ```ts
* await db.schema
* .createIndex('person_full_name_unique_index')
* .on('person')
* .columns(['first_name', 'last_name'])
* .execute()
* ```
*/
createIndex(indexName: string): CreateIndexBuilder;
/**
* Drop an index.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropIndex('person_full_name_unique_index')
* .execute()
* ```
*/
dropIndex(indexName: string): DropIndexBuilder;
/**
* Create a new schema.
*
* ### Examples
*
* ```ts
* await db.schema
* .createSchema('some_schema')
* .execute()
* ```
*/
createSchema(schema: string): CreateSchemaBuilder;
/**
* Drop a schema.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropSchema('some_schema')
* .execute()
* ```
*/
dropSchema(schema: string): DropSchemaBuilder;
/**
* Alter a table.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .alterColumn('first_name', (ac) => ac.setDataType('text'))
* .execute()
* ```
*/
alterTable(table: string): AlterTableBuilder;
/**
* Create a new view.
*
* ### Examples
*
* ```ts
* await db.schema
* .createView('dogs')
* .orReplace()
* .as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
* .execute()
* ```
*/
createView(viewName: string): CreateViewBuilder;
/**
* Refresh a materialized view.
*
* ### Examples
*
* ```ts
* await db.schema
* .refreshMaterializedView('my_view')
* .concurrently()
* .execute()
* ```
*/
refreshMaterializedView(viewName: string): RefreshMaterializedViewBuilder;
/**
* Drop a view.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropView('dogs')
* .ifExists()
* .execute()
* ```
*/
dropView(viewName: string): DropViewBuilder;
/**
* Create a new type.
*
* Only some dialects like PostgreSQL have user-defined types.
*
* ### Examples
*
* ```ts
* await db.schema
* .createType('species')
* .asEnum(['dog', 'cat', 'frog'])
* .execute()
* ```
*/
createType(typeName: string): CreateTypeBuilder;
/**
* Drop a type.
*
* Only some dialects like PostgreSQL have user-defined types.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropType('species')
* .ifExists()
* .execute()
* ```
*/
dropType(typeName: string): DropTypeBuilder;
/**
* Returns a copy of this schema module with the given plugin installed.
*/
withPlugin(plugin: KyselyPlugin): SchemaModule;
/**
* Returns a copy of this schema module without any plugins.
*/
withoutPlugins(): SchemaModule;
/**
* See {@link QueryCreator.withSchema}
*/
withSchema(schema: string): SchemaModule;
}

328
node_modules/kysely/dist/cjs/schema/schema.js generated vendored Normal file
View File

@@ -0,0 +1,328 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaModule = void 0;
const alter_table_node_js_1 = require("../operation-node/alter-table-node.js");
const create_index_node_js_1 = require("../operation-node/create-index-node.js");
const create_schema_node_js_1 = require("../operation-node/create-schema-node.js");
const create_table_node_js_1 = require("../operation-node/create-table-node.js");
const drop_index_node_js_1 = require("../operation-node/drop-index-node.js");
const drop_schema_node_js_1 = require("../operation-node/drop-schema-node.js");
const drop_table_node_js_1 = require("../operation-node/drop-table-node.js");
const table_parser_js_1 = require("../parser/table-parser.js");
const alter_table_builder_js_1 = require("./alter-table-builder.js");
const create_index_builder_js_1 = require("./create-index-builder.js");
const create_schema_builder_js_1 = require("./create-schema-builder.js");
const create_table_builder_js_1 = require("./create-table-builder.js");
const drop_index_builder_js_1 = require("./drop-index-builder.js");
const drop_schema_builder_js_1 = require("./drop-schema-builder.js");
const drop_table_builder_js_1 = require("./drop-table-builder.js");
const query_id_js_1 = require("../util/query-id.js");
const with_schema_plugin_js_1 = require("../plugin/with-schema/with-schema-plugin.js");
const create_view_builder_js_1 = require("./create-view-builder.js");
const create_view_node_js_1 = require("../operation-node/create-view-node.js");
const drop_view_builder_js_1 = require("./drop-view-builder.js");
const drop_view_node_js_1 = require("../operation-node/drop-view-node.js");
const create_type_builder_js_1 = require("./create-type-builder.js");
const drop_type_builder_js_1 = require("./drop-type-builder.js");
const create_type_node_js_1 = require("../operation-node/create-type-node.js");
const drop_type_node_js_1 = require("../operation-node/drop-type-node.js");
const identifier_parser_js_1 = require("../parser/identifier-parser.js");
const refresh_materialized_view_builder_js_1 = require("./refresh-materialized-view-builder.js");
const refresh_materialized_view_node_js_1 = require("../operation-node/refresh-materialized-view-node.js");
/**
* Provides methods for building database schema.
*/
class SchemaModule {
#executor;
constructor(executor) {
this.#executor = executor;
}
/**
* Create a new table.
*
* ### Examples
*
* This example creates a new table with columns `id`, `first_name`,
* `last_name` and `gender`:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
* .addColumn('first_name', 'varchar', col => col.notNull())
* .addColumn('last_name', 'varchar', col => col.notNull())
* .addColumn('gender', 'varchar')
* .execute()
* ```
*
* This example creates a table with a foreign key. Not all database
* engines support column-level foreign key constraint definitions.
* For example if you are using MySQL 5.X see the next example after
* this one.
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
* .addColumn('owner_id', 'integer', col => col
* .references('person.id')
* .onDelete('cascade')
* )
* .execute()
* ```
*
* This example adds a foreign key constraint for a columns just
* like the previous example, but using a table-level statement.
* On MySQL 5.X you need to define foreign key constraints like
* this:
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
* .addColumn('owner_id', 'integer')
* .addForeignKeyConstraint(
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
* (constraint) => constraint.onDelete('cascade')
* )
* .execute()
* ```
*/
createTable(table) {
return new create_table_builder_js_1.CreateTableBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: create_table_node_js_1.CreateTableNode.create((0, table_parser_js_1.parseTable)(table)),
});
}
/**
* Drop a table.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropTable('person')
* .execute()
* ```
*/
dropTable(table) {
return new drop_table_builder_js_1.DropTableBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: drop_table_node_js_1.DropTableNode.create((0, table_parser_js_1.parseTable)(table)),
});
}
/**
* Create a new index.
*
* ### Examples
*
* ```ts
* await db.schema
* .createIndex('person_full_name_unique_index')
* .on('person')
* .columns(['first_name', 'last_name'])
* .execute()
* ```
*/
createIndex(indexName) {
return new create_index_builder_js_1.CreateIndexBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: create_index_node_js_1.CreateIndexNode.create(indexName),
});
}
/**
* Drop an index.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropIndex('person_full_name_unique_index')
* .execute()
* ```
*/
dropIndex(indexName) {
return new drop_index_builder_js_1.DropIndexBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: drop_index_node_js_1.DropIndexNode.create(indexName),
});
}
/**
* Create a new schema.
*
* ### Examples
*
* ```ts
* await db.schema
* .createSchema('some_schema')
* .execute()
* ```
*/
createSchema(schema) {
return new create_schema_builder_js_1.CreateSchemaBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: create_schema_node_js_1.CreateSchemaNode.create(schema),
});
}
/**
* Drop a schema.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropSchema('some_schema')
* .execute()
* ```
*/
dropSchema(schema) {
return new drop_schema_builder_js_1.DropSchemaBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: drop_schema_node_js_1.DropSchemaNode.create(schema),
});
}
/**
* Alter a table.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .alterColumn('first_name', (ac) => ac.setDataType('text'))
* .execute()
* ```
*/
alterTable(table) {
return new alter_table_builder_js_1.AlterTableBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: alter_table_node_js_1.AlterTableNode.create((0, table_parser_js_1.parseTable)(table)),
});
}
/**
* Create a new view.
*
* ### Examples
*
* ```ts
* await db.schema
* .createView('dogs')
* .orReplace()
* .as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
* .execute()
* ```
*/
createView(viewName) {
return new create_view_builder_js_1.CreateViewBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: create_view_node_js_1.CreateViewNode.create(viewName),
});
}
/**
* Refresh a materialized view.
*
* ### Examples
*
* ```ts
* await db.schema
* .refreshMaterializedView('my_view')
* .concurrently()
* .execute()
* ```
*/
refreshMaterializedView(viewName) {
return new refresh_materialized_view_builder_js_1.RefreshMaterializedViewBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: refresh_materialized_view_node_js_1.RefreshMaterializedViewNode.create(viewName),
});
}
/**
* Drop a view.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropView('dogs')
* .ifExists()
* .execute()
* ```
*/
dropView(viewName) {
return new drop_view_builder_js_1.DropViewBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: drop_view_node_js_1.DropViewNode.create(viewName),
});
}
/**
* Create a new type.
*
* Only some dialects like PostgreSQL have user-defined types.
*
* ### Examples
*
* ```ts
* await db.schema
* .createType('species')
* .asEnum(['dog', 'cat', 'frog'])
* .execute()
* ```
*/
createType(typeName) {
return new create_type_builder_js_1.CreateTypeBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: create_type_node_js_1.CreateTypeNode.create((0, identifier_parser_js_1.parseSchemableIdentifier)(typeName)),
});
}
/**
* Drop a type.
*
* Only some dialects like PostgreSQL have user-defined types.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropType('species')
* .ifExists()
* .execute()
* ```
*/
dropType(typeName) {
return new drop_type_builder_js_1.DropTypeBuilder({
queryId: (0, query_id_js_1.createQueryId)(),
executor: this.#executor,
node: drop_type_node_js_1.DropTypeNode.create((0, identifier_parser_js_1.parseSchemableIdentifier)(typeName)),
});
}
/**
* Returns a copy of this schema module with the given plugin installed.
*/
withPlugin(plugin) {
return new SchemaModule(this.#executor.withPlugin(plugin));
}
/**
* Returns a copy of this schema module without any plugins.
*/
withoutPlugins() {
return new SchemaModule(this.#executor.withoutPlugins());
}
/**
* See {@link QueryCreator.withSchema}
*/
withSchema(schema) {
return new SchemaModule(this.#executor.withPluginAtFront(new with_schema_plugin_js_1.WithSchemaPlugin(schema)));
}
}
exports.SchemaModule = SchemaModule;

View File

@@ -0,0 +1,23 @@
import type { OperationNodeSource } from '../operation-node/operation-node-source.js';
import { UniqueConstraintNode } from '../operation-node/unique-constraint-node.js';
export declare class UniqueConstraintNodeBuilder implements OperationNodeSource {
#private;
constructor(node: UniqueConstraintNode);
/**
* Adds `nulls not distinct` to the unique constraint definition
*
* Supported by PostgreSQL dialect only
*/
nullsNotDistinct(): UniqueConstraintNodeBuilder;
deferrable(): UniqueConstraintNodeBuilder;
notDeferrable(): UniqueConstraintNodeBuilder;
initiallyDeferred(): UniqueConstraintNodeBuilder;
initiallyImmediate(): UniqueConstraintNodeBuilder;
/**
* 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(): UniqueConstraintNode;
}
export type UniqueConstraintNodeBuilderCallback = (builder: UniqueConstraintNodeBuilder) => UniqueConstraintNodeBuilder;

View File

@@ -0,0 +1,45 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniqueConstraintNodeBuilder = void 0;
const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
class UniqueConstraintNodeBuilder {
#node;
constructor(node) {
this.#node = node;
}
/**
* Adds `nulls not distinct` to the unique constraint definition
*
* Supported by PostgreSQL dialect only
*/
nullsNotDistinct() {
return new UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.cloneWith(this.#node, { nullsNotDistinct: true }));
}
deferrable() {
return new UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.cloneWith(this.#node, { deferrable: true }));
}
notDeferrable() {
return new UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.cloneWith(this.#node, { deferrable: false }));
}
initiallyDeferred() {
return new UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.cloneWith(this.#node, {
initiallyDeferred: true,
}));
}
initiallyImmediate() {
return new UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.cloneWith(this.#node, {
initiallyDeferred: false,
}));
}
/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
*/
$call(func) {
return func(this);
}
toOperationNode() {
return this.#node;
}
}
exports.UniqueConstraintNodeBuilder = UniqueConstraintNodeBuilder;