UNPKG

1.53 kBTypeScriptView Raw
1/// <reference types="node" />
2import * as fs from 'fs';
3import type { Umzug } from './umzug';
4export type FileLockerOptions = {
5 path: string;
6 fs?: typeof fs;
7};
8/**
9 * Simple locker using the filesystem. Only one lock can be held per file. An error will be thrown if the
10 * lock file already exists.
11 *
12 * @example
13 * const umzug = new Umzug({ ... })
14 * FileLocker.attach(umzug, { path: 'path/to/lockfile' })
15 *
16 * @docs
17 * To wait for the lock to be free instead of throwing, you could extend it (the below example uses `setInterval`,
18 * but depending on your use-case, you may want to use a library with retry/backoff):
19 *
20 * @example
21 * class WaitingFileLocker extends FileLocker {
22 * async getLock() {
23 * return new Promise(resolve => setInterval(
24 * () => super.getLock().then(resolve).catch(),
25 * 500,
26 * )
27 * }
28 * }
29 *
30 * const locker = new WaitingFileLocker({ path: 'path/to/lockfile' })
31 * locker.attachTo(umzug)
32 */
33export declare class FileLocker {
34 private readonly lockFile;
35 private readonly fs;
36 constructor(params: FileLockerOptions);
37 /** Attach `beforeAll` and `afterAll` events to an umzug instance which use the specified filepath */
38 static attach(umzug: Umzug, params: FileLockerOptions): void;
39 /** Attach lock handlers to `beforeCommand` and `afterCommand` events on an umzug instance */
40 attachTo(umzug: Umzug): void;
41 private readFile;
42 private writeFile;
43 private removeFile;
44 getLock(): Promise<void>;
45 releaseLock(): Promise<void>;
46}