kysely 0.14.0 → 0.15.2

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 (73) hide show
  1. package/README.md +95 -39
  2. package/dist/cjs/dialect/mysql/mysql-introspector.js +3 -3
  3. package/dist/cjs/dialect/postgres/postgres-introspector.js +3 -3
  4. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +3 -3
  5. package/dist/cjs/index-nodeless.d.ts +148 -0
  6. package/dist/cjs/index-nodeless.js +149 -0
  7. package/dist/cjs/index.d.ts +6 -150
  8. package/dist/cjs/index.js +6 -139
  9. package/dist/cjs/kysely.d.ts +7 -39
  10. package/dist/cjs/kysely.js +8 -40
  11. package/dist/cjs/migration/file-migration-provider.d.ts +52 -0
  12. package/dist/cjs/migration/file-migration-provider.js +52 -0
  13. package/dist/cjs/migration/{migration.d.ts → migrator.d.ts} +159 -134
  14. package/dist/cjs/migration/{migration.js → migrator.js} +166 -57
  15. package/dist/cjs/parser/insert-values-parser.d.ts +4 -4
  16. package/dist/cjs/parser/insert-values-parser.js +2 -3
  17. package/dist/cjs/parser/reference-parser.d.ts +4 -3
  18. package/dist/cjs/parser/select-parser.d.ts +10 -4
  19. package/dist/cjs/parser/update-set-parser.d.ts +2 -1
  20. package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -4
  21. package/dist/cjs/query-builder/insert-query-builder.d.ts +5 -21
  22. package/dist/cjs/query-builder/insert-query-builder.js +2 -7
  23. package/dist/cjs/query-builder/on-conflict-builder.d.ts +5 -3
  24. package/dist/cjs/query-builder/on-conflict-builder.js +2 -2
  25. package/dist/cjs/query-builder/returning-interface.d.ts +2 -4
  26. package/dist/cjs/query-builder/select-query-builder.d.ts +1 -1
  27. package/dist/cjs/query-builder/update-query-builder.d.ts +2 -4
  28. package/dist/cjs/query-creator.d.ts +0 -2
  29. package/dist/cjs/query-creator.js +0 -2
  30. package/dist/cjs/util/column-type.d.ts +165 -0
  31. package/dist/cjs/util/column-type.js +2 -0
  32. package/dist/cjs/util/private-symbols.d.ts +5 -0
  33. package/dist/cjs/util/private-symbols.js +8 -0
  34. package/dist/cjs/util/type-utils.d.ts +6 -42
  35. package/dist/esm/dialect/mysql/mysql-introspector.js +1 -1
  36. package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
  37. package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -1
  38. package/dist/esm/index-nodeless.d.ts +148 -0
  39. package/dist/esm/index-nodeless.js +137 -0
  40. package/dist/esm/index.d.ts +6 -150
  41. package/dist/esm/index.js +6 -139
  42. package/dist/esm/kysely.d.ts +7 -39
  43. package/dist/esm/kysely.js +8 -40
  44. package/dist/esm/migration/file-migration-provider.d.ts +52 -0
  45. package/dist/esm/migration/file-migration-provider.js +48 -0
  46. package/dist/esm/migration/{migration.d.ts → migrator.d.ts} +159 -134
  47. package/dist/esm/migration/{migration.js → migrator.js} +165 -56
  48. package/dist/esm/parser/insert-values-parser.d.ts +4 -4
  49. package/dist/esm/parser/insert-values-parser.js +2 -3
  50. package/dist/esm/parser/reference-parser.d.ts +4 -3
  51. package/dist/esm/parser/select-parser.d.ts +10 -4
  52. package/dist/esm/parser/update-set-parser.d.ts +2 -1
  53. package/dist/esm/query-builder/delete-query-builder.d.ts +2 -4
  54. package/dist/esm/query-builder/insert-query-builder.d.ts +5 -21
  55. package/dist/esm/query-builder/insert-query-builder.js +1 -6
  56. package/dist/esm/query-builder/on-conflict-builder.d.ts +5 -3
  57. package/dist/esm/query-builder/on-conflict-builder.js +2 -2
  58. package/dist/esm/query-builder/returning-interface.d.ts +2 -4
  59. package/dist/esm/query-builder/select-query-builder.d.ts +1 -1
  60. package/dist/esm/query-builder/update-query-builder.d.ts +2 -4
  61. package/dist/esm/query-creator.d.ts +0 -2
  62. package/dist/esm/query-creator.js +0 -2
  63. package/dist/esm/util/column-type.d.ts +165 -0
  64. package/dist/esm/util/column-type.js +1 -0
  65. package/dist/esm/util/private-symbols.d.ts +5 -0
  66. package/dist/esm/util/private-symbols.js +5 -0
  67. package/dist/esm/util/type-utils.d.ts +6 -42
  68. package/package.json +1 -1
  69. package/test.js +3 -0
  70. package/dist/cjs/util/generated-placeholder.d.ts +0 -3
  71. package/dist/cjs/util/generated-placeholder.js +0 -11
  72. package/dist/esm/util/generated-placeholder.d.ts +0 -3
  73. package/dist/esm/util/generated-placeholder.js +0 -7
@@ -1,101 +1,86 @@
1
1
  import { Kysely } from '../kysely.js';
2
- import { DialectAdapter } from '../dialect/dialect-adapter.js';
3
2
  export declare const MIGRATION_TABLE = "kysely_migration";
4
3
  export declare const MIGRATION_LOCK_TABLE = "kysely_migration_lock";
5
4
  export declare const MIGRATION_LOCK_ID = "migration_lock";
6
5
  export declare const NO_MIGRATIONS: NoMigrations;
7
- export declare type MigrationsOrFolderPath = Record<string, Migration> | string;
8
6
  export interface Migration {
9
7
  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
8
  /**
25
- * This is defined if something went wrong.
9
+ * An optional down method.
26
10
  *
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.
11
+ * If you don't provide a down method, the migration is skipped when
12
+ * migrating down.
48
13
  */
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';
14
+ down?(db: Kysely<any>): Promise<void>;
72
15
  }
73
- export declare class MigrationModule {
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 {
74
42
  #private;
75
- constructor(db: Kysely<any>, adapter: DialectAdapter);
43
+ constructor(props: MigratorProps);
76
44
  /**
77
45
  * Runs all migrations that have not yet been run.
78
46
  *
79
- * The only argument must either be a file path to the folder that contains all migrations
80
- * OR an object that contains all migrations (not just the ones that need to be executed).
81
- * The keys in the object must be the unique migration names.
82
- *
83
47
  * This method returns a {@link MigrationResultSet} instance and _never_ throws.
84
48
  * {@link MigrationResultSet.error} holds the error if something went wrong.
85
49
  * {@link MigrationResultSet.results} contains information about which migrations
86
50
  * were executed and which failed. See the examples below.
87
51
  *
88
- * This method goes through all possible migrations (passed as the argument) and runs the
89
- * ones whose names are alphabetically after the last migration that has been run. If the
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
90
54
  * list of executed migrations doesn't match the beginning of the list of possible migrations
91
55
  * an error is thrown.
92
56
  *
93
57
  * ### Examples
94
58
  *
95
59
  * ```ts
96
- * const { error, results } = await db.migration.migrateToLatest(
97
- * path.join(__dirname, 'migrations')
98
- * )
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()
99
84
  *
100
85
  * results?.forEach((it) => {
101
86
  * if (it.status === 'Success') {
@@ -110,100 +95,140 @@ export declare class MigrationModule {
110
95
  * console.error(error)
111
96
  * }
112
97
  * ```
113
- *
114
- * In the next example, we use a record of migration objects instead of file folder path.
115
- * The keys in the object are migration names that can be anything you want. The order of
116
- * the migrations is determined based on the alphabetical order of the keys in the record.
117
- * This version of the `migrateToLatest` method can be useful if you are using a bundler
118
- * like webpack or esbuild.
119
- *
120
- * ```ts
121
- * await db.migration.migrateToLatest({
122
- * migration1: {
123
- * async up(db: Kysely<any>): Promise<void> {
124
- * ...
125
- * },
126
- *
127
- * async down(db: Kysely<any>): Promise<void> {
128
- * ...
129
- * }
130
- * },
131
- *
132
- * migration2: {
133
- * async up(db: Kysely<any>): Promise<void> {
134
- * ...
135
- * },
136
- *
137
- * async down(db: Kysely<any>): Promise<void> {
138
- * ...
139
- * }
140
- * },
141
- *
142
- * ...
143
- * })
144
- * ```
145
98
  */
146
- migrateToLatest(migrationsFolderPath: string): Promise<MigrationResultSet>;
147
- migrateToLatest(allMigrations: Record<string, Migration>): Promise<MigrationResultSet>;
99
+ migrateToLatest(): Promise<MigrationResultSet>;
148
100
  /**
149
101
  * Migrate up/down to a specific migration.
150
102
  *
151
- * Otherwise works just like {@link migrateToLatest}. The first argument
152
- * behaves the same, the output is the same etc.
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.
153
107
  *
154
108
  * ### Examples
155
109
  *
156
110
  * ```ts
157
- * await db.migration.migrateTo(
158
- * path.join(__dirname, 'migrations'),
159
- * 'some_migration'
160
- * )
111
+ * await migrator.migrateTo('some_migration')
161
112
  * ```
162
113
  *
163
114
  * 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
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,
166
117
  * you can use a special constant `NO_MIGRATIONS`:
167
118
  *
168
119
  * ```ts
169
- * await db.migration.migrateTo(
170
- * path.join(__dirname, 'migrations'),
171
- * NO_MIGRATIONS,
172
- * )
120
+ * await migrator.migrateTo(NO_MIGRATIONS)
173
121
  * ```
174
122
  */
175
- migrateTo(migrationsFolderPath: string, targetMigrationName: string | NoMigrations): Promise<MigrationResultSet>;
176
- migrateTo(allMigrations: Record<string, Migration>, targetMigrationName: string | NoMigrations): Promise<MigrationResultSet>;
123
+ migrateTo(targetMigrationName: string | NoMigrations): Promise<MigrationResultSet>;
177
124
  /**
178
125
  * Migrate one step up.
179
126
  *
180
- * Otherwise works just like {@link migrateToLatest}. The only argument
181
- * behaves the same, the output is the same etc.
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.
182
131
  *
183
132
  * ### Examples
184
133
  *
185
134
  * ```ts
186
- * await db.migration.migrateUp(
187
- * path.join(__dirname, 'migrations'),
188
- * )
135
+ * await migrator.migrateUp()
189
136
  * ```
190
137
  */
191
- migrateUp(migrationsFolderPath: string): Promise<MigrationResultSet>;
192
- migrateUp(allMigrations: Record<string, Migration>): Promise<MigrationResultSet>;
138
+ migrateUp(): Promise<MigrationResultSet>;
193
139
  /**
194
140
  * Migrate one step down.
195
141
  *
196
- * Otherwise works just like {@link migrateToLatest}. The only argument
197
- * behaves the same, the output is the same etc.
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.
198
146
  *
199
147
  * ### Examples
200
148
  *
201
149
  * ```ts
202
- * await db.migration.migrateDown(
203
- * path.join(__dirname, 'migrations'),
204
- * )
150
+ * await migrator.migrateDown()
205
151
  * ```
206
152
  */
207
- migrateDown(migrationsFolderPath: string): Promise<MigrationResultSet>;
208
- migrateDown(allMigrations: Record<string, Migration>): Promise<MigrationResultSet>;
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;
209
234
  }