kysely 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -4
- package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/cjs/dialect/mysql/mysql-query-compiler.js +0 -29
- package/dist/cjs/operation-node/primitive-value-list-node.js +1 -1
- package/dist/cjs/parser/join-parser.d.ts +6 -3
- package/dist/cjs/parser/table-parser.d.ts +8 -8
- package/dist/cjs/query-builder/function-builder.d.ts +6 -6
- package/dist/cjs/query-builder/function-builder.js +6 -6
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +12 -18
- package/dist/cjs/query-creator.d.ts +6 -3
- package/dist/cjs/query-creator.js +5 -2
- package/dist/cjs/schema/alter-table-builder.d.ts +3 -0
- package/dist/cjs/schema/alter-table-builder.js +3 -0
- package/dist/cjs/schema/column-definition-builder.d.ts +14 -5
- package/dist/cjs/schema/create-table-builder.d.ts +55 -10
- package/dist/cjs/schema/create-table-builder.js +52 -13
- package/dist/cjs/schema/schema.d.ts +37 -2
- package/dist/cjs/schema/schema.js +36 -1
- package/dist/cjs/util/object-utils.d.ts +1 -0
- package/dist/cjs/util/object-utils.js +5 -1
- package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/esm/dialect/mysql/mysql-query-compiler.js +0 -29
- package/dist/esm/operation-node/primitive-value-list-node.js +1 -1
- package/dist/esm/parser/join-parser.d.ts +6 -3
- package/dist/esm/parser/table-parser.d.ts +8 -8
- package/dist/esm/query-builder/function-builder.d.ts +6 -6
- package/dist/esm/query-builder/function-builder.js +6 -6
- package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -0
- package/dist/esm/query-compiler/default-query-compiler.js +12 -18
- package/dist/esm/query-creator.d.ts +6 -3
- package/dist/esm/query-creator.js +5 -2
- package/dist/esm/schema/alter-table-builder.d.ts +3 -0
- package/dist/esm/schema/alter-table-builder.js +3 -0
- package/dist/esm/schema/column-definition-builder.d.ts +14 -5
- package/dist/esm/schema/create-table-builder.d.ts +55 -10
- package/dist/esm/schema/create-table-builder.js +53 -14
- package/dist/esm/schema/schema.d.ts +37 -2
- package/dist/esm/schema/schema.js +36 -1
- package/dist/esm/util/object-utils.d.ts +1 -0
- package/dist/esm/util/object-utils.js +3 -0
- package/package.json +1 -1
|
@@ -11,6 +11,9 @@ const column_node_js_1 = require("../operation-node/column-node.js");
|
|
|
11
11
|
const table_node_js_1 = require("../operation-node/table-node.js");
|
|
12
12
|
const foreign_key_constraint_builder_js_1 = require("./foreign-key-constraint-builder.js");
|
|
13
13
|
const data_type_parser_js_1 = require("../parser/data-type-parser.js");
|
|
14
|
+
/**
|
|
15
|
+
* This builder can be used to create a `create table` query.
|
|
16
|
+
*/
|
|
14
17
|
class CreateTableBuilder {
|
|
15
18
|
#props;
|
|
16
19
|
constructor(props) {
|
|
@@ -34,18 +37,45 @@ class CreateTableBuilder {
|
|
|
34
37
|
* ```ts
|
|
35
38
|
* await db.schema
|
|
36
39
|
* .createTable('person')
|
|
37
|
-
* .addColumn('
|
|
40
|
+
* .addColumn('id', 'integer', (col) => col.increments().primaryKey()),
|
|
38
41
|
* .addColumn('first_name', 'varchar(50), (col) => col.notNull())
|
|
39
|
-
* .addColumn('
|
|
40
|
-
* .addColumn('numeric(8, 2)'
|
|
42
|
+
* .addColumn('last_name', 'varchar')
|
|
43
|
+
* .addColumn('bank_balance', 'numeric(8, 2)')
|
|
41
44
|
* .addColumn('data', db.raw('customtype'))
|
|
45
|
+
* .addColumn('parent_id', 'integer', (col) =>
|
|
46
|
+
* col.references('person.id').onDelete('cascade'))
|
|
47
|
+
* )
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* With this method, it's once again good to remember that Kysely just builds the query
|
|
51
|
+
* that's as close to the structure of your builder method calls as possible, and doesn't
|
|
52
|
+
* provide the same API for all databses. For example, some databases like older MySQL
|
|
53
|
+
* don't support `references` statement in the column definition. Instead foreign key
|
|
54
|
+
* constraints need to be defined in at the level of the `create table` query. See
|
|
55
|
+
* the next example:
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* .addColumn('parent_id', 'integer')
|
|
60
|
+
* .addForeignKeyConstraint(
|
|
61
|
+
* 'person_parent_id_fk', ['parent_id'], 'person', ['id'],
|
|
62
|
+
* (cb) => cb.onDelete('cascade')
|
|
63
|
+
* )
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* Another good example is that PostgreSQL doesn't support the `auto_increment`
|
|
67
|
+
* keyword and you need to define an autoincrementing column for example using
|
|
68
|
+
* `serial`:
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* await db.schema
|
|
73
|
+
* .createTable('person')
|
|
74
|
+
* .addColumn('id', 'serial', (col) => col.primaryKey()),
|
|
42
75
|
* ```
|
|
43
76
|
*/
|
|
44
|
-
addColumn(columnName, dataType, build) {
|
|
45
|
-
|
|
46
|
-
if (build) {
|
|
47
|
-
columnBuilder = build(columnBuilder);
|
|
48
|
-
}
|
|
77
|
+
addColumn(columnName, dataType, build = object_utils_js_1.noop) {
|
|
78
|
+
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))));
|
|
49
79
|
return new CreateTableBuilder({
|
|
50
80
|
...this.#props,
|
|
51
81
|
createTableNode: create_table_node_js_1.CreateTableNode.cloneWithColumn(this.#props.createTableNode, columnBuilder.toOperationNode()),
|
|
@@ -54,6 +84,9 @@ class CreateTableBuilder {
|
|
|
54
84
|
/**
|
|
55
85
|
* Adds a primary key constraint for one or more columns.
|
|
56
86
|
*
|
|
87
|
+
* The constraint name can be anything you want, but it must be unique
|
|
88
|
+
* across the whole database.
|
|
89
|
+
*
|
|
57
90
|
* @example
|
|
58
91
|
* ```ts
|
|
59
92
|
* addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
|
|
@@ -68,6 +101,9 @@ class CreateTableBuilder {
|
|
|
68
101
|
/**
|
|
69
102
|
* Adds a unique constraint for one or more columns.
|
|
70
103
|
*
|
|
104
|
+
* The constraint name can be anything you want, but it must be unique
|
|
105
|
+
* across the whole database.
|
|
106
|
+
*
|
|
71
107
|
* @example
|
|
72
108
|
* ```ts
|
|
73
109
|
* addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
|
|
@@ -82,6 +118,9 @@ class CreateTableBuilder {
|
|
|
82
118
|
/**
|
|
83
119
|
* Adds a check constraint.
|
|
84
120
|
*
|
|
121
|
+
* The constraint name can be anything you want, but it must be unique
|
|
122
|
+
* across the whole database.
|
|
123
|
+
*
|
|
85
124
|
* @example
|
|
86
125
|
* ```ts
|
|
87
126
|
* addCheckConstraint('check_legs', 'number_of_legs < 5')
|
|
@@ -96,6 +135,9 @@ class CreateTableBuilder {
|
|
|
96
135
|
/**
|
|
97
136
|
* Adds a foreign key constraint.
|
|
98
137
|
*
|
|
138
|
+
* The constraint name can be anything you want, but it must be unique
|
|
139
|
+
* across the whole database.
|
|
140
|
+
*
|
|
99
141
|
* @example
|
|
100
142
|
* ```ts
|
|
101
143
|
* addForeignKeyConstraint(
|
|
@@ -117,11 +159,8 @@ class CreateTableBuilder {
|
|
|
117
159
|
* )
|
|
118
160
|
* ```
|
|
119
161
|
*/
|
|
120
|
-
addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build) {
|
|
121
|
-
|
|
122
|
-
if (build) {
|
|
123
|
-
builder = build(builder);
|
|
124
|
-
}
|
|
162
|
+
addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build = object_utils_js_1.noop) {
|
|
163
|
+
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), table_node_js_1.TableNode.create(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
|
|
125
164
|
return new CreateTableBuilder({
|
|
126
165
|
...this.#props,
|
|
127
166
|
createTableNode: create_table_node_js_1.CreateTableNode.cloneWithForeignKeyConstraint(this.#props.createTableNode, builder.toOperationNode()),
|
|
@@ -22,14 +22,49 @@ export declare class SchemaModule {
|
|
|
22
22
|
* ```ts
|
|
23
23
|
* await db.schema
|
|
24
24
|
* .createTable('person')
|
|
25
|
-
* .addColumn('id', '
|
|
25
|
+
* .addColumn('id', 'integer', col => col.primaryKey().increments())
|
|
26
26
|
* .addColumn('first_name', 'varchar', col => col.notNull())
|
|
27
27
|
* .addColumn('last_name', 'varchar', col => col.notNull())
|
|
28
28
|
* .addColumn('gender', 'varchar')
|
|
29
29
|
* .execute()
|
|
30
30
|
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* This example creates a table with a foreign key. Not all database
|
|
34
|
+
* engines support column-level foreign key constraint definitions.
|
|
35
|
+
* For example if you are using MySQL 5.X see the next example after
|
|
36
|
+
* this one.
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* await db.schema
|
|
40
|
+
* .createTable('pet')
|
|
41
|
+
* .addColumn('id', 'integer', col => col.primaryKey().increments())
|
|
42
|
+
* .addColumn('owner_id', 'integer', col => col
|
|
43
|
+
* .references('person.id')
|
|
44
|
+
* .onDelete('cascade')
|
|
45
|
+
* )
|
|
46
|
+
* .execute()
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* This example adds a foreign key constraint for a columns just
|
|
51
|
+
* like the previous example, but using a table-level statement.
|
|
52
|
+
* On MySQL 5.X you need to define foreign key constraints like
|
|
53
|
+
* this:
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* await db.schema
|
|
57
|
+
* .createTable('pet')
|
|
58
|
+
* .addColumn('id', 'integer', col => col.primaryKey().increments())
|
|
59
|
+
* .addColumn('owner_id', 'integer')
|
|
60
|
+
* .addForeignKeyConstraint(
|
|
61
|
+
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
|
|
62
|
+
* (constraint) => constraint.onDelete('cascade')
|
|
63
|
+
* )
|
|
64
|
+
* .execute()
|
|
65
|
+
* ```
|
|
31
66
|
*/
|
|
32
|
-
createTable(table:
|
|
67
|
+
createTable<TB extends string>(table: TB): CreateTableBuilder<TB, never>;
|
|
33
68
|
/**
|
|
34
69
|
* Drop a table.
|
|
35
70
|
*
|
|
@@ -36,12 +36,47 @@ class SchemaModule {
|
|
|
36
36
|
* ```ts
|
|
37
37
|
* await db.schema
|
|
38
38
|
* .createTable('person')
|
|
39
|
-
* .addColumn('id', '
|
|
39
|
+
* .addColumn('id', 'integer', col => col.primaryKey().increments())
|
|
40
40
|
* .addColumn('first_name', 'varchar', col => col.notNull())
|
|
41
41
|
* .addColumn('last_name', 'varchar', col => col.notNull())
|
|
42
42
|
* .addColumn('gender', 'varchar')
|
|
43
43
|
* .execute()
|
|
44
44
|
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* This example creates a table with a foreign key. Not all database
|
|
48
|
+
* engines support column-level foreign key constraint definitions.
|
|
49
|
+
* For example if you are using MySQL 5.X see the next example after
|
|
50
|
+
* this one.
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* await db.schema
|
|
54
|
+
* .createTable('pet')
|
|
55
|
+
* .addColumn('id', 'integer', col => col.primaryKey().increments())
|
|
56
|
+
* .addColumn('owner_id', 'integer', col => col
|
|
57
|
+
* .references('person.id')
|
|
58
|
+
* .onDelete('cascade')
|
|
59
|
+
* )
|
|
60
|
+
* .execute()
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* This example adds a foreign key constraint for a columns just
|
|
65
|
+
* like the previous example, but using a table-level statement.
|
|
66
|
+
* On MySQL 5.X you need to define foreign key constraints like
|
|
67
|
+
* this:
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* await db.schema
|
|
71
|
+
* .createTable('pet')
|
|
72
|
+
* .addColumn('id', 'integer', col => col.primaryKey().increments())
|
|
73
|
+
* .addColumn('owner_id', 'integer')
|
|
74
|
+
* .addForeignKeyConstraint(
|
|
75
|
+
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
|
|
76
|
+
* (constraint) => constraint.onDelete('cascade')
|
|
77
|
+
* )
|
|
78
|
+
* .execute()
|
|
79
|
+
* ```
|
|
45
80
|
*/
|
|
46
81
|
createTable(table) {
|
|
47
82
|
return new create_table_builder_js_1.CreateTableBuilder({
|
|
@@ -16,3 +16,4 @@ export declare function freeze<T>(obj: T): Readonly<T>;
|
|
|
16
16
|
export declare function asArray<T>(arg: T | T[]): T[];
|
|
17
17
|
export declare function asReadonlyArray<T>(arg: T | ReadonlyArray<T>): ReadonlyArray<T>;
|
|
18
18
|
export declare function isReadonlyArray(arg: unknown): arg is ReadonlyArray<unknown>;
|
|
19
|
+
export declare function noop<T>(obj: T): T;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isReadonlyArray = exports.asReadonlyArray = exports.asArray = exports.freeze = exports.getLast = exports.isObject = exports.isFunction = exports.isPrimitive = exports.isBigInt = exports.isBuffer = exports.isDate = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.isEmpty = void 0;
|
|
3
|
+
exports.noop = exports.isReadonlyArray = exports.asReadonlyArray = exports.asArray = exports.freeze = exports.getLast = exports.isObject = exports.isFunction = exports.isPrimitive = exports.isBigInt = exports.isBuffer = exports.isDate = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.isEmpty = void 0;
|
|
4
4
|
function isEmpty(obj) {
|
|
5
5
|
if (Array.isArray(obj) || isString(obj) || Buffer.isBuffer(obj)) {
|
|
6
6
|
return obj.length === 0;
|
|
@@ -87,3 +87,7 @@ function isReadonlyArray(arg) {
|
|
|
87
87
|
return Array.isArray(arg);
|
|
88
88
|
}
|
|
89
89
|
exports.isReadonlyArray = isReadonlyArray;
|
|
90
|
+
function noop(obj) {
|
|
91
|
+
return obj;
|
|
92
|
+
}
|
|
93
|
+
exports.noop = noop;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { ColumnDefinitionNode } from '../../operation-node/column-definition-node.js';
|
|
2
1
|
import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
|
|
3
2
|
export declare class MysqlQueryCompiler extends DefaultQueryCompiler {
|
|
4
|
-
protected visitColumnDefinition(node: ColumnDefinitionNode): void;
|
|
5
3
|
protected visitReturning(): void;
|
|
6
4
|
protected getSql(): string;
|
|
7
5
|
protected getCurrentParameterPlaceholder(): string;
|
|
@@ -1,34 +1,5 @@
|
|
|
1
1
|
import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
|
|
2
2
|
export class MysqlQueryCompiler extends DefaultQueryCompiler {
|
|
3
|
-
visitColumnDefinition(node) {
|
|
4
|
-
this.visitNode(node.column);
|
|
5
|
-
this.append(' ');
|
|
6
|
-
this.visitNode(node.dataType);
|
|
7
|
-
if (node.defaultTo) {
|
|
8
|
-
this.append(' default ');
|
|
9
|
-
this.visitNode(node.defaultTo);
|
|
10
|
-
}
|
|
11
|
-
if (!node.isNullable || node.isAutoIncrementing) {
|
|
12
|
-
this.append(' not null');
|
|
13
|
-
}
|
|
14
|
-
if (node.isAutoIncrementing) {
|
|
15
|
-
this.append(' auto_increment');
|
|
16
|
-
}
|
|
17
|
-
if (node.isUnique) {
|
|
18
|
-
this.append(' unique');
|
|
19
|
-
}
|
|
20
|
-
if (node.isPrimaryKey) {
|
|
21
|
-
this.append(' primary key');
|
|
22
|
-
}
|
|
23
|
-
if (node.references) {
|
|
24
|
-
this.append(' ');
|
|
25
|
-
this.visitNode(node.references);
|
|
26
|
-
}
|
|
27
|
-
if (node.check) {
|
|
28
|
-
this.append(' ');
|
|
29
|
-
this.visitNode(node.check);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
3
|
visitReturning() {
|
|
33
4
|
// Do nothing.
|
|
34
5
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { JoinNode, JoinType } from '../operation-node/join-node.js';
|
|
2
2
|
import { AnyColumn, AnyColumnWithTable } from '../util/type-utils.js';
|
|
3
|
-
import { ExtractAliasFromTableExpression,
|
|
3
|
+
import { ExtractAliasFromTableExpression, TableExpressionDatabase } from './table-parser.js';
|
|
4
4
|
import { JoinBuilder } from '../query-builder/join-builder.js';
|
|
5
5
|
import { ParseContext } from './parse-context.js';
|
|
6
|
-
export declare type JoinReferenceExpression<DB, TB extends keyof DB, F> =
|
|
7
|
-
export declare type JoinCallbackExpression<DB, TB extends keyof DB, F> = (join: JoinBuilder<
|
|
6
|
+
export declare type JoinReferenceExpression<DB, TB extends keyof DB, F> = AnyJoinColumn<DB, TB, F> | AnyJoinColumnWithTable<DB, TB, F>;
|
|
7
|
+
export declare type JoinCallbackExpression<DB, TB extends keyof DB, F> = (join: JoinBuilder<TableExpressionDatabase<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>) => JoinBuilder<any, any>;
|
|
8
|
+
declare type AnyJoinColumn<DB, TB extends keyof DB, F> = AnyColumn<TableExpressionDatabase<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>;
|
|
9
|
+
declare type AnyJoinColumnWithTable<DB, TB extends keyof DB, F> = AnyColumnWithTable<TableExpressionDatabase<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>;
|
|
8
10
|
export declare function parseJoin(ctx: ParseContext, joinType: JoinType, args: any[]): JoinNode;
|
|
11
|
+
export {};
|
|
@@ -7,20 +7,20 @@ import { ParseContext } from './parse-context.js';
|
|
|
7
7
|
export declare type TableExpression<DB, TB extends keyof DB> = TableReference<DB> | AnyAliasedQueryBuilder | AliasedQueryBuilderFactory<DB, TB>;
|
|
8
8
|
export declare type TableExpressionOrList<DB, TB extends keyof DB> = TableExpression<DB, TB> | ReadonlyArray<TableExpression<DB, TB>>;
|
|
9
9
|
export declare type TableReference<DB> = AnyAliasedTable<DB, any, any> | AnyTable<DB> | AnyAliasedRawBuilder;
|
|
10
|
-
export declare type QueryBuilderWithTable<DB, TB extends keyof DB, O, TE> = QueryBuilder<
|
|
11
|
-
export declare type
|
|
10
|
+
export declare type QueryBuilderWithTable<DB, TB extends keyof DB, O, TE> = QueryBuilder<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
|
|
11
|
+
export declare type TableExpressionDatabase<DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
|
|
12
12
|
[C in keyof DB | A]: C extends A ? ExtractRowTypeFromTableExpression<DB, TE, C> : C extends keyof DB ? DB[C] : never;
|
|
13
13
|
};
|
|
14
|
-
export declare type QueryBuilderWithLeftJoin<DB, TB extends keyof DB, O, TE> = QueryBuilder<
|
|
15
|
-
export declare type
|
|
14
|
+
export declare type QueryBuilderWithLeftJoin<DB, TB extends keyof DB, O, TE> = QueryBuilder<LeftJoinTableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
|
|
15
|
+
export declare type LeftJoinTableExpressionDatabase<DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
|
|
16
16
|
[C in keyof DB | A]: C extends A ? Nullable<ExtractRowTypeFromTableExpression<DB, TE, C>> : C extends keyof DB ? DB[C] : never;
|
|
17
17
|
};
|
|
18
|
-
export declare type QueryBuilderWithRightJoin<DB, TB extends keyof DB, O, TE> = QueryBuilder<
|
|
19
|
-
export declare type
|
|
18
|
+
export declare type QueryBuilderWithRightJoin<DB, TB extends keyof DB, O, TE> = QueryBuilder<RightJoinTableExpressionDatabase<DB, TB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
|
|
19
|
+
export declare type RightJoinTableExpressionDatabase<DB, TB extends keyof DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
|
|
20
20
|
[C in keyof DB | A]: C extends A ? ExtractRowTypeFromTableExpression<DB, TE, C> : C extends keyof DB ? C extends TB ? Nullable<DB[C]> : DB[C] : never;
|
|
21
21
|
};
|
|
22
|
-
export declare type QueryBuilderWithFullJoin<DB, TB extends keyof DB, O, TE> = QueryBuilder<
|
|
23
|
-
export declare type
|
|
22
|
+
export declare type QueryBuilderWithFullJoin<DB, TB extends keyof DB, O, TE> = QueryBuilder<FullJoinTableExpressionDatabase<DB, TB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
|
|
23
|
+
export declare type FullJoinTableExpressionDatabase<DB, TB extends keyof DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
|
|
24
24
|
[C in keyof DB | A]: C extends A ? Nullable<ExtractRowTypeFromTableExpression<DB, TE, C>> : C extends keyof DB ? C extends TB ? Nullable<DB[C]> : DB[C] : never;
|
|
25
25
|
};
|
|
26
26
|
export declare type ExtractAliasFromTableExpression<DB, TE> = TE extends `${string} as ${infer TA}` ? TA : TE extends keyof DB ? TE : TE extends AliasedQueryBuilder<any, any, any, infer QA> ? QA : TE extends (qb: any) => AliasedQueryBuilder<any, any, any, infer QA> ? QA : TE extends AliasedRawBuilder<any, infer RA> ? RA : TE extends (qb: any) => AliasedRawBuilder<any, infer RA> ? RA : never;
|
|
@@ -66,9 +66,9 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
66
66
|
* Calls the `sum` function for the column given as the argument.
|
|
67
67
|
*
|
|
68
68
|
* If this is used in a `select` statement the type of the selected expression
|
|
69
|
-
* will be `number | string` by default. This is because Kysely can't
|
|
70
|
-
* type the db driver outputs. Sometimes the output can be larger than
|
|
71
|
-
* largest javascript number and a string is returned instead. Most drivers
|
|
69
|
+
* will be `number | string | BigInt` by default. This is because Kysely can't
|
|
70
|
+
* know the type the db driver outputs. Sometimes the output can be larger than
|
|
71
|
+
* the largest javascript number and a string is returned instead. Most drivers
|
|
72
72
|
* allow you to configure the output type of large numbers and Kysely can't
|
|
73
73
|
* know if you've done so.
|
|
74
74
|
*
|
|
@@ -88,9 +88,9 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
88
88
|
* Calls the `count` function for the column given as the argument.
|
|
89
89
|
*
|
|
90
90
|
* If this is used in a `select` statement the type of the selected expression
|
|
91
|
-
* will be `number | string` by default. This is because Kysely can't
|
|
92
|
-
* type the db driver outputs. Sometimes the output can be larger than
|
|
93
|
-
* largest javascript number and a string is returned instead. Most drivers
|
|
91
|
+
* will be `number | string | BigInt` by default. This is because Kysely can't
|
|
92
|
+
* know the type the db driver outputs. Sometimes the output can be larger than
|
|
93
|
+
* the largest javascript number and a string is returned instead. Most drivers
|
|
94
94
|
* allow you to configure the output type of large numbers and Kysely can't
|
|
95
95
|
* know if you've done so.
|
|
96
96
|
*
|
|
@@ -79,9 +79,9 @@ export class FunctionBuilder {
|
|
|
79
79
|
* Calls the `sum` function for the column given as the argument.
|
|
80
80
|
*
|
|
81
81
|
* If this is used in a `select` statement the type of the selected expression
|
|
82
|
-
* will be `number | string` by default. This is because Kysely can't
|
|
83
|
-
* type the db driver outputs. Sometimes the output can be larger than
|
|
84
|
-
* largest javascript number and a string is returned instead. Most drivers
|
|
82
|
+
* will be `number | string | BigInt` by default. This is because Kysely can't
|
|
83
|
+
* know the type the db driver outputs. Sometimes the output can be larger than
|
|
84
|
+
* the largest javascript number and a string is returned instead. Most drivers
|
|
85
85
|
* allow you to configure the output type of large numbers and Kysely can't
|
|
86
86
|
* know if you've done so.
|
|
87
87
|
*
|
|
@@ -103,9 +103,9 @@ export class FunctionBuilder {
|
|
|
103
103
|
* Calls the `count` function for the column given as the argument.
|
|
104
104
|
*
|
|
105
105
|
* If this is used in a `select` statement the type of the selected expression
|
|
106
|
-
* will be `number | string` by default. This is because Kysely can't
|
|
107
|
-
* type the db driver outputs. Sometimes the output can be larger than
|
|
108
|
-
* largest javascript number and a string is returned instead. Most drivers
|
|
106
|
+
* will be `number | string | BigInt` by default. This is because Kysely can't
|
|
107
|
+
* know the type the db driver outputs. Sometimes the output can be larger than
|
|
108
|
+
* the largest javascript number and a string is returned instead. Most drivers
|
|
109
109
|
* allow you to configure the output type of large numbers and Kysely can't
|
|
110
110
|
* know if you've done so.
|
|
111
111
|
*
|
|
@@ -92,6 +92,7 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
92
92
|
protected visitTable(node: TableNode): void;
|
|
93
93
|
protected visitCreateTable(node: CreateTableNode): void;
|
|
94
94
|
protected visitColumnDefinition(node: ColumnDefinitionNode): void;
|
|
95
|
+
protected getAutoIncrement(): string;
|
|
95
96
|
protected visitReferences(node: ReferencesNode): void;
|
|
96
97
|
protected visitDropTable(node: DropTableNode): void;
|
|
97
98
|
protected visitDataType(node: DataTypeNode): void;
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { DataTypeNode } from '../operation-node/data-type-node.js';
|
|
2
1
|
import { OperationNodeVisitor } from '../operation-node/operation-node-visitor.js';
|
|
3
2
|
import { QueryNode } from '../operation-node/query-node.js';
|
|
4
3
|
import { RawNode } from '../operation-node/raw-node.js';
|
|
5
4
|
import { getLast, freeze, isString, isNumber, isBoolean, isNull, isDate, isBigInt, } from '../util/object-utils.js';
|
|
6
5
|
export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
7
|
-
#
|
|
6
|
+
#sql = '';
|
|
8
7
|
#parameters = [];
|
|
9
8
|
get numParameters() {
|
|
10
9
|
return this.#parameters.length;
|
|
11
10
|
}
|
|
12
11
|
compileQuery(node) {
|
|
13
|
-
this.#
|
|
12
|
+
this.#sql = '';
|
|
14
13
|
this.#parameters = [];
|
|
15
14
|
this.visitNode(node);
|
|
16
15
|
return freeze({
|
|
@@ -19,7 +18,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
19
18
|
});
|
|
20
19
|
}
|
|
21
20
|
getSql() {
|
|
22
|
-
return this.#
|
|
21
|
+
return this.#sql;
|
|
23
22
|
}
|
|
24
23
|
visitSelectQuery(node) {
|
|
25
24
|
// This is a sub query if this is not the root node.
|
|
@@ -289,19 +288,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
289
288
|
visitColumnDefinition(node) {
|
|
290
289
|
this.visitNode(node.column);
|
|
291
290
|
this.append(' ');
|
|
292
|
-
|
|
293
|
-
// Postgres overrides the data type for autoincrementing columns.
|
|
294
|
-
if (DataTypeNode.is(node.dataType) &&
|
|
295
|
-
node.dataType.dataType === 'bigint') {
|
|
296
|
-
this.append('bigserial');
|
|
297
|
-
}
|
|
298
|
-
else {
|
|
299
|
-
this.append('serial');
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
else {
|
|
303
|
-
this.visitNode(node.dataType);
|
|
304
|
-
}
|
|
291
|
+
this.visitNode(node.dataType);
|
|
305
292
|
if (node.defaultTo) {
|
|
306
293
|
this.append(' default ');
|
|
307
294
|
this.visitNode(node.defaultTo);
|
|
@@ -309,6 +296,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
309
296
|
if (!node.isNullable) {
|
|
310
297
|
this.append(' not null');
|
|
311
298
|
}
|
|
299
|
+
if (node.isAutoIncrementing) {
|
|
300
|
+
this.append(' ');
|
|
301
|
+
this.append(this.getAutoIncrement());
|
|
302
|
+
}
|
|
312
303
|
if (node.isUnique) {
|
|
313
304
|
this.append(' unique');
|
|
314
305
|
}
|
|
@@ -324,6 +315,9 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
324
315
|
this.visitNode(node.check);
|
|
325
316
|
}
|
|
326
317
|
}
|
|
318
|
+
getAutoIncrement() {
|
|
319
|
+
return 'auto_increment';
|
|
320
|
+
}
|
|
327
321
|
visitReferences(node) {
|
|
328
322
|
this.append('references ');
|
|
329
323
|
this.visitNode(node.table);
|
|
@@ -634,7 +628,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
634
628
|
this.visitNode(node.constraintName);
|
|
635
629
|
}
|
|
636
630
|
append(str) {
|
|
637
|
-
this.#
|
|
631
|
+
this.#sql += str;
|
|
638
632
|
}
|
|
639
633
|
appendValue(value) {
|
|
640
634
|
this.addParameter(value);
|
|
@@ -10,7 +10,7 @@ export declare class QueryCreator<DB> {
|
|
|
10
10
|
#private;
|
|
11
11
|
constructor(props: QueryCreatorProps);
|
|
12
12
|
/**
|
|
13
|
-
* Creates a `select` query builder
|
|
13
|
+
* Creates a `select` query builder for the given table or tables.
|
|
14
14
|
*
|
|
15
15
|
* The tables passed to this method are built as the query's `from` clause.
|
|
16
16
|
*
|
|
@@ -221,6 +221,9 @@ export declare class QueryCreator<DB> {
|
|
|
221
221
|
* Sets the schema to be used for all table references that don't explicitly
|
|
222
222
|
* specify a schema.
|
|
223
223
|
*
|
|
224
|
+
* This only affects the query created through the builder returned from
|
|
225
|
+
* this method and doesn't modify the `db` instance.
|
|
226
|
+
*
|
|
224
227
|
* @example
|
|
225
228
|
* ```
|
|
226
229
|
* await db.withSchema('mammals')
|
|
@@ -261,10 +264,10 @@ export declare class QueryCreator<DB> {
|
|
|
261
264
|
* Provides a way to pass arbitrary SQL into your query and executing completely
|
|
262
265
|
* raw queries.
|
|
263
266
|
*
|
|
264
|
-
* You can use strings `?` and `??` in the `sql` to bind parameters such as
|
|
267
|
+
* You can use the strings `?` and `??` in the `sql` to bind parameters such as
|
|
265
268
|
* user input to the SQL. You should never EVER concatenate untrusted user
|
|
266
269
|
* input to the SQL string to avoid injection vulnerabilities. Instead use `?`
|
|
267
|
-
* in place of
|
|
270
|
+
* in place of a value and pass the actual value in the `parameters` list. See
|
|
268
271
|
* the examples below.
|
|
269
272
|
*
|
|
270
273
|
* You should only use `raw` when there is no other way to get the job done. This is
|
|
@@ -155,6 +155,9 @@ export class QueryCreator {
|
|
|
155
155
|
* Sets the schema to be used for all table references that don't explicitly
|
|
156
156
|
* specify a schema.
|
|
157
157
|
*
|
|
158
|
+
* This only affects the query created through the builder returned from
|
|
159
|
+
* this method and doesn't modify the `db` instance.
|
|
160
|
+
*
|
|
158
161
|
* @example
|
|
159
162
|
* ```
|
|
160
163
|
* await db.withSchema('mammals')
|
|
@@ -200,10 +203,10 @@ export class QueryCreator {
|
|
|
200
203
|
* Provides a way to pass arbitrary SQL into your query and executing completely
|
|
201
204
|
* raw queries.
|
|
202
205
|
*
|
|
203
|
-
* You can use strings `?` and `??` in the `sql` to bind parameters such as
|
|
206
|
+
* You can use the strings `?` and `??` in the `sql` to bind parameters such as
|
|
204
207
|
* user input to the SQL. You should never EVER concatenate untrusted user
|
|
205
208
|
* input to the SQL string to avoid injection vulnerabilities. Instead use `?`
|
|
206
|
-
* in place of
|
|
209
|
+
* in place of a value and pass the actual value in the `parameters` list. See
|
|
207
210
|
* the examples below.
|
|
208
211
|
*
|
|
209
212
|
* You should only use `raw` when there is no other way to get the job done. This is
|
|
@@ -12,6 +12,9 @@ import { QueryId } from '../util/query-id.js';
|
|
|
12
12
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
13
13
|
import { DataTypeExpression } from '../parser/data-type-parser.js';
|
|
14
14
|
import { ForeignKeyConstraintBuilder, ForeignKeyConstraintBuilderInterface } from './foreign-key-constraint-builder.js';
|
|
15
|
+
/**
|
|
16
|
+
* This builder can be used to create a `alter table` query.
|
|
17
|
+
*/
|
|
15
18
|
export declare class AlterTableBuilder {
|
|
16
19
|
#private;
|
|
17
20
|
constructor(props: AlterTableBuilderProps);
|
|
@@ -20,6 +20,9 @@ import { UniqueConstraintNode } from '../operation-node/unique-constraint-node.j
|
|
|
20
20
|
import { CheckConstraintNode } from '../operation-node/check-constraint-node.js';
|
|
21
21
|
import { ForeignKeyConstraintNode } from '../operation-node/foreign-key-constraint-node.js';
|
|
22
22
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
23
|
+
/**
|
|
24
|
+
* This builder can be used to create a `alter table` query.
|
|
25
|
+
*/
|
|
23
26
|
export class AlterTableBuilder {
|
|
24
27
|
#props;
|
|
25
28
|
constructor(props) {
|
|
@@ -6,11 +6,11 @@ import { AnyRawBuilder } from '../util/type-utils.js';
|
|
|
6
6
|
import { DefaultValueExpression } from '../parser/default-value-parser.js';
|
|
7
7
|
export interface ColumnDefinitionBuilderInterface<R> {
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Adds `auto_increment` or `autoincrement` to the column definition
|
|
10
|
+
* depending on the dialect.
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* have specified before.
|
|
12
|
+
* Some dialects like PostgreSQL don't support this. On PostgreSQL
|
|
13
|
+
* you can use the `serial` or `bigserial` data type instead.
|
|
14
14
|
*/
|
|
15
15
|
increments(): R;
|
|
16
16
|
/**
|
|
@@ -23,6 +23,10 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
23
23
|
/**
|
|
24
24
|
* Adds a foreign key constraint for the column.
|
|
25
25
|
*
|
|
26
|
+
* If your database engine doesn't support foreign key constraints in the
|
|
27
|
+
* column definition (like MySQL 5) you need to call the table level
|
|
28
|
+
* {@link TableBuilder.addForeignKeyConstraint} method instead.
|
|
29
|
+
*
|
|
26
30
|
* @example
|
|
27
31
|
* ```ts
|
|
28
32
|
* col.references('person.id')
|
|
@@ -31,6 +35,11 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
31
35
|
references(ref: string): R;
|
|
32
36
|
/**
|
|
33
37
|
* Adds an `on delete` constraint for the foreign key column.
|
|
38
|
+
*
|
|
39
|
+
* If your database engine doesn't support foreign key constraints in the
|
|
40
|
+
* column definition (like MySQL 5) you need to call the table level
|
|
41
|
+
* {@link TableBuilder.addForeignKeyConstraint} method instead.
|
|
42
|
+
*
|
|
34
43
|
*/
|
|
35
44
|
onDelete(onDelete: OnModifyForeignAction): R;
|
|
36
45
|
/**
|
|
@@ -64,7 +73,7 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
64
73
|
* ```ts
|
|
65
74
|
* db.schema
|
|
66
75
|
* .createTable('pet')
|
|
67
|
-
* .addColumn('number_of_legs', (col) => col.check('number_of_legs < 5'
|
|
76
|
+
* .addColumn('number_of_legs', 'integer', (col) => col.check('number_of_legs < 5'))
|
|
68
77
|
* .execute()
|
|
69
78
|
* ```
|
|
70
79
|
*/
|