kysely 0.27.4 → 0.27.6

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 (222) hide show
  1. package/.renovaterc.json +3 -0
  2. package/CODE_OF_CONDUCT.md +183 -0
  3. package/CONTRIBUTING.md +121 -23
  4. package/FUNDING.md +25 -0
  5. package/README.md +13 -2
  6. package/deno.check.d.ts +86 -0
  7. package/deno.check.json +21 -0
  8. package/dist/cjs/dialect/dialect-adapter-base.d.ts +11 -2
  9. package/dist/cjs/dialect/dialect-adapter.d.ts +11 -2
  10. package/dist/cjs/dialect/mssql/mssql-adapter.d.ts +11 -2
  11. package/dist/cjs/dialect/mssql/mssql-dialect-config.d.ts +37 -29
  12. package/dist/cjs/dialect/mssql/mssql-driver.js +20 -13
  13. package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +11 -2
  14. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +4 -0
  15. package/dist/cjs/dialect/mysql/mysql-driver.js +3 -0
  16. package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +11 -2
  17. package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +10 -3
  18. package/dist/cjs/dialect/postgres/postgres-driver.js +3 -0
  19. package/dist/cjs/dialect/postgres/postgres-introspector.js +5 -1
  20. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +11 -2
  21. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +66 -44
  22. package/dist/cjs/driver/dummy-driver.d.ts +13 -12
  23. package/dist/cjs/driver/dummy-driver.js +13 -12
  24. package/dist/cjs/dynamic/dynamic.d.ts +6 -6
  25. package/dist/cjs/dynamic/dynamic.js +6 -6
  26. package/dist/cjs/expression/expression-builder.d.ts +154 -61
  27. package/dist/cjs/expression/expression-wrapper.d.ts +92 -23
  28. package/dist/cjs/expression/expression.d.ts +40 -5
  29. package/dist/cjs/helpers/mssql.d.ts +74 -14
  30. package/dist/cjs/helpers/mssql.js +74 -14
  31. package/dist/cjs/helpers/mysql.d.ts +14 -10
  32. package/dist/cjs/helpers/mysql.js +14 -10
  33. package/dist/cjs/helpers/postgres.d.ts +7 -4
  34. package/dist/cjs/helpers/postgres.js +7 -4
  35. package/dist/cjs/helpers/sqlite.d.ts +41 -16
  36. package/dist/cjs/helpers/sqlite.js +41 -16
  37. package/dist/cjs/kysely.d.ts +99 -41
  38. package/dist/cjs/kysely.js +60 -36
  39. package/dist/cjs/migration/file-migration-provider.d.ts +2 -2
  40. package/dist/cjs/migration/file-migration-provider.js +2 -2
  41. package/dist/cjs/migration/migrator.d.ts +90 -15
  42. package/dist/cjs/migration/migrator.js +95 -17
  43. package/dist/cjs/operation-node/aggregate-function-node.d.ts +4 -0
  44. package/dist/cjs/operation-node/aggregate-function-node.js +9 -0
  45. package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
  46. package/dist/cjs/operation-node/data-type-node.js +12 -0
  47. package/dist/cjs/operation-node/delete-query-node.d.ts +1 -0
  48. package/dist/cjs/operation-node/insert-query-node.d.ts +1 -0
  49. package/dist/cjs/operation-node/merge-query-node.d.ts +1 -0
  50. package/dist/cjs/operation-node/operation-node-transformer.d.ts +9 -3
  51. package/dist/cjs/operation-node/operation-node-transformer.js +14 -3
  52. package/dist/cjs/operation-node/query-node.d.ts +4 -0
  53. package/dist/cjs/operation-node/query-node.js +8 -0
  54. package/dist/cjs/operation-node/select-query-node.d.ts +0 -1
  55. package/dist/cjs/operation-node/select-query-node.js +0 -8
  56. package/dist/cjs/operation-node/update-query-node.d.ts +1 -0
  57. package/dist/cjs/parser/insert-values-parser.js +3 -1
  58. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +52 -25
  59. package/dist/cjs/plugin/camel-case/camel-case-plugin.js +24 -19
  60. package/dist/cjs/plugin/camel-case/camel-case.js +4 -1
  61. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +29 -7
  62. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +1 -1
  63. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +2 -2
  64. package/dist/cjs/plugin/immediate-value/immediate-value-plugin.d.ts +28 -6
  65. package/dist/cjs/plugin/kysely-plugin.d.ts +28 -6
  66. package/dist/cjs/plugin/noop-plugin.d.ts +28 -6
  67. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.d.ts +62 -9
  68. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.js +34 -3
  69. package/dist/cjs/plugin/with-schema/with-schema-plugin.d.ts +28 -6
  70. package/dist/cjs/plugin/with-schema/with-schema-transformer.d.ts +4 -0
  71. package/dist/cjs/plugin/with-schema/with-schema-transformer.js +26 -0
  72. package/dist/cjs/query-builder/aggregate-function-builder.d.ts +43 -1
  73. package/dist/cjs/query-builder/aggregate-function-builder.js +30 -0
  74. package/dist/cjs/query-builder/delete-query-builder.d.ts +137 -45
  75. package/dist/cjs/query-builder/delete-query-builder.js +76 -26
  76. package/dist/cjs/query-builder/function-module.d.ts +35 -88
  77. package/dist/cjs/query-builder/insert-query-builder.d.ts +239 -65
  78. package/dist/cjs/query-builder/insert-query-builder.js +188 -52
  79. package/dist/cjs/query-builder/insert-result.d.ts +10 -5
  80. package/dist/cjs/query-builder/insert-result.js +10 -5
  81. package/dist/cjs/query-builder/json-path-builder.d.ts +33 -9
  82. package/dist/cjs/query-builder/json-path-builder.js +8 -5
  83. package/dist/cjs/query-builder/merge-query-builder.d.ts +159 -21
  84. package/dist/cjs/query-builder/merge-query-builder.js +68 -6
  85. package/dist/cjs/query-builder/on-conflict-builder.d.ts +51 -18
  86. package/dist/cjs/query-builder/on-conflict-builder.js +41 -16
  87. package/dist/cjs/query-builder/output-interface.d.ts +50 -7
  88. package/dist/cjs/query-builder/returning-interface.d.ts +5 -5
  89. package/dist/cjs/query-builder/select-query-builder-expression.d.ts +8 -0
  90. package/dist/cjs/query-builder/select-query-builder.d.ts +295 -63
  91. package/dist/cjs/query-builder/select-query-builder.js +7 -7
  92. package/dist/cjs/query-builder/update-query-builder.d.ts +138 -29
  93. package/dist/cjs/query-builder/update-query-builder.js +75 -10
  94. package/dist/cjs/query-builder/update-result.d.ts +2 -2
  95. package/dist/cjs/query-builder/update-result.js +2 -2
  96. package/dist/cjs/query-builder/where-interface.d.ts +5 -1
  97. package/dist/cjs/query-compiler/default-query-compiler.js +20 -0
  98. package/dist/cjs/query-creator.d.ts +66 -18
  99. package/dist/cjs/query-creator.js +2 -2
  100. package/dist/cjs/raw-builder/raw-builder.d.ts +29 -7
  101. package/dist/cjs/raw-builder/sql.d.ts +27 -8
  102. package/dist/cjs/schema/alter-column-builder.d.ts +17 -3
  103. package/dist/cjs/schema/alter-column-builder.js +17 -3
  104. package/dist/cjs/schema/alter-table-add-index-builder.d.ts +43 -9
  105. package/dist/cjs/schema/alter-table-add-index-builder.js +26 -9
  106. package/dist/cjs/schema/column-definition-builder.d.ts +283 -26
  107. package/dist/cjs/schema/column-definition-builder.js +283 -26
  108. package/dist/cjs/schema/create-table-builder.d.ts +86 -33
  109. package/dist/cjs/schema/create-table-builder.js +86 -33
  110. package/dist/cjs/util/column-type.d.ts +5 -9
  111. package/dist/cjs/util/require-all-props.d.ts +4 -2
  112. package/dist/cjs/util/require-all-props.js +4 -2
  113. package/dist/cjs/util/streamable.d.ts +1 -1
  114. package/dist/cjs/util/type-utils.d.ts +4 -2
  115. package/dist/esm/dialect/dialect-adapter-base.d.ts +11 -2
  116. package/dist/esm/dialect/dialect-adapter.d.ts +11 -2
  117. package/dist/esm/dialect/mssql/mssql-adapter.d.ts +11 -2
  118. package/dist/esm/dialect/mssql/mssql-dialect-config.d.ts +37 -29
  119. package/dist/esm/dialect/mssql/mssql-driver.js +20 -13
  120. package/dist/esm/dialect/mysql/mysql-adapter.d.ts +11 -2
  121. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +4 -0
  122. package/dist/esm/dialect/mysql/mysql-driver.js +3 -0
  123. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +11 -2
  124. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +10 -3
  125. package/dist/esm/dialect/postgres/postgres-driver.js +3 -0
  126. package/dist/esm/dialect/postgres/postgres-introspector.js +5 -1
  127. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +11 -2
  128. package/dist/esm/dialect/sqlite/sqlite-introspector.js +66 -44
  129. package/dist/esm/driver/dummy-driver.d.ts +13 -12
  130. package/dist/esm/driver/dummy-driver.js +13 -12
  131. package/dist/esm/dynamic/dynamic.d.ts +6 -6
  132. package/dist/esm/dynamic/dynamic.js +6 -6
  133. package/dist/esm/expression/expression-builder.d.ts +154 -61
  134. package/dist/esm/expression/expression-wrapper.d.ts +92 -23
  135. package/dist/esm/expression/expression.d.ts +40 -5
  136. package/dist/esm/helpers/mssql.d.ts +74 -14
  137. package/dist/esm/helpers/mssql.js +74 -14
  138. package/dist/esm/helpers/mysql.d.ts +14 -10
  139. package/dist/esm/helpers/mysql.js +14 -10
  140. package/dist/esm/helpers/postgres.d.ts +7 -4
  141. package/dist/esm/helpers/postgres.js +7 -4
  142. package/dist/esm/helpers/sqlite.d.ts +41 -16
  143. package/dist/esm/helpers/sqlite.js +41 -16
  144. package/dist/esm/kysely.d.ts +99 -41
  145. package/dist/esm/kysely.js +60 -36
  146. package/dist/esm/migration/file-migration-provider.d.ts +2 -2
  147. package/dist/esm/migration/file-migration-provider.js +2 -2
  148. package/dist/esm/migration/migrator.d.ts +90 -15
  149. package/dist/esm/migration/migrator.js +95 -17
  150. package/dist/esm/operation-node/aggregate-function-node.d.ts +4 -0
  151. package/dist/esm/operation-node/aggregate-function-node.js +9 -0
  152. package/dist/esm/operation-node/data-type-node.d.ts +1 -1
  153. package/dist/esm/operation-node/data-type-node.js +12 -0
  154. package/dist/esm/operation-node/delete-query-node.d.ts +1 -0
  155. package/dist/esm/operation-node/insert-query-node.d.ts +1 -0
  156. package/dist/esm/operation-node/merge-query-node.d.ts +1 -0
  157. package/dist/esm/operation-node/operation-node-transformer.d.ts +9 -3
  158. package/dist/esm/operation-node/operation-node-transformer.js +14 -3
  159. package/dist/esm/operation-node/query-node.d.ts +4 -0
  160. package/dist/esm/operation-node/query-node.js +8 -0
  161. package/dist/esm/operation-node/select-query-node.d.ts +0 -1
  162. package/dist/esm/operation-node/select-query-node.js +0 -8
  163. package/dist/esm/operation-node/update-query-node.d.ts +1 -0
  164. package/dist/esm/parser/insert-values-parser.js +3 -1
  165. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +52 -25
  166. package/dist/esm/plugin/camel-case/camel-case-plugin.js +24 -19
  167. package/dist/esm/plugin/camel-case/camel-case.js +4 -1
  168. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +29 -7
  169. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +1 -1
  170. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +2 -2
  171. package/dist/esm/plugin/immediate-value/immediate-value-plugin.d.ts +28 -6
  172. package/dist/esm/plugin/kysely-plugin.d.ts +28 -6
  173. package/dist/esm/plugin/noop-plugin.d.ts +28 -6
  174. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.d.ts +62 -9
  175. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.js +34 -3
  176. package/dist/esm/plugin/with-schema/with-schema-plugin.d.ts +28 -6
  177. package/dist/esm/plugin/with-schema/with-schema-transformer.d.ts +4 -0
  178. package/dist/esm/plugin/with-schema/with-schema-transformer.js +26 -0
  179. package/dist/esm/query-builder/aggregate-function-builder.d.ts +43 -1
  180. package/dist/esm/query-builder/aggregate-function-builder.js +30 -0
  181. package/dist/esm/query-builder/delete-query-builder.d.ts +137 -45
  182. package/dist/esm/query-builder/delete-query-builder.js +76 -26
  183. package/dist/esm/query-builder/function-module.d.ts +35 -88
  184. package/dist/esm/query-builder/insert-query-builder.d.ts +239 -65
  185. package/dist/esm/query-builder/insert-query-builder.js +188 -52
  186. package/dist/esm/query-builder/insert-result.d.ts +10 -5
  187. package/dist/esm/query-builder/insert-result.js +10 -5
  188. package/dist/esm/query-builder/json-path-builder.d.ts +33 -9
  189. package/dist/esm/query-builder/json-path-builder.js +8 -5
  190. package/dist/esm/query-builder/merge-query-builder.d.ts +159 -21
  191. package/dist/esm/query-builder/merge-query-builder.js +68 -6
  192. package/dist/esm/query-builder/on-conflict-builder.d.ts +51 -18
  193. package/dist/esm/query-builder/on-conflict-builder.js +41 -16
  194. package/dist/esm/query-builder/output-interface.d.ts +50 -7
  195. package/dist/esm/query-builder/returning-interface.d.ts +5 -5
  196. package/dist/esm/query-builder/select-query-builder-expression.d.ts +8 -0
  197. package/dist/esm/query-builder/select-query-builder.d.ts +295 -63
  198. package/dist/esm/query-builder/select-query-builder.js +7 -7
  199. package/dist/esm/query-builder/update-query-builder.d.ts +138 -29
  200. package/dist/esm/query-builder/update-query-builder.js +75 -10
  201. package/dist/esm/query-builder/update-result.d.ts +2 -2
  202. package/dist/esm/query-builder/update-result.js +2 -2
  203. package/dist/esm/query-builder/where-interface.d.ts +5 -1
  204. package/dist/esm/query-compiler/default-query-compiler.js +20 -0
  205. package/dist/esm/query-creator.d.ts +66 -18
  206. package/dist/esm/query-creator.js +2 -2
  207. package/dist/esm/raw-builder/raw-builder.d.ts +29 -7
  208. package/dist/esm/raw-builder/sql.d.ts +27 -8
  209. package/dist/esm/schema/alter-column-builder.d.ts +17 -3
  210. package/dist/esm/schema/alter-column-builder.js +17 -3
  211. package/dist/esm/schema/alter-table-add-index-builder.d.ts +43 -9
  212. package/dist/esm/schema/alter-table-add-index-builder.js +26 -9
  213. package/dist/esm/schema/column-definition-builder.d.ts +283 -26
  214. package/dist/esm/schema/column-definition-builder.js +283 -26
  215. package/dist/esm/schema/create-table-builder.d.ts +86 -33
  216. package/dist/esm/schema/create-table-builder.js +86 -33
  217. package/dist/esm/util/column-type.d.ts +5 -9
  218. package/dist/esm/util/require-all-props.d.ts +4 -2
  219. package/dist/esm/util/require-all-props.js +4 -2
  220. package/dist/esm/util/streamable.d.ts +1 -1
  221. package/dist/esm/util/type-utils.d.ts +4 -2
  222. 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)
@@ -25,33 +25,23 @@ import { WithSchemaPlugin } from './plugin/with-schema/with-schema-plugin.js';
25
25
  *
26
26
  * ### Examples
27
27
  *
28
- * This example assumes your database has tables `person` and `pet`:
28
+ * This example assumes your database has a "person" table:
29
29
  *
30
30
  * ```ts
31
- * import { Kysely, Generated, PostgresDialect } from 'kysely'
32
- *
33
- * interface PersonTable {
34
- * id: Generated<number>
35
- * first_name: string
36
- * last_name: string
37
- * }
38
- *
39
- * interface PetTable {
40
- * id: Generated<number>
41
- * owner_id: number
42
- * name: string
43
- * species: 'cat' | 'dog'
44
- * }
31
+ * import * as Sqlite from 'better-sqlite3'
32
+ * import { type Generated, Kysely, SqliteDialect } from 'kysely'
45
33
  *
46
34
  * interface Database {
47
- * person: PersonTable,
48
- * pet: PetTable
35
+ * person: {
36
+ * id: Generated<number>
37
+ * first_name: string
38
+ * last_name: string | null
39
+ * }
49
40
  * }
50
41
  *
51
42
  * const db = new Kysely<Database>({
52
- * dialect: new PostgresDialect({
53
- * host: 'localhost',
54
- * database: 'kysely_test',
43
+ * dialect: new SqliteDialect({
44
+ * database: new Sqlite(':memory:'),
55
45
  * })
56
46
  * })
57
47
  * ```
@@ -116,30 +106,52 @@ export class Kysely extends QueryCreator {
116
106
  });
117
107
  }
118
108
  /**
119
- * Returns a {@link FunctionModule} that can be used to write type safe function
109
+ * Returns a {@link FunctionModule} that can be used to write somewhat type-safe function
120
110
  * calls.
121
111
  *
122
112
  * ```ts
113
+ * const { count } = db.fn
114
+ *
123
115
  * await db.selectFrom('person')
124
116
  * .innerJoin('pet', 'pet.owner_id', 'person.id')
125
- * .select((eb) => [
126
- * 'person.id',
127
- * eb.fn.count('pet.id').as('pet_count')
117
+ * .select([
118
+ * 'id',
119
+ * count('pet.id').as('person_count'),
128
120
  * ])
129
121
  * .groupBy('person.id')
130
- * .having((eb) => eb.fn.count('pet.id'), '>', 10)
122
+ * .having(count('pet.id'), '>', 10)
131
123
  * .execute()
132
124
  * ```
133
125
  *
134
126
  * The generated SQL (PostgreSQL):
135
127
  *
136
128
  * ```sql
137
- * select "person"."id", count("pet"."id") as "pet_count"
129
+ * select "person"."id", count("pet"."id") as "person_count"
138
130
  * from "person"
139
131
  * inner join "pet" on "pet"."owner_id" = "person"."id"
140
132
  * group by "person"."id"
141
133
  * having count("pet"."id") > $1
142
134
  * ```
135
+ *
136
+ * Why "somewhat" type-safe? Because the function calls are not bound to the
137
+ * current query context. They allow you to reference columns and tables that
138
+ * are not in the current query. E.g. remove the `innerJoin` from the previous
139
+ * query and TypeScript won't even complain.
140
+ *
141
+ * If you want to make the function calls fully type-safe, you can use the
142
+ * {@link ExpressionBuilder.fn} getter for a query context-aware, stricter {@link FunctionModule}.
143
+ *
144
+ * ```ts
145
+ * await db.selectFrom('person')
146
+ * .innerJoin('pet', 'pet.owner_id', 'person.id')
147
+ * .select((eb) => [
148
+ * 'person.id',
149
+ * eb.fn.count('pet.id').as('pet_count')
150
+ * ])
151
+ * .groupBy('person.id')
152
+ * .having((eb) => eb.fn.count('pet.id'), '>', 10)
153
+ * .execute()
154
+ * ```
143
155
  */
144
156
  get fn() {
145
157
  return createFunctionModule();
@@ -150,8 +162,11 @@ export class Kysely extends QueryCreator {
150
162
  * The returned {@link TransactionBuilder} can be used to configure the transaction. The
151
163
  * {@link TransactionBuilder.execute} method can then be called to run the transaction.
152
164
  * {@link TransactionBuilder.execute} takes a function that is run inside the
153
- * transaction. If the function throws, the transaction is rolled back. Otherwise
154
- * the transaction is committed.
165
+ * transaction. If the function throws an exception,
166
+ * 1. the exception is caught,
167
+ * 2. the transaction is rolled back, and
168
+ * 3. the exception is thrown again.
169
+ * Otherwise the transaction is committed.
155
170
  *
156
171
  * The callback function passed to the {@link TransactionBuilder.execute | execute}
157
172
  * method gets the transaction object as its only argument. The transaction is
@@ -162,9 +177,12 @@ export class Kysely extends QueryCreator {
162
177
  *
163
178
  * <!-- siteExample("transactions", "Simple transaction", 10) -->
164
179
  *
165
- * This example inserts two rows in a transaction. If an error is thrown inside
166
- * the callback passed to the `execute` method, the transaction is rolled back.
167
- * Otherwise it's committed.
180
+ * This example inserts two rows in a transaction. If an exception is thrown inside
181
+ * the callback passed to the `execute` method,
182
+ * 1. the exception is caught,
183
+ * 2. the transaction is rolled back, and
184
+ * 3. the exception is thrown again.
185
+ * Otherwise the transaction is committed.
168
186
  *
169
187
  * ```ts
170
188
  * const catto = await db.transaction().execute(async (trx) => {
@@ -192,12 +210,18 @@ export class Kysely extends QueryCreator {
192
210
  * Setting the isolation level:
193
211
  *
194
212
  * ```ts
213
+ * import type { Kysely } from 'kysely'
214
+ *
195
215
  * await db
196
216
  * .transaction()
197
217
  * .setIsolationLevel('serializable')
198
218
  * .execute(async (trx) => {
199
219
  * await doStuff(trx)
200
220
  * })
221
+ *
222
+ * async function doStuff(kysely: typeof db) {
223
+ * // ...
224
+ * }
201
225
  * ```
202
226
  */
203
227
  transaction() {
@@ -217,6 +241,10 @@ export class Kysely extends QueryCreator {
217
241
  * // the same connection.
218
242
  * await doStuff(db)
219
243
  * })
244
+ *
245
+ * async function doStuff(kysely: typeof db) {
246
+ * // ...
247
+ * }
220
248
  * ```
221
249
  */
222
250
  connection() {
@@ -260,7 +288,6 @@ export class Kysely extends QueryCreator {
260
288
  *
261
289
  * The following example adds and uses a temporary table:
262
290
  *
263
- * @example
264
291
  * ```ts
265
292
  * await db.schema
266
293
  * .createTable('temp_table')
@@ -309,7 +336,7 @@ export class Kysely extends QueryCreator {
309
336
  /**
310
337
  * Executes a given compiled query or query builder.
311
338
  *
312
- * 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.
339
+ * 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.
313
340
  */
314
341
  executeQuery(query, queryId = createQueryId()) {
315
342
  const compiledQuery = isCompilable(query) ? query.compile() : query;
@@ -349,9 +376,6 @@ export class Transaction extends Kysely {
349
376
  executor: this.#props.executor.withoutPlugins(),
350
377
  });
351
378
  }
352
- /**
353
- * @override
354
- */
355
379
  withSchema(schema) {
356
380
  return new Transaction({
357
381
  ...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,
@@ -6,8 +6,8 @@ import { isFunction, isObject } from '../util/object-utils.js';
6
6
  * ### Examples
7
7
  *
8
8
  * ```ts
9
- * import { promises as fs } from 'fs'
10
- * import path from 'path'
9
+ * import { promises as fs } from 'node:fs'
10
+ * import path from 'node:path'
11
11
  *
12
12
  * new FileMigrationProvider({
13
13
  * 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
  */
@@ -203,7 +271,7 @@ export interface MigratorProps {
203
271
  *
204
272
  * When false, migrations must be run in their exact alpha-numeric order.
205
273
  * This is checked against the migrations already run in the database
206
- * (`migrationTableName'). This ensures your migrations are always run in
274
+ * (`migrationTableName`). This ensures your migrations are always run in
207
275
  * the same order and is the safest option.
208
276
  *
209
277
  * When true, migrations are still run in alpha-numeric order, but
@@ -212,6 +280,13 @@ export interface MigratorProps {
212
280
  * order.
213
281
  */
214
282
  readonly allowUnorderedMigrations?: boolean;
283
+ /**
284
+ * A function that compares migration names, used when sorting migrations in
285
+ * ascending order.
286
+ *
287
+ * Default is `name0.localeCompare(name1)`.
288
+ */
289
+ readonly nameComparator?: (name0: string, name1: string) => number;
215
290
  }
216
291
  /**
217
292
  * All migration methods ({@link Migrator.migrateTo | migrateTo},