kysely 0.12.1 → 0.14.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 (140) hide show
  1. package/README.md +34 -10
  2. package/dist/cjs/{introspection → dialect}/database-introspector.d.ts +0 -0
  3. package/dist/cjs/{introspection → dialect}/database-introspector.js +0 -0
  4. package/dist/cjs/dialect/dialect.d.ts +1 -1
  5. package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +2 -2
  6. package/dist/cjs/dialect/mysql/mysql-adapter.js +2 -1
  7. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +1 -1
  8. package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +1 -1
  9. package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +2 -2
  10. package/dist/cjs/dialect/postgres/postgres-adapter.js +2 -1
  11. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +1 -1
  12. package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
  13. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +2 -2
  14. package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +1 -1
  15. package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +1 -1
  16. package/dist/cjs/index.d.ts +5 -3
  17. package/dist/cjs/index.js +4 -2
  18. package/dist/cjs/kysely.d.ts +152 -1
  19. package/dist/cjs/kysely.js +37 -0
  20. package/dist/cjs/migration/migration.d.ts +154 -11
  21. package/dist/cjs/migration/migration.js +256 -135
  22. package/dist/cjs/operation-node/check-constraint-node.d.ts +1 -1
  23. package/dist/cjs/operation-node/check-constraint-node.js +2 -3
  24. package/dist/cjs/operation-node/column-definition-node.d.ts +8 -7
  25. package/dist/cjs/operation-node/column-definition-node.js +0 -4
  26. package/dist/cjs/operation-node/common-table-expression-name-node.d.ts +15 -0
  27. package/dist/cjs/operation-node/common-table-expression-name-node.js +23 -0
  28. package/dist/cjs/operation-node/common-table-expression-node.d.ts +3 -3
  29. package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
  30. package/dist/cjs/operation-node/generated-node.d.ts +20 -0
  31. package/dist/cjs/operation-node/generated-node.js +31 -0
  32. package/dist/cjs/operation-node/on-conflict-node.d.ts +13 -2
  33. package/dist/cjs/operation-node/on-conflict-node.js +39 -1
  34. package/dist/cjs/operation-node/operation-node-transformer.d.ts +4 -2
  35. package/dist/cjs/operation-node/operation-node-transformer.js +22 -6
  36. package/dist/cjs/operation-node/operation-node-visitor.d.ts +4 -2
  37. package/dist/cjs/operation-node/operation-node-visitor.js +2 -1
  38. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  39. package/dist/cjs/operation-node/with-node.d.ts +3 -1
  40. package/dist/cjs/operation-node/with-node.js +2 -1
  41. package/dist/cjs/parser/reference-parser.d.ts +2 -2
  42. package/dist/cjs/parser/select-parser.d.ts +2 -2
  43. package/dist/cjs/parser/with-parser.d.ts +32 -5
  44. package/dist/cjs/parser/with-parser.js +13 -2
  45. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +2 -2
  46. package/dist/cjs/plugin/with-schema/with-schema-transformer.js +8 -0
  47. package/dist/cjs/query-builder/insert-query-builder.d.ts +76 -75
  48. package/dist/cjs/query-builder/insert-query-builder.js +150 -15
  49. package/dist/cjs/query-builder/on-conflict-builder.d.ts +686 -0
  50. package/dist/cjs/query-builder/on-conflict-builder.js +323 -0
  51. package/dist/cjs/query-compiler/default-query-compiler.d.ts +4 -2
  52. package/dist/cjs/query-compiler/default-query-compiler.js +48 -8
  53. package/dist/cjs/query-creator.d.ts +27 -3
  54. package/dist/cjs/query-creator.js +33 -1
  55. package/dist/cjs/query-executor/default-query-executor.d.ts +19 -0
  56. package/dist/cjs/query-executor/noop-query-executor.d.ts +19 -0
  57. package/dist/cjs/raw-builder/raw-builder.js +1 -1
  58. package/dist/cjs/schema/alter-table-builder.d.ts +41 -9
  59. package/dist/cjs/schema/alter-table-builder.js +45 -9
  60. package/dist/cjs/schema/column-definition-builder.d.ts +40 -8
  61. package/dist/cjs/schema/column-definition-builder.js +21 -8
  62. package/dist/cjs/schema/create-index-builder.d.ts +3 -2
  63. package/dist/cjs/schema/create-index-builder.js +2 -2
  64. package/dist/cjs/schema/create-table-builder.d.ts +3 -2
  65. package/dist/cjs/schema/create-table-builder.js +2 -2
  66. package/dist/cjs/util/type-utils.d.ts +11 -7
  67. package/dist/esm/{introspection → dialect}/database-introspector.d.ts +0 -0
  68. package/dist/esm/{introspection → dialect}/database-introspector.js +0 -0
  69. package/dist/esm/dialect/dialect.d.ts +1 -1
  70. package/dist/esm/dialect/mysql/mysql-adapter.d.ts +2 -2
  71. package/dist/esm/dialect/mysql/mysql-adapter.js +2 -1
  72. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +1 -1
  73. package/dist/esm/dialect/mysql/mysql-introspector.d.ts +1 -1
  74. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +2 -2
  75. package/dist/esm/dialect/postgres/postgres-adapter.js +2 -1
  76. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +1 -1
  77. package/dist/esm/dialect/postgres/postgres-introspector.d.ts +1 -1
  78. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +2 -2
  79. package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +1 -1
  80. package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +1 -1
  81. package/dist/esm/index.d.ts +5 -3
  82. package/dist/esm/index.js +4 -2
  83. package/dist/esm/kysely.d.ts +152 -1
  84. package/dist/esm/kysely.js +37 -0
  85. package/dist/esm/migration/migration.d.ts +154 -11
  86. package/dist/esm/migration/migration.js +255 -134
  87. package/dist/esm/operation-node/check-constraint-node.d.ts +1 -1
  88. package/dist/esm/operation-node/check-constraint-node.js +2 -3
  89. package/dist/esm/operation-node/column-definition-node.d.ts +8 -7
  90. package/dist/esm/operation-node/column-definition-node.js +0 -4
  91. package/dist/esm/operation-node/common-table-expression-name-node.d.ts +15 -0
  92. package/dist/esm/operation-node/common-table-expression-name-node.js +20 -0
  93. package/dist/esm/operation-node/common-table-expression-node.d.ts +3 -3
  94. package/dist/esm/operation-node/data-type-node.d.ts +1 -1
  95. package/dist/esm/operation-node/generated-node.d.ts +20 -0
  96. package/dist/esm/operation-node/generated-node.js +28 -0
  97. package/dist/esm/operation-node/on-conflict-node.d.ts +13 -2
  98. package/dist/esm/operation-node/on-conflict-node.js +39 -1
  99. package/dist/esm/operation-node/operation-node-transformer.d.ts +4 -2
  100. package/dist/esm/operation-node/operation-node-transformer.js +22 -6
  101. package/dist/esm/operation-node/operation-node-visitor.d.ts +4 -2
  102. package/dist/esm/operation-node/operation-node-visitor.js +2 -1
  103. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  104. package/dist/esm/operation-node/with-node.d.ts +3 -1
  105. package/dist/esm/operation-node/with-node.js +2 -1
  106. package/dist/esm/parser/reference-parser.d.ts +2 -2
  107. package/dist/esm/parser/select-parser.d.ts +2 -2
  108. package/dist/esm/parser/with-parser.d.ts +32 -5
  109. package/dist/esm/parser/with-parser.js +13 -2
  110. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +2 -2
  111. package/dist/esm/plugin/with-schema/with-schema-transformer.js +8 -0
  112. package/dist/esm/query-builder/insert-query-builder.d.ts +76 -75
  113. package/dist/esm/query-builder/insert-query-builder.js +150 -15
  114. package/dist/esm/query-builder/on-conflict-builder.d.ts +686 -0
  115. package/dist/esm/query-builder/on-conflict-builder.js +317 -0
  116. package/dist/esm/query-compiler/default-query-compiler.d.ts +4 -2
  117. package/dist/esm/query-compiler/default-query-compiler.js +48 -8
  118. package/dist/esm/query-creator.d.ts +27 -3
  119. package/dist/esm/query-creator.js +33 -1
  120. package/dist/esm/query-executor/default-query-executor.d.ts +19 -0
  121. package/dist/esm/query-executor/noop-query-executor.d.ts +19 -0
  122. package/dist/esm/raw-builder/raw-builder.js +1 -1
  123. package/dist/esm/schema/alter-table-builder.d.ts +41 -9
  124. package/dist/esm/schema/alter-table-builder.js +45 -9
  125. package/dist/esm/schema/column-definition-builder.d.ts +40 -8
  126. package/dist/esm/schema/column-definition-builder.js +21 -8
  127. package/dist/esm/schema/create-index-builder.d.ts +3 -2
  128. package/dist/esm/schema/create-index-builder.js +2 -2
  129. package/dist/esm/schema/create-table-builder.d.ts +3 -2
  130. package/dist/esm/schema/create-table-builder.js +2 -2
  131. package/dist/esm/util/type-utils.d.ts +11 -7
  132. package/package.json +2 -2
  133. package/dist/cjs/operation-node/generated-always-as-node.d.ts +0 -16
  134. package/dist/cjs/operation-node/generated-always-as-node.js +0 -25
  135. package/dist/cjs/parser/on-conflict-parser.d.ts +0 -10
  136. package/dist/cjs/parser/on-conflict-parser.js +0 -35
  137. package/dist/esm/operation-node/generated-always-as-node.d.ts +0 -16
  138. package/dist/esm/operation-node/generated-always-as-node.js +0 -22
  139. package/dist/esm/parser/on-conflict-parser.d.ts +0 -10
  140. package/dist/esm/parser/on-conflict-parser.js +0 -30
package/dist/esm/index.js CHANGED
@@ -15,6 +15,7 @@ export * from './query-builder/function-builder.js';
15
15
  export * from './query-builder/insert-result.js';
16
16
  export * from './query-builder/delete-result.js';
17
17
  export * from './query-builder/update-result.js';
18
+ export * from './query-builder/on-conflict-builder.js';
18
19
  export * from './raw-builder/raw-builder.js';
19
20
  export * from './query-executor/query-executor.js';
20
21
  export * from './query-executor/default-query-executor.js';
@@ -73,6 +74,7 @@ export * from './operation-node/column-definition-node.js';
73
74
  export * from './operation-node/column-node.js';
74
75
  export * from './operation-node/column-update-node.js';
75
76
  export * from './operation-node/common-table-expression-node.js';
77
+ export * from './operation-node/common-table-expression-name-node.js';
76
78
  export * from './operation-node/constraint-node.js';
77
79
  export * from './operation-node/create-index-node.js';
78
80
  export * from './operation-node/create-schema-node.js';
@@ -90,7 +92,7 @@ export * from './operation-node/drop-view-node.js';
90
92
  export * from './operation-node/filter-node.js';
91
93
  export * from './operation-node/foreign-key-constraint-node.js';
92
94
  export * from './operation-node/from-node.js';
93
- export * from './operation-node/generated-always-as-node.js';
95
+ export * from './operation-node/generated-node.js';
94
96
  export * from './operation-node/group-by-item-node.js';
95
97
  export * from './operation-node/group-by-node.js';
96
98
  export * from './operation-node/having-node.js';
@@ -135,6 +137,6 @@ export * from './operation-node/value-node.js';
135
137
  export * from './operation-node/values-node.js';
136
138
  export * from './operation-node/where-node.js';
137
139
  export * from './operation-node/with-node.js';
138
- export * from './introspection/database-introspector.js';
140
+ export * from './dialect/database-introspector.js';
139
141
  export * from './util/compilable.js';
140
142
  export * from './util/log.js';
@@ -6,7 +6,7 @@ import { QueryExecutor } from './query-executor/query-executor.js';
6
6
  import { QueryCreator } from './query-creator.js';
7
7
  import { KyselyPlugin } from './plugin/kysely-plugin.js';
8
8
  import { GeneratedPlaceholder } from './util/type-utils.js';
9
- import { DatabaseIntrospector } from './introspection/database-introspector.js';
9
+ import { DatabaseIntrospector } from './dialect/database-introspector.js';
10
10
  import { Driver, IsolationLevel } from './driver/driver.js';
11
11
  import { ParseContext } from './parser/parse-context.js';
12
12
  import { FunctionBuilder } from './query-builder/function-builder.js';
@@ -207,6 +207,38 @@ export declare class Kysely<DB> extends QueryCreator<DB> {
207
207
  * Returns a copy of this Kysely instance without any plugins.
208
208
  */
209
209
  withoutPlugins(): Kysely<DB>;
210
+ /**
211
+ * Returns a copy of this Kysely instance with tables added to its
212
+ * database type.
213
+ *
214
+ * This method only modifies the types and doesn't affect any of the
215
+ * executed queries in any way.
216
+ *
217
+ * ### Examples
218
+ *
219
+ * The following example adds and uses a temporary table:
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * await db.schema
224
+ * .createTable('temp_table')
225
+ * .temporary()
226
+ * .addColumn('some_column', 'integer')
227
+ * .execute()
228
+ *
229
+ * const tempDb = db.withTables<{
230
+ * temp_table: {
231
+ * some_column: number
232
+ * }
233
+ * }>()
234
+ *
235
+ * await tempDb
236
+ * .insertInto('temp_table')
237
+ * .values({ some_column: 100 })
238
+ * .execute()
239
+ * ```
240
+ */
241
+ withTables<T extends Record<string, Record<string, any>>>(): Kysely<DB & T>;
210
242
  /**
211
243
  * Releases all resources and disconnects from the database.
212
244
  *
@@ -223,13 +255,132 @@ export declare class Kysely<DB> extends QueryCreator<DB> {
223
255
  export declare class Transaction<DB> extends Kysely<DB> {
224
256
  #private;
225
257
  constructor(props: KyselyProps);
258
+ /**
259
+ * Returns true if this `Kysely` instance is a transaction.
260
+ *
261
+ * You can also use `db instanceof Transaction`.
262
+ */
226
263
  get isTransaction(): true;
264
+ /**
265
+ * Returns the {@link MigrationModule} module for managing and running migrations.
266
+ */
227
267
  get migration(): MigrationModule;
268
+ /**
269
+ * Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
270
+ *
271
+ * The returned {@link TransactionBuilder} can be used to configure the transaction. The
272
+ * {@link TransactionBuilder.execute} method can then be called to run the transaction.
273
+ * {@link TransactionBuilder.execute} takes a function that is run inside the
274
+ * transaction. If the function throws, the transaction is rolled back. Otherwise
275
+ * the transaction is committed.
276
+ *
277
+ * The callback function passed to the {@link TransactionBuilder.execute | execute}
278
+ * method gets the transaction object as its only argument. The transaction is
279
+ * of type {@link Transaction} which inherits {@link Kysely}. Any query
280
+ * started through the transaction object is executed inside the transaction.
281
+ *
282
+ * ### Examples
283
+ *
284
+ * ```ts
285
+ * const catto = await db.transaction().execute(async (trx) => {
286
+ * const jennifer = await trx.insertInto('person')
287
+ * .values({
288
+ * id: db.generated,
289
+ * first_name: 'Jennifer',
290
+ * last_name: 'Aniston',
291
+ * })
292
+ * .returning('id')
293
+ * .executeTakeFirstOrThrow()
294
+ *
295
+ * await someFunction(trx, jennifer)
296
+ *
297
+ * return await trx.insertInto('pet')
298
+ * .values({
299
+ * id: db.generated,
300
+ * user_id: jennifer.id,
301
+ * name: 'Catto',
302
+ * species: 'cat'
303
+ * })
304
+ * .returning('*')
305
+ * .executeTakeFirst()
306
+ * })
307
+ * ```
308
+ *
309
+ * Setting the isolation level:
310
+ *
311
+ * ```ts
312
+ * await db
313
+ * .transaction()
314
+ * .setIsolationLevel('serializable')
315
+ * .execute(async (trx) => {
316
+ * await doStuff(trx)
317
+ * })
318
+ * ```
319
+ */
228
320
  transaction(): TransactionBuilder<DB>;
321
+ /**
322
+ * Provides a kysely instance bound to a single database connection.
323
+ *
324
+ * ### Examples
325
+ *
326
+ * ```ts
327
+ * await db
328
+ * .connection()
329
+ * .execute(async (db) => {
330
+ * // `db` is an instance of `Kysely` that's bound to a single
331
+ * // database connection. All queries executed through `db` use
332
+ * // the same connection.
333
+ * await doStuff(db)
334
+ * })
335
+ * ```
336
+ */
229
337
  connection(): ConnectionBuilder<DB>;
338
+ /**
339
+ * Releases all resources and disconnects from the database.
340
+ *
341
+ * You need to call this when you are done using the `Kysely` instance.
342
+ */
230
343
  destroy(): Promise<void>;
344
+ /**
345
+ * Returns a copy of this Kysely instance with the given plugin installed.
346
+ */
231
347
  withPlugin(plugin: KyselyPlugin): Transaction<DB>;
348
+ /**
349
+ * Returns a copy of this Kysely instance without any plugins.
350
+ */
232
351
  withoutPlugins(): Transaction<DB>;
352
+ /**
353
+ * Returns a copy of this Kysely instance with tables added to its
354
+ * database type.
355
+ *
356
+ * This method only modifies the types and doesn't affect any of the
357
+ * executed queries in any way.
358
+ *
359
+ * ### Examples
360
+ *
361
+ * The following example adds and uses a temporary table:
362
+ *
363
+ * @example
364
+ * ```ts
365
+ * await db.schema
366
+ * .createTable('temp_table')
367
+ * .temporary()
368
+ * .addColumn('some_column', 'integer')
369
+ * .execute()
370
+ *
371
+ * const tempDb = db.withTables<{
372
+ * temp_table: {
373
+ * some_column: number
374
+ * }
375
+ * }>()
376
+ *
377
+ * await tempDb
378
+ * .insertInto('temp_table')
379
+ * .values({ some_column: 100 })
380
+ * .execute()
381
+ * ```
382
+ */
383
+ withTables<T extends Record<string, Record<string, any>>>(): Transaction<DB & T>;
233
384
  }
234
385
  export interface KyselyProps {
235
386
  readonly config: KyselyConfig;
@@ -258,6 +258,40 @@ export class Kysely extends QueryCreator {
258
258
  executor: this.#props.executor.withoutPlugins(),
259
259
  });
260
260
  }
261
+ /**
262
+ * Returns a copy of this Kysely instance with tables added to its
263
+ * database type.
264
+ *
265
+ * This method only modifies the types and doesn't affect any of the
266
+ * executed queries in any way.
267
+ *
268
+ * ### Examples
269
+ *
270
+ * The following example adds and uses a temporary table:
271
+ *
272
+ * @example
273
+ * ```ts
274
+ * await db.schema
275
+ * .createTable('temp_table')
276
+ * .temporary()
277
+ * .addColumn('some_column', 'integer')
278
+ * .execute()
279
+ *
280
+ * const tempDb = db.withTables<{
281
+ * temp_table: {
282
+ * some_column: number
283
+ * }
284
+ * }>()
285
+ *
286
+ * await tempDb
287
+ * .insertInto('temp_table')
288
+ * .values({ some_column: 100 })
289
+ * .execute()
290
+ * ```
291
+ */
292
+ withTables() {
293
+ return new Kysely({ ...this.#props });
294
+ }
261
295
  /**
262
296
  * Releases all resources and disconnects from the database.
263
297
  *
@@ -311,6 +345,9 @@ export class Transaction extends Kysely {
311
345
  executor: this.#props.executor.withoutPlugins(),
312
346
  });
313
347
  }
348
+ withTables() {
349
+ return new Transaction({ ...this.#props });
350
+ }
314
351
  }
315
352
  export function isKyselyProps(obj) {
316
353
  return (isObject(obj) &&
@@ -3,6 +3,73 @@ import { DialectAdapter } from '../dialect/dialect-adapter.js';
3
3
  export declare const MIGRATION_TABLE = "kysely_migration";
4
4
  export declare const MIGRATION_LOCK_TABLE = "kysely_migration_lock";
5
5
  export declare const MIGRATION_LOCK_ID = "migration_lock";
6
+ export declare const NO_MIGRATIONS: NoMigrations;
7
+ export declare type MigrationsOrFolderPath = Record<string, Migration> | string;
8
+ export interface Migration {
9
+ up(db: Kysely<any>): Promise<void>;
10
+ down?(db: Kysely<any>): Promise<void>;
11
+ }
12
+ /**
13
+ * Type for the NO_MIGRATIONS constant. Never create one of these.
14
+ */
15
+ export interface NoMigrations {
16
+ readonly __noMigrations__: true;
17
+ }
18
+ /**
19
+ * All migration methods ({@link MigrationModule.migrateTo | migrateTo},
20
+ * {@link MigrationModule.migrateToLatest | migrateToLatest} etc.) never
21
+ * throw but return this object instead.
22
+ */
23
+ export interface MigrationResultSet {
24
+ /**
25
+ * This is defined if something went wrong.
26
+ *
27
+ * An error may have occurred in one of the migrations in which case the
28
+ * {@link results} list contains an item with `status === 'Error'` to
29
+ * indicate which migration failed.
30
+ *
31
+ * An error may also have occurred before Kysely was able to figure out
32
+ * which migrations should be executed, in which case the {@link results}
33
+ * list is undefined.
34
+ */
35
+ readonly error?: unknown;
36
+ /**
37
+ * {@link MigrationResult} for each individual migration that was supposed
38
+ * to be executed by the operation.
39
+ *
40
+ * If all went well, each result's `status` is `Success`. If some migration
41
+ * failed, the failed migration's result's `status` is `Error` and all
42
+ * results after that one have `status` ´NotExecuted`.
43
+ *
44
+ * This property can be undefined if an error occurred before Kysely was
45
+ * able to figure out which migrations should be executed.
46
+ *
47
+ * If this list is empty, there were no migrations to execute.
48
+ */
49
+ readonly results?: MigrationResult[];
50
+ }
51
+ export interface MigrationResult {
52
+ readonly migrationName: string;
53
+ /**
54
+ * The direction in which this migration was executed.
55
+ */
56
+ readonly direction: 'Up' | 'Down';
57
+ /**
58
+ * The execution status.
59
+ *
60
+ * - `Success` means the migration was successfully executed. Note that
61
+ * if any of the later migrations in the {@link MigrationResult.results}
62
+ * list failed (have status `Error`) AND the dialect supports transactional
63
+ * DDL, even the successfull migrations were rolled back.
64
+ *
65
+ * - `Error` means the migration failed. In this case the
66
+ * {@link MigrationResult.error} contains the error.
67
+ *
68
+ * - `NotExecuted` means that the migration was supposed to be executed
69
+ * but wasn't because an earlier migration failed.
70
+ */
71
+ readonly status: 'Success' | 'Error' | 'NotExecuted';
72
+ }
6
73
  export declare class MigrationModule {
7
74
  #private;
8
75
  constructor(db: Kysely<any>, adapter: DialectAdapter);
@@ -10,26 +77,45 @@ export declare class MigrationModule {
10
77
  * Runs all migrations that have not yet been run.
11
78
  *
12
79
  * The only argument must either be a file path to the folder that contains all migrations
13
- * OR an object that contains all migrations (not just the ones that need to be run).
80
+ * OR an object that contains all migrations (not just the ones that need to be executed).
14
81
  * The keys in the object must be the unique migration names.
15
82
  *
83
+ * This method returns a {@link MigrationResultSet} instance and _never_ throws.
84
+ * {@link MigrationResultSet.error} holds the error if something went wrong.
85
+ * {@link MigrationResultSet.results} contains information about which migrations
86
+ * were executed and which failed. See the examples below.
87
+ *
16
88
  * This method goes through all possible migrations (passed as the argument) and runs the
17
89
  * ones whose names are alphabetically after the last migration that has been run. If the
18
- * list of executed migrations doesn't match the list of possible migrations, and error
19
- * is thrown.
90
+ * list of executed migrations doesn't match the beginning of the list of possible migrations
91
+ * an error is thrown.
20
92
  *
21
93
  * ### Examples
22
94
  *
23
95
  * ```ts
24
- * await db.migration.migrateToLatest(
96
+ * const { error, results } = await db.migration.migrateToLatest(
25
97
  * path.join(__dirname, 'migrations')
26
98
  * )
99
+ *
100
+ * results?.forEach((it) => {
101
+ * if (it.status === 'Success') {
102
+ * console.log(`migration "${it.migrationName}" was executed successfully`)
103
+ * } else if (it.status === 'Error') {
104
+ * console.error(`failed to execute migration "${it.migrationName}"`)
105
+ * }
106
+ * })
107
+ *
108
+ * if (error) {
109
+ * console.error('failed to run `migrateToLatest`')
110
+ * console.error(error)
111
+ * }
27
112
  * ```
28
113
  *
29
114
  * In the next example, we use a record of migration objects instead of file folder path.
30
115
  * The keys in the object are migration names that can be anything you want. The order of
31
116
  * the migrations is determined based on the alphabetical order of the keys in the record.
32
- * This can be useful if you are using a bundler like webpack or esbuild.
117
+ * This version of the `migrateToLatest` method can be useful if you are using a bundler
118
+ * like webpack or esbuild.
33
119
  *
34
120
  * ```ts
35
121
  * await db.migration.migrateToLatest({
@@ -57,10 +143,67 @@ export declare class MigrationModule {
57
143
  * })
58
144
  * ```
59
145
  */
60
- migrateToLatest(migrationsFolderPath: string): Promise<void>;
61
- migrateToLatest(allMigrations: Record<string, Migration>): Promise<void>;
62
- }
63
- export interface Migration {
64
- up(db: Kysely<any>): Promise<void>;
65
- down?(db: Kysely<any>): Promise<void>;
146
+ migrateToLatest(migrationsFolderPath: string): Promise<MigrationResultSet>;
147
+ migrateToLatest(allMigrations: Record<string, Migration>): Promise<MigrationResultSet>;
148
+ /**
149
+ * Migrate up/down to a specific migration.
150
+ *
151
+ * Otherwise works just like {@link migrateToLatest}. The first argument
152
+ * behaves the same, the output is the same etc.
153
+ *
154
+ * ### Examples
155
+ *
156
+ * ```ts
157
+ * await db.migration.migrateTo(
158
+ * path.join(__dirname, 'migrations'),
159
+ * 'some_migration'
160
+ * )
161
+ * ```
162
+ *
163
+ * If you specify the name of the first migration, this method migrates
164
+ * down to the first migration, but doesn't run the `down` migration for
165
+ * the first migration. In case you want to migrate down ALL migrations
166
+ * you can use a special constant `NO_MIGRATIONS`:
167
+ *
168
+ * ```ts
169
+ * await db.migration.migrateTo(
170
+ * path.join(__dirname, 'migrations'),
171
+ * NO_MIGRATIONS,
172
+ * )
173
+ * ```
174
+ */
175
+ migrateTo(migrationsFolderPath: string, targetMigrationName: string | NoMigrations): Promise<MigrationResultSet>;
176
+ migrateTo(allMigrations: Record<string, Migration>, targetMigrationName: string | NoMigrations): Promise<MigrationResultSet>;
177
+ /**
178
+ * Migrate one step up.
179
+ *
180
+ * Otherwise works just like {@link migrateToLatest}. The only argument
181
+ * behaves the same, the output is the same etc.
182
+ *
183
+ * ### Examples
184
+ *
185
+ * ```ts
186
+ * await db.migration.migrateUp(
187
+ * path.join(__dirname, 'migrations'),
188
+ * )
189
+ * ```
190
+ */
191
+ migrateUp(migrationsFolderPath: string): Promise<MigrationResultSet>;
192
+ migrateUp(allMigrations: Record<string, Migration>): Promise<MigrationResultSet>;
193
+ /**
194
+ * Migrate one step down.
195
+ *
196
+ * Otherwise works just like {@link migrateToLatest}. The only argument
197
+ * behaves the same, the output is the same etc.
198
+ *
199
+ * ### Examples
200
+ *
201
+ * ```ts
202
+ * await db.migration.migrateDown(
203
+ * path.join(__dirname, 'migrations'),
204
+ * )
205
+ * ```
206
+ */
207
+ migrateDown(migrationsFolderPath: string): Promise<MigrationResultSet>;
208
+ migrateDown(allMigrations: Record<string, Migration>): Promise<MigrationResultSet>;
66
209
  }