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.
Files changed (42) hide show
  1. package/README.md +47 -4
  2. package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +0 -2
  3. package/dist/cjs/dialect/mysql/mysql-query-compiler.js +0 -29
  4. package/dist/cjs/operation-node/primitive-value-list-node.js +1 -1
  5. package/dist/cjs/parser/join-parser.d.ts +6 -3
  6. package/dist/cjs/parser/table-parser.d.ts +8 -8
  7. package/dist/cjs/query-builder/function-builder.d.ts +6 -6
  8. package/dist/cjs/query-builder/function-builder.js +6 -6
  9. package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -0
  10. package/dist/cjs/query-compiler/default-query-compiler.js +12 -18
  11. package/dist/cjs/query-creator.d.ts +6 -3
  12. package/dist/cjs/query-creator.js +5 -2
  13. package/dist/cjs/schema/alter-table-builder.d.ts +3 -0
  14. package/dist/cjs/schema/alter-table-builder.js +3 -0
  15. package/dist/cjs/schema/column-definition-builder.d.ts +14 -5
  16. package/dist/cjs/schema/create-table-builder.d.ts +55 -10
  17. package/dist/cjs/schema/create-table-builder.js +52 -13
  18. package/dist/cjs/schema/schema.d.ts +37 -2
  19. package/dist/cjs/schema/schema.js +36 -1
  20. package/dist/cjs/util/object-utils.d.ts +1 -0
  21. package/dist/cjs/util/object-utils.js +5 -1
  22. package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +0 -2
  23. package/dist/esm/dialect/mysql/mysql-query-compiler.js +0 -29
  24. package/dist/esm/operation-node/primitive-value-list-node.js +1 -1
  25. package/dist/esm/parser/join-parser.d.ts +6 -3
  26. package/dist/esm/parser/table-parser.d.ts +8 -8
  27. package/dist/esm/query-builder/function-builder.d.ts +6 -6
  28. package/dist/esm/query-builder/function-builder.js +6 -6
  29. package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -0
  30. package/dist/esm/query-compiler/default-query-compiler.js +12 -18
  31. package/dist/esm/query-creator.d.ts +6 -3
  32. package/dist/esm/query-creator.js +5 -2
  33. package/dist/esm/schema/alter-table-builder.d.ts +3 -0
  34. package/dist/esm/schema/alter-table-builder.js +3 -0
  35. package/dist/esm/schema/column-definition-builder.d.ts +14 -5
  36. package/dist/esm/schema/create-table-builder.d.ts +55 -10
  37. package/dist/esm/schema/create-table-builder.js +53 -14
  38. package/dist/esm/schema/schema.d.ts +37 -2
  39. package/dist/esm/schema/schema.js +36 -1
  40. package/dist/esm/util/object-utils.d.ts +1 -0
  41. package/dist/esm/util/object-utils.js +3 -0
  42. package/package.json +1 -1
package/README.md CHANGED
@@ -4,7 +4,8 @@
4
4
  # [Kysely](https://koskimas.github.io/kysely/index.html)
5
5
 
6
6
  A type-safe and autocompletion-friendly typescript SQL query builder for node.js. Inspired by
7
- [knex](http://knexjs.org/). You can find an introduction to Kysely [here](https://www.jakso.me/blog/kysely-a-type-safe-sql-query-builder-for-typescript).
7
+ [knex](http://knexjs.org/). [Here's](https://www.jakso.me/blog/kysely-a-type-safe-sql-query-builder-for-typescript)
8
+ an introductory blog post for you.
8
9
 
9
10
  ![](https://github.com/koskimas/kysely/blob/master/assets/demo.gif)
10
11
 
@@ -25,7 +26,8 @@ type-safety! See the [DynamicModule](https://koskimas.github.io/kysely/classes/D
25
26
  for more info.
26
27
 
27
28
  Kysely is still young and some useful features are propably not yet implemented. If you start
28
- using Kysely and can't find something you'd want to use, please open an issue.
29
+ using Kysely and can't find something you'd want to use, please open an issue or join our
30
+ [discord server](https://discord.gg/C9aJ49mCra).
29
31
 
30
32
  # Table of contents
31
33
 
@@ -161,13 +163,15 @@ changes completely. Migrations need to be "frozen in time".
161
163
  The migrations can use the [Kysely.schema](https://koskimas.github.io/kysely/classes/SchemaModule.html)
162
164
  module to modify the schema. Migrations can also run normal queries to modify the data.
163
165
 
166
+ ### PostgreSQL migration example
167
+
164
168
  ```ts
165
169
  import { Kysely } from 'kysely'
166
170
 
167
171
  export async function up(db: Kysely<any>): Promise<void> {
168
172
  await db.schema
169
173
  .createTable('person')
170
- .addColumn('id', 'integer', (col) => col.increments().primaryKey())
174
+ .addColumn('id', 'serial', (col) => col.primaryKey())
171
175
  .addColumn('first_name', 'varchar')
172
176
  .addColumn('last_name', 'varchar')
173
177
  .addColumn('gender', 'varchar(50)')
@@ -175,7 +179,7 @@ export async function up(db: Kysely<any>): Promise<void> {
175
179
 
176
180
  await db.schema
177
181
  .createTable('pet')
178
- .addColumn('id', 'integer', (col) => col.increments().primaryKey())
182
+ .addColumn('id', 'serial', (col) => col.primaryKey())
179
183
  .addColumn('name', 'varchar', (col) => col.notNull().unique())
180
184
  .addColumn('owner_id', 'integer', (col) =>
181
185
  col.references('person.id').onDelete('cascade')
@@ -196,6 +200,45 @@ export async function down(db: Kysely<any>): Promise<void> {
196
200
  }
197
201
  ```
198
202
 
203
+ ### MySQL migration example
204
+
205
+ ```ts
206
+ import { Kysely } from 'kysely'
207
+
208
+ export async function up(db: Kysely<any>): Promise<void> {
209
+ await db.schema
210
+ .createTable('person')
211
+ .addColumn('id', 'integer', (col) => col.increments().primaryKey())
212
+ .addColumn('first_name', 'varchar')
213
+ .addColumn('last_name', 'varchar')
214
+ .addColumn('gender', 'varchar(50)')
215
+ .execute()
216
+
217
+ await db.schema
218
+ .createTable('pet')
219
+ .addColumn('id', 'integer', (col) => col.increments().primaryKey())
220
+ .addColumn('name', 'varchar', (col) => col.notNull().unique())
221
+ .addColumn('owner_id', 'integer')
222
+ .addColumn('species', 'varchar')
223
+ .addForeignKeyConstraint(
224
+ 'pet_owner_id_fk', ['owner_id'], 'person', ['id'],
225
+ (cb) => cb.onDelete('cascade')
226
+ )
227
+ .execute()
228
+
229
+ await db.schema
230
+ .createIndex('pet_owner_id_index')
231
+ .on('pet')
232
+ .column('owner_id')
233
+ .execute()
234
+ }
235
+
236
+ export async function down(db: Kysely<any>): Promise<void> {
237
+ await db.schema.dropTable('pet').execute()
238
+ await db.schema.dropTable('person').execute()
239
+ }
240
+ ```
241
+
199
242
  You can then use
200
243
 
201
244
  ```ts
@@ -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;
@@ -3,35 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MysqlQueryCompiler = void 0;
4
4
  const default_query_compiler_js_1 = require("../../query-compiler/default-query-compiler.js");
5
5
  class MysqlQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
6
- visitColumnDefinition(node) {
7
- this.visitNode(node.column);
8
- this.append(' ');
9
- this.visitNode(node.dataType);
10
- if (node.defaultTo) {
11
- this.append(' default ');
12
- this.visitNode(node.defaultTo);
13
- }
14
- if (!node.isNullable || node.isAutoIncrementing) {
15
- this.append(' not null');
16
- }
17
- if (node.isAutoIncrementing) {
18
- this.append(' auto_increment');
19
- }
20
- if (node.isUnique) {
21
- this.append(' unique');
22
- }
23
- if (node.isPrimaryKey) {
24
- this.append(' primary key');
25
- }
26
- if (node.references) {
27
- this.append(' ');
28
- this.visitNode(node.references);
29
- }
30
- if (node.check) {
31
- this.append(' ');
32
- this.visitNode(node.check);
33
- }
34
- }
35
6
  visitReturning() {
36
7
  // Do nothing.
37
8
  }
@@ -12,7 +12,7 @@ exports.PrimitiveValueListNode = (0, object_utils_js_1.freeze)({
12
12
  create(values) {
13
13
  return (0, object_utils_js_1.freeze)({
14
14
  kind: 'PrimitiveValueListNode',
15
- values: (0, object_utils_js_1.freeze)(values),
15
+ values: (0, object_utils_js_1.freeze)([...values]),
16
16
  });
17
17
  },
18
18
  });
@@ -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, TableExpressionDatabaseType } from './table-parser.js';
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> = AnyColumn<TableExpressionDatabaseType<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>> | AnyColumnWithTable<TableExpressionDatabaseType<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>;
7
- export declare type JoinCallbackExpression<DB, TB extends keyof DB, F> = (join: JoinBuilder<TableExpressionDatabaseType<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>) => JoinBuilder<any, any>;
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<TableExpressionDatabaseType<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
11
- export declare type TableExpressionDatabaseType<DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
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<LeftJoinTableExpressionDatabaseType<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
15
- export declare type LeftJoinTableExpressionDatabaseType<DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
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<RightJoinTableExpressionDatabaseType<DB, TB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
19
- export declare type RightJoinTableExpressionDatabaseType<DB, TB extends keyof DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
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<FullJoinTableExpressionDatabaseType<DB, TB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
23
- export declare type FullJoinTableExpressionDatabaseType<DB, TB extends keyof DB, TE, A extends keyof any = ExtractAliasFromTableExpression<DB, TE>> = {
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 know the
70
- * type the db driver outputs. Sometimes the output can be larger than the
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 know the
92
- * type the db driver outputs. Sometimes the output can be larger than the
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
  *
@@ -82,9 +82,9 @@ class FunctionBuilder {
82
82
  * Calls the `sum` function for the column given as the argument.
83
83
  *
84
84
  * If this is used in a `select` statement the type of the selected expression
85
- * will be `number | string` by default. This is because Kysely can't know the
86
- * type the db driver outputs. Sometimes the output can be larger than the
87
- * largest javascript number and a string is returned instead. Most drivers
85
+ * will be `number | string | BigInt` by default. This is because Kysely can't
86
+ * know the type the db driver outputs. Sometimes the output can be larger than
87
+ * the largest javascript number and a string is returned instead. Most drivers
88
88
  * allow you to configure the output type of large numbers and Kysely can't
89
89
  * know if you've done so.
90
90
  *
@@ -106,9 +106,9 @@ class FunctionBuilder {
106
106
  * Calls the `count` function for the column given as the argument.
107
107
  *
108
108
  * If this is used in a `select` statement the type of the selected expression
109
- * will be `number | string` by default. This is because Kysely can't know the
110
- * type the db driver outputs. Sometimes the output can be larger than the
111
- * largest javascript number and a string is returned instead. Most drivers
109
+ * will be `number | string | BigInt` by default. This is because Kysely can't
110
+ * know the type the db driver outputs. Sometimes the output can be larger than
111
+ * the largest javascript number and a string is returned instead. Most drivers
112
112
  * allow you to configure the output type of large numbers and Kysely can't
113
113
  * know if you've done so.
114
114
  *
@@ -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,19 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultQueryCompiler = void 0;
4
- const data_type_node_js_1 = require("../operation-node/data-type-node.js");
5
4
  const operation_node_visitor_js_1 = require("../operation-node/operation-node-visitor.js");
6
5
  const query_node_js_1 = require("../operation-node/query-node.js");
7
6
  const raw_node_js_1 = require("../operation-node/raw-node.js");
8
7
  const object_utils_js_1 = require("../util/object-utils.js");
9
8
  class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisitor {
10
- #sqlFragments = [];
9
+ #sql = '';
11
10
  #parameters = [];
12
11
  get numParameters() {
13
12
  return this.#parameters.length;
14
13
  }
15
14
  compileQuery(node) {
16
- this.#sqlFragments = [];
15
+ this.#sql = '';
17
16
  this.#parameters = [];
18
17
  this.visitNode(node);
19
18
  return (0, object_utils_js_1.freeze)({
@@ -22,7 +21,7 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
22
21
  });
23
22
  }
24
23
  getSql() {
25
- return this.#sqlFragments.join('');
24
+ return this.#sql;
26
25
  }
27
26
  visitSelectQuery(node) {
28
27
  // This is a sub query if this is not the root node.
@@ -292,19 +291,7 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
292
291
  visitColumnDefinition(node) {
293
292
  this.visitNode(node.column);
294
293
  this.append(' ');
295
- if (node.isAutoIncrementing) {
296
- // Postgres overrides the data type for autoincrementing columns.
297
- if (data_type_node_js_1.DataTypeNode.is(node.dataType) &&
298
- node.dataType.dataType === 'bigint') {
299
- this.append('bigserial');
300
- }
301
- else {
302
- this.append('serial');
303
- }
304
- }
305
- else {
306
- this.visitNode(node.dataType);
307
- }
294
+ this.visitNode(node.dataType);
308
295
  if (node.defaultTo) {
309
296
  this.append(' default ');
310
297
  this.visitNode(node.defaultTo);
@@ -312,6 +299,10 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
312
299
  if (!node.isNullable) {
313
300
  this.append(' not null');
314
301
  }
302
+ if (node.isAutoIncrementing) {
303
+ this.append(' ');
304
+ this.append(this.getAutoIncrement());
305
+ }
315
306
  if (node.isUnique) {
316
307
  this.append(' unique');
317
308
  }
@@ -327,6 +318,9 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
327
318
  this.visitNode(node.check);
328
319
  }
329
320
  }
321
+ getAutoIncrement() {
322
+ return 'auto_increment';
323
+ }
330
324
  visitReferences(node) {
331
325
  this.append('references ');
332
326
  this.visitNode(node.table);
@@ -637,7 +631,7 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
637
631
  this.visitNode(node.constraintName);
638
632
  }
639
633
  append(str) {
640
- this.#sqlFragments.push(str);
634
+ this.#sql += str;
641
635
  }
642
636
  appendValue(value) {
643
637
  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 against the given table/tables.
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 the value and pass the actual value in the `params` list. See
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
@@ -158,6 +158,9 @@ class QueryCreator {
158
158
  * Sets the schema to be used for all table references that don't explicitly
159
159
  * specify a schema.
160
160
  *
161
+ * This only affects the query created through the builder returned from
162
+ * this method and doesn't modify the `db` instance.
163
+ *
161
164
  * @example
162
165
  * ```
163
166
  * await db.withSchema('mammals')
@@ -203,10 +206,10 @@ class QueryCreator {
203
206
  * Provides a way to pass arbitrary SQL into your query and executing completely
204
207
  * raw queries.
205
208
  *
206
- * You can use strings `?` and `??` in the `sql` to bind parameters such as
209
+ * You can use the strings `?` and `??` in the `sql` to bind parameters such as
207
210
  * user input to the SQL. You should never EVER concatenate untrusted user
208
211
  * input to the SQL string to avoid injection vulnerabilities. Instead use `?`
209
- * in place of the value and pass the actual value in the `params` list. See
212
+ * in place of a value and pass the actual value in the `parameters` list. See
210
213
  * the examples below.
211
214
  *
212
215
  * 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);
@@ -23,6 +23,9 @@ const unique_constraint_node_js_1 = require("../operation-node/unique-constraint
23
23
  const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
24
24
  const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
25
25
  const column_node_js_1 = require("../operation-node/column-node.js");
26
+ /**
27
+ * This builder can be used to create a `alter table` query.
28
+ */
26
29
  class AlterTableBuilder {
27
30
  #props;
28
31
  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
- * Makes the column automatically incrementing.
9
+ * Adds `auto_increment` or `autoincrement` to the column definition
10
+ * depending on the dialect.
10
11
  *
11
- * On some dialects this may change the column type as well. For example
12
- * on postgres this sets the column type to `serial` no matter what you
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', 'integer'))
76
+ * .addColumn('number_of_legs', 'integer', (col) => col.check('number_of_legs < 5'))
68
77
  * .execute()
69
78
  * ```
70
79
  */
@@ -7,7 +7,10 @@ import { ColumnDefinitionBuilder } from './column-definition-builder.js';
7
7
  import { QueryId } from '../util/query-id.js';
8
8
  import { ForeignKeyConstraintBuilder } from './foreign-key-constraint-builder.js';
9
9
  import { DataTypeExpression } from '../parser/data-type-parser.js';
10
- export declare class CreateTableBuilder implements OperationNodeSource, Compilable {
10
+ /**
11
+ * This builder can be used to create a `create table` query.
12
+ */
13
+ export declare class CreateTableBuilder<TB extends string, C extends string = never> implements OperationNodeSource, Compilable {
11
14
  #private;
12
15
  constructor(props: CreateTableBuilderProps);
13
16
  /**
@@ -15,7 +18,7 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
15
18
  *
16
19
  * If the table already exists, no error is thrown if this method has been called.
17
20
  */
18
- ifNotExists(): CreateTableBuilder;
21
+ ifNotExists(): CreateTableBuilder<TB, C>;
19
22
  /**
20
23
  * Adds a column to the table.
21
24
  *
@@ -23,44 +26,86 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
23
26
  * ```ts
24
27
  * await db.schema
25
28
  * .createTable('person')
26
- * .addColumn('int', 'id', (col) => col.increments().primary()),
29
+ * .addColumn('id', 'integer', (col) => col.increments().primaryKey()),
27
30
  * .addColumn('first_name', 'varchar(50), (col) => col.notNull())
28
- * .addColumn('varchar', 'last_name')
29
- * .addColumn('numeric(8, 2)', 'bank_balance')
31
+ * .addColumn('last_name', 'varchar')
32
+ * .addColumn('bank_balance', 'numeric(8, 2)')
30
33
  * .addColumn('data', db.raw('customtype'))
34
+ * .addColumn('parent_id', 'integer', (col) =>
35
+ * col.references('person.id').onDelete('cascade'))
36
+ * )
37
+ * ```
38
+ *
39
+ * With this method, it's once again good to remember that Kysely just builds the query
40
+ * that's as close to the structure of your builder method calls as possible, and doesn't
41
+ * provide the same API for all databses. For example, some databases like older MySQL
42
+ * don't support `references` statement in the column definition. Instead foreign key
43
+ * constraints need to be defined in at the level of the `create table` query. See
44
+ * the next example:
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * .addColumn('parent_id', 'integer')
49
+ * .addForeignKeyConstraint(
50
+ * 'person_parent_id_fk', ['parent_id'], 'person', ['id'],
51
+ * (cb) => cb.onDelete('cascade')
52
+ * )
53
+ * ```
54
+ *
55
+ * Another good example is that PostgreSQL doesn't support the `auto_increment`
56
+ * keyword and you need to define an autoincrementing column for example using
57
+ * `serial`:
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * await db.schema
62
+ * .createTable('person')
63
+ * .addColumn('id', 'serial', (col) => col.primaryKey()),
31
64
  * ```
32
65
  */
33
- addColumn(columnName: string, dataType: DataTypeExpression, build?: ColumnBuilderCallback): CreateTableBuilder;
66
+ addColumn<CN extends string>(columnName: CN, dataType: DataTypeExpression, build?: ColumnBuilderCallback): CreateTableBuilder<TB, C | CN>;
34
67
  /**
35
68
  * Adds a primary key constraint for one or more columns.
36
69
  *
70
+ * The constraint name can be anything you want, but it must be unique
71
+ * across the whole database.
72
+ *
37
73
  * @example
38
74
  * ```ts
39
75
  * addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
40
76
  * ```
41
77
  */
42
- addPrimaryKeyConstraint(constraintName: string, columns: string[]): CreateTableBuilder;
78
+ addPrimaryKeyConstraint(constraintName: string, columns: C[]): CreateTableBuilder<TB, C>;
43
79
  /**
44
80
  * Adds a unique constraint for one or more columns.
45
81
  *
82
+ * The constraint name can be anything you want, but it must be unique
83
+ * across the whole database.
84
+ *
46
85
  * @example
47
86
  * ```ts
48
87
  * addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
49
88
  * ```
50
89
  */
51
- addUniqueConstraint(constraintName: string, columns: string[]): CreateTableBuilder;
90
+ addUniqueConstraint(constraintName: string, columns: C[]): CreateTableBuilder<TB, C>;
52
91
  /**
53
92
  * Adds a check constraint.
54
93
  *
94
+ * The constraint name can be anything you want, but it must be unique
95
+ * across the whole database.
96
+ *
55
97
  * @example
56
98
  * ```ts
57
99
  * addCheckConstraint('check_legs', 'number_of_legs < 5')
58
100
  * ```
59
101
  */
60
- addCheckConstraint(constraintName: string, checkExpression: string): CreateTableBuilder;
102
+ addCheckConstraint(constraintName: string, checkExpression: string): CreateTableBuilder<TB, C>;
61
103
  /**
62
104
  * Adds a foreign key constraint.
63
105
  *
106
+ * The constraint name can be anything you want, but it must be unique
107
+ * across the whole database.
108
+ *
64
109
  * @example
65
110
  * ```ts
66
111
  * addForeignKeyConstraint(
@@ -82,7 +127,7 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
82
127
  * )
83
128
  * ```
84
129
  */
85
- addForeignKeyConstraint(constraintName: string, columns: string[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): CreateTableBuilder;
130
+ addForeignKeyConstraint(constraintName: string, columns: C[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): CreateTableBuilder<TB, C>;
86
131
  toOperationNode(): CreateTableNode;
87
132
  compile(): CompiledQuery;
88
133
  execute(): Promise<void>;