kysely 0.12.2 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +34 -10
  2. package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +2 -2
  3. package/dist/cjs/dialect/mysql/mysql-adapter.js +2 -1
  4. package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +2 -2
  5. package/dist/cjs/dialect/postgres/postgres-adapter.js +2 -1
  6. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +2 -2
  7. package/dist/cjs/index.d.ts +1 -1
  8. package/dist/cjs/migration/migration.d.ts +154 -11
  9. package/dist/cjs/migration/migration.js +256 -135
  10. package/dist/cjs/operation-node/column-definition-node.d.ts +6 -5
  11. package/dist/cjs/operation-node/column-definition-node.js +0 -4
  12. package/dist/cjs/operation-node/operation-node-transformer.js +2 -1
  13. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +2 -2
  14. package/dist/cjs/query-compiler/default-query-compiler.js +4 -1
  15. package/dist/cjs/schema/alter-table-builder.d.ts +12 -0
  16. package/dist/cjs/schema/alter-table-builder.js +12 -0
  17. package/dist/cjs/schema/column-definition-builder.d.ts +12 -0
  18. package/dist/cjs/schema/column-definition-builder.js +4 -1
  19. package/dist/esm/dialect/mysql/mysql-adapter.d.ts +2 -2
  20. package/dist/esm/dialect/mysql/mysql-adapter.js +2 -1
  21. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +2 -2
  22. package/dist/esm/dialect/postgres/postgres-adapter.js +2 -1
  23. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +2 -2
  24. package/dist/esm/index.d.ts +1 -1
  25. package/dist/esm/migration/migration.d.ts +154 -11
  26. package/dist/esm/migration/migration.js +255 -134
  27. package/dist/esm/operation-node/column-definition-node.d.ts +6 -5
  28. package/dist/esm/operation-node/column-definition-node.js +0 -4
  29. package/dist/esm/operation-node/operation-node-transformer.js +2 -1
  30. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +2 -2
  31. package/dist/esm/query-compiler/default-query-compiler.js +4 -1
  32. package/dist/esm/schema/alter-table-builder.d.ts +12 -0
  33. package/dist/esm/schema/alter-table-builder.js +12 -0
  34. package/dist/esm/schema/column-definition-builder.d.ts +12 -0
  35. package/dist/esm/schema/column-definition-builder.js +4 -1
  36. package/package.json +1 -1
package/README.md CHANGED
@@ -252,32 +252,56 @@ await db.migration.migrateToLatest(pathToMigrationsFolder)
252
252
  ```
253
253
 
254
254
  to run all migrations that have not yet been run. The migrations are executed in alphabetical
255
- order by their file name.
255
+ order by their name. See the [migration module](https://koskimas.github.io/kysely/classes/MigrationModule.html)
256
+ documentation for more info.
256
257
 
257
258
  Kysely doesn't have a CLI for running migrations and probably never will. This is because Kysely's
258
259
  migrations are also written in typescript. To run the migrations, you need to first build the
259
260
  typescript code into javascript. The CLI would cause confusion over which migrations are being
260
- run, the typescript ones or the javascript ones. If we added support for both, it would mean the
261
- CLI would depend on a typescript compiler, which most production environments don't (and shouldn't)
261
+ run, the typescript ones or the javascript ones. If we added support for both, the CLI would
262
+ need to depend on a typescript compiler, which most production environments don't (and shouldn't)
262
263
  have. You will probably want to add a simple migration script to your projects like this:
263
264
 
264
265
  ```ts
265
266
  import path from 'path'
267
+
268
+ // This example assumes you have a file named `database.ts` that
269
+ // creates and exports a `Kysely` instance `db`. You can just
270
+ // as well create a new `Kysely` instance in the function below
271
+ // or do something completely different to obtain an instance
272
+ // of `Kysely`.
266
273
  import { db } from './database'
267
274
 
268
- db.migration
269
- .migrateToLatest(path.join(__dirname, 'migrations'))
270
- .then(() => db.destroy())
275
+ async function migrateToLatest() {
276
+ const { error, results } = await db.migration.migrateToLatest(
277
+ path.join(__dirname, 'migrations')
278
+ )
279
+
280
+ // Destroy the `Kysely` instance to make the script exit faster.
281
+ await db.destroy()
282
+
283
+ results?.forEach((it) => {
284
+ if (it.status === 'Success') {
285
+ console.log(`migration "${it.migrationName}" was executed successfully`)
286
+ } else if (it.status === 'Error') {
287
+ console.error(`failed to execute migration "${it.migrationName}"`)
288
+ }
289
+ })
290
+
291
+ if (error) {
292
+ console.error('failed to migrate')
293
+ console.error(error)
294
+ process.exit(1)
295
+ }
296
+ }
297
+
298
+ migrateToLatest()
271
299
  ```
272
300
 
273
301
  The migration methods use a lock on the database level, and parallel calls are executed serially.
274
302
  This means that you can safely call `migrateToLatest` and other migration methods from multiple
275
303
  server instances simultaneously and the migrations are guaranteed to only be executed once.
276
304
 
277
- NOTE: Only `db.migration.migrateToLatest` method is implemented at the moment. There is no way
278
- to run the down migrations, or to go forward to a specific migration. These methods will be
279
- added soon.
280
-
281
305
  # Why not just contribute to knex
282
306
 
283
307
  Kysely is very similar to knex, but it also attempts to fix things that I personally find not-so-good
@@ -1,6 +1,6 @@
1
1
  import { Kysely } from '../../kysely.js';
2
- import { DialectAdapter } from '../dialect-adapter.js';
3
- export declare class MysqlAdapter implements DialectAdapter {
2
+ import { DialectAdapterBase } from '../dialect-adapter-base.js';
3
+ export declare class MysqlAdapter extends DialectAdapterBase {
4
4
  /**
5
5
  * Whether or not this dialect supports transactional DDL.
6
6
  *
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MysqlAdapter = void 0;
4
+ const dialect_adapter_base_js_1 = require("../dialect-adapter-base.js");
4
5
  const LOCK_ID = 'ea586330-2c93-47c8-908d-981d9d270f9d';
5
6
  const LOCK_TIMEOUT_SECONDS = 60 * 60;
6
- class MysqlAdapter {
7
+ class MysqlAdapter extends dialect_adapter_base_js_1.DialectAdapterBase {
7
8
  get supportsTransactionalDdl() {
8
9
  return false;
9
10
  }
@@ -1,6 +1,6 @@
1
1
  import { Kysely } from '../../kysely.js';
2
- import { DialectAdapter } from '../dialect-adapter.js';
3
- export declare class PostgresAdapter implements DialectAdapter {
2
+ import { DialectAdapterBase } from '../dialect-adapter-base.js';
3
+ export declare class PostgresAdapter extends DialectAdapterBase {
4
4
  /**
5
5
  * Whether or not this dialect supports transactional DDL.
6
6
  *
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PostgresAdapter = void 0;
4
+ const dialect_adapter_base_js_1 = require("../dialect-adapter-base.js");
4
5
  // Random id for our transaction lock.
5
6
  const LOCK_ID = '3853314791062309107';
6
- class PostgresAdapter {
7
+ class PostgresAdapter extends dialect_adapter_base_js_1.DialectAdapterBase {
7
8
  get supportsTransactionalDdl() {
8
9
  return true;
9
10
  }
@@ -1,5 +1,5 @@
1
- import { DialectAdapter } from '../dialect-adapter.js';
2
- export declare class SqliteAdapter implements DialectAdapter {
1
+ import { DialectAdapterBase } from '../dialect-adapter-base.js';
2
+ export declare class SqliteAdapter implements DialectAdapterBase {
3
3
  /**
4
4
  * Whether or not this dialect supports transactional DDL.
5
5
  *
@@ -140,7 +140,7 @@ export * from './introspection/database-introspector.js';
140
140
  export * from './util/compilable.js';
141
141
  export * from './util/log.js';
142
142
  export { AnyColumn, UnknownRow, AnySelectQueryBuilder, } from './util/type-utils.js';
143
- export { SelectExpression, SelectExpressionOrList, } from './parser/select-parser.js';
143
+ export { SelectExpression, SelectExpressionOrList, Selection, } from './parser/select-parser.js';
144
144
  export { ReferenceExpression, ReferenceExpressionOrList, } from './parser/reference-parser.js';
145
145
  export { ValueExpression, ValueExpressionOrList, } from './parser/value-parser.js';
146
146
  export { FilterValueExpression, FilterValueExpressionOrList, ExistsExpression, FilterOperator, } from './parser/filter-parser.js';
@@ -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
  }