UNPKG

4.45 kBTypeScriptView Raw
1import emittery from 'emittery';
2import * as errorCause from 'pony-cause';
3import type { CommandLineParserOptions } from './cli';
4import { UmzugCLI } from './cli';
5import type { MigrateDownOptions, MigrateUpOptions, MigrationMeta, MigrationParams, Resolver, RunnableMigration, UmzugEvents, UmzugOptions } from './types';
6type MigrationErrorParams = {
7 direction: 'up' | 'down';
8} & MigrationParams<unknown>;
9export declare class MigrationError extends errorCause.ErrorWithCause<unknown> {
10 name: string;
11 migration: MigrationErrorParams;
12 jse_cause: unknown;
13 constructor(migration: MigrationErrorParams, original: unknown);
14 get info(): MigrationErrorParams;
15 private static errorString;
16}
17export declare class Umzug<Ctx extends object = object> extends emittery<UmzugEvents<Ctx>> {
18 readonly options: UmzugOptions<Ctx>;
19 private readonly storage;
20 readonly migrations: (ctx: Ctx) => Promise<ReadonlyArray<RunnableMigration<Ctx>>>;
21 /**
22 * Compile-time only property for type inference. After creating an Umzug instance, it can be used as type alias for
23 * a user-defined migration. The function receives a migration name, path and the context for an umzug instance
24 * @example
25 * ```
26 * // migrator.ts
27 * import { Umzug } from 'umzug'
28 *
29 * const umzug = new Umzug({...})
30 * export type Migration = typeof umzug._types.migration;
31 *
32 * umzug.up();
33 * ```
34 * ___
35 *
36 * ```
37 * // migration-1.ts
38 * import type { Migration } from '../migrator'
39 *
40 * // name and context will now be strongly-typed
41 * export const up: Migration = ({name, context}) => context.query(...)
42 * export const down: Migration = ({name, context}) => context.query(...)
43 * ```
44 */
45 readonly _types: {
46 migration: (params: MigrationParams<Ctx>) => Promise<unknown>;
47 context: Ctx;
48 };
49 /** creates a new Umzug instance */
50 constructor(options: UmzugOptions<Ctx>);
51 private logging;
52 static defaultResolver: Resolver<unknown>;
53 /**
54 * Get an UmzugCLI instance. This can be overriden in a subclass to add/remove commands - only use if you really know you need this,
55 * and are OK to learn about/interact with the API of @rushstack/ts-command-line.
56 */
57 protected getCli(options?: CommandLineParserOptions): UmzugCLI;
58 /**
59 * 'Run' an umzug instance as a CLI. This will read `process.argv`, execute commands based on that, and call
60 * `process.exit` after running. If that isn't what you want, stick to the programmatic API.
61 * You probably want to run only if a file is executed as the process's 'main' module with something like:
62 * @example
63 * if (require.main === module) {
64 * myUmzugInstance.runAsCLI()
65 * }
66 */
67 runAsCLI(argv?: string[]): Promise<boolean>;
68 /** Get the list of migrations which have already been applied */
69 executed(): Promise<MigrationMeta[]>;
70 /** Get the list of migrations which have already been applied */
71 private _executed;
72 /** Get the list of migrations which are yet to be applied */
73 pending(): Promise<MigrationMeta[]>;
74 private _pending;
75 protected runCommand<T>(command: string, cb: (commandParams: {
76 context: Ctx;
77 }) => Promise<T>): Promise<T>;
78 /**
79 * Apply migrations. By default, runs all pending migrations.
80 * @see MigrateUpOptions for other use cases using `to`, `migrations` and `rerun`.
81 */
82 up(options?: MigrateUpOptions): Promise<MigrationMeta[]>;
83 /**
84 * Revert migrations. By default, the last executed migration is reverted.
85 * @see MigrateDownOptions for other use cases using `to`, `migrations` and `rerun`.
86 */
87 down(options?: MigrateDownOptions): Promise<MigrationMeta[]>;
88 create(options: {
89 name: string;
90 folder?: string;
91 prefix?: 'TIMESTAMP' | 'DATE' | 'NONE';
92 allowExtension?: string;
93 allowConfusingOrdering?: boolean;
94 skipVerify?: boolean;
95 /** Optionally define the content for the new file. If not set, the configured template will be used. */
96 content?: string;
97 }): Promise<void>;
98 private static defaultCreationTemplate;
99 private findNameIndex;
100 private findMigrations;
101 private getContext;
102 /** helper for parsing input migrations into a callback returning a list of ready-to-run migrations */
103 private getMigrationsResolver;
104}
105export {};