1 | import type * as typeFest from 'type-fest';
|
2 | import type { UmzugStorage } from './storage';
|
3 | /**
|
4 | * Create a type that has mutually exclusive keys.
|
5 | * Wrapper for @see `import('type-fest').MergeExclusive` that works for three types
|
6 | */
|
7 | export type MergeExclusive<A, B, C> = typeFest.MergeExclusive<A, typeFest.MergeExclusive<B, C>>;
|
8 | export type Promisable<T> = T | PromiseLike<T>;
|
9 | export type LogFn = (message: Record<string, unknown>) => void;
|
10 | /** Constructor options for the Umzug class */
|
11 | export type UmzugOptions<Ctx extends {} = Record<string, unknown>> = {
|
12 | /** The migrations that the Umzug instance should perform */
|
13 | migrations: InputMigrations<Ctx>;
|
14 | /** A logging function. Pass `console` to use stdout, or pass in your own logger. Pass `undefined` explicitly to disable logging. */
|
15 | logger: Record<'info' | 'warn' | 'error' | 'debug', LogFn> | undefined;
|
16 | /** The storage implementation. By default, `JSONStorage` will be used */
|
17 | storage?: UmzugStorage<Ctx>;
|
18 | /** An optional context object, which will be passed to each migration function, if defined */
|
19 | context?: Ctx | (() => Promise<Ctx> | Ctx);
|
20 | /** Options for file creation */
|
21 | create?: {
|
22 | /**
|
23 | * A function for generating placeholder migration files. Specify to make sure files generated via CLI or using `.create` follow team conventions.
|
24 | * Should return an array of [filepath, content] pairs. Usually, only one pair is needed, but to put `down` migrations in a separate
|
25 | * file, more than one can be returned.
|
26 | */
|
27 | template?: (filepath: string) => Promisable<Array<[string, string]>>;
|
28 | /**
|
29 | * The default folder that new migration files should be generated in. If this is not specified, the new migration file will be created
|
30 | * in the same folder as the last existing migration. The value here can be overriden by passing `folder` when calling `create`.
|
31 | */
|
32 | folder?: string;
|
33 | };
|
34 | };
|
35 | /** Serializeable metadata for a migration. The structure returned by the external-facing `pending()` and `executed()` methods. */
|
36 | export type MigrationMeta = {
|
37 | /** Name - this is used as the migration unique identifier within storage */
|
38 | name: string;
|
39 | /** An optional filepath for the migration. Note: this may be undefined, since not all migrations correspond to files on the filesystem */
|
40 | path?: string;
|
41 | };
|
42 | export type MigrationParams<T> = {
|
43 | name: string;
|
44 | path?: string;
|
45 | context: T;
|
46 | };
|
47 | /** A callable function for applying or reverting a migration */
|
48 | export type MigrationFn<T = unknown> = (params: MigrationParams<T>) => Promise<unknown>;
|
49 | /**
|
50 | * A runnable migration. Represents a migration object with an `up` function which can be called directly, with no arguments, and an optional `down` function to revert it.
|
51 | */
|
52 | export type RunnableMigration<T> = {
|
53 | /** The effect of applying the migration */
|
54 | up: MigrationFn<T>;
|
55 | /** The effect of reverting the migration */
|
56 | down?: MigrationFn<T>;
|
57 | } & MigrationMeta;
|
58 | /** Glob instructions for migration files */
|
59 | export type GlobInputMigrations<T> = {
|
60 | /**
|
61 | * A glob string for migration files. Can also be in the format `[path/to/migrations/*.js', {cwd: 'some/base/dir', ignore: '**ignoreme.js' }]`
|
62 | * See https://npmjs.com/package/glob for more details on the glob format - this package is used internally.
|
63 | */
|
64 | glob: string | [string, {
|
65 | cwd?: string;
|
66 | ignore?: string | string[];
|
67 | }];
|
68 | /** Will be supplied to every migration function. Can be a database client, for example */
|
69 | /** A function which takes a migration name, path and context, and returns an object with `up` and `down` functions. */
|
70 | resolve?: Resolver<T>;
|
71 | };
|
72 | /**
|
73 | * Allowable inputs for migrations. Can be either glob instructions for migration files, a list of runnable migrations, or a
|
74 | * function which receives a context and returns a list of migrations.
|
75 | */
|
76 | export type InputMigrations<T> = GlobInputMigrations<T> | Array<RunnableMigration<T>> | ((context: T) => Promisable<InputMigrations<T>>);
|
77 | /** A function which takes a migration name, path and context, and returns an object with `up` and `down` functions. */
|
78 | export type Resolver<T> = (params: MigrationParams<T>) => RunnableMigration<T>;
|
79 | export declare const RerunBehavior: {
|
80 | /** Hard error if an up migration that has already been run, or a down migration that hasn't, is encountered */
|
81 | readonly THROW: "THROW";
|
82 | /** Silently skip up migrations that have already been run, or down migrations that haven't */
|
83 | readonly SKIP: "SKIP";
|
84 | /** Re-run up migrations that have already been run, or down migrations that haven't */
|
85 | readonly ALLOW: "ALLOW";
|
86 | };
|
87 | export type RerunBehavior = keyof typeof RerunBehavior;
|
88 | export type MigrateUpOptions = MergeExclusive<{
|
89 | /** If specified, migrations up to and including this name will be run. Otherwise, all pending migrations will be run */
|
90 | to?: string;
|
91 | }, {
|
92 | /** Only run this many migrations. If not specified, all pending migrations will be run */
|
93 | step: number;
|
94 | }, {
|
95 | /** If specified, only the migrations with these names migrations will be run. An error will be thrown if any of the names are not found in the list of available migrations */
|
96 | migrations: string[];
|
97 | /** What to do if a migration that has already been run is explicitly specified. Default is `THROW`. */
|
98 | rerun?: RerunBehavior;
|
99 | }>;
|
100 | export type MigrateDownOptions = MergeExclusive<{
|
101 | /** If specified, migrations down to and including this name will be revert. Otherwise, only the last executed will be reverted */
|
102 | to?: string | 0;
|
103 | }, {
|
104 | /** Revert this many migrations. If not specified, only the most recent migration will be reverted */
|
105 | step: number;
|
106 | }, {
|
107 | /**
|
108 | * If specified, only the migrations with these names migrations will be reverted. An error will be thrown if any of the names are not found in the list of executed migrations.
|
109 | * Note, migrations will be run in the order specified.
|
110 | */
|
111 | migrations: string[];
|
112 | /** What to do if a migration that has not been run is explicitly specified. Default is `THROW`. */
|
113 | rerun?: RerunBehavior;
|
114 | }>;
|
115 | /** Map of eventName -> eventData type, where the keys are the string events that are emitted by an umzug instances, and the values are the payload emitted with the corresponding event. */
|
116 | export type UmzugEvents<Ctx> = {
|
117 | migrating: MigrationParams<Ctx>;
|
118 | migrated: MigrationParams<Ctx>;
|
119 | reverting: MigrationParams<Ctx>;
|
120 | reverted: MigrationParams<Ctx>;
|
121 | beforeCommand: {
|
122 | command: string;
|
123 | context: Ctx;
|
124 | };
|
125 | afterCommand: {
|
126 | command: string;
|
127 | context: Ctx;
|
128 | };
|
129 | };
|