kysely 0.27.4 → 0.27.5

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 (209) hide show
  1. package/.renovaterc.json +3 -0
  2. package/CONTRIBUTING.md +1 -1
  3. package/deno.check.d.ts +86 -0
  4. package/deno.check.json +21 -0
  5. package/dist/cjs/dialect/dialect-adapter-base.d.ts +11 -2
  6. package/dist/cjs/dialect/dialect-adapter.d.ts +11 -2
  7. package/dist/cjs/dialect/mssql/mssql-adapter.d.ts +11 -2
  8. package/dist/cjs/dialect/mssql/mssql-dialect-config.d.ts +37 -29
  9. package/dist/cjs/dialect/mssql/mssql-driver.js +20 -13
  10. package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +11 -2
  11. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +4 -0
  12. package/dist/cjs/dialect/mysql/mysql-driver.js +3 -0
  13. package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +11 -2
  14. package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +10 -3
  15. package/dist/cjs/dialect/postgres/postgres-driver.js +3 -0
  16. package/dist/cjs/dialect/postgres/postgres-introspector.js +5 -1
  17. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +11 -2
  18. package/dist/cjs/driver/dummy-driver.d.ts +13 -12
  19. package/dist/cjs/driver/dummy-driver.js +13 -12
  20. package/dist/cjs/dynamic/dynamic.d.ts +6 -6
  21. package/dist/cjs/dynamic/dynamic.js +6 -6
  22. package/dist/cjs/expression/expression-builder.d.ts +152 -61
  23. package/dist/cjs/expression/expression-wrapper.d.ts +92 -23
  24. package/dist/cjs/expression/expression.d.ts +40 -5
  25. package/dist/cjs/helpers/mssql.d.ts +74 -14
  26. package/dist/cjs/helpers/mssql.js +74 -14
  27. package/dist/cjs/helpers/mysql.d.ts +14 -10
  28. package/dist/cjs/helpers/mysql.js +14 -10
  29. package/dist/cjs/helpers/postgres.d.ts +7 -4
  30. package/dist/cjs/helpers/postgres.js +7 -4
  31. package/dist/cjs/helpers/sqlite.d.ts +41 -16
  32. package/dist/cjs/helpers/sqlite.js +41 -16
  33. package/dist/cjs/kysely.d.ts +99 -41
  34. package/dist/cjs/kysely.js +60 -36
  35. package/dist/cjs/migration/file-migration-provider.d.ts +2 -2
  36. package/dist/cjs/migration/file-migration-provider.js +2 -2
  37. package/dist/cjs/migration/migrator.d.ts +82 -14
  38. package/dist/cjs/migration/migrator.js +82 -14
  39. package/dist/cjs/operation-node/aggregate-function-node.d.ts +4 -0
  40. package/dist/cjs/operation-node/aggregate-function-node.js +9 -0
  41. package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
  42. package/dist/cjs/operation-node/data-type-node.js +12 -0
  43. package/dist/cjs/operation-node/delete-query-node.d.ts +1 -0
  44. package/dist/cjs/operation-node/insert-query-node.d.ts +1 -0
  45. package/dist/cjs/operation-node/merge-query-node.d.ts +1 -0
  46. package/dist/cjs/operation-node/operation-node-transformer.d.ts +9 -3
  47. package/dist/cjs/operation-node/operation-node-transformer.js +14 -3
  48. package/dist/cjs/operation-node/query-node.d.ts +4 -0
  49. package/dist/cjs/operation-node/query-node.js +8 -0
  50. package/dist/cjs/operation-node/select-query-node.d.ts +0 -1
  51. package/dist/cjs/operation-node/select-query-node.js +0 -8
  52. package/dist/cjs/operation-node/update-query-node.d.ts +1 -0
  53. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +52 -25
  54. package/dist/cjs/plugin/camel-case/camel-case-plugin.js +24 -19
  55. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +29 -7
  56. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +1 -1
  57. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +2 -2
  58. package/dist/cjs/plugin/immediate-value/immediate-value-plugin.d.ts +28 -6
  59. package/dist/cjs/plugin/kysely-plugin.d.ts +28 -6
  60. package/dist/cjs/plugin/noop-plugin.d.ts +28 -6
  61. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.d.ts +62 -9
  62. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.js +34 -3
  63. package/dist/cjs/plugin/with-schema/with-schema-plugin.d.ts +28 -6
  64. package/dist/cjs/query-builder/aggregate-function-builder.d.ts +43 -1
  65. package/dist/cjs/query-builder/aggregate-function-builder.js +30 -0
  66. package/dist/cjs/query-builder/delete-query-builder.d.ts +137 -45
  67. package/dist/cjs/query-builder/delete-query-builder.js +76 -26
  68. package/dist/cjs/query-builder/function-module.d.ts +35 -88
  69. package/dist/cjs/query-builder/insert-query-builder.d.ts +239 -65
  70. package/dist/cjs/query-builder/insert-query-builder.js +188 -52
  71. package/dist/cjs/query-builder/insert-result.d.ts +10 -5
  72. package/dist/cjs/query-builder/insert-result.js +10 -5
  73. package/dist/cjs/query-builder/json-path-builder.d.ts +33 -9
  74. package/dist/cjs/query-builder/json-path-builder.js +8 -5
  75. package/dist/cjs/query-builder/merge-query-builder.d.ts +159 -21
  76. package/dist/cjs/query-builder/merge-query-builder.js +68 -6
  77. package/dist/cjs/query-builder/on-conflict-builder.d.ts +51 -18
  78. package/dist/cjs/query-builder/on-conflict-builder.js +41 -16
  79. package/dist/cjs/query-builder/output-interface.d.ts +50 -7
  80. package/dist/cjs/query-builder/returning-interface.d.ts +5 -5
  81. package/dist/cjs/query-builder/select-query-builder-expression.d.ts +8 -0
  82. package/dist/cjs/query-builder/select-query-builder.d.ts +295 -63
  83. package/dist/cjs/query-builder/select-query-builder.js +7 -7
  84. package/dist/cjs/query-builder/update-query-builder.d.ts +138 -29
  85. package/dist/cjs/query-builder/update-query-builder.js +75 -10
  86. package/dist/cjs/query-builder/update-result.d.ts +2 -2
  87. package/dist/cjs/query-builder/update-result.js +2 -2
  88. package/dist/cjs/query-builder/where-interface.d.ts +5 -1
  89. package/dist/cjs/query-compiler/default-query-compiler.js +20 -0
  90. package/dist/cjs/query-creator.d.ts +66 -18
  91. package/dist/cjs/query-creator.js +2 -2
  92. package/dist/cjs/raw-builder/raw-builder.d.ts +29 -7
  93. package/dist/cjs/raw-builder/sql.d.ts +26 -7
  94. package/dist/cjs/schema/alter-column-builder.d.ts +17 -3
  95. package/dist/cjs/schema/alter-column-builder.js +17 -3
  96. package/dist/cjs/schema/alter-table-add-index-builder.d.ts +43 -9
  97. package/dist/cjs/schema/alter-table-add-index-builder.js +26 -9
  98. package/dist/cjs/schema/column-definition-builder.d.ts +283 -26
  99. package/dist/cjs/schema/column-definition-builder.js +283 -26
  100. package/dist/cjs/schema/create-table-builder.d.ts +86 -33
  101. package/dist/cjs/schema/create-table-builder.js +86 -33
  102. package/dist/cjs/util/column-type.d.ts +5 -9
  103. package/dist/cjs/util/require-all-props.d.ts +4 -2
  104. package/dist/cjs/util/require-all-props.js +4 -2
  105. package/dist/cjs/util/streamable.d.ts +1 -1
  106. package/dist/cjs/util/type-utils.d.ts +4 -2
  107. package/dist/esm/dialect/dialect-adapter-base.d.ts +11 -2
  108. package/dist/esm/dialect/dialect-adapter.d.ts +11 -2
  109. package/dist/esm/dialect/mssql/mssql-adapter.d.ts +11 -2
  110. package/dist/esm/dialect/mssql/mssql-dialect-config.d.ts +37 -29
  111. package/dist/esm/dialect/mssql/mssql-driver.js +20 -13
  112. package/dist/esm/dialect/mysql/mysql-adapter.d.ts +11 -2
  113. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +4 -0
  114. package/dist/esm/dialect/mysql/mysql-driver.js +3 -0
  115. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +11 -2
  116. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +10 -3
  117. package/dist/esm/dialect/postgres/postgres-driver.js +3 -0
  118. package/dist/esm/dialect/postgres/postgres-introspector.js +5 -1
  119. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +11 -2
  120. package/dist/esm/driver/dummy-driver.d.ts +13 -12
  121. package/dist/esm/driver/dummy-driver.js +13 -12
  122. package/dist/esm/dynamic/dynamic.d.ts +6 -6
  123. package/dist/esm/dynamic/dynamic.js +6 -6
  124. package/dist/esm/expression/expression-builder.d.ts +152 -61
  125. package/dist/esm/expression/expression-wrapper.d.ts +92 -23
  126. package/dist/esm/expression/expression.d.ts +40 -5
  127. package/dist/esm/helpers/mssql.d.ts +74 -14
  128. package/dist/esm/helpers/mssql.js +74 -14
  129. package/dist/esm/helpers/mysql.d.ts +14 -10
  130. package/dist/esm/helpers/mysql.js +14 -10
  131. package/dist/esm/helpers/postgres.d.ts +7 -4
  132. package/dist/esm/helpers/postgres.js +7 -4
  133. package/dist/esm/helpers/sqlite.d.ts +41 -16
  134. package/dist/esm/helpers/sqlite.js +41 -16
  135. package/dist/esm/kysely.d.ts +99 -41
  136. package/dist/esm/kysely.js +60 -36
  137. package/dist/esm/migration/file-migration-provider.d.ts +2 -2
  138. package/dist/esm/migration/file-migration-provider.js +2 -2
  139. package/dist/esm/migration/migrator.d.ts +82 -14
  140. package/dist/esm/migration/migrator.js +82 -14
  141. package/dist/esm/operation-node/aggregate-function-node.d.ts +4 -0
  142. package/dist/esm/operation-node/aggregate-function-node.js +9 -0
  143. package/dist/esm/operation-node/data-type-node.d.ts +1 -1
  144. package/dist/esm/operation-node/data-type-node.js +12 -0
  145. package/dist/esm/operation-node/delete-query-node.d.ts +1 -0
  146. package/dist/esm/operation-node/insert-query-node.d.ts +1 -0
  147. package/dist/esm/operation-node/merge-query-node.d.ts +1 -0
  148. package/dist/esm/operation-node/operation-node-transformer.d.ts +9 -3
  149. package/dist/esm/operation-node/operation-node-transformer.js +14 -3
  150. package/dist/esm/operation-node/query-node.d.ts +4 -0
  151. package/dist/esm/operation-node/query-node.js +8 -0
  152. package/dist/esm/operation-node/select-query-node.d.ts +0 -1
  153. package/dist/esm/operation-node/select-query-node.js +0 -8
  154. package/dist/esm/operation-node/update-query-node.d.ts +1 -0
  155. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +52 -25
  156. package/dist/esm/plugin/camel-case/camel-case-plugin.js +24 -19
  157. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +29 -7
  158. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +1 -1
  159. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +2 -2
  160. package/dist/esm/plugin/immediate-value/immediate-value-plugin.d.ts +28 -6
  161. package/dist/esm/plugin/kysely-plugin.d.ts +28 -6
  162. package/dist/esm/plugin/noop-plugin.d.ts +28 -6
  163. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.d.ts +62 -9
  164. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.js +34 -3
  165. package/dist/esm/plugin/with-schema/with-schema-plugin.d.ts +28 -6
  166. package/dist/esm/query-builder/aggregate-function-builder.d.ts +43 -1
  167. package/dist/esm/query-builder/aggregate-function-builder.js +30 -0
  168. package/dist/esm/query-builder/delete-query-builder.d.ts +137 -45
  169. package/dist/esm/query-builder/delete-query-builder.js +76 -26
  170. package/dist/esm/query-builder/function-module.d.ts +35 -88
  171. package/dist/esm/query-builder/insert-query-builder.d.ts +239 -65
  172. package/dist/esm/query-builder/insert-query-builder.js +188 -52
  173. package/dist/esm/query-builder/insert-result.d.ts +10 -5
  174. package/dist/esm/query-builder/insert-result.js +10 -5
  175. package/dist/esm/query-builder/json-path-builder.d.ts +33 -9
  176. package/dist/esm/query-builder/json-path-builder.js +8 -5
  177. package/dist/esm/query-builder/merge-query-builder.d.ts +159 -21
  178. package/dist/esm/query-builder/merge-query-builder.js +68 -6
  179. package/dist/esm/query-builder/on-conflict-builder.d.ts +51 -18
  180. package/dist/esm/query-builder/on-conflict-builder.js +41 -16
  181. package/dist/esm/query-builder/output-interface.d.ts +50 -7
  182. package/dist/esm/query-builder/returning-interface.d.ts +5 -5
  183. package/dist/esm/query-builder/select-query-builder-expression.d.ts +8 -0
  184. package/dist/esm/query-builder/select-query-builder.d.ts +295 -63
  185. package/dist/esm/query-builder/select-query-builder.js +7 -7
  186. package/dist/esm/query-builder/update-query-builder.d.ts +138 -29
  187. package/dist/esm/query-builder/update-query-builder.js +75 -10
  188. package/dist/esm/query-builder/update-result.d.ts +2 -2
  189. package/dist/esm/query-builder/update-result.js +2 -2
  190. package/dist/esm/query-builder/where-interface.d.ts +5 -1
  191. package/dist/esm/query-compiler/default-query-compiler.js +20 -0
  192. package/dist/esm/query-creator.d.ts +66 -18
  193. package/dist/esm/query-creator.js +2 -2
  194. package/dist/esm/raw-builder/raw-builder.d.ts +29 -7
  195. package/dist/esm/raw-builder/sql.d.ts +26 -7
  196. package/dist/esm/schema/alter-column-builder.d.ts +17 -3
  197. package/dist/esm/schema/alter-column-builder.js +17 -3
  198. package/dist/esm/schema/alter-table-add-index-builder.d.ts +43 -9
  199. package/dist/esm/schema/alter-table-add-index-builder.js +26 -9
  200. package/dist/esm/schema/column-definition-builder.d.ts +283 -26
  201. package/dist/esm/schema/column-definition-builder.js +283 -26
  202. package/dist/esm/schema/create-table-builder.d.ts +86 -33
  203. package/dist/esm/schema/create-table-builder.js +86 -33
  204. package/dist/esm/util/column-type.d.ts +5 -9
  205. package/dist/esm/util/require-all-props.d.ts +4 -2
  206. package/dist/esm/util/require-all-props.js +4 -2
  207. package/dist/esm/util/streamable.d.ts +1 -1
  208. package/dist/esm/util/type-utils.d.ts +4 -2
  209. package/package.json +28 -23
@@ -24,33 +24,23 @@ import { DrainOuterGeneric } from './util/type-utils.js';
24
24
  *
25
25
  * ### Examples
26
26
  *
27
- * This example assumes your database has tables `person` and `pet`:
27
+ * This example assumes your database has a "person" table:
28
28
  *
29
29
  * ```ts
30
- * import { Kysely, Generated, PostgresDialect } from 'kysely'
31
- *
32
- * interface PersonTable {
33
- * id: Generated<number>
34
- * first_name: string
35
- * last_name: string
36
- * }
37
- *
38
- * interface PetTable {
39
- * id: Generated<number>
40
- * owner_id: number
41
- * name: string
42
- * species: 'cat' | 'dog'
43
- * }
30
+ * import * as Sqlite from 'better-sqlite3'
31
+ * import { type Generated, Kysely, SqliteDialect } from 'kysely'
44
32
  *
45
33
  * interface Database {
46
- * person: PersonTable,
47
- * pet: PetTable
34
+ * person: {
35
+ * id: Generated<number>
36
+ * first_name: string
37
+ * last_name: string | null
38
+ * }
48
39
  * }
49
40
  *
50
41
  * const db = new Kysely<Database>({
51
- * dialect: new PostgresDialect({
52
- * host: 'localhost',
53
- * database: 'kysely_test',
42
+ * dialect: new SqliteDialect({
43
+ * database: new Sqlite(':memory:'),
54
44
  * })
55
45
  * })
56
46
  * ```
@@ -86,30 +76,52 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
86
76
  case(): CaseBuilder<DB, keyof DB>;
87
77
  case<V>(value: Expression<V>): CaseBuilder<DB, keyof DB, V>;
88
78
  /**
89
- * Returns a {@link FunctionModule} that can be used to write type safe function
79
+ * Returns a {@link FunctionModule} that can be used to write somewhat type-safe function
90
80
  * calls.
91
81
  *
92
82
  * ```ts
83
+ * const { count } = db.fn
84
+ *
93
85
  * await db.selectFrom('person')
94
86
  * .innerJoin('pet', 'pet.owner_id', 'person.id')
95
- * .select((eb) => [
96
- * 'person.id',
97
- * eb.fn.count('pet.id').as('pet_count')
87
+ * .select([
88
+ * 'id',
89
+ * count('pet.id').as('person_count'),
98
90
  * ])
99
91
  * .groupBy('person.id')
100
- * .having((eb) => eb.fn.count('pet.id'), '>', 10)
92
+ * .having(count('pet.id'), '>', 10)
101
93
  * .execute()
102
94
  * ```
103
95
  *
104
96
  * The generated SQL (PostgreSQL):
105
97
  *
106
98
  * ```sql
107
- * select "person"."id", count("pet"."id") as "pet_count"
99
+ * select "person"."id", count("pet"."id") as "person_count"
108
100
  * from "person"
109
101
  * inner join "pet" on "pet"."owner_id" = "person"."id"
110
102
  * group by "person"."id"
111
103
  * having count("pet"."id") > $1
112
104
  * ```
105
+ *
106
+ * Why "somewhat" type-safe? Because the function calls are not bound to the
107
+ * current query context. They allow you to reference columns and tables that
108
+ * are not in the current query. E.g. remove the `innerJoin` from the previous
109
+ * query and TypeScript won't even complain.
110
+ *
111
+ * If you want to make the function calls fully type-safe, you can use the
112
+ * {@link ExpressionBuilder.fn} getter for a query context-aware, stricter {@link FunctionModule}.
113
+ *
114
+ * ```ts
115
+ * await db.selectFrom('person')
116
+ * .innerJoin('pet', 'pet.owner_id', 'person.id')
117
+ * .select((eb) => [
118
+ * 'person.id',
119
+ * eb.fn.count('pet.id').as('pet_count')
120
+ * ])
121
+ * .groupBy('person.id')
122
+ * .having((eb) => eb.fn.count('pet.id'), '>', 10)
123
+ * .execute()
124
+ * ```
113
125
  */
114
126
  get fn(): FunctionModule<DB, keyof DB>;
115
127
  /**
@@ -118,8 +130,11 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
118
130
  * The returned {@link TransactionBuilder} can be used to configure the transaction. The
119
131
  * {@link TransactionBuilder.execute} method can then be called to run the transaction.
120
132
  * {@link TransactionBuilder.execute} takes a function that is run inside the
121
- * transaction. If the function throws, the transaction is rolled back. Otherwise
122
- * the transaction is committed.
133
+ * transaction. If the function throws an exception,
134
+ * 1. the exception is caught,
135
+ * 2. the transaction is rolled back, and
136
+ * 3. the exception is thrown again.
137
+ * Otherwise the transaction is committed.
123
138
  *
124
139
  * The callback function passed to the {@link TransactionBuilder.execute | execute}
125
140
  * method gets the transaction object as its only argument. The transaction is
@@ -130,9 +145,12 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
130
145
  *
131
146
  * <!-- siteExample("transactions", "Simple transaction", 10) -->
132
147
  *
133
- * This example inserts two rows in a transaction. If an error is thrown inside
134
- * the callback passed to the `execute` method, the transaction is rolled back.
135
- * Otherwise it's committed.
148
+ * This example inserts two rows in a transaction. If an exception is thrown inside
149
+ * the callback passed to the `execute` method,
150
+ * 1. the exception is caught,
151
+ * 2. the transaction is rolled back, and
152
+ * 3. the exception is thrown again.
153
+ * Otherwise the transaction is committed.
136
154
  *
137
155
  * ```ts
138
156
  * const catto = await db.transaction().execute(async (trx) => {
@@ -160,12 +178,18 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
160
178
  * Setting the isolation level:
161
179
  *
162
180
  * ```ts
181
+ * import type { Kysely } from 'kysely'
182
+ *
163
183
  * await db
164
184
  * .transaction()
165
185
  * .setIsolationLevel('serializable')
166
186
  * .execute(async (trx) => {
167
187
  * await doStuff(trx)
168
188
  * })
189
+ *
190
+ * async function doStuff(kysely: typeof db) {
191
+ * // ...
192
+ * }
169
193
  * ```
170
194
  */
171
195
  transaction(): TransactionBuilder<DB>;
@@ -183,6 +207,10 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
183
207
  * // the same connection.
184
208
  * await doStuff(db)
185
209
  * })
210
+ *
211
+ * async function doStuff(kysely: typeof db) {
212
+ * // ...
213
+ * }
186
214
  * ```
187
215
  */
188
216
  connection(): ConnectionBuilder<DB>;
@@ -209,7 +237,6 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
209
237
  *
210
238
  * The following example adds and uses a temporary table:
211
239
  *
212
- * @example
213
240
  * ```ts
214
241
  * await db.schema
215
242
  * .createTable('temp_table')
@@ -250,7 +277,7 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
250
277
  /**
251
278
  * Executes a given compiled query or query builder.
252
279
  *
253
- * See {@link https://github.com/koskimas/kysely/blob/master/site/docs/recipes/splitting-build-compile-and-execute-code.md#execute-compiled-queries splitting build, compile and execute code recipe} for more information.
280
+ * See {@link https://github.com/kysely-org/kysely/blob/master/site/docs/recipes/0004-splitting-query-building-and-execution.md#execute-compiled-queries splitting build, compile and execute code recipe} for more information.
254
281
  */
255
282
  executeQuery<R>(query: CompiledQuery<R> | Compilable<R>, queryId?: QueryId): Promise<QueryResult<R>>;
256
283
  }
@@ -269,8 +296,11 @@ export declare class Transaction<DB> extends Kysely<DB> {
269
296
  * The returned {@link TransactionBuilder} can be used to configure the transaction. The
270
297
  * {@link TransactionBuilder.execute} method can then be called to run the transaction.
271
298
  * {@link TransactionBuilder.execute} takes a function that is run inside the
272
- * transaction. If the function throws, the transaction is rolled back. Otherwise
273
- * the transaction is committed.
299
+ * transaction. If the function throws an exception,
300
+ * 1. the exception is caught,
301
+ * 2. the transaction is rolled back, and
302
+ * 3. the exception is thrown again.
303
+ * Otherwise the transaction is committed.
274
304
  *
275
305
  * The callback function passed to the {@link TransactionBuilder.execute | execute}
276
306
  * method gets the transaction object as its only argument. The transaction is
@@ -281,9 +311,12 @@ export declare class Transaction<DB> extends Kysely<DB> {
281
311
  *
282
312
  * <!-- siteExample("transactions", "Simple transaction", 10) -->
283
313
  *
284
- * This example inserts two rows in a transaction. If an error is thrown inside
285
- * the callback passed to the `execute` method, the transaction is rolled back.
286
- * Otherwise it's committed.
314
+ * This example inserts two rows in a transaction. If an exception is thrown inside
315
+ * the callback passed to the `execute` method,
316
+ * 1. the exception is caught,
317
+ * 2. the transaction is rolled back, and
318
+ * 3. the exception is thrown again.
319
+ * Otherwise the transaction is committed.
287
320
  *
288
321
  * ```ts
289
322
  * const catto = await db.transaction().execute(async (trx) => {
@@ -311,12 +344,18 @@ export declare class Transaction<DB> extends Kysely<DB> {
311
344
  * Setting the isolation level:
312
345
  *
313
346
  * ```ts
347
+ * import type { Kysely } from 'kysely'
348
+ *
314
349
  * await db
315
350
  * .transaction()
316
351
  * .setIsolationLevel('serializable')
317
352
  * .execute(async (trx) => {
318
353
  * await doStuff(trx)
319
354
  * })
355
+ *
356
+ * async function doStuff(kysely: typeof db) {
357
+ * // ...
358
+ * }
320
359
  * ```
321
360
  */
322
361
  transaction(): TransactionBuilder<DB>;
@@ -334,6 +373,10 @@ export declare class Transaction<DB> extends Kysely<DB> {
334
373
  * // the same connection.
335
374
  * await doStuff(db)
336
375
  * })
376
+ *
377
+ * async function doStuff(kysely: typeof db) {
378
+ * // ...
379
+ * }
337
380
  * ```
338
381
  */
339
382
  connection(): ConnectionBuilder<DB>;
@@ -366,7 +409,6 @@ export declare class Transaction<DB> extends Kysely<DB> {
366
409
  *
367
410
  * The following example adds and uses a temporary table:
368
411
  *
369
- * @example
370
412
  * ```ts
371
413
  * await db.schema
372
414
  * .createTable('temp_table')
@@ -406,16 +448,32 @@ export interface KyselyConfig {
406
448
  *
407
449
  * ### Examples
408
450
  *
451
+ * Setting up built-in logging for preferred log levels:
452
+ *
409
453
  * ```ts
454
+ * import * as Sqlite from 'better-sqlite3'
455
+ * import { Kysely, SqliteDialect } from 'kysely'
456
+ * import type { Database } from 'type-editor' // imaginary module
457
+ *
410
458
  * const db = new Kysely<Database>({
411
- * dialect: new PostgresDialect(postgresConfig),
459
+ * dialect: new SqliteDialect({
460
+ * database: new Sqlite(':memory:'),
461
+ * }),
412
462
  * log: ['query', 'error']
413
463
  * })
414
464
  * ```
415
465
  *
466
+ * Setting up custom logging:
467
+ *
416
468
  * ```ts
469
+ * import * as Sqlite from 'better-sqlite3'
470
+ * import { Kysely, SqliteDialect } from 'kysely'
471
+ * import type { Database } from 'type-editor' // imaginary module
472
+ *
417
473
  * const db = new Kysely<Database>({
418
- * dialect: new PostgresDialect(postgresConfig),
474
+ * dialect: new SqliteDialect({
475
+ * database: new Sqlite(':memory:'),
476
+ * }),
419
477
  * log(event): void {
420
478
  * if (event.level === 'query') {
421
479
  * console.log(event.query.sql)
@@ -28,33 +28,23 @@ const with_schema_plugin_js_1 = require("./plugin/with-schema/with-schema-plugin
28
28
  *
29
29
  * ### Examples
30
30
  *
31
- * This example assumes your database has tables `person` and `pet`:
31
+ * This example assumes your database has a "person" table:
32
32
  *
33
33
  * ```ts
34
- * import { Kysely, Generated, PostgresDialect } from 'kysely'
35
- *
36
- * interface PersonTable {
37
- * id: Generated<number>
38
- * first_name: string
39
- * last_name: string
40
- * }
41
- *
42
- * interface PetTable {
43
- * id: Generated<number>
44
- * owner_id: number
45
- * name: string
46
- * species: 'cat' | 'dog'
47
- * }
34
+ * import * as Sqlite from 'better-sqlite3'
35
+ * import { type Generated, Kysely, SqliteDialect } from 'kysely'
48
36
  *
49
37
  * interface Database {
50
- * person: PersonTable,
51
- * pet: PetTable
38
+ * person: {
39
+ * id: Generated<number>
40
+ * first_name: string
41
+ * last_name: string | null
42
+ * }
52
43
  * }
53
44
  *
54
45
  * const db = new Kysely<Database>({
55
- * dialect: new PostgresDialect({
56
- * host: 'localhost',
57
- * database: 'kysely_test',
46
+ * dialect: new SqliteDialect({
47
+ * database: new Sqlite(':memory:'),
58
48
  * })
59
49
  * })
60
50
  * ```
@@ -119,30 +109,52 @@ class Kysely extends query_creator_js_1.QueryCreator {
119
109
  });
120
110
  }
121
111
  /**
122
- * Returns a {@link FunctionModule} that can be used to write type safe function
112
+ * Returns a {@link FunctionModule} that can be used to write somewhat type-safe function
123
113
  * calls.
124
114
  *
125
115
  * ```ts
116
+ * const { count } = db.fn
117
+ *
126
118
  * await db.selectFrom('person')
127
119
  * .innerJoin('pet', 'pet.owner_id', 'person.id')
128
- * .select((eb) => [
129
- * 'person.id',
130
- * eb.fn.count('pet.id').as('pet_count')
120
+ * .select([
121
+ * 'id',
122
+ * count('pet.id').as('person_count'),
131
123
  * ])
132
124
  * .groupBy('person.id')
133
- * .having((eb) => eb.fn.count('pet.id'), '>', 10)
125
+ * .having(count('pet.id'), '>', 10)
134
126
  * .execute()
135
127
  * ```
136
128
  *
137
129
  * The generated SQL (PostgreSQL):
138
130
  *
139
131
  * ```sql
140
- * select "person"."id", count("pet"."id") as "pet_count"
132
+ * select "person"."id", count("pet"."id") as "person_count"
141
133
  * from "person"
142
134
  * inner join "pet" on "pet"."owner_id" = "person"."id"
143
135
  * group by "person"."id"
144
136
  * having count("pet"."id") > $1
145
137
  * ```
138
+ *
139
+ * Why "somewhat" type-safe? Because the function calls are not bound to the
140
+ * current query context. They allow you to reference columns and tables that
141
+ * are not in the current query. E.g. remove the `innerJoin` from the previous
142
+ * query and TypeScript won't even complain.
143
+ *
144
+ * If you want to make the function calls fully type-safe, you can use the
145
+ * {@link ExpressionBuilder.fn} getter for a query context-aware, stricter {@link FunctionModule}.
146
+ *
147
+ * ```ts
148
+ * await db.selectFrom('person')
149
+ * .innerJoin('pet', 'pet.owner_id', 'person.id')
150
+ * .select((eb) => [
151
+ * 'person.id',
152
+ * eb.fn.count('pet.id').as('pet_count')
153
+ * ])
154
+ * .groupBy('person.id')
155
+ * .having((eb) => eb.fn.count('pet.id'), '>', 10)
156
+ * .execute()
157
+ * ```
146
158
  */
147
159
  get fn() {
148
160
  return (0, function_module_js_1.createFunctionModule)();
@@ -153,8 +165,11 @@ class Kysely extends query_creator_js_1.QueryCreator {
153
165
  * The returned {@link TransactionBuilder} can be used to configure the transaction. The
154
166
  * {@link TransactionBuilder.execute} method can then be called to run the transaction.
155
167
  * {@link TransactionBuilder.execute} takes a function that is run inside the
156
- * transaction. If the function throws, the transaction is rolled back. Otherwise
157
- * the transaction is committed.
168
+ * transaction. If the function throws an exception,
169
+ * 1. the exception is caught,
170
+ * 2. the transaction is rolled back, and
171
+ * 3. the exception is thrown again.
172
+ * Otherwise the transaction is committed.
158
173
  *
159
174
  * The callback function passed to the {@link TransactionBuilder.execute | execute}
160
175
  * method gets the transaction object as its only argument. The transaction is
@@ -165,9 +180,12 @@ class Kysely extends query_creator_js_1.QueryCreator {
165
180
  *
166
181
  * <!-- siteExample("transactions", "Simple transaction", 10) -->
167
182
  *
168
- * This example inserts two rows in a transaction. If an error is thrown inside
169
- * the callback passed to the `execute` method, the transaction is rolled back.
170
- * Otherwise it's committed.
183
+ * This example inserts two rows in a transaction. If an exception is thrown inside
184
+ * the callback passed to the `execute` method,
185
+ * 1. the exception is caught,
186
+ * 2. the transaction is rolled back, and
187
+ * 3. the exception is thrown again.
188
+ * Otherwise the transaction is committed.
171
189
  *
172
190
  * ```ts
173
191
  * const catto = await db.transaction().execute(async (trx) => {
@@ -195,12 +213,18 @@ class Kysely extends query_creator_js_1.QueryCreator {
195
213
  * Setting the isolation level:
196
214
  *
197
215
  * ```ts
216
+ * import type { Kysely } from 'kysely'
217
+ *
198
218
  * await db
199
219
  * .transaction()
200
220
  * .setIsolationLevel('serializable')
201
221
  * .execute(async (trx) => {
202
222
  * await doStuff(trx)
203
223
  * })
224
+ *
225
+ * async function doStuff(kysely: typeof db) {
226
+ * // ...
227
+ * }
204
228
  * ```
205
229
  */
206
230
  transaction() {
@@ -220,6 +244,10 @@ class Kysely extends query_creator_js_1.QueryCreator {
220
244
  * // the same connection.
221
245
  * await doStuff(db)
222
246
  * })
247
+ *
248
+ * async function doStuff(kysely: typeof db) {
249
+ * // ...
250
+ * }
223
251
  * ```
224
252
  */
225
253
  connection() {
@@ -263,7 +291,6 @@ class Kysely extends query_creator_js_1.QueryCreator {
263
291
  *
264
292
  * The following example adds and uses a temporary table:
265
293
  *
266
- * @example
267
294
  * ```ts
268
295
  * await db.schema
269
296
  * .createTable('temp_table')
@@ -312,7 +339,7 @@ class Kysely extends query_creator_js_1.QueryCreator {
312
339
  /**
313
340
  * Executes a given compiled query or query builder.
314
341
  *
315
- * See {@link https://github.com/koskimas/kysely/blob/master/site/docs/recipes/splitting-build-compile-and-execute-code.md#execute-compiled-queries splitting build, compile and execute code recipe} for more information.
342
+ * See {@link https://github.com/kysely-org/kysely/blob/master/site/docs/recipes/0004-splitting-query-building-and-execution.md#execute-compiled-queries splitting build, compile and execute code recipe} for more information.
316
343
  */
317
344
  executeQuery(query, queryId = (0, query_id_js_1.createQueryId)()) {
318
345
  const compiledQuery = (0, compilable_js_1.isCompilable)(query) ? query.compile() : query;
@@ -353,9 +380,6 @@ class Transaction extends Kysely {
353
380
  executor: this.#props.executor.withoutPlugins(),
354
381
  });
355
382
  }
356
- /**
357
- * @override
358
- */
359
383
  withSchema(schema) {
360
384
  return new Transaction({
361
385
  ...this.#props,
@@ -5,8 +5,8 @@ import { Migration, MigrationProvider } from './migrator.js';
5
5
  * ### Examples
6
6
  *
7
7
  * ```ts
8
- * import { promises as fs } from 'fs'
9
- * import path from 'path'
8
+ * import { promises as fs } from 'node:fs'
9
+ * import path from 'node:path'
10
10
  *
11
11
  * new FileMigrationProvider({
12
12
  * fs,
@@ -8,8 +8,8 @@ const object_utils_js_1 = require("../util/object-utils.js");
8
8
  * ### Examples
9
9
  *
10
10
  * ```ts
11
- * import { promises as fs } from 'fs'
12
- * import path from 'path'
11
+ * import { promises as fs } from 'node:fs'
12
+ * import path from 'node:path'
13
13
  *
14
14
  * new FileMigrationProvider({
15
15
  * fs,
@@ -25,16 +25,29 @@ export interface Migration {
25
25
  * other way.
26
26
  *
27
27
  * ```ts
28
- * import { promises as fs } from 'fs'
29
- * import path from 'path'
28
+ * import { promises as fs } from 'node:fs'
29
+ * import path from 'node:path'
30
+ * import * as Sqlite from 'better-sqlite3'
31
+ * import {
32
+ * FileMigrationProvider,
33
+ * Kysely,
34
+ * Migrator,
35
+ * SqliteDialect
36
+ * } from 'kysely'
37
+ *
38
+ * const db = new Kysely<any>({
39
+ * dialect: new SqliteDialect({
40
+ * database: Sqlite(':memory:')
41
+ * })
42
+ * })
30
43
  *
31
44
  * const migrator = new Migrator({
32
45
  * db,
33
46
  * provider: new FileMigrationProvider({
34
47
  * fs,
35
- * path,
36
48
  * // Path to the folder that contains all your migrations.
37
- * migrationFolder: 'some/path/to/migrations'
49
+ * migrationFolder: 'some/path/to/migrations',
50
+ * path,
38
51
  * })
39
52
  * })
40
53
  * ```
@@ -64,19 +77,18 @@ export declare class Migrator {
64
77
  * ### Examples
65
78
  *
66
79
  * ```ts
67
- * const db = new Kysely<Database>({
68
- * dialect: new PostgresDialect({
69
- * host: 'localhost',
70
- * database: 'kysely_test',
71
- * }),
72
- * })
80
+ * import { promises as fs } from 'node:fs'
81
+ * import path from 'node:path'
82
+ * import * as Sqlite from 'better-sqlite3'
83
+ * import { FileMigrationProvider, Migrator } from 'kysely'
73
84
  *
74
85
  * const migrator = new Migrator({
75
86
  * db,
76
- * provider: new FileMigrationProvider(
77
- * // Path to the folder that contains all your migrations.
78
- * 'some/path/to/migrations'
79
- * )
87
+ * provider: new FileMigrationProvider({
88
+ * fs,
89
+ * migrationFolder: 'some/path/to/migrations',
90
+ * path,
91
+ * })
80
92
  * })
81
93
  *
82
94
  * const { error, results } = await migrator.migrateToLatest()
@@ -107,6 +119,20 @@ export declare class Migrator {
107
119
  * ### Examples
108
120
  *
109
121
  * ```ts
122
+ * import { promises as fs } from 'node:fs'
123
+ * import path from 'node:path'
124
+ * import { FileMigrationProvider, Migrator } from 'kysely'
125
+ *
126
+ * const migrator = new Migrator({
127
+ * db,
128
+ * provider: new FileMigrationProvider({
129
+ * fs,
130
+ * // Path to the folder that contains all your migrations.
131
+ * migrationFolder: 'some/path/to/migrations',
132
+ * path,
133
+ * })
134
+ * })
135
+ *
110
136
  * await migrator.migrateTo('some_migration')
111
137
  * ```
112
138
  *
@@ -116,6 +142,20 @@ export declare class Migrator {
116
142
  * you can use a special constant `NO_MIGRATIONS`:
117
143
  *
118
144
  * ```ts
145
+ * import { promises as fs } from 'node:fs'
146
+ * import path from 'node:path'
147
+ * import { FileMigrationProvider, Migrator, NO_MIGRATIONS } from 'kysely'
148
+ *
149
+ * const migrator = new Migrator({
150
+ * db,
151
+ * provider: new FileMigrationProvider({
152
+ * fs,
153
+ * // Path to the folder that contains all your migrations.
154
+ * migrationFolder: 'some/path/to/migrations',
155
+ * path,
156
+ * })
157
+ * })
158
+ *
119
159
  * await migrator.migrateTo(NO_MIGRATIONS)
120
160
  * ```
121
161
  */
@@ -131,6 +171,20 @@ export declare class Migrator {
131
171
  * ### Examples
132
172
  *
133
173
  * ```ts
174
+ * import { promises as fs } from 'node:fs'
175
+ * import path from 'node:path'
176
+ * import { FileMigrationProvider, Migrator } from 'kysely'
177
+ *
178
+ * const migrator = new Migrator({
179
+ * db,
180
+ * provider: new FileMigrationProvider({
181
+ * fs,
182
+ * // Path to the folder that contains all your migrations.
183
+ * migrationFolder: 'some/path/to/migrations',
184
+ * path,
185
+ * })
186
+ * })
187
+ *
134
188
  * await migrator.migrateUp()
135
189
  * ```
136
190
  */
@@ -146,6 +200,20 @@ export declare class Migrator {
146
200
  * ### Examples
147
201
  *
148
202
  * ```ts
203
+ * import { promises as fs } from 'node:fs'
204
+ * import path from 'node:path'
205
+ * import { FileMigrationProvider, Migrator } from 'kysely'
206
+ *
207
+ * const migrator = new Migrator({
208
+ * db,
209
+ * provider: new FileMigrationProvider({
210
+ * fs,
211
+ * // Path to the folder that contains all your migrations.
212
+ * migrationFolder: 'some/path/to/migrations',
213
+ * path,
214
+ * })
215
+ * })
216
+ *
149
217
  * await migrator.migrateDown()
150
218
  * ```
151
219
  */