kysely 0.9.0 → 0.9.4

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 (109) hide show
  1. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +9 -0
  2. package/dist/cjs/dialect/mysql/mysql-dialect.js +7 -0
  3. package/dist/cjs/dialect/mysql/mysql-driver.js +2 -2
  4. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +11 -1
  5. package/dist/cjs/dialect/postgres/postgres-dialect.js +10 -0
  6. package/dist/cjs/driver/runtime-driver.d.ts +3 -2
  7. package/dist/cjs/driver/runtime-driver.js +43 -4
  8. package/dist/cjs/index.d.ts +1 -1
  9. package/dist/cjs/kysely.d.ts +26 -9
  10. package/dist/cjs/kysely.js +16 -9
  11. package/dist/cjs/migration/migration.d.ts +32 -0
  12. package/dist/cjs/operation-node/create-schema-node.d.ts +1 -1
  13. package/dist/cjs/operation-node/create-schema-node.js +2 -2
  14. package/dist/cjs/operation-node/create-table-node.d.ts +1 -5
  15. package/dist/cjs/operation-node/create-table-node.js +1 -31
  16. package/dist/cjs/operation-node/join-node.d.ts +2 -1
  17. package/dist/cjs/operation-node/join-node.js +8 -6
  18. package/dist/cjs/operation-node/operation-node-source.d.ts +1 -1
  19. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  20. package/dist/cjs/operation-node/operation-node-transformer.js +11 -1
  21. package/dist/cjs/operation-node/operation-node-visitor.d.ts +61 -59
  22. package/dist/cjs/operation-node/operation-node-visitor.js +1 -316
  23. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  24. package/dist/cjs/operation-node/query-node.d.ts +4 -3
  25. package/dist/cjs/operation-node/query-node.js +10 -2
  26. package/dist/cjs/operation-node/select-query-node.d.ts +5 -1
  27. package/dist/cjs/operation-node/select-query-node.js +18 -2
  28. package/dist/cjs/operation-node/union-node.d.ts +16 -0
  29. package/dist/cjs/operation-node/union-node.js +19 -0
  30. package/dist/cjs/parser/group-by-parser.d.ts +4 -0
  31. package/dist/cjs/parser/group-by-parser.js +9 -0
  32. package/dist/cjs/parser/insert-values-parser.d.ts +1 -1
  33. package/dist/cjs/parser/join-parser.d.ts +4 -4
  34. package/dist/cjs/parser/order-by-parser.d.ts +2 -4
  35. package/dist/cjs/parser/order-by-parser.js +6 -3
  36. package/dist/cjs/parser/reference-parser.d.ts +1 -1
  37. package/dist/cjs/parser/reference-parser.js +1 -1
  38. package/dist/cjs/parser/union-parser.d.ts +5 -0
  39. package/dist/cjs/parser/union-parser.js +13 -0
  40. package/dist/cjs/parser/value-parser.d.ts +1 -1
  41. package/dist/cjs/query-builder/join-builder.js +8 -8
  42. package/dist/cjs/query-builder/query-builder.d.ts +65 -1
  43. package/dist/cjs/query-builder/query-builder.js +103 -22
  44. package/dist/cjs/query-compiler/compiled-query.d.ts +1 -1
  45. package/dist/cjs/query-compiler/default-query-compiler.d.ts +7 -2
  46. package/dist/cjs/query-compiler/default-query-compiler.js +21 -4
  47. package/dist/cjs/schema/create-schema-builder.js +1 -1
  48. package/dist/cjs/schema/create-table-builder.js +7 -4
  49. package/dist/cjs/util/log.d.ts +8 -0
  50. package/dist/cjs/util/log.js +21 -0
  51. package/dist/cjs/util/object-utils.d.ts +11 -11
  52. package/dist/cjs/util/object-utils.js +1 -1
  53. package/dist/cjs/util/random-string.js +6 -3
  54. package/dist/cjs/util/type-utils.d.ts +4 -4
  55. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +9 -0
  56. package/dist/esm/dialect/mysql/mysql-dialect.js +7 -0
  57. package/dist/esm/dialect/mysql/mysql-driver.js +3 -3
  58. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +11 -1
  59. package/dist/esm/dialect/postgres/postgres-dialect.js +10 -0
  60. package/dist/esm/driver/runtime-driver.d.ts +3 -2
  61. package/dist/esm/driver/runtime-driver.js +43 -4
  62. package/dist/esm/index.d.ts +1 -1
  63. package/dist/esm/kysely.d.ts +26 -9
  64. package/dist/esm/kysely.js +16 -9
  65. package/dist/esm/migration/migration.d.ts +32 -0
  66. package/dist/esm/operation-node/create-schema-node.d.ts +1 -1
  67. package/dist/esm/operation-node/create-schema-node.js +2 -2
  68. package/dist/esm/operation-node/create-table-node.d.ts +1 -5
  69. package/dist/esm/operation-node/create-table-node.js +1 -31
  70. package/dist/esm/operation-node/join-node.d.ts +2 -1
  71. package/dist/esm/operation-node/join-node.js +8 -6
  72. package/dist/esm/operation-node/operation-node-source.d.ts +1 -1
  73. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  74. package/dist/esm/operation-node/operation-node-transformer.js +11 -1
  75. package/dist/esm/operation-node/operation-node-visitor.d.ts +61 -59
  76. package/dist/esm/operation-node/operation-node-visitor.js +1 -316
  77. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  78. package/dist/esm/operation-node/query-node.d.ts +4 -3
  79. package/dist/esm/operation-node/query-node.js +10 -2
  80. package/dist/esm/operation-node/select-query-node.d.ts +5 -1
  81. package/dist/esm/operation-node/select-query-node.js +18 -2
  82. package/dist/esm/operation-node/union-node.d.ts +16 -0
  83. package/dist/esm/operation-node/union-node.js +16 -0
  84. package/dist/esm/parser/group-by-parser.d.ts +4 -0
  85. package/dist/esm/parser/group-by-parser.js +5 -0
  86. package/dist/esm/parser/insert-values-parser.d.ts +1 -1
  87. package/dist/esm/parser/join-parser.d.ts +4 -4
  88. package/dist/esm/parser/order-by-parser.d.ts +2 -4
  89. package/dist/esm/parser/order-by-parser.js +6 -2
  90. package/dist/esm/parser/reference-parser.d.ts +1 -1
  91. package/dist/esm/parser/reference-parser.js +2 -2
  92. package/dist/esm/parser/union-parser.d.ts +5 -0
  93. package/dist/esm/parser/union-parser.js +9 -0
  94. package/dist/esm/parser/value-parser.d.ts +1 -1
  95. package/dist/esm/query-builder/join-builder.js +8 -8
  96. package/dist/esm/query-builder/query-builder.d.ts +65 -1
  97. package/dist/esm/query-builder/query-builder.js +104 -23
  98. package/dist/esm/query-compiler/compiled-query.d.ts +1 -1
  99. package/dist/esm/query-compiler/default-query-compiler.d.ts +7 -2
  100. package/dist/esm/query-compiler/default-query-compiler.js +21 -4
  101. package/dist/esm/schema/create-schema-builder.js +1 -1
  102. package/dist/esm/schema/create-table-builder.js +7 -4
  103. package/dist/esm/util/log.d.ts +8 -0
  104. package/dist/esm/util/log.js +17 -0
  105. package/dist/esm/util/object-utils.d.ts +11 -11
  106. package/dist/esm/util/object-utils.js +1 -1
  107. package/dist/esm/util/random-string.js +6 -3
  108. package/dist/esm/util/type-utils.d.ts +4 -4
  109. 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
+ const primary_constraint_node_js_1 = require("../operation-node/primary-constraint-node.js");
15
+ const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
16
+ const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
14
17
  /**
15
18
  * This builder can be used to create a `create table` query.
16
19
  */
@@ -95,7 +98,7 @@ class CreateTableBuilder {
95
98
  addPrimaryKeyConstraint(constraintName, columns) {
96
99
  return new CreateTableBuilder({
97
100
  ...this.#props,
98
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithPrimaryKeyConstraint(this.#props.createTableNode, constraintName, columns),
101
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, primary_constraint_node_js_1.PrimaryConstraintNode.create(columns, constraintName)),
99
102
  });
100
103
  }
101
104
  /**
@@ -112,7 +115,7 @@ class CreateTableBuilder {
112
115
  addUniqueConstraint(constraintName, columns) {
113
116
  return new CreateTableBuilder({
114
117
  ...this.#props,
115
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithUniqueConstraint(this.#props.createTableNode, constraintName, columns),
118
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, unique_constraint_node_js_1.UniqueConstraintNode.create(columns, constraintName)),
116
119
  });
117
120
  }
118
121
  /**
@@ -129,7 +132,7 @@ class CreateTableBuilder {
129
132
  addCheckConstraint(constraintName, checkExpression) {
130
133
  return new CreateTableBuilder({
131
134
  ...this.#props,
132
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithCheckConstraint(this.#props.createTableNode, constraintName, checkExpression),
135
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, check_constraint_node_js_1.CheckConstraintNode.create(checkExpression, constraintName)),
133
136
  });
134
137
  }
135
138
  /**
@@ -163,7 +166,7 @@ class CreateTableBuilder {
163
166
  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)));
164
167
  return new CreateTableBuilder({
165
168
  ...this.#props,
166
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithForeignKeyConstraint(this.#props.createTableNode, builder.toOperationNode()),
169
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, builder.toOperationNode()),
167
170
  });
168
171
  }
169
172
  toOperationNode() {
@@ -0,0 +1,8 @@
1
+ import { ArrayItemType } from './type-utils.js';
2
+ export declare const LOG_LEVELS: readonly ["query"];
3
+ export declare type LogLevel = ArrayItemType<typeof LOG_LEVELS>;
4
+ export declare class Log {
5
+ #private;
6
+ constructor(levels: ReadonlyArray<LogLevel>);
7
+ query(callback: (log: (message: string) => void) => void): void;
8
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Log = exports.LOG_LEVELS = void 0;
4
+ exports.LOG_LEVELS = ['query'];
5
+ class Log {
6
+ #levels;
7
+ constructor(levels) {
8
+ this.#levels = {
9
+ query: levels.includes('query'),
10
+ };
11
+ }
12
+ query(callback) {
13
+ if (this.#levels.query) {
14
+ callback(this.#query);
15
+ }
16
+ }
17
+ #query(message) {
18
+ console.log(`kysely:query: ${message}`);
19
+ }
20
+ }
21
+ exports.Log = Log;
@@ -1,16 +1,16 @@
1
1
  /// <reference types="node" />
2
2
  export declare type PrimitiveValue = string | number | boolean | null | Date | Buffer | BigInt;
3
- export declare function isEmpty(obj: ArrayLike<any> | string | object | Buffer): boolean;
4
- export declare function isString(obj: any): obj is string;
5
- export declare function isNumber(obj: any): obj is number;
6
- export declare function isBoolean(obj: any): obj is boolean;
7
- export declare function isNull(obj: any): obj is null;
8
- export declare function isDate(obj: any): obj is Date;
9
- export declare function isBuffer(obj: any): obj is Buffer;
10
- export declare function isBigInt(obj: any): obj is BigInt;
11
- export declare function isPrimitive(obj: any): obj is PrimitiveValue;
12
- export declare function isFunction(obj: any): obj is Function;
13
- export declare function isObject(obj: any): obj is Record<string, any>;
3
+ export declare function isEmpty(obj: ArrayLike<unknown> | string | object | Buffer): boolean;
4
+ export declare function isString(obj: unknown): obj is string;
5
+ export declare function isNumber(obj: unknown): obj is number;
6
+ export declare function isBoolean(obj: unknown): obj is boolean;
7
+ export declare function isNull(obj: unknown): obj is null;
8
+ export declare function isDate(obj: unknown): obj is Date;
9
+ export declare function isBuffer(obj: unknown): obj is Buffer;
10
+ export declare function isBigInt(obj: unknown): obj is BigInt;
11
+ export declare function isPrimitive(obj: unknown): obj is PrimitiveValue;
12
+ export declare function isFunction(obj: unknown): obj is Function;
13
+ export declare function isObject(obj: unknown): obj is Record<string, unknown>;
14
14
  export declare function getLast<T>(arr: ArrayLike<T>): T | undefined;
15
15
  export declare function freeze<T>(obj: T): Readonly<T>;
16
16
  export declare function asArray<T>(arg: T | T[]): T[];
@@ -54,7 +54,7 @@ function isFunction(obj) {
54
54
  }
55
55
  exports.isFunction = isFunction;
56
56
  function isObject(obj) {
57
- return obj && typeof obj === 'object';
57
+ return typeof obj === 'object' && obj !== null;
58
58
  }
59
59
  exports.isObject = isObject;
60
60
  function getLast(arr) {
@@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomString = void 0;
4
4
  const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
5
5
  function randomString(length) {
6
- let chars = new Array(length);
6
+ let chars = '';
7
7
  for (let i = 0; i < length; ++i) {
8
- chars[i] = CHARS[Math.floor(Math.random() * CHARS.length)];
8
+ chars += randomChar();
9
9
  }
10
- return chars.join('');
10
+ return chars;
11
11
  }
12
12
  exports.randomString = randomString;
13
+ function randomChar() {
14
+ return CHARS[Math.floor(Math.random() * CHARS.length)];
15
+ }
@@ -136,19 +136,19 @@ export declare type RawBuilderFactory<DB, TB extends keyof DB> = (qb: Expression
136
136
  export declare type AliasedRawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedRawBuilder;
137
137
  export interface InsertResultTypeTag {
138
138
  /** @internal */
139
- __isInsertResultTypeTag__: true;
139
+ readonly __isInsertResultTypeTag__: true;
140
140
  }
141
141
  export interface DeleteResultTypeTag {
142
142
  /** @internal */
143
- __isDeleteResultTypeTag__: true;
143
+ readonly __isDeleteResultTypeTag__: true;
144
144
  }
145
145
  export interface UpdateResultTypeTag {
146
146
  /** @internal */
147
- __isUpdateResultTypeTag__: true;
147
+ readonly __isUpdateResultTypeTag__: true;
148
148
  }
149
149
  export interface GeneratedPlaceholder {
150
150
  /** @internal */
151
- __isGeneratedPlaceholder__: true;
151
+ readonly __isGeneratedPlaceholder__: true;
152
152
  }
153
153
  export declare type ManyResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
154
154
  export declare type SingleResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O | undefined;
@@ -5,6 +5,13 @@ import { Dialect } from '../dialect.js';
5
5
  import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
6
6
  import { DialectAdapter } from '../dialect-adapter.js';
7
7
  import { DatabaseConnection } from '../../driver/database-connection.js';
8
+ /**
9
+ * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
10
+ *
11
+ * The {@link MysqlDialectConfig | configuration} passed to the constructor
12
+ * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
13
+ * method.
14
+ */
8
15
  export declare class MysqlDialect implements Dialect {
9
16
  #private;
10
17
  constructor(config: MysqlDialectConfig);
@@ -17,6 +24,8 @@ export declare class MysqlDialect implements Dialect {
17
24
  * Config for the mysql dialect.
18
25
  *
19
26
  * This interface is equal to `mysql2` library's pool config.
27
+ *
28
+ * https://github.com/sidorares/node-mysql2#using-connection-pools
20
29
  */
21
30
  export interface MysqlDialectConfig {
22
31
  /**
@@ -2,6 +2,13 @@ import { MysqlDriver } from './mysql-driver.js';
2
2
  import { MysqlQueryCompiler } from './mysql-query-compiler.js';
3
3
  import { MysqlIntrospector } from './mysql-introspector.js';
4
4
  import { MysqlAdapter } from './mysql-adapter.js';
5
+ /**
6
+ * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
7
+ *
8
+ * The {@link MysqlDialectConfig | configuration} passed to the constructor
9
+ * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
10
+ * method.
11
+ */
5
12
  export class MysqlDialect {
6
13
  #config;
7
14
  constructor(config) {
@@ -1,4 +1,4 @@
1
- import { isFunction, isObject } from '../../util/object-utils.js';
1
+ import { isFunction, isNumber, isObject } from '../../util/object-utils.js';
2
2
  const PRIVATE_RELEASE_METHOD = Symbol();
3
3
  export class MysqlDriver {
4
4
  #config;
@@ -103,13 +103,13 @@ class MysqlConnection {
103
103
  async executeQuery(compiledQuery) {
104
104
  const result = await this.#executeQuery(compiledQuery);
105
105
  if (isObject(result) && 'insertId' in result && 'affectedRows' in result) {
106
- if (result.insertId) {
106
+ if (isNumber(result.insertId) && result.insertId > 0) {
107
107
  return {
108
108
  insertedPrimaryKey: result.insertId,
109
109
  rows: [],
110
110
  };
111
111
  }
112
- else {
112
+ else if (isNumber(result.affectedRows)) {
113
113
  return {
114
114
  numUpdatedOrDeletedRows: result.affectedRows,
115
115
  rows: [],
@@ -8,6 +8,16 @@ import { Dialect } from '../dialect.js';
8
8
  import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
9
9
  import { DialectAdapter } from '../dialect-adapter.js';
10
10
  import { DatabaseConnection } from '../../driver/database-connection.js';
11
+ /**
12
+ * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
13
+ *
14
+ * The {@link PostgresDialectConfig | configuration} passed to the constructor
15
+ * is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
16
+ * constructor. See the following two links for more documentation:
17
+ *
18
+ * https://node-postgres.com/api/pool
19
+ * https://node-postgres.com/api/client
20
+ */
11
21
  export declare class PostgresDialect implements Dialect {
12
22
  #private;
13
23
  constructor(config: PostgresDialectConfig);
@@ -19,7 +29,7 @@ export declare class PostgresDialect implements Dialect {
19
29
  /**
20
30
  * Config for the postgres dialect.
21
31
  *
22
- * This interface is equal to `pg` library's pool config:
32
+ * This interface is equal to `pg` library's `Pool` config:
23
33
  *
24
34
  * https://node-postgres.com/api/pool
25
35
  * https://node-postgres.com/api/client
@@ -2,6 +2,16 @@ import { PostgresDriver } from './postgres-driver.js';
2
2
  import { PostgresIntrospector } from './postgres-introspector.js';
3
3
  import { PostgresQueryCompiler } from './postgres-query-compiler.js';
4
4
  import { PostgresAdapter } from './postgres-adapter.js';
5
+ /**
6
+ * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
7
+ *
8
+ * The {@link PostgresDialectConfig | configuration} passed to the constructor
9
+ * is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
10
+ * constructor. See the following two links for more documentation:
11
+ *
12
+ * https://node-postgres.com/api/pool
13
+ * https://node-postgres.com/api/client
14
+ */
5
15
  export class PostgresDialect {
6
16
  #config;
7
17
  constructor(config) {
@@ -1,3 +1,4 @@
1
+ import { Log } from '../util/log.js';
1
2
  import { DatabaseConnection } from './database-connection.js';
2
3
  import { Driver, TransactionSettings } from './driver.js';
3
4
  /**
@@ -7,10 +8,10 @@ import { Driver, TransactionSettings } from './driver.js';
7
8
  */
8
9
  export declare class RuntimeDriver implements Driver {
9
10
  #private;
10
- constructor(driver: Driver);
11
+ constructor(driver: Driver, log: Log);
11
12
  init(): Promise<void>;
12
13
  acquireConnection(): Promise<DatabaseConnection>;
13
- releaseConnection(connection: DatabaseConnection): Promise<void>;
14
+ releaseConnection(runtimeConnection: DatabaseConnection): Promise<void>;
14
15
  beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
15
16
  commitTransaction(connection: DatabaseConnection): Promise<void>;
16
17
  rollbackTransaction(connection: DatabaseConnection): Promise<void>;
@@ -5,10 +5,13 @@
5
5
  */
6
6
  export class RuntimeDriver {
7
7
  #driver;
8
+ #log;
8
9
  #initPromise;
9
10
  #destroyPromise;
10
- constructor(driver) {
11
+ #connections = new WeakMap();
12
+ constructor(driver, log) {
11
13
  this.#driver = driver;
14
+ this.#log = log;
12
15
  }
13
16
  async init() {
14
17
  if (!this.#initPromise) {
@@ -21,10 +24,18 @@ export class RuntimeDriver {
21
24
  }
22
25
  async acquireConnection() {
23
26
  await this.init();
24
- return this.#driver.acquireConnection();
27
+ const connection = await this.#driver.acquireConnection();
28
+ let runtimeConnection = this.#connections.get(connection);
29
+ if (!runtimeConnection) {
30
+ runtimeConnection = new RuntimeConnection(connection, this.#log);
31
+ this.#connections.set(connection, runtimeConnection);
32
+ }
33
+ return runtimeConnection;
25
34
  }
26
- releaseConnection(connection) {
27
- return this.#driver.releaseConnection(connection);
35
+ async releaseConnection(runtimeConnection) {
36
+ if (runtimeConnection instanceof RuntimeConnection) {
37
+ await this.#driver.releaseConnection(runtimeConnection.connection);
38
+ }
28
39
  }
29
40
  beginTransaction(connection, settings) {
30
41
  return this.#driver.beginTransaction(connection, settings);
@@ -49,3 +60,31 @@ export class RuntimeDriver {
49
60
  await this.#destroyPromise;
50
61
  }
51
62
  }
63
+ class RuntimeConnection {
64
+ #connection;
65
+ #log;
66
+ get connection() {
67
+ return this.#connection;
68
+ }
69
+ constructor(connection, log) {
70
+ this.#connection = connection;
71
+ this.#log = log;
72
+ }
73
+ async executeQuery(compiledQuery) {
74
+ const startTime = process.hrtime.bigint();
75
+ try {
76
+ return await this.#connection.executeQuery(compiledQuery);
77
+ }
78
+ finally {
79
+ this.#log.query((log) => {
80
+ log(compiledQuery.sql);
81
+ log(`duration: ${this.#calculateDurationMillis(startTime)}ms`);
82
+ });
83
+ }
84
+ }
85
+ #calculateDurationMillis(startTime) {
86
+ const endTime = process.hrtime.bigint();
87
+ const durationTensMillis = Number((endTime - startTime) / 100000n);
88
+ return durationTensMillis / 10;
89
+ }
90
+ }
@@ -110,7 +110,7 @@ export * from './operation-node/operation-node-source.js';
110
110
  export * from './operation-node/order-by-node.js';
111
111
  export * from './introspection/database-introspector.js';
112
112
  export * from './util/compilable.js';
113
- export { AnyColumn, UnknownRow as AnyRow } from './util/type-utils.js';
113
+ export { AnyColumn, UnknownRow, AnyQueryBuilder } from './util/type-utils.js';
114
114
  export { SelectExpression, SelectExpressionOrList, } from './parser/select-parser.js';
115
115
  export { ReferenceExpression, ReferenceExpressionOrList, } from './parser/reference-parser.js';
116
116
  export { ValueExpression, ValueExpressionOrList, } from './parser/value-parser.js';
@@ -10,6 +10,7 @@ import { DatabaseIntrospector } from './introspection/database-introspector.js';
10
10
  import { Driver, IsolationLevel } from './driver/driver.js';
11
11
  import { ParseContext } from './parser/parse-context.js';
12
12
  import { FunctionBuilder } from './query-builder/function-builder.js';
13
+ import { LogLevel } from './util/log.js';
13
14
  /**
14
15
  * The main Kysely class.
15
16
  *
@@ -127,15 +128,20 @@ export declare class Kysely<DB> extends QueryCreator<DB> {
127
128
  */
128
129
  get fn(): FunctionBuilder<DB, keyof DB>;
129
130
  /**
130
- * Starts a transaction. If the callback throws the transaction is rolled back,
131
- * otherwise it's committed.
131
+ * Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
132
132
  *
133
- * @example
134
- * In the example below if either query fails or `someFunction` throws, both inserts
135
- * will be rolled back. Otherwise the transaction will be committed by the time the
136
- * `transaction` function returns the output value. The output value of the
137
- * `transaction` method is the value returned from the callback.
133
+ * The returned {@link TransactionBuilder} can be used to configure the transaction. The
134
+ * {@link TransactionBuilder.execute} method can then be called to run the transaction.
135
+ * {@link TransactionBuilder.execute} takes a function that is run inside the
136
+ * transaction. If the function throws, the transaction is rolled back. Otherwise
137
+ * the transaction is committed.
138
+ *
139
+ * The callback function passed to the {@link TransactionBuilder.execute | execute}
140
+ * method gets the transaction object as its only argument. The transaction is
141
+ * of type {@link Transaction} which inherits {@link Kysely}. Any query
142
+ * started through the transaction object is executed inside the transaction.
138
143
  *
144
+ * @example
139
145
  * ```ts
140
146
  * const catto = await db.transaction().execute(async (trx) => {
141
147
  * const jennifer = await trx.insertInto('person')
@@ -220,8 +226,19 @@ export interface KyselyProps {
220
226
  }
221
227
  export declare function isKyselyProps(obj: unknown): obj is KyselyProps;
222
228
  export interface KyselyConfig {
223
- dialect: Dialect;
224
- plugins?: KyselyPlugin[];
229
+ readonly dialect: Dialect;
230
+ readonly plugins?: KyselyPlugin[];
231
+ /**
232
+ * A list of log levels to log.
233
+ *
234
+ * Currently there's only one level: `query` and it's logged using
235
+ * `console.log`. This will be expanded based on user request later.
236
+ *
237
+ * Log levels:
238
+ *
239
+ * - query: Log each query's SQL and duration.
240
+ */
241
+ readonly log?: ReadonlyArray<LogLevel>;
225
242
  }
226
243
  export declare class ConnectionBuilder<DB> {
227
244
  #private;
@@ -12,6 +12,7 @@ import { TRANSACTION_ISOLATION_LEVELS, } from './driver/driver.js';
12
12
  import { preventAwait } from './util/prevent-await.js';
13
13
  import { DefaultParseContext } from './parser/parse-context.js';
14
14
  import { FunctionBuilder } from './query-builder/function-builder.js';
15
+ import { Log } from './util/log.js';
15
16
  /**
16
17
  * The main Kysely class.
17
18
  *
@@ -57,15 +58,16 @@ export class Kysely extends QueryCreator {
57
58
  constructor(args) {
58
59
  if (isKyselyProps(args)) {
59
60
  super({ executor: args.executor, parseContext: args.parseContext });
60
- this.#props = freeze(args);
61
+ this.#props = freeze({ ...args });
61
62
  }
62
63
  else {
63
64
  const dialect = args.dialect;
64
65
  const driver = dialect.createDriver();
65
66
  const compiler = dialect.createQueryCompiler();
66
67
  const adapter = dialect.createAdapter();
68
+ const log = new Log(args.log ?? []);
67
69
  const parseContext = new DefaultParseContext(adapter);
68
- const runtimeDriver = new RuntimeDriver(driver);
70
+ const runtimeDriver = new RuntimeDriver(driver, log);
69
71
  const connectionProvider = new DefaultConnectionProvider(runtimeDriver);
70
72
  const executor = new DefaultQueryExecutor(compiler, connectionProvider, args.plugins ?? []);
71
73
  super({ executor, parseContext });
@@ -163,15 +165,20 @@ export class Kysely extends QueryCreator {
163
165
  return new FunctionBuilder({ executor: this.#props.executor });
164
166
  }
165
167
  /**
166
- * Starts a transaction. If the callback throws the transaction is rolled back,
167
- * otherwise it's committed.
168
+ * Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
168
169
  *
169
- * @example
170
- * In the example below if either query fails or `someFunction` throws, both inserts
171
- * will be rolled back. Otherwise the transaction will be committed by the time the
172
- * `transaction` function returns the output value. The output value of the
173
- * `transaction` method is the value returned from the callback.
170
+ * The returned {@link TransactionBuilder} can be used to configure the transaction. The
171
+ * {@link TransactionBuilder.execute} method can then be called to run the transaction.
172
+ * {@link TransactionBuilder.execute} takes a function that is run inside the
173
+ * transaction. If the function throws, the transaction is rolled back. Otherwise
174
+ * the transaction is committed.
175
+ *
176
+ * The callback function passed to the {@link TransactionBuilder.execute | execute}
177
+ * method gets the transaction object as its only argument. The transaction is
178
+ * of type {@link Transaction} which inherits {@link Kysely}. Any query
179
+ * started through the transaction object is executed inside the transaction.
174
180
  *
181
+ * @example
175
182
  * ```ts
176
183
  * const catto = await db.transaction().execute(async (trx) => {
177
184
  * const jennifer = await trx.insertInto('person')
@@ -24,6 +24,38 @@ export declare class MigrationModule {
24
24
  * path.join(__dirname, 'migrations')
25
25
  * )
26
26
  * ```
27
+ *
28
+ * In the next example, we use a record of migration objects instead of file folder path.
29
+ * The keys in the object are migration names that can be anything you want. The order of
30
+ * the migrations is determined based on the alphabetical order of the keys in the record.
31
+ * This can be useful if you are using a bundler like webpack or esbuild.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * await db.migration.migrateToLatest({
36
+ * migration1: {
37
+ * async up(db: Kysely<any>): Promise<void> {
38
+ * ...
39
+ * },
40
+ *
41
+ * async down(db: Kysely<any>): Promise<void> {
42
+ * ...
43
+ * }
44
+ * },
45
+ *
46
+ * migration2: {
47
+ * async up(db: Kysely<any>): Promise<void> {
48
+ * ...
49
+ * },
50
+ *
51
+ * async down(db: Kysely<any>): Promise<void> {
52
+ * ...
53
+ * }
54
+ * },
55
+ *
56
+ * ...
57
+ * })
58
+ * ```
27
59
  */
28
60
  migrateToLatest(migrationsFolderPath: string): Promise<void>;
29
61
  migrateToLatest(allMigrations: Record<string, Migration>): Promise<void>;
@@ -13,5 +13,5 @@ export interface CreateSchemaNode extends OperationNode {
13
13
  export declare const CreateSchemaNode: Readonly<{
14
14
  is(node: OperationNode): node is CreateSchemaNode;
15
15
  create(schema: string, params?: CreateSchemaNodeParams | undefined): CreateSchemaNode;
16
- cloneWithModifier(createSchema: CreateSchemaNode, modifier: CreateSchemaNodeModifier): CreateSchemaNode;
16
+ cloneWith(createSchema: CreateSchemaNode, params: CreateSchemaNodeParams): CreateSchemaNode;
17
17
  }>;
@@ -14,10 +14,10 @@ export const CreateSchemaNode = freeze({
14
14
  ...params,
15
15
  });
16
16
  },
17
- cloneWithModifier(createSchema, modifier) {
17
+ cloneWith(createSchema, params) {
18
18
  return freeze({
19
19
  ...createSchema,
20
- modifier,
20
+ ...params,
21
21
  });
22
22
  },
23
23
  });
@@ -1,6 +1,5 @@
1
1
  import { OperationNode } from './operation-node.js';
2
2
  import { TableNode } from './table-node.js';
3
- import { ForeignKeyConstraintNode } from './foreign-key-constraint-node.js';
4
3
  import { ConstraintNode } from './constraint-node.js';
5
4
  import { ColumnDefinitionNode } from './column-definition-node.js';
6
5
  export declare type CreateTableNodeModifier = 'IfNotExists';
@@ -19,8 +18,5 @@ export declare const CreateTableNode: Readonly<{
19
18
  create(table: TableNode): CreateTableNode;
20
19
  cloneWithColumn(createTable: CreateTableNode, column: ColumnDefinitionNode): CreateTableNode;
21
20
  cloneWithModifier(createTable: CreateTableNode, modifier: CreateTableNodeModifier): CreateTableNode;
22
- cloneWithPrimaryKeyConstraint(createTable: CreateTableNode, constraintName: string, columns: string[]): CreateTableNode;
23
- cloneWithUniqueConstraint(createTable: CreateTableNode, constraintName: string, columns: string[]): CreateTableNode;
24
- cloneWithCheckConstraint(createTable: CreateTableNode, constraintName: string, sql: string): CreateTableNode;
25
- cloneWithForeignKeyConstraint(createTable: CreateTableNode, constraint: ForeignKeyConstraintNode): CreateTableNode;
21
+ cloneWithConstraint(createTable: CreateTableNode, constraint: ConstraintNode): CreateTableNode;
26
22
  }>;
@@ -1,7 +1,4 @@
1
1
  import { freeze } from '../util/object-utils.js';
2
- import { CheckConstraintNode } from './check-constraint-node.js';
3
- import { PrimaryConstraintNode } from './primary-constraint-node.js';
4
- import { UniqueConstraintNode } from './unique-constraint-node.js';
5
2
  /**
6
3
  * @internal
7
4
  */
@@ -28,34 +25,7 @@ export const CreateTableNode = freeze({
28
25
  modifier,
29
26
  });
30
27
  },
31
- cloneWithPrimaryKeyConstraint(createTable, constraintName, columns) {
32
- const constraint = PrimaryConstraintNode.create(columns, constraintName);
33
- return freeze({
34
- ...createTable,
35
- constraints: createTable.constraints
36
- ? freeze([...createTable.constraints, constraint])
37
- : freeze([constraint]),
38
- });
39
- },
40
- cloneWithUniqueConstraint(createTable, constraintName, columns) {
41
- const constraint = UniqueConstraintNode.create(columns, constraintName);
42
- return freeze({
43
- ...createTable,
44
- constraints: createTable.constraints
45
- ? freeze([...createTable.constraints, constraint])
46
- : freeze([constraint]),
47
- });
48
- },
49
- cloneWithCheckConstraint(createTable, constraintName, sql) {
50
- const constraint = CheckConstraintNode.create(sql, constraintName);
51
- return freeze({
52
- ...createTable,
53
- constraints: createTable.constraints
54
- ? freeze([...createTable.constraints, constraint])
55
- : freeze([constraint]),
56
- });
57
- },
58
- cloneWithForeignKeyConstraint(createTable, constraint) {
28
+ cloneWithConstraint(createTable, constraint) {
59
29
  return freeze({
60
30
  ...createTable,
61
31
  constraints: createTable.constraints
@@ -21,5 +21,6 @@ export declare const JoinNode: Readonly<{
21
21
  is(node: OperationNode): node is JoinNode;
22
22
  create(joinType: JoinType, table: JoinTableNode): JoinNode;
23
23
  createWithOn(joinType: JoinType, table: JoinTableNode, on: JoinNodeOnNode): JoinNode;
24
- cloneWithOn(joinNode: JoinNode, op: 'And' | 'Or', on: JoinNodeOnNode): JoinNode;
24
+ cloneWithOn(joinNode: JoinNode, on: JoinNodeOnNode): JoinNode;
25
+ cloneWithOrOn(joinNode: JoinNode, on: JoinNodeOnNode): JoinNode;
25
26
  }>;
@@ -24,14 +24,16 @@ export const JoinNode = freeze({
24
24
  on,
25
25
  });
26
26
  },
27
- cloneWithOn(joinNode, op, on) {
27
+ cloneWithOn(joinNode, on) {
28
28
  return freeze({
29
29
  ...joinNode,
30
- on: joinNode.on
31
- ? op === 'And'
32
- ? AndNode.create(joinNode.on, on)
33
- : OrNode.create(joinNode.on, on)
34
- : on,
30
+ on: joinNode.on ? AndNode.create(joinNode.on, on) : on,
31
+ });
32
+ },
33
+ cloneWithOrOn(joinNode, on) {
34
+ return freeze({
35
+ ...joinNode,
36
+ on: joinNode.on ? OrNode.create(joinNode.on, on) : on,
35
37
  });
36
38
  },
37
39
  });
@@ -2,4 +2,4 @@ import { OperationNode } from './operation-node.js';
2
2
  export interface OperationNodeSource {
3
3
  toOperationNode(): OperationNode;
4
4
  }
5
- export declare function isOperationNodeSource(obj: any): obj is OperationNodeSource;
5
+ export declare function isOperationNodeSource(obj: unknown): obj is OperationNodeSource;