kysely 0.10.1 → 0.11.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 (134) hide show
  1. package/README.md +7 -3
  2. package/dist/cjs/dialect/dialect-adapter.d.ts +3 -2
  3. package/dist/cjs/dialect/mysql/mysql-adapter.js +1 -1
  4. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +3 -3
  5. package/dist/cjs/dialect/mysql/mysql-driver.js +25 -32
  6. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +2 -2
  7. package/dist/cjs/dialect/postgres/postgres-driver.js +8 -19
  8. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +7 -0
  9. package/dist/cjs/dialect/sqlite/sqlite-adapter.js +18 -0
  10. package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +64 -0
  11. package/dist/cjs/dialect/sqlite/sqlite-dialect.js +33 -0
  12. package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +14 -0
  13. package/dist/cjs/dialect/sqlite/sqlite-driver.js +116 -0
  14. package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +7 -0
  15. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +31 -0
  16. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +7 -0
  17. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +19 -0
  18. package/dist/cjs/driver/database-connection.d.ts +2 -2
  19. package/dist/cjs/index.d.ts +8 -0
  20. package/dist/cjs/index.js +8 -0
  21. package/dist/cjs/kysely.d.ts +6 -4
  22. package/dist/cjs/kysely.js +6 -4
  23. package/dist/cjs/migration/migration.d.ts +1 -1
  24. package/dist/cjs/migration/migration.js +3 -3
  25. package/dist/cjs/operation-node/create-view-node.d.ts +1 -0
  26. package/dist/cjs/operation-node/having-node.d.ts +4 -8
  27. package/dist/cjs/operation-node/having-node.js +3 -3
  28. package/dist/cjs/operation-node/join-node.d.ts +6 -9
  29. package/dist/cjs/operation-node/join-node.js +10 -7
  30. package/dist/cjs/operation-node/on-node.d.ts +14 -0
  31. package/dist/cjs/operation-node/on-node.js +28 -0
  32. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  33. package/dist/cjs/operation-node/operation-node-transformer.js +8 -0
  34. package/dist/cjs/operation-node/operation-node-visitor.d.ts +3 -0
  35. package/dist/cjs/operation-node/operation-node-visitor.js +4 -0
  36. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  37. package/dist/cjs/operation-node/query-node.d.ts +3 -3
  38. package/dist/cjs/operation-node/select-query-node.d.ts +4 -4
  39. package/dist/cjs/operation-node/where-node.d.ts +4 -8
  40. package/dist/cjs/operation-node/where-node.js +3 -3
  41. package/dist/cjs/parser/filter-parser.js +1 -1
  42. package/dist/cjs/parser/returning-parser.d.ts +4 -2
  43. package/dist/cjs/parser/select-parser.d.ts +2 -2
  44. package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.d.ts +1 -1
  45. package/dist/cjs/query-builder/delete-result.d.ts +5 -0
  46. package/dist/cjs/query-builder/delete-result.js +13 -0
  47. package/dist/cjs/query-builder/expression-builder.d.ts +2 -2
  48. package/dist/cjs/query-builder/expression-builder.js +1 -1
  49. package/dist/cjs/query-builder/function-builder.d.ts +5 -5
  50. package/dist/cjs/query-builder/function-builder.js +3 -3
  51. package/dist/cjs/query-builder/insert-result.d.ts +27 -0
  52. package/dist/cjs/query-builder/insert-result.js +35 -0
  53. package/dist/cjs/query-builder/query-builder.d.ts +91 -81
  54. package/dist/cjs/query-builder/query-builder.js +38 -41
  55. package/dist/cjs/query-builder/update-result.d.ts +5 -0
  56. package/dist/cjs/query-builder/update-result.js +13 -0
  57. package/dist/cjs/query-compiler/compiled-query.d.ts +5 -0
  58. package/dist/cjs/query-compiler/compiled-query.js +12 -0
  59. package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -0
  60. package/dist/cjs/query-compiler/default-query-compiler.js +24 -13
  61. package/dist/cjs/query-creator.d.ts +36 -26
  62. package/dist/cjs/query-creator.js +25 -17
  63. package/dist/cjs/schema/create-view-builder.d.ts +4 -0
  64. package/dist/cjs/schema/create-view-builder.js +11 -0
  65. package/dist/cjs/util/dynamic-import.d.ts +2 -0
  66. package/dist/cjs/util/dynamic-import.js +32 -0
  67. package/dist/cjs/util/type-utils.d.ts +4 -15
  68. package/dist/esm/dialect/dialect-adapter.d.ts +3 -2
  69. package/dist/esm/dialect/mysql/mysql-adapter.js +1 -1
  70. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +3 -3
  71. package/dist/esm/dialect/mysql/mysql-driver.js +26 -33
  72. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +2 -2
  73. package/dist/esm/dialect/postgres/postgres-driver.js +8 -19
  74. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +7 -0
  75. package/dist/esm/dialect/sqlite/sqlite-adapter.js +14 -0
  76. package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +64 -0
  77. package/dist/esm/dialect/sqlite/sqlite-dialect.js +29 -0
  78. package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +14 -0
  79. package/dist/esm/dialect/sqlite/sqlite-driver.js +112 -0
  80. package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +7 -0
  81. package/dist/esm/dialect/sqlite/sqlite-introspector.js +27 -0
  82. package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +7 -0
  83. package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +15 -0
  84. package/dist/esm/driver/database-connection.d.ts +2 -2
  85. package/dist/esm/index.d.ts +8 -0
  86. package/dist/esm/index.js +8 -0
  87. package/dist/esm/kysely.d.ts +6 -4
  88. package/dist/esm/kysely.js +6 -4
  89. package/dist/esm/migration/migration.d.ts +1 -1
  90. package/dist/esm/migration/migration.js +3 -3
  91. package/dist/esm/operation-node/create-view-node.d.ts +1 -0
  92. package/dist/esm/operation-node/having-node.d.ts +4 -8
  93. package/dist/esm/operation-node/having-node.js +3 -3
  94. package/dist/esm/operation-node/join-node.d.ts +6 -9
  95. package/dist/esm/operation-node/join-node.js +10 -7
  96. package/dist/esm/operation-node/on-node.d.ts +14 -0
  97. package/dist/esm/operation-node/on-node.js +25 -0
  98. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  99. package/dist/esm/operation-node/operation-node-transformer.js +8 -0
  100. package/dist/esm/operation-node/operation-node-visitor.d.ts +3 -0
  101. package/dist/esm/operation-node/operation-node-visitor.js +4 -0
  102. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  103. package/dist/esm/operation-node/query-node.d.ts +3 -3
  104. package/dist/esm/operation-node/select-query-node.d.ts +4 -4
  105. package/dist/esm/operation-node/where-node.d.ts +4 -8
  106. package/dist/esm/operation-node/where-node.js +3 -3
  107. package/dist/esm/parser/filter-parser.js +1 -1
  108. package/dist/esm/parser/returning-parser.d.ts +4 -2
  109. package/dist/esm/parser/select-parser.d.ts +2 -2
  110. package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.d.ts +1 -1
  111. package/dist/esm/query-builder/delete-result.d.ts +5 -0
  112. package/dist/esm/query-builder/delete-result.js +9 -0
  113. package/dist/esm/query-builder/expression-builder.d.ts +2 -2
  114. package/dist/esm/query-builder/expression-builder.js +1 -1
  115. package/dist/esm/query-builder/function-builder.d.ts +5 -5
  116. package/dist/esm/query-builder/function-builder.js +3 -3
  117. package/dist/esm/query-builder/insert-result.d.ts +27 -0
  118. package/dist/esm/query-builder/insert-result.js +31 -0
  119. package/dist/esm/query-builder/query-builder.d.ts +91 -81
  120. package/dist/esm/query-builder/query-builder.js +38 -41
  121. package/dist/esm/query-builder/update-result.d.ts +5 -0
  122. package/dist/esm/query-builder/update-result.js +9 -0
  123. package/dist/esm/query-compiler/compiled-query.d.ts +5 -0
  124. package/dist/esm/query-compiler/compiled-query.js +11 -1
  125. package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -0
  126. package/dist/esm/query-compiler/default-query-compiler.js +24 -13
  127. package/dist/esm/query-creator.d.ts +36 -26
  128. package/dist/esm/query-creator.js +25 -17
  129. package/dist/esm/schema/create-view-builder.d.ts +4 -0
  130. package/dist/esm/schema/create-view-builder.js +11 -0
  131. package/dist/esm/util/dynamic-import.d.ts +2 -0
  132. package/dist/esm/util/dynamic-import.js +27 -0
  133. package/dist/esm/util/type-utils.d.ts +4 -15
  134. package/package.json +3 -1
@@ -29,20 +29,17 @@ class QueryCreator {
29
29
  /**
30
30
  * Creates an insert query.
31
31
  *
32
- * The return value of this query is `number | undefined` because of the differences
33
- * between database engines. Most engines (like Mysql) return the auto incrementing
34
- * primary key (if it exists), but some (like postgres) return nothing by default.
35
- * If you are running a database engine like `Mysql` that always returns the primary
36
- * key, you can safely use `!` or the {@link QueryBuilder.castTo | castTo} method
37
- * to cast away the `undefined` from the type.
32
+ * The return value of this query is an instance of {@link InsertResult}. {@link InsertResult}
33
+ * has the {@link InsertResult.insertId | insertId} field that holds the auto incremented id of
34
+ * the inserted row if the db returned one.
38
35
  *
39
36
  * See the {@link QueryBuilder.values | values} method for more info and examples. Also see
40
37
  * the {@link QueryBuilder.returning | returning} method for a way to return columns
41
- * on supported databases like postgres.
38
+ * on supported databases like PostgreSQL.
42
39
  *
43
40
  * @example
44
41
  * ```ts
45
- * const maybePrimaryKey: number | undefined = await db
42
+ * const result = await db
46
43
  * .insertInto('person')
47
44
  * .values({
48
45
  * id: db.generated,
@@ -50,10 +47,12 @@ class QueryCreator {
50
47
  * last_name: 'Aniston'
51
48
  * })
52
49
  * .executeTakeFirst()
50
+ *
51
+ * console.log(result.insertId)
53
52
  * ```
54
53
  *
55
54
  * @example
56
- * Some databases like postgres support the `returning` method:
55
+ * Some databases like PostgreSQL support the `returning` method:
57
56
  *
58
57
  * ```ts
59
58
  * const { id } = await db
@@ -79,14 +78,18 @@ class QueryCreator {
79
78
  * Creates a delete query.
80
79
  *
81
80
  * See the {@link QueryBuilder.where} method for examples on how to specify
82
- * a where clause for the delete operation .
81
+ * a where clause for the delete operation.
82
+ *
83
+ * The return value of the query is an instance of {@link DeleteResult}.
83
84
  *
84
85
  * @example
85
86
  * ```ts
86
- * const numAffectedRows = await db
87
+ * const result = await db
87
88
  * .deleteFrom('person')
88
89
  * .where('person.id', '=', 1)
89
90
  * .executeTakeFirst()
91
+ *
92
+ * console.log(result.numDeletedRows)
90
93
  * ```
91
94
  */
92
95
  deleteFrom(table) {
@@ -106,13 +109,17 @@ class QueryCreator {
106
109
  * See the {@link QueryBuilder.set} method for examples on how to
107
110
  * specify the updates.
108
111
  *
112
+ * The return value of the query is an {@link UpdateResult}.
113
+ *
109
114
  * @example
110
115
  * ```ts
111
- * const numAffectedRows = await db
116
+ * const result = await db
112
117
  * .updateTable('person')
113
118
  * .set({ first_name: 'Jennifer' })
114
119
  * .where('person.id', '=', 1)
115
120
  * .executeTakeFirst()
121
+ *
122
+ * console.log(result.numUpdatedRows)
116
123
  * ```
117
124
  */
118
125
  updateTable(table) {
@@ -170,7 +177,7 @@ class QueryCreator {
170
177
  * .execute()
171
178
  * ```
172
179
  *
173
- * The generated SQL (postgresql):
180
+ * The generated SQL (PostgreSQL):
174
181
  *
175
182
  * ```sql
176
183
  * select * from "mammals"."pet"
@@ -190,7 +197,7 @@ class QueryCreator {
190
197
  * .execute()
191
198
  * ```
192
199
  *
193
- * The generated SQL (postgresql):
200
+ * The generated SQL (PostgreSQL):
194
201
  *
195
202
  * ```sql
196
203
  * select "p"."name" from "mammals"."pet" as "p"
@@ -240,7 +247,7 @@ class QueryCreator {
240
247
  * console.log(person.name)
241
248
  * ```
242
249
  *
243
- * The generated SQL (postgresql):
250
+ * The generated SQL (PostgreSQL):
244
251
  *
245
252
  * ```sql
246
253
  * select concat(first_name, ' ', last_name) as "name"
@@ -298,7 +305,7 @@ class QueryCreator {
298
305
  * }
299
306
  * ```
300
307
  *
301
- * The generated SQL (postgresql):
308
+ * The generated SQL (PostgreSQL):
302
309
  *
303
310
  * ```sql
304
311
  * select * from "person" where now() - birth_date > interval $1 year
@@ -312,7 +319,8 @@ class QueryCreator {
312
319
  * Example of creating a completely raw query from scratch:
313
320
  *
314
321
  * ```ts
315
- * const persons = await db.raw<Person>('select p.* from person p').execute()
322
+ * const result = await db.raw<Person>('select p.* from person p').execute()
323
+ * const persons = result.rows
316
324
  * ```
317
325
  *
318
326
  * For a raw query, you need to specify the type of the returned __row__. In
@@ -10,6 +10,10 @@ export declare class CreateViewBuilder implements OperationNodeSource, Compilabl
10
10
  #private;
11
11
  constructor(props: CreateViewBuilderProps);
12
12
  materialized(): CreateViewBuilder;
13
+ /**
14
+ * Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
15
+ */
16
+ ifNotExists(): CreateViewBuilder;
13
17
  orReplace(): CreateViewBuilder;
14
18
  columns(columns: string[]): CreateViewBuilder;
15
19
  as(query: AnyQueryBuilder | AnyRawBuilder): CreateViewBuilder;
@@ -31,6 +31,17 @@ class CreateViewBuilder {
31
31
  }),
32
32
  });
33
33
  }
34
+ /**
35
+ * Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
36
+ */
37
+ ifNotExists() {
38
+ return new CreateViewBuilder({
39
+ ...this.#props,
40
+ createViewNode: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.createViewNode, {
41
+ ifNotExists: true,
42
+ }),
43
+ });
44
+ }
34
45
  orReplace() {
35
46
  return new CreateViewBuilder({
36
47
  ...this.#props,
@@ -0,0 +1,2 @@
1
+ export declare function importFunction<T extends Function>(modulePath: string): Promise<T>;
2
+ export declare function importNamedFunction<T extends Function>(modulePath: string, exportName: string): Promise<T>;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.importNamedFunction = exports.importFunction = void 0;
4
+ const object_utils_js_1 = require("./object-utils.js");
5
+ async function importFunction(modulePath) {
6
+ const module = await Promise.resolve().then(() => require(modulePath));
7
+ const errorMessage = `module "${modulePath}" doesn export a function`;
8
+ if ((0, object_utils_js_1.isFunction)(module)) {
9
+ return module;
10
+ }
11
+ else if ((0, object_utils_js_1.isObject)(module) && (0, object_utils_js_1.isFunction)(module.default)) {
12
+ return module.default;
13
+ }
14
+ throw new Error(errorMessage);
15
+ }
16
+ exports.importFunction = importFunction;
17
+ async function importNamedFunction(modulePath, exportName) {
18
+ const module = await Promise.resolve().then(() => require(modulePath));
19
+ const errorMessage = `no function export named "${exportName}" found in module "${modulePath}"`;
20
+ if (!(0, object_utils_js_1.isObject)(module)) {
21
+ throw new Error(errorMessage);
22
+ }
23
+ if ((0, object_utils_js_1.isFunction)(module[exportName])) {
24
+ return module[exportName];
25
+ }
26
+ else if ((0, object_utils_js_1.isObject)(module.default) &&
27
+ (0, object_utils_js_1.isFunction)(module.default[exportName])) {
28
+ return module.default[exportName];
29
+ }
30
+ throw new Error(errorMessage);
31
+ }
32
+ exports.importNamedFunction = importNamedFunction;
@@ -1,6 +1,9 @@
1
1
  import { AliasedRawBuilder, RawBuilder } from '../raw-builder/raw-builder.js';
2
2
  import { AliasedQueryBuilder, QueryBuilder } from '../query-builder/query-builder.js';
3
3
  import { ExpressionBuilder } from '../query-builder/expression-builder.js';
4
+ import { InsertResult } from '../query-builder/insert-result.js';
5
+ import { DeleteResult } from '../query-builder/delete-result.js';
6
+ import { UpdateResult } from '../query-builder/update-result.js';
4
7
  /**
5
8
  * Given an object type, extracts the union of all value types.
6
9
  */
@@ -134,25 +137,11 @@ export declare type QueryBuilderFactory<DB, TB extends keyof DB> = (qb: Expressi
134
137
  export declare type AliasedQueryBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedQueryBuilder;
135
138
  export declare type RawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyRawBuilder;
136
139
  export declare type AliasedRawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedRawBuilder;
137
- export interface InsertResultTypeTag {
138
- /** @internal */
139
- readonly __isInsertResultTypeTag__: true;
140
- }
141
- export interface DeleteResultTypeTag {
142
- /** @internal */
143
- readonly __isDeleteResultTypeTag__: true;
144
- }
145
- export interface UpdateResultTypeTag {
146
- /** @internal */
147
- readonly __isUpdateResultTypeTag__: true;
148
- }
149
140
  export interface GeneratedPlaceholder {
150
141
  /** @internal */
151
142
  readonly __isGeneratedPlaceholder__: true;
152
143
  }
153
- export declare type ManyResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
154
- export declare type SingleResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O | undefined;
155
- export declare type NonEmptySingleResultRowType<O> = O extends InsertResultTypeTag ? number : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
144
+ export declare type SingleResultType<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : O | undefined;
156
145
  export declare type UnknownRow = Record<string, unknown>;
157
146
  export declare type Nullable<T> = {
158
147
  [P in keyof T]: T[P] | null;
@@ -24,7 +24,7 @@ export interface DialectAdapter {
24
24
  * it's not possible for two migration processes to run in parallel.
25
25
  *
26
26
  * Most dialects have explicit locks that can be used, like advisory locks
27
- * in postgres and the get_lock function in mysql.
27
+ * in PostgreSQL and the get_lock function in MySQL.
28
28
  *
29
29
  * If the dialect doesn't have explicit locks the {@link MIGRATION_LOCK_TABLE}
30
30
  * created by Kysely can be used instead. {@link MIGRATION_LOCK_TABLE}
@@ -44,7 +44,8 @@ export interface DialectAdapter {
44
44
  *
45
45
  * If `supportsTransactionalDdl` is `true` then the `db` passed to this method
46
46
  * is a transaction inside which the migrations were executed. Otherwise `db`
47
- * is a single connection (session) that was used to execute the migrations.
47
+ * is a single connection (session) that was used to execute the migrations
48
+ * and the `acquireMigrationLock` call.
48
49
  */
49
50
  releaseMigrationLock(db: Kysely<any>): Promise<void>;
50
51
  }
@@ -13,7 +13,7 @@ export class MysqlAdapter {
13
13
  // released when the connection is destroyed (session ends) or when the lock
14
14
  // is released using `release_lock`. This way we know that the lock is either
15
15
  // released by us after successfull or failed migrations OR it's released by
16
- // mysql if the process gets killed for some reason.
16
+ // MySQL if the process gets killed for some reason.
17
17
  await db
18
18
  .raw(`select get_lock('${LOCK_ID}', ${LOCK_TIMEOUT_SECONDS})`)
19
19
  .execute();
@@ -21,7 +21,7 @@ export declare class MysqlDialect implements Dialect {
21
21
  createIntrospector(db: Kysely<any>): DatabaseIntrospector;
22
22
  }
23
23
  /**
24
- * Config for the mysql dialect.
24
+ * Config for the MySQL dialect.
25
25
  *
26
26
  * This interface is equal to `mysql2` library's pool config.
27
27
  *
@@ -137,7 +137,7 @@ export interface MysqlDialectConfig {
137
137
  */
138
138
  trace?: boolean;
139
139
  /**
140
- * Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
140
+ * Allow multiple MySQL statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
141
141
  */
142
142
  multipleStatements?: boolean;
143
143
  /**
@@ -186,7 +186,7 @@ export interface MysqlDialectConfig {
186
186
  /**
187
187
  * Called once for each created connection.
188
188
  *
189
- * This is a Kysely specific feature and does not come from the `pg` module.
189
+ * This is a Kysely specific feature and does not come from the `mysql2` module.
190
190
  */
191
191
  onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
192
192
  }
@@ -1,4 +1,6 @@
1
- import { isFunction, isNumber, isObject } from '../../util/object-utils.js';
1
+ import { CompiledQuery } from '../../query-compiler/compiled-query.js';
2
+ import { importNamedFunction } from '../../util/dynamic-import.js';
3
+ import { isObject } from '../../util/object-utils.js';
2
4
  const PRIVATE_RELEASE_METHOD = Symbol();
3
5
  export class MysqlDriver {
4
6
  #config;
@@ -47,19 +49,16 @@ export class MysqlDriver {
47
49
  }
48
50
  async beginTransaction(connection, settings) {
49
51
  if (settings.isolationLevel) {
50
- // On mysql this sets the isolation level of the next transaction.
51
- await connection.executeQuery({
52
- sql: `set transaction isolation level ${settings.isolationLevel}`,
53
- parameters: [],
54
- });
52
+ // On MySQL this sets the isolation level of the next transaction.
53
+ await connection.executeQuery(CompiledQuery.raw(`set transaction isolation level ${settings.isolationLevel}`));
55
54
  }
56
- await connection.executeQuery({ sql: 'begin', parameters: [] });
55
+ await connection.executeQuery(CompiledQuery.raw('begin'));
57
56
  }
58
57
  async commitTransaction(connection) {
59
- await connection.executeQuery({ sql: 'commit', parameters: [] });
58
+ await connection.executeQuery(CompiledQuery.raw('commit'));
60
59
  }
61
60
  async rollbackTransaction(connection) {
62
- await connection.executeQuery({ sql: 'rollback', parameters: [] });
61
+ await connection.executeQuery(CompiledQuery.raw('rollback'));
63
62
  }
64
63
  async releaseConnection(connection) {
65
64
  const mysqlConnection = connection;
@@ -80,19 +79,10 @@ export class MysqlDriver {
80
79
  }
81
80
  async function importMysqlPoolFactory() {
82
81
  try {
83
- // For this to work with both esm and cjs modules we need
84
- // this hacky crap here.
85
- const mysql = (await import('mysql2'));
86
- if (isFunction(mysql.createPool)) {
87
- return mysql.createPool;
88
- }
89
- else {
90
- // With esm the imported module doesn't match the typings.
91
- return mysql.default.createPool;
92
- }
82
+ return await importNamedFunction('mysql2', 'createPool');
93
83
  }
94
84
  catch (error) {
95
- throw new Error('Mysql client not installed. Please run `npm install mysql2`');
85
+ throw new Error('MySQL client not installed. Please run `npm install mysql2`');
96
86
  }
97
87
  }
98
88
  class MysqlConnection {
@@ -102,19 +92,19 @@ class MysqlConnection {
102
92
  }
103
93
  async executeQuery(compiledQuery) {
104
94
  const result = await this.#executeQuery(compiledQuery);
105
- if (isObject(result) && 'insertId' in result && 'affectedRows' in result) {
106
- if (isNumber(result.insertId) && result.insertId > 0) {
107
- return {
108
- insertedPrimaryKey: result.insertId,
109
- rows: [],
110
- };
111
- }
112
- else if (isNumber(result.affectedRows)) {
113
- return {
114
- numUpdatedOrDeletedRows: result.affectedRows,
115
- rows: [],
116
- };
117
- }
95
+ if (isOkPacket(result)) {
96
+ const { insertId, affectedRows } = result;
97
+ return {
98
+ insertId: insertId !== undefined &&
99
+ insertId !== null &&
100
+ insertId.toString() !== '0'
101
+ ? BigInt(insertId)
102
+ : undefined,
103
+ numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
104
+ ? BigInt(affectedRows)
105
+ : undefined,
106
+ rows: [],
107
+ };
118
108
  }
119
109
  else if (Array.isArray(result)) {
120
110
  return {
@@ -141,3 +131,6 @@ class MysqlConnection {
141
131
  this.#rawConnection.release();
142
132
  }
143
133
  }
134
+ function isOkPacket(obj) {
135
+ return isObject(obj) && 'insertId' in obj && 'affectedRows' in obj;
136
+ }
@@ -27,7 +27,7 @@ export declare class PostgresDialect implements Dialect {
27
27
  createIntrospector(db: Kysely<any>): DatabaseIntrospector;
28
28
  }
29
29
  /**
30
- * Config for the postgres dialect.
30
+ * Config for the PostgreSQL dialect.
31
31
  *
32
32
  * This interface is equal to `pg` library's `Pool` config:
33
33
  *
@@ -91,7 +91,7 @@ export interface PostgresDialectConfig {
91
91
  *
92
92
  * Setting `allowExitOnIdle: true` in the config will allow the node event loop to exit
93
93
  * as soon as all clients in the pool are idle, even if their socket is still open
94
- * to the postgres server. This can be handy in scripts & tests
94
+ * to the PostgreSQL server. This can be handy in scripts & tests
95
95
  * where you don't want to wait for your clients to go idle before your process exits.
96
96
  */
97
97
  allowExitOnIdle?: boolean;
@@ -1,4 +1,5 @@
1
- import { isFunction } from '../../util/object-utils.js';
1
+ import { CompiledQuery } from '../../query-compiler/compiled-query.js';
2
+ import { importNamedFunction } from '../../util/dynamic-import.js';
2
3
  const PRIVATE_RELEASE_METHOD = Symbol();
3
4
  export class PostgresDriver {
4
5
  #config;
@@ -35,20 +36,17 @@ export class PostgresDriver {
35
36
  }
36
37
  async beginTransaction(connection, settings) {
37
38
  if (settings.isolationLevel) {
38
- await connection.executeQuery({
39
- sql: `start transaction isolation level ${settings.isolationLevel}`,
40
- parameters: [],
41
- });
39
+ await connection.executeQuery(CompiledQuery.raw(`start transaction isolation level ${settings.isolationLevel}`));
42
40
  }
43
41
  else {
44
- await connection.executeQuery({ sql: 'begin', parameters: [] });
42
+ await connection.executeQuery(CompiledQuery.raw('begin'));
45
43
  }
46
44
  }
47
45
  async commitTransaction(connection) {
48
- await connection.executeQuery({ sql: 'commit', parameters: [] });
46
+ await connection.executeQuery(CompiledQuery.raw('commit'));
49
47
  }
50
48
  async rollbackTransaction(connection) {
51
- await connection.executeQuery({ sql: 'rollback', parameters: [] });
49
+ await connection.executeQuery(CompiledQuery.raw('rollback'));
52
50
  }
53
51
  async releaseConnection(connection) {
54
52
  const pgConnection = connection;
@@ -64,16 +62,7 @@ export class PostgresDriver {
64
62
  }
65
63
  async function importPgPool() {
66
64
  try {
67
- // For this to work with both esm and cjs modules we need
68
- // this hacky crap here.
69
- const pg = (await import('pg'));
70
- if (isFunction(pg.Pool)) {
71
- return pg.Pool;
72
- }
73
- else {
74
- // With esm the imported module doesn't match the typings.
75
- return pg.default.Pool;
76
- }
65
+ return await importNamedFunction('pg', 'Pool');
77
66
  }
78
67
  catch (error) {
79
68
  throw new Error('Postgres client not installed. Please run `npm install pg`');
@@ -90,7 +79,7 @@ class PostgresConnection {
90
79
  ]);
91
80
  if (result.command === 'UPDATE' || result.command === 'DELETE') {
92
81
  return {
93
- numUpdatedOrDeletedRows: result.rowCount,
82
+ numUpdatedOrDeletedRows: BigInt(result.rowCount),
94
83
  rows: result.rows ?? [],
95
84
  };
96
85
  }
@@ -0,0 +1,7 @@
1
+ import { DialectAdapter } from '../dialect-adapter.js';
2
+ export declare class SqliteAdapter implements DialectAdapter {
3
+ get supportsTransactionalDdl(): boolean;
4
+ get supportsReturning(): boolean;
5
+ acquireMigrationLock(): Promise<void>;
6
+ releaseMigrationLock(): Promise<void>;
7
+ }
@@ -0,0 +1,14 @@
1
+ export class SqliteAdapter {
2
+ get supportsTransactionalDdl() {
3
+ return false;
4
+ }
5
+ get supportsReturning() {
6
+ return false;
7
+ }
8
+ async acquireMigrationLock() {
9
+ // Nothing to do here.
10
+ }
11
+ async releaseMigrationLock() {
12
+ // Nothing to do here.
13
+ }
14
+ }
@@ -0,0 +1,64 @@
1
+ import { Driver } from '../../driver/driver.js';
2
+ import { Kysely } from '../../kysely.js';
3
+ import { QueryCompiler } from '../../query-compiler/query-compiler.js';
4
+ import { Dialect } from '../dialect.js';
5
+ import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
6
+ import { DialectAdapter } from '../dialect-adapter.js';
7
+ import { DatabaseConnection } from '../../driver/database-connection.js';
8
+ /**
9
+ * SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
10
+ *
11
+ * The {@link SqliteDialectConfig | configuration} is used to construct an instance of
12
+ * [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
13
+ * class. The configuration values are passed directly to the `Database` constructor.
14
+ */
15
+ export declare class SqliteDialect implements Dialect {
16
+ #private;
17
+ constructor(config: SqliteDialectConfig);
18
+ createDriver(): Driver;
19
+ createQueryCompiler(): QueryCompiler;
20
+ createAdapter(): DialectAdapter;
21
+ createIntrospector(db: Kysely<any>): DatabaseIntrospector;
22
+ }
23
+ /**
24
+ * Config for the SQLite dialect.
25
+ *
26
+ * This interface is equal to `better-sqlite3` library's `Databse` class's constructor parameters.
27
+ *
28
+ * https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
29
+ */
30
+ export interface SqliteDialectConfig {
31
+ /**
32
+ * The database file path.
33
+ *
34
+ * This is passed as the first argument for the [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
35
+ * class constructor.
36
+ */
37
+ databasePath: string;
38
+ /**
39
+ * Open the database connection in readonly mode (default: false)
40
+ */
41
+ readonly?: boolean;
42
+ /**
43
+ * If the database does not exist, an Error will be thrown instead of creating a new file.
44
+ * This option is ignored for in-memory, temporary, or readonly database connections
45
+ * (default: false)
46
+ */
47
+ fileMustExist?: boolean;
48
+ /**
49
+ * The number of milliseconds to wait when executing queries on a locked database,
50
+ * before throwing a SQLITE_BUSY error (default: 5000)
51
+ */
52
+ timeout?: number;
53
+ /**
54
+ * Provide a function that gets called with every SQL string executed by the database connection
55
+ * (default: null)
56
+ */
57
+ verbose?: (sql: string) => void;
58
+ /**
59
+ * Called once when the first query is executed.
60
+ *
61
+ * This is a Kysely specific feature and does not come from the `better-sqlite3` module.
62
+ */
63
+ onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
64
+ }
@@ -0,0 +1,29 @@
1
+ import { SqliteDriver } from './sqlite-driver.js';
2
+ import { SqliteQueryCompiler } from './sqlite-query-compiler.js';
3
+ import { SqliteIntrospector } from './sqlite-introspector.js';
4
+ import { SqliteAdapter } from './sqlite-adapter.js';
5
+ /**
6
+ * SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
7
+ *
8
+ * The {@link SqliteDialectConfig | configuration} is used to construct an instance of
9
+ * [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
10
+ * class. The configuration values are passed directly to the `Database` constructor.
11
+ */
12
+ export class SqliteDialect {
13
+ #config;
14
+ constructor(config) {
15
+ this.#config = config;
16
+ }
17
+ createDriver() {
18
+ return new SqliteDriver(this.#config);
19
+ }
20
+ createQueryCompiler() {
21
+ return new SqliteQueryCompiler();
22
+ }
23
+ createAdapter() {
24
+ return new SqliteAdapter();
25
+ }
26
+ createIntrospector(db) {
27
+ return new SqliteIntrospector(db);
28
+ }
29
+ }
@@ -0,0 +1,14 @@
1
+ import { DatabaseConnection } from '../../driver/database-connection.js';
2
+ import { Driver } from '../../driver/driver.js';
3
+ import { SqliteDialectConfig } from './sqlite-dialect.js';
4
+ export declare class SqliteDriver implements Driver {
5
+ #private;
6
+ constructor(config: SqliteDialectConfig);
7
+ init(): Promise<void>;
8
+ acquireConnection(): Promise<DatabaseConnection>;
9
+ beginTransaction(connection: DatabaseConnection): Promise<void>;
10
+ commitTransaction(connection: DatabaseConnection): Promise<void>;
11
+ rollbackTransaction(connection: DatabaseConnection): Promise<void>;
12
+ releaseConnection(): Promise<void>;
13
+ destroy(): Promise<void>;
14
+ }