kysely 0.12.2 → 0.15.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.
- package/README.md +115 -31
- package/dist/cjs/{introspection → dialect}/database-introspector.d.ts +0 -0
- package/dist/cjs/{introspection → dialect}/database-introspector.js +0 -0
- package/dist/cjs/dialect/dialect.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +2 -2
- package/dist/cjs/dialect/mysql/mysql-adapter.js +2 -1
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.js +3 -3
- package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +2 -2
- package/dist/cjs/dialect/postgres/postgres-adapter.js +2 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.js +3 -3
- package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +2 -2
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +3 -3
- package/dist/cjs/index.d.ts +7 -4
- package/dist/cjs/index.js +6 -3
- package/dist/cjs/kysely.d.ts +8 -40
- package/dist/cjs/kysely.js +8 -40
- package/dist/cjs/migration/file-migration-provider.d.ts +52 -0
- package/dist/cjs/migration/file-migration-provider.js +52 -0
- package/dist/cjs/migration/migrator.d.ts +234 -0
- package/dist/cjs/migration/migrator.js +417 -0
- package/dist/cjs/operation-node/check-constraint-node.d.ts +1 -1
- package/dist/cjs/operation-node/check-constraint-node.js +2 -3
- package/dist/cjs/operation-node/column-definition-node.d.ts +8 -7
- package/dist/cjs/operation-node/column-definition-node.js +0 -4
- package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
- package/dist/cjs/operation-node/generated-node.d.ts +20 -0
- package/dist/cjs/operation-node/generated-node.js +31 -0
- package/dist/cjs/operation-node/on-conflict-node.d.ts +13 -2
- package/dist/cjs/operation-node/on-conflict-node.js +39 -1
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +13 -6
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -2
- package/dist/cjs/operation-node/operation-node-visitor.js +1 -1
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/parser/insert-values-parser.d.ts +4 -4
- package/dist/cjs/parser/insert-values-parser.js +2 -3
- package/dist/cjs/parser/reference-parser.d.ts +4 -3
- package/dist/cjs/parser/select-parser.d.ts +10 -4
- package/dist/cjs/parser/update-set-parser.d.ts +2 -1
- package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +2 -2
- package/dist/cjs/query-builder/delete-query-builder.d.ts +0 -3
- package/dist/cjs/query-builder/insert-query-builder.d.ts +78 -94
- package/dist/cjs/query-builder/insert-query-builder.js +145 -15
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +686 -0
- package/dist/cjs/query-builder/on-conflict-builder.js +323 -0
- package/dist/cjs/query-builder/returning-interface.d.ts +0 -3
- package/dist/cjs/query-builder/update-query-builder.d.ts +0 -3
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -2
- package/dist/cjs/query-compiler/default-query-compiler.js +37 -8
- package/dist/cjs/query-creator.d.ts +0 -2
- package/dist/cjs/query-creator.js +0 -2
- package/dist/cjs/schema/alter-table-builder.d.ts +41 -9
- package/dist/cjs/schema/alter-table-builder.js +45 -9
- package/dist/cjs/schema/column-definition-builder.d.ts +40 -8
- package/dist/cjs/schema/column-definition-builder.js +21 -8
- package/dist/cjs/schema/create-index-builder.d.ts +3 -2
- package/dist/cjs/schema/create-index-builder.js +2 -2
- package/dist/cjs/schema/create-table-builder.d.ts +3 -2
- package/dist/cjs/schema/create-table-builder.js +2 -2
- package/dist/cjs/util/column-type.d.ts +165 -0
- package/dist/cjs/util/column-type.js +2 -0
- package/dist/cjs/util/private-symbols.d.ts +5 -0
- package/dist/cjs/util/private-symbols.js +8 -0
- package/dist/cjs/util/type-utils.d.ts +6 -42
- package/dist/esm/{introspection → dialect}/database-introspector.d.ts +0 -0
- package/dist/esm/{introspection → dialect}/database-introspector.js +0 -0
- package/dist/esm/dialect/dialect.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-adapter.d.ts +2 -2
- package/dist/esm/dialect/mysql/mysql-adapter.js +2 -1
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-introspector.js +1 -1
- package/dist/esm/dialect/postgres/postgres-adapter.d.ts +2 -2
- package/dist/esm/dialect/postgres/postgres-adapter.js +2 -1
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
- package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +2 -2
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -1
- package/dist/esm/index.d.ts +7 -4
- package/dist/esm/index.js +6 -3
- package/dist/esm/kysely.d.ts +8 -40
- package/dist/esm/kysely.js +8 -40
- package/dist/esm/migration/file-migration-provider.d.ts +52 -0
- package/dist/esm/migration/file-migration-provider.js +48 -0
- package/dist/esm/migration/migrator.d.ts +234 -0
- package/dist/esm/migration/migrator.js +413 -0
- package/dist/esm/operation-node/check-constraint-node.d.ts +1 -1
- package/dist/esm/operation-node/check-constraint-node.js +2 -3
- package/dist/esm/operation-node/column-definition-node.d.ts +8 -7
- package/dist/esm/operation-node/column-definition-node.js +0 -4
- package/dist/esm/operation-node/data-type-node.d.ts +1 -1
- package/dist/esm/operation-node/generated-node.d.ts +20 -0
- package/dist/esm/operation-node/generated-node.js +28 -0
- package/dist/esm/operation-node/on-conflict-node.d.ts +13 -2
- package/dist/esm/operation-node/on-conflict-node.js +39 -1
- package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -2
- package/dist/esm/operation-node/operation-node-transformer.js +13 -6
- package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -2
- package/dist/esm/operation-node/operation-node-visitor.js +1 -1
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/parser/insert-values-parser.d.ts +4 -4
- package/dist/esm/parser/insert-values-parser.js +2 -3
- package/dist/esm/parser/reference-parser.d.ts +4 -3
- package/dist/esm/parser/select-parser.d.ts +10 -4
- package/dist/esm/parser/update-set-parser.d.ts +2 -1
- package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +2 -2
- package/dist/esm/query-builder/delete-query-builder.d.ts +0 -3
- package/dist/esm/query-builder/insert-query-builder.d.ts +78 -94
- package/dist/esm/query-builder/insert-query-builder.js +145 -15
- package/dist/esm/query-builder/on-conflict-builder.d.ts +686 -0
- package/dist/esm/query-builder/on-conflict-builder.js +317 -0
- package/dist/esm/query-builder/returning-interface.d.ts +0 -3
- package/dist/esm/query-builder/update-query-builder.d.ts +0 -3
- package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -2
- package/dist/esm/query-compiler/default-query-compiler.js +37 -8
- package/dist/esm/query-creator.d.ts +0 -2
- package/dist/esm/query-creator.js +0 -2
- package/dist/esm/schema/alter-table-builder.d.ts +41 -9
- package/dist/esm/schema/alter-table-builder.js +45 -9
- package/dist/esm/schema/column-definition-builder.d.ts +40 -8
- package/dist/esm/schema/column-definition-builder.js +21 -8
- package/dist/esm/schema/create-index-builder.d.ts +3 -2
- package/dist/esm/schema/create-index-builder.js +2 -2
- package/dist/esm/schema/create-table-builder.d.ts +3 -2
- package/dist/esm/schema/create-table-builder.js +2 -2
- package/dist/esm/util/column-type.d.ts +165 -0
- package/dist/esm/util/column-type.js +1 -0
- package/dist/esm/util/private-symbols.d.ts +5 -0
- package/dist/esm/util/private-symbols.js +5 -0
- package/dist/esm/util/type-utils.d.ts +6 -42
- package/package.json +1 -1
- package/dist/cjs/migration/migration.d.ts +0 -66
- package/dist/cjs/migration/migration.js +0 -187
- package/dist/cjs/operation-node/generated-always-as-node.d.ts +0 -16
- package/dist/cjs/operation-node/generated-always-as-node.js +0 -25
- package/dist/cjs/parser/on-conflict-parser.d.ts +0 -10
- package/dist/cjs/parser/on-conflict-parser.js +0 -35
- package/dist/cjs/util/generated-placeholder.d.ts +0 -3
- package/dist/cjs/util/generated-placeholder.js +0 -11
- package/dist/esm/migration/migration.d.ts +0 -66
- package/dist/esm/migration/migration.js +0 -183
- package/dist/esm/operation-node/generated-always-as-node.d.ts +0 -16
- package/dist/esm/operation-node/generated-always-as-node.js +0 -22
- package/dist/esm/parser/on-conflict-parser.d.ts +0 -10
- package/dist/esm/parser/on-conflict-parser.js +0 -30
- package/dist/esm/util/generated-placeholder.d.ts +0 -3
- package/dist/esm/util/generated-placeholder.js +0 -7
package/dist/cjs/kysely.js
CHANGED
|
@@ -4,9 +4,7 @@ exports.TransactionBuilder = exports.ConnectionBuilder = exports.isKyselyProps =
|
|
|
4
4
|
const schema_js_1 = require("./schema/schema.js");
|
|
5
5
|
const dynamic_js_1 = require("./dynamic/dynamic.js");
|
|
6
6
|
const default_connection_provider_js_1 = require("./driver/default-connection-provider.js");
|
|
7
|
-
const migration_js_1 = require("./migration/migration.js");
|
|
8
7
|
const query_creator_js_1 = require("./query-creator.js");
|
|
9
|
-
const generated_placeholder_js_1 = require("./util/generated-placeholder.js");
|
|
10
8
|
const default_query_executor_js_1 = require("./query-executor/default-query-executor.js");
|
|
11
9
|
const object_utils_js_1 = require("./util/object-utils.js");
|
|
12
10
|
const runtime_driver_js_1 = require("./driver/runtime-driver.js");
|
|
@@ -16,6 +14,7 @@ const prevent_await_js_1 = require("./util/prevent-await.js");
|
|
|
16
14
|
const parse_context_js_1 = require("./parser/parse-context.js");
|
|
17
15
|
const function_builder_js_1 = require("./query-builder/function-builder.js");
|
|
18
16
|
const log_js_1 = require("./util/log.js");
|
|
17
|
+
const private_symbols_js_1 = require("./util/private-symbols.js");
|
|
19
18
|
/**
|
|
20
19
|
* The main Kysely class.
|
|
21
20
|
*
|
|
@@ -90,12 +89,6 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
90
89
|
get schema() {
|
|
91
90
|
return new schema_js_1.SchemaModule(this.#props.executor);
|
|
92
91
|
}
|
|
93
|
-
/**
|
|
94
|
-
* Returns the {@link MigrationModule} module for managing and running migrations.
|
|
95
|
-
*/
|
|
96
|
-
get migration() {
|
|
97
|
-
return new migration_js_1.MigrationModule(this, this.#props.parseContext.adapter);
|
|
98
|
-
}
|
|
99
92
|
/**
|
|
100
93
|
* Returns a the {@link DynamicModule} module.
|
|
101
94
|
*
|
|
@@ -111,33 +104,6 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
111
104
|
get introspection() {
|
|
112
105
|
return this.#props.dialect.createIntrospector(this.withoutPlugins());
|
|
113
106
|
}
|
|
114
|
-
/**
|
|
115
|
-
* A value to be used in place of columns that are generated in the database
|
|
116
|
-
* when inserting rows.
|
|
117
|
-
*
|
|
118
|
-
* ### Examples
|
|
119
|
-
*
|
|
120
|
-
* In this example the `Person` table has non-null properties `id` and `created_at`
|
|
121
|
-
* which are both automatically genereted by the database. Since their types are
|
|
122
|
-
* `number` and `string` respectively instead of `number | null` and `string | null`
|
|
123
|
-
* the `values` method requires you to give a value for them. the `generated`
|
|
124
|
-
* placeholder can be used in these cases.
|
|
125
|
-
*
|
|
126
|
-
* ```ts
|
|
127
|
-
* await db.insertInto('person')
|
|
128
|
-
* .values({
|
|
129
|
-
* id: db.generated,
|
|
130
|
-
* created_at: db.generated,
|
|
131
|
-
* first_name: 'Jennifer',
|
|
132
|
-
* last_name: 'Aniston',
|
|
133
|
-
* gender: 'female'
|
|
134
|
-
* })
|
|
135
|
-
* .execute()
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
get generated() {
|
|
139
|
-
return generated_placeholder_js_1.GENERATED_PLACEHOLDER;
|
|
140
|
-
}
|
|
141
107
|
/**
|
|
142
108
|
* Returns a {@link FunctionBuilder} that can be used to write type safe function
|
|
143
109
|
* calls.
|
|
@@ -189,7 +155,6 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
189
155
|
* const catto = await db.transaction().execute(async (trx) => {
|
|
190
156
|
* const jennifer = await trx.insertInto('person')
|
|
191
157
|
* .values({
|
|
192
|
-
* id: db.generated,
|
|
193
158
|
* first_name: 'Jennifer',
|
|
194
159
|
* last_name: 'Aniston',
|
|
195
160
|
* })
|
|
@@ -200,7 +165,6 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
200
165
|
*
|
|
201
166
|
* return await trx.insertInto('pet')
|
|
202
167
|
* .values({
|
|
203
|
-
* id: db.generated,
|
|
204
168
|
* user_id: jennifer.id,
|
|
205
169
|
* name: 'Catto',
|
|
206
170
|
* species: 'cat'
|
|
@@ -311,6 +275,13 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
311
275
|
get isTransaction() {
|
|
312
276
|
return false;
|
|
313
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* @internal
|
|
280
|
+
* @private
|
|
281
|
+
*/
|
|
282
|
+
get [private_symbols_js_1.PRIVATE_ADAPTER]() {
|
|
283
|
+
return this.#props.parseContext.adapter;
|
|
284
|
+
}
|
|
314
285
|
}
|
|
315
286
|
exports.Kysely = Kysely;
|
|
316
287
|
class Transaction extends Kysely {
|
|
@@ -325,9 +296,6 @@ class Transaction extends Kysely {
|
|
|
325
296
|
get isTransaction() {
|
|
326
297
|
return true;
|
|
327
298
|
}
|
|
328
|
-
get migration() {
|
|
329
|
-
throw new Error('the migration module is not available for a transaction. Use the main Kysely instance to run migrations');
|
|
330
|
-
}
|
|
331
299
|
transaction() {
|
|
332
300
|
throw new Error('calling the transaction method for a Transaction is not supported');
|
|
333
301
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Migration, MigrationProvider } from './migrator.js';
|
|
2
|
+
/**
|
|
3
|
+
* A function that takes a folder path and returns names of the
|
|
4
|
+
* files insider that folder.
|
|
5
|
+
*/
|
|
6
|
+
export declare type ReadDir = (migrationFolderPath: string) => Promise<string[]>;
|
|
7
|
+
/**
|
|
8
|
+
* A function that combines two file path parts into a single file path.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* joinPath('/some/path', 'file.ts')
|
|
12
|
+
* // ==> '/some/path/file.ts'
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare type JoinPath = (part1: string, part2: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Reads all migrations from a folder.
|
|
18
|
+
*
|
|
19
|
+
* You need to provide a function for reading files in a folder and a function for
|
|
20
|
+
* joining file paths together. The API is like this to make Kysely independent of
|
|
21
|
+
* node.js or any other environment. See the example usage on node.js below.
|
|
22
|
+
*
|
|
23
|
+
* ### Examples
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import * as path from 'path'
|
|
27
|
+
* import * as fs from 'fs/promises'
|
|
28
|
+
*
|
|
29
|
+
* new FileMigrationProvider(
|
|
30
|
+
* fs.readdir,
|
|
31
|
+
* path.join,
|
|
32
|
+
* path.join(__dirname, 'migrations')
|
|
33
|
+
* )
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare class FileMigrationProvider implements MigrationProvider {
|
|
37
|
+
#private;
|
|
38
|
+
constructor(readDir: ReadDir, joinPath: JoinPath, migrationFolderPath: string);
|
|
39
|
+
/**
|
|
40
|
+
* Returns all migrations, old and new.
|
|
41
|
+
*
|
|
42
|
+
* For example if you have your migrations in a folder as separate files,
|
|
43
|
+
* you can use the {@link FileMigrationProvider} that implements this
|
|
44
|
+
* method to return all migrations in a folder.
|
|
45
|
+
*
|
|
46
|
+
* The keys of the returned object are migration names and values are the
|
|
47
|
+
* migrations. The order of the migrations is determined by the alphabetical
|
|
48
|
+
* order of the migration names. The items in the object don't need to be
|
|
49
|
+
* sorted, they are sorted by Kysely.
|
|
50
|
+
*/
|
|
51
|
+
getMigrations(): Promise<Record<string, Migration>>;
|
|
52
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileMigrationProvider = void 0;
|
|
4
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
/**
|
|
6
|
+
* Reads all migrations from a folder.
|
|
7
|
+
*
|
|
8
|
+
* You need to provide a function for reading files in a folder and a function for
|
|
9
|
+
* joining file paths together. The API is like this to make Kysely independent of
|
|
10
|
+
* node.js or any other environment. See the example usage on node.js below.
|
|
11
|
+
*
|
|
12
|
+
* ### Examples
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import * as path from 'path'
|
|
16
|
+
* import * as fs from 'fs/promises'
|
|
17
|
+
*
|
|
18
|
+
* new FileMigrationProvider(
|
|
19
|
+
* fs.readdir,
|
|
20
|
+
* path.join,
|
|
21
|
+
* path.join(__dirname, 'migrations')
|
|
22
|
+
* )
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
class FileMigrationProvider {
|
|
26
|
+
#readDir;
|
|
27
|
+
#joinPath;
|
|
28
|
+
#migrationFolderPath;
|
|
29
|
+
constructor(readDir, joinPath, migrationFolderPath) {
|
|
30
|
+
this.#readDir = readDir;
|
|
31
|
+
this.#joinPath = joinPath;
|
|
32
|
+
this.#migrationFolderPath = migrationFolderPath;
|
|
33
|
+
}
|
|
34
|
+
async getMigrations() {
|
|
35
|
+
const migrations = {};
|
|
36
|
+
const files = await this.#readDir(this.#migrationFolderPath);
|
|
37
|
+
for (const fileName of files) {
|
|
38
|
+
if ((fileName.endsWith('.js') || fileName.endsWith('.ts')) &&
|
|
39
|
+
!fileName.endsWith('.d.ts')) {
|
|
40
|
+
const migration = await Promise.resolve().then(() => require(this.#joinPath(this.#migrationFolderPath, fileName)));
|
|
41
|
+
if (isMigration(migration)) {
|
|
42
|
+
migrations[fileName.substring(0, fileName.length - 3)] = migration;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return migrations;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.FileMigrationProvider = FileMigrationProvider;
|
|
50
|
+
function isMigration(obj) {
|
|
51
|
+
return (0, object_utils_js_1.isObject)(obj) && (0, object_utils_js_1.isFunction)(obj.up);
|
|
52
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { Kysely } from '../kysely.js';
|
|
2
|
+
export declare const MIGRATION_TABLE = "kysely_migration";
|
|
3
|
+
export declare const MIGRATION_LOCK_TABLE = "kysely_migration_lock";
|
|
4
|
+
export declare const MIGRATION_LOCK_ID = "migration_lock";
|
|
5
|
+
export declare const NO_MIGRATIONS: NoMigrations;
|
|
6
|
+
export interface Migration {
|
|
7
|
+
up(db: Kysely<any>): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* An optional down method.
|
|
10
|
+
*
|
|
11
|
+
* If you don't provide a down method, the migration is skipped when
|
|
12
|
+
* migrating down.
|
|
13
|
+
*/
|
|
14
|
+
down?(db: Kysely<any>): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A class for running migrations.
|
|
18
|
+
*
|
|
19
|
+
* ### Example
|
|
20
|
+
*
|
|
21
|
+
* This example uses the {@link FileMigrationProvider} that reads migrations
|
|
22
|
+
* files from a single folder. You can easily implement your own
|
|
23
|
+
* {@link MigrationProvider} if you want to provide migrations some
|
|
24
|
+
* other way.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* const migrator = new Migrator({
|
|
28
|
+
* db,
|
|
29
|
+
* // The constructor signature of FileMigrationProvider is a
|
|
30
|
+
* // bit weird to keep Kysely independent of node.js or any
|
|
31
|
+
* // other environment.
|
|
32
|
+
* provider: new FileMigrationProvider(
|
|
33
|
+
* fs.readdir,
|
|
34
|
+
* path.join,
|
|
35
|
+
* // Path to the folder that contains all your migrations.
|
|
36
|
+
* 'migrations'
|
|
37
|
+
* )
|
|
38
|
+
* })
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare class Migrator {
|
|
42
|
+
#private;
|
|
43
|
+
constructor(props: MigratorProps);
|
|
44
|
+
/**
|
|
45
|
+
* Runs all migrations that have not yet been run.
|
|
46
|
+
*
|
|
47
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
48
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
49
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
50
|
+
* were executed and which failed. See the examples below.
|
|
51
|
+
*
|
|
52
|
+
* This method goes through all possible migrations provided by the provider and runs the
|
|
53
|
+
* ones whose names come alphabetically after the last migration that has been run. If the
|
|
54
|
+
* list of executed migrations doesn't match the beginning of the list of possible migrations
|
|
55
|
+
* an error is thrown.
|
|
56
|
+
*
|
|
57
|
+
* ### Examples
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import * as path from 'path'
|
|
61
|
+
* import * as fs from 'fs/promises'
|
|
62
|
+
*
|
|
63
|
+
* const db = new Kysely<Database>({
|
|
64
|
+
* dialect: new PostgresDialect({
|
|
65
|
+
* host: 'localhost',
|
|
66
|
+
* database: 'kysely_test',
|
|
67
|
+
* }),
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* const migrator = new Migrator({
|
|
71
|
+
* db,
|
|
72
|
+
* // The constructor signature of FileMigrationProvider is a
|
|
73
|
+
* // bit weird to keep Kysely independent of node.js or any
|
|
74
|
+
* // other environment.
|
|
75
|
+
* provider: new FileMigrationProvider(
|
|
76
|
+
* fs.readdir,
|
|
77
|
+
* path.join,
|
|
78
|
+
* // Path to the folder that contains all your migrations.
|
|
79
|
+
* 'migrations'
|
|
80
|
+
* )
|
|
81
|
+
* })
|
|
82
|
+
*
|
|
83
|
+
* const { error, results } = await migrator.migrateToLatest()
|
|
84
|
+
*
|
|
85
|
+
* results?.forEach((it) => {
|
|
86
|
+
* if (it.status === 'Success') {
|
|
87
|
+
* console.log(`migration "${it.migrationName}" was executed successfully`)
|
|
88
|
+
* } else if (it.status === 'Error') {
|
|
89
|
+
* console.error(`failed to execute migration "${it.migrationName}"`)
|
|
90
|
+
* }
|
|
91
|
+
* })
|
|
92
|
+
*
|
|
93
|
+
* if (error) {
|
|
94
|
+
* console.error('failed to run `migrateToLatest`')
|
|
95
|
+
* console.error(error)
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
migrateToLatest(): Promise<MigrationResultSet>;
|
|
100
|
+
/**
|
|
101
|
+
* Migrate up/down to a specific migration.
|
|
102
|
+
*
|
|
103
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
104
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
105
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
106
|
+
* were executed and which failed.
|
|
107
|
+
*
|
|
108
|
+
* ### Examples
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* await migrator.migrateTo('some_migration')
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* If you specify the name of the first migration, this method migrates
|
|
115
|
+
* down to the first migration, but doesn't run the `down` method of
|
|
116
|
+
* the first migration. In case you want to migrate all the way down,
|
|
117
|
+
* you can use a special constant `NO_MIGRATIONS`:
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* await migrator.migrateTo(NO_MIGRATIONS)
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
migrateTo(targetMigrationName: string | NoMigrations): Promise<MigrationResultSet>;
|
|
124
|
+
/**
|
|
125
|
+
* Migrate one step up.
|
|
126
|
+
*
|
|
127
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
128
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
129
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
130
|
+
* were executed and which failed.
|
|
131
|
+
*
|
|
132
|
+
* ### Examples
|
|
133
|
+
*
|
|
134
|
+
* ```ts
|
|
135
|
+
* await migrator.migrateUp()
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
migrateUp(): Promise<MigrationResultSet>;
|
|
139
|
+
/**
|
|
140
|
+
* Migrate one step down.
|
|
141
|
+
*
|
|
142
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
143
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
144
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
145
|
+
* were executed and which failed.
|
|
146
|
+
*
|
|
147
|
+
* ### Examples
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* await migrator.migrateDown()
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
migrateDown(): Promise<MigrationResultSet>;
|
|
154
|
+
}
|
|
155
|
+
export interface MigratorProps {
|
|
156
|
+
readonly db: Kysely<any>;
|
|
157
|
+
readonly provider: MigrationProvider;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* All migration methods ({@link MigrationModule.migrateTo | migrateTo},
|
|
161
|
+
* {@link MigrationModule.migrateToLatest | migrateToLatest} etc.) never
|
|
162
|
+
* throw but return this object instead.
|
|
163
|
+
*/
|
|
164
|
+
export interface MigrationResultSet {
|
|
165
|
+
/**
|
|
166
|
+
* This is defined if something went wrong.
|
|
167
|
+
*
|
|
168
|
+
* An error may have occurred in one of the migrations in which case the
|
|
169
|
+
* {@link results} list contains an item with `status === 'Error'` to
|
|
170
|
+
* indicate which migration failed.
|
|
171
|
+
*
|
|
172
|
+
* An error may also have occurred before Kysely was able to figure out
|
|
173
|
+
* which migrations should be executed, in which case the {@link results}
|
|
174
|
+
* list is undefined.
|
|
175
|
+
*/
|
|
176
|
+
readonly error?: unknown;
|
|
177
|
+
/**
|
|
178
|
+
* {@link MigrationResult} for each individual migration that was supposed
|
|
179
|
+
* to be executed by the operation.
|
|
180
|
+
*
|
|
181
|
+
* If all went well, each result's `status` is `Success`. If some migration
|
|
182
|
+
* failed, the failed migration's result's `status` is `Error` and all
|
|
183
|
+
* results after that one have `status` ´NotExecuted`.
|
|
184
|
+
*
|
|
185
|
+
* This property can be undefined if an error occurred before Kysely was
|
|
186
|
+
* able to figure out which migrations should be executed.
|
|
187
|
+
*
|
|
188
|
+
* If this list is empty, there were no migrations to execute.
|
|
189
|
+
*/
|
|
190
|
+
readonly results?: MigrationResult[];
|
|
191
|
+
}
|
|
192
|
+
export interface MigrationResult {
|
|
193
|
+
readonly migrationName: string;
|
|
194
|
+
/**
|
|
195
|
+
* The direction in which this migration was executed.
|
|
196
|
+
*/
|
|
197
|
+
readonly direction: 'Up' | 'Down';
|
|
198
|
+
/**
|
|
199
|
+
* The execution status.
|
|
200
|
+
*
|
|
201
|
+
* - `Success` means the migration was successfully executed. Note that
|
|
202
|
+
* if any of the later migrations in the {@link MigrationResult.results}
|
|
203
|
+
* list failed (have status `Error`) AND the dialect supports transactional
|
|
204
|
+
* DDL, even the successfull migrations were rolled back.
|
|
205
|
+
*
|
|
206
|
+
* - `Error` means the migration failed. In this case the
|
|
207
|
+
* {@link MigrationResult.error} contains the error.
|
|
208
|
+
*
|
|
209
|
+
* - `NotExecuted` means that the migration was supposed to be executed
|
|
210
|
+
* but wasn't because an earlier migration failed.
|
|
211
|
+
*/
|
|
212
|
+
readonly status: 'Success' | 'Error' | 'NotExecuted';
|
|
213
|
+
}
|
|
214
|
+
export interface MigrationProvider {
|
|
215
|
+
/**
|
|
216
|
+
* Returns all migrations, old and new.
|
|
217
|
+
*
|
|
218
|
+
* For example if you have your migrations in a folder as separate files,
|
|
219
|
+
* you can use the {@link FileMigrationProvider} that implements this
|
|
220
|
+
* method to return all migrations in a folder.
|
|
221
|
+
*
|
|
222
|
+
* The keys of the returned object are migration names and values are the
|
|
223
|
+
* migrations. The order of the migrations is determined by the alphabetical
|
|
224
|
+
* order of the migration names. The items in the object don't need to be
|
|
225
|
+
* sorted, they are sorted by Kysely.
|
|
226
|
+
*/
|
|
227
|
+
getMigrations(): Promise<Record<string, Migration>>;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Type for the {@link NO_MIGRATIONS} constant. Never create one of these.
|
|
231
|
+
*/
|
|
232
|
+
export interface NoMigrations {
|
|
233
|
+
readonly __noMigrations__: true;
|
|
234
|
+
}
|