kysely 0.9.9 → 0.11.1

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 (206) hide show
  1. package/README.md +22 -16
  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 +44 -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/driver/runtime-driver.js +3 -4
  20. package/dist/cjs/index.d.ts +8 -0
  21. package/dist/cjs/index.js +8 -0
  22. package/dist/cjs/kysely.d.ts +6 -4
  23. package/dist/cjs/kysely.js +6 -4
  24. package/dist/cjs/migration/migration.d.ts +1 -1
  25. package/dist/cjs/migration/migration.js +3 -3
  26. package/dist/cjs/operation-node/column-definition-node.d.ts +8 -6
  27. package/dist/cjs/operation-node/column-definition-node.js +4 -4
  28. package/dist/cjs/operation-node/create-schema-node.d.ts +1 -2
  29. package/dist/cjs/operation-node/create-table-node.d.ts +2 -3
  30. package/dist/cjs/operation-node/create-table-node.js +2 -2
  31. package/dist/cjs/operation-node/create-view-node.d.ts +3 -2
  32. package/dist/cjs/operation-node/default-value-node.d.ts +14 -0
  33. package/dist/cjs/operation-node/default-value-node.js +18 -0
  34. package/dist/cjs/operation-node/drop-index-node.d.ts +1 -2
  35. package/dist/cjs/operation-node/drop-schema-node.d.ts +2 -3
  36. package/dist/cjs/operation-node/drop-schema-node.js +2 -2
  37. package/dist/cjs/operation-node/drop-table-node.d.ts +2 -3
  38. package/dist/cjs/operation-node/drop-table-node.js +2 -2
  39. package/dist/cjs/operation-node/drop-view-node.d.ts +3 -3
  40. package/dist/cjs/operation-node/drop-view-node.js +2 -2
  41. package/dist/cjs/operation-node/generated-always-as-node.d.ts +16 -0
  42. package/dist/cjs/operation-node/generated-always-as-node.js +25 -0
  43. package/dist/cjs/operation-node/having-node.d.ts +4 -8
  44. package/dist/cjs/operation-node/having-node.js +3 -3
  45. package/dist/cjs/operation-node/join-node.d.ts +6 -9
  46. package/dist/cjs/operation-node/join-node.js +10 -7
  47. package/dist/cjs/operation-node/on-node.d.ts +14 -0
  48. package/dist/cjs/operation-node/on-node.js +28 -0
  49. package/dist/cjs/operation-node/operation-node-transformer.d.ts +6 -0
  50. package/dist/cjs/operation-node/operation-node-transformer.js +39 -13
  51. package/dist/cjs/operation-node/operation-node-visitor.d.ts +7 -0
  52. package/dist/cjs/operation-node/operation-node-visitor.js +9 -2
  53. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  54. package/dist/cjs/operation-node/query-node.d.ts +3 -3
  55. package/dist/cjs/operation-node/raw-node.d.ts +1 -1
  56. package/dist/cjs/operation-node/select-query-node.d.ts +5 -5
  57. package/dist/cjs/operation-node/select-query-node.js +1 -1
  58. package/dist/cjs/operation-node/where-node.d.ts +4 -8
  59. package/dist/cjs/operation-node/where-node.js +3 -3
  60. package/dist/cjs/parser/filter-parser.js +1 -1
  61. package/dist/cjs/parser/returning-parser.d.ts +4 -2
  62. package/dist/cjs/parser/select-parser.d.ts +2 -2
  63. package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.d.ts +1 -1
  64. package/dist/cjs/query-builder/delete-result.d.ts +5 -0
  65. package/dist/cjs/query-builder/delete-result.js +13 -0
  66. package/dist/cjs/query-builder/expression-builder.d.ts +2 -2
  67. package/dist/cjs/query-builder/expression-builder.js +1 -1
  68. package/dist/cjs/query-builder/function-builder.d.ts +5 -5
  69. package/dist/cjs/query-builder/function-builder.js +3 -3
  70. package/dist/cjs/query-builder/insert-result.d.ts +27 -0
  71. package/dist/cjs/query-builder/insert-result.js +35 -0
  72. package/dist/cjs/query-builder/query-builder.d.ts +91 -81
  73. package/dist/cjs/query-builder/query-builder.js +38 -41
  74. package/dist/cjs/query-builder/update-result.d.ts +5 -0
  75. package/dist/cjs/query-builder/update-result.js +13 -0
  76. package/dist/cjs/query-compiler/compiled-query.d.ts +5 -0
  77. package/dist/cjs/query-compiler/compiled-query.js +12 -0
  78. package/dist/cjs/query-compiler/default-query-compiler.d.ts +6 -0
  79. package/dist/cjs/query-compiler/default-query-compiler.js +60 -26
  80. package/dist/cjs/query-creator.d.ts +36 -26
  81. package/dist/cjs/query-creator.js +25 -17
  82. package/dist/cjs/raw-builder/raw-builder.d.ts +1 -1
  83. package/dist/cjs/schema/alter-table-builder.d.ts +6 -2
  84. package/dist/cjs/schema/alter-table-builder.js +28 -4
  85. package/dist/cjs/schema/column-definition-builder.d.ts +43 -2
  86. package/dist/cjs/schema/column-definition-builder.js +23 -6
  87. package/dist/cjs/schema/create-schema-builder.js +1 -1
  88. package/dist/cjs/schema/create-table-builder.d.ts +7 -8
  89. package/dist/cjs/schema/create-table-builder.js +8 -9
  90. package/dist/cjs/schema/create-view-builder.d.ts +5 -0
  91. package/dist/cjs/schema/create-view-builder.js +20 -1
  92. package/dist/cjs/schema/drop-index-builder.js +1 -1
  93. package/dist/cjs/schema/drop-schema-builder.js +3 -1
  94. package/dist/cjs/schema/drop-table-builder.js +3 -1
  95. package/dist/cjs/schema/drop-view-builder.d.ts +1 -0
  96. package/dist/cjs/schema/drop-view-builder.js +11 -1
  97. package/dist/cjs/schema/schema.d.ts +3 -3
  98. package/dist/cjs/schema/schema.js +3 -3
  99. package/dist/cjs/util/dynamic-import.d.ts +2 -0
  100. package/dist/cjs/util/dynamic-import.js +32 -0
  101. package/dist/cjs/util/performance-now.d.ts +1 -0
  102. package/dist/cjs/util/performance-now.js +13 -0
  103. package/dist/cjs/util/type-utils.d.ts +4 -15
  104. package/dist/esm/dialect/dialect-adapter.d.ts +3 -2
  105. package/dist/esm/dialect/mysql/mysql-adapter.js +1 -1
  106. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +3 -3
  107. package/dist/esm/dialect/mysql/mysql-driver.js +26 -33
  108. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +2 -2
  109. package/dist/esm/dialect/postgres/postgres-driver.js +8 -19
  110. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +7 -0
  111. package/dist/esm/dialect/sqlite/sqlite-adapter.js +14 -0
  112. package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +64 -0
  113. package/dist/esm/dialect/sqlite/sqlite-dialect.js +29 -0
  114. package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +14 -0
  115. package/dist/esm/dialect/sqlite/sqlite-driver.js +112 -0
  116. package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +7 -0
  117. package/dist/esm/dialect/sqlite/sqlite-introspector.js +40 -0
  118. package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +7 -0
  119. package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +15 -0
  120. package/dist/esm/driver/database-connection.d.ts +2 -2
  121. package/dist/esm/driver/runtime-driver.js +3 -4
  122. package/dist/esm/index.d.ts +8 -0
  123. package/dist/esm/index.js +8 -0
  124. package/dist/esm/kysely.d.ts +6 -4
  125. package/dist/esm/kysely.js +6 -4
  126. package/dist/esm/migration/migration.d.ts +1 -1
  127. package/dist/esm/migration/migration.js +3 -3
  128. package/dist/esm/operation-node/column-definition-node.d.ts +8 -6
  129. package/dist/esm/operation-node/column-definition-node.js +4 -4
  130. package/dist/esm/operation-node/create-schema-node.d.ts +1 -2
  131. package/dist/esm/operation-node/create-table-node.d.ts +2 -3
  132. package/dist/esm/operation-node/create-table-node.js +2 -2
  133. package/dist/esm/operation-node/create-view-node.d.ts +3 -2
  134. package/dist/esm/operation-node/default-value-node.d.ts +14 -0
  135. package/dist/esm/operation-node/default-value-node.js +15 -0
  136. package/dist/esm/operation-node/drop-index-node.d.ts +1 -2
  137. package/dist/esm/operation-node/drop-schema-node.d.ts +2 -3
  138. package/dist/esm/operation-node/drop-schema-node.js +2 -2
  139. package/dist/esm/operation-node/drop-table-node.d.ts +2 -3
  140. package/dist/esm/operation-node/drop-table-node.js +2 -2
  141. package/dist/esm/operation-node/drop-view-node.d.ts +3 -3
  142. package/dist/esm/operation-node/drop-view-node.js +2 -2
  143. package/dist/esm/operation-node/generated-always-as-node.d.ts +16 -0
  144. package/dist/esm/operation-node/generated-always-as-node.js +22 -0
  145. package/dist/esm/operation-node/having-node.d.ts +4 -8
  146. package/dist/esm/operation-node/having-node.js +3 -3
  147. package/dist/esm/operation-node/join-node.d.ts +6 -9
  148. package/dist/esm/operation-node/join-node.js +10 -7
  149. package/dist/esm/operation-node/on-node.d.ts +14 -0
  150. package/dist/esm/operation-node/on-node.js +25 -0
  151. package/dist/esm/operation-node/operation-node-transformer.d.ts +6 -0
  152. package/dist/esm/operation-node/operation-node-transformer.js +39 -13
  153. package/dist/esm/operation-node/operation-node-visitor.d.ts +7 -0
  154. package/dist/esm/operation-node/operation-node-visitor.js +9 -2
  155. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  156. package/dist/esm/operation-node/query-node.d.ts +3 -3
  157. package/dist/esm/operation-node/raw-node.d.ts +1 -1
  158. package/dist/esm/operation-node/select-query-node.d.ts +5 -5
  159. package/dist/esm/operation-node/select-query-node.js +1 -1
  160. package/dist/esm/operation-node/where-node.d.ts +4 -8
  161. package/dist/esm/operation-node/where-node.js +3 -3
  162. package/dist/esm/parser/filter-parser.js +1 -1
  163. package/dist/esm/parser/returning-parser.d.ts +4 -2
  164. package/dist/esm/parser/select-parser.d.ts +2 -2
  165. package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.d.ts +1 -1
  166. package/dist/esm/query-builder/delete-result.d.ts +5 -0
  167. package/dist/esm/query-builder/delete-result.js +9 -0
  168. package/dist/esm/query-builder/expression-builder.d.ts +2 -2
  169. package/dist/esm/query-builder/expression-builder.js +1 -1
  170. package/dist/esm/query-builder/function-builder.d.ts +5 -5
  171. package/dist/esm/query-builder/function-builder.js +3 -3
  172. package/dist/esm/query-builder/insert-result.d.ts +27 -0
  173. package/dist/esm/query-builder/insert-result.js +31 -0
  174. package/dist/esm/query-builder/query-builder.d.ts +91 -81
  175. package/dist/esm/query-builder/query-builder.js +38 -41
  176. package/dist/esm/query-builder/update-result.d.ts +5 -0
  177. package/dist/esm/query-builder/update-result.js +9 -0
  178. package/dist/esm/query-compiler/compiled-query.d.ts +5 -0
  179. package/dist/esm/query-compiler/compiled-query.js +11 -1
  180. package/dist/esm/query-compiler/default-query-compiler.d.ts +6 -0
  181. package/dist/esm/query-compiler/default-query-compiler.js +60 -26
  182. package/dist/esm/query-creator.d.ts +36 -26
  183. package/dist/esm/query-creator.js +25 -17
  184. package/dist/esm/raw-builder/raw-builder.d.ts +1 -1
  185. package/dist/esm/schema/alter-table-builder.d.ts +6 -2
  186. package/dist/esm/schema/alter-table-builder.js +28 -4
  187. package/dist/esm/schema/column-definition-builder.d.ts +43 -2
  188. package/dist/esm/schema/column-definition-builder.js +23 -6
  189. package/dist/esm/schema/create-schema-builder.js +1 -1
  190. package/dist/esm/schema/create-table-builder.d.ts +7 -8
  191. package/dist/esm/schema/create-table-builder.js +8 -9
  192. package/dist/esm/schema/create-view-builder.d.ts +5 -0
  193. package/dist/esm/schema/create-view-builder.js +20 -1
  194. package/dist/esm/schema/drop-index-builder.js +1 -1
  195. package/dist/esm/schema/drop-schema-builder.js +3 -1
  196. package/dist/esm/schema/drop-table-builder.js +3 -1
  197. package/dist/esm/schema/drop-view-builder.d.ts +1 -0
  198. package/dist/esm/schema/drop-view-builder.js +11 -1
  199. package/dist/esm/schema/schema.d.ts +3 -3
  200. package/dist/esm/schema/schema.js +3 -3
  201. package/dist/esm/util/dynamic-import.d.ts +2 -0
  202. package/dist/esm/util/dynamic-import.js +27 -0
  203. package/dist/esm/util/performance-now.d.ts +1 -0
  204. package/dist/esm/util/performance-now.js +9 -0
  205. package/dist/esm/util/type-utils.d.ts +4 -15
  206. package/package.json +5 -3
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.performanceNow = void 0;
4
+ const object_utils_js_1 = require("./object-utils.js");
5
+ function performanceNow() {
6
+ if (typeof performance !== 'undefined' && (0, object_utils_js_1.isFunction)(performance.now)) {
7
+ return performance.now();
8
+ }
9
+ else {
10
+ return Date.now();
11
+ }
12
+ }
13
+ exports.performanceNow = performanceNow;
@@ -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
+ }
@@ -0,0 +1,112 @@
1
+ import { CompiledQuery } from '../../query-compiler/compiled-query.js';
2
+ import { importFunction } from '../../util/dynamic-import.js';
3
+ export class SqliteDriver {
4
+ #config;
5
+ #connectionMutex = new ConnectionMutex();
6
+ #db;
7
+ #connection;
8
+ constructor(config) {
9
+ this.#config = config;
10
+ }
11
+ async init() {
12
+ // Import the `better-sqlite3` module here instead at the top of the file
13
+ // so that this file can be loaded by node without `better-sqlite3` driver
14
+ // installed. As you can see, there IS an import from `better-sqlite3` at
15
+ // the top level too, but that's only for types. It doesn't get compiled
16
+ // into javascript. You can check the built javascript code.
17
+ const DatabaseConstructor = await importBetterSqlite3Database();
18
+ const options = {};
19
+ if (typeof this.#config.readonly === 'boolean') {
20
+ options.readonly = this.#config.readonly;
21
+ }
22
+ if (typeof this.#config.fileMustExist === 'boolean') {
23
+ options.fileMustExist = this.#config.fileMustExist;
24
+ }
25
+ if (typeof this.#config.timeout === 'number') {
26
+ options.timeout = this.#config.timeout;
27
+ }
28
+ if (typeof this.#config.verbose === 'function') {
29
+ options.verbose = this.#config.verbose;
30
+ }
31
+ this.#db = new DatabaseConstructor(this.#config.databasePath, options);
32
+ this.#connection = new SqliteConnection(this.#db);
33
+ if (this.#config.onCreateConnection) {
34
+ await this.#config.onCreateConnection(this.#connection);
35
+ }
36
+ }
37
+ async acquireConnection() {
38
+ // sqlite effectively only has one single connection. We use a mutex
39
+ // here to wait until the single connection has been released.
40
+ await this.#connectionMutex.lock();
41
+ return this.#connection;
42
+ }
43
+ async beginTransaction(connection) {
44
+ await connection.executeQuery(CompiledQuery.raw('begin'));
45
+ }
46
+ async commitTransaction(connection) {
47
+ await connection.executeQuery(CompiledQuery.raw('commit'));
48
+ }
49
+ async rollbackTransaction(connection) {
50
+ await connection.executeQuery(CompiledQuery.raw('rollback'));
51
+ }
52
+ async releaseConnection() {
53
+ this.#connectionMutex.unlock();
54
+ }
55
+ async destroy() {
56
+ this.#db?.close();
57
+ }
58
+ }
59
+ async function importBetterSqlite3Database() {
60
+ try {
61
+ return await importFunction('better-sqlite3');
62
+ }
63
+ catch (error) {
64
+ console.error(error);
65
+ throw new Error('SQLite client not installed. Please run `npm install better-sqlite3`');
66
+ }
67
+ }
68
+ class SqliteConnection {
69
+ #db;
70
+ constructor(db) {
71
+ this.#db = db;
72
+ }
73
+ executeQuery(compiledQuery) {
74
+ const { sql, parameters } = compiledQuery;
75
+ const stmt = this.#db.prepare(sql);
76
+ if (stmt.reader) {
77
+ return Promise.resolve({
78
+ rows: stmt.all(parameters),
79
+ });
80
+ }
81
+ else {
82
+ const { changes, lastInsertRowid } = stmt.run(parameters);
83
+ return Promise.resolve({
84
+ numUpdatedOrDeletedRows: changes !== undefined && changes !== null
85
+ ? BigInt(changes)
86
+ : undefined,
87
+ insertId: lastInsertRowid !== undefined && lastInsertRowid !== null
88
+ ? BigInt(lastInsertRowid)
89
+ : undefined,
90
+ rows: [],
91
+ });
92
+ }
93
+ }
94
+ }
95
+ class ConnectionMutex {
96
+ #promise;
97
+ #resolve;
98
+ async lock() {
99
+ while (this.#promise) {
100
+ await this.#promise;
101
+ }
102
+ this.#promise = new Promise((resolve) => {
103
+ this.#resolve = resolve;
104
+ });
105
+ }
106
+ unlock() {
107
+ const resolve = this.#resolve;
108
+ this.#promise = undefined;
109
+ this.#resolve = undefined;
110
+ resolve?.();
111
+ }
112
+ }
@@ -0,0 +1,7 @@
1
+ import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions } from '../../introspection/database-introspector.js';
2
+ import { Kysely } from '../../kysely.js';
3
+ export declare class SqliteIntrospector implements DatabaseIntrospector {
4
+ #private;
5
+ constructor(db: Kysely<any>);
6
+ getMetadata(options?: DatabaseMetadataOptions): Promise<DatabaseMetadata>;
7
+ }
@@ -0,0 +1,40 @@
1
+ import { MIGRATION_LOCK_TABLE, MIGRATION_TABLE, } from '../../migration/migration.js';
2
+ export class SqliteIntrospector {
3
+ #db;
4
+ constructor(db) {
5
+ this.#db = db;
6
+ }
7
+ async getMetadata(options = { withInternalKyselyTables: false }) {
8
+ let query = this.#db
9
+ .selectFrom('sqlite_schema')
10
+ .where('type', '=', 'table')
11
+ .where('name', 'not like', 'sqlite_%')
12
+ .select('name')
13
+ .castTo();
14
+ if (!options.withInternalKyselyTables) {
15
+ query = query
16
+ .where('name', '!=', MIGRATION_TABLE)
17
+ .where('name', '!=', MIGRATION_LOCK_TABLE);
18
+ }
19
+ const tables = await query.execute();
20
+ return {
21
+ tables: await Promise.all(tables.map(({ name }) => this.#getTableMetadata(name))),
22
+ };
23
+ }
24
+ async #getTableMetadata(table) {
25
+ const db = this.#db;
26
+ const columns = await db
27
+ .selectFrom(db.raw(`PRAGMA_TABLE_INFO(?)`, [table]).as('table_info'))
28
+ .select(['name', 'type', 'notnull'])
29
+ .castTo()
30
+ .execute();
31
+ return {
32
+ name: table,
33
+ columns: columns.map((col) => ({
34
+ name: col.name,
35
+ dataType: col.type,
36
+ isNullable: !col.notnull,
37
+ })),
38
+ };
39
+ }
40
+ }
@@ -0,0 +1,7 @@
1
+ import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
+ export declare class SqliteQueryCompiler extends DefaultQueryCompiler {
3
+ protected visitReturning(): void;
4
+ protected getSql(): string;
5
+ protected getCurrentParameterPlaceholder(): string;
6
+ protected getAutoIncrement(): string;
7
+ }
@@ -0,0 +1,15 @@
1
+ import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
+ export class SqliteQueryCompiler extends DefaultQueryCompiler {
3
+ visitReturning() {
4
+ // Do nothing.
5
+ }
6
+ getSql() {
7
+ return super.getSql().trimEnd();
8
+ }
9
+ getCurrentParameterPlaceholder() {
10
+ return '?';
11
+ }
12
+ getAutoIncrement() {
13
+ return 'autoincrement';
14
+ }
15
+ }
@@ -12,12 +12,12 @@ export interface QueryResult<O> {
12
12
  * This is defined for update and delete queries and contains
13
13
  * the number of rows the query updated/deleted.
14
14
  */
15
- readonly numUpdatedOrDeletedRows?: number;
15
+ readonly numUpdatedOrDeletedRows?: bigint;
16
16
  /**
17
17
  * This is defined for insert queries on dialects that return
18
18
  * the auto incrementing primary key from an insert.
19
19
  */
20
- readonly insertedPrimaryKey?: number;
20
+ readonly insertId?: bigint;
21
21
  /**
22
22
  * The rows returned by the query. This is always defined and is
23
23
  * empty if the query returned no rows.
@@ -1,3 +1,4 @@
1
+ import { performanceNow } from '../util/performance-now.js';
1
2
  /**
2
3
  * A small wrapper around {@link Driver} that makes sure the driver is
3
4
  * initialized before it is used, only initialized and destroyed
@@ -71,7 +72,7 @@ class RuntimeConnection {
71
72
  this.#log = log;
72
73
  }
73
74
  async executeQuery(compiledQuery) {
74
- const startTime = process.hrtime.bigint();
75
+ const startTime = performanceNow();
75
76
  try {
76
77
  return await this.#connection.executeQuery(compiledQuery);
77
78
  }
@@ -97,8 +98,6 @@ class RuntimeConnection {
97
98
  }));
98
99
  }
99
100
  #calculateDurationMillis(startTime) {
100
- const endTime = process.hrtime.bigint();
101
- const durationNanos = Number(endTime - startTime);
102
- return durationNanos / 1_000_000;
101
+ return performanceNow() - startTime;
103
102
  }
104
103
  }