-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.ts
92 lines (92 loc) · 2.43 KB
/
migration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { promises as fs } from 'fs';
import { path } from '@biorate/tools';
import { init, injectable, inject, Types } from '@biorate/inversion';
import { IConfig } from '@biorate/config';
import { IConnector, IConnectorConfig } from '@biorate/connector';
/**
* @description Abstract migration class
*/
@injectable()
export abstract class Migration {
/**
* @description config property
*/
@inject(Types.Config) protected config: IConfig;
/**
* @description connector property
*/
protected abstract connector: IConnector<IConnectorConfig>;
/**
* @description Get migration type
*/
protected get type() {
return this.constructor.name.toLowerCase();
}
/**
* @description Scan migration directory
*/
protected async scan(...args: string[]) {
try {
return (await fs.readdir(this.path(...args))).map((item) =>
this.path(...args, item),
);
} catch {
return [] as string[];
}
}
/**
* @description Create path to migrations directory
*/
protected path(...args: string[]) {
return path.create(
process.cwd(),
this.config.get<string>('migrations.directory', 'migrations'),
this.type,
...args,
);
}
/**
* @description Logging method
*/
protected log(...args: string[]) {
console.info(this.type, ...args, 'up!');
}
/**
* @description Initialize method
*/
@init() protected async initialize() {
console.log(this.constructor.name);
await this.process();
}
/**
* @description For each migration item
*/
protected async forEach<T extends { name: string }, C>(
namespace: string,
callback: (config: T, connection: C, paths: string[]) => Promise<void>,
) {
for (const config of this.config.get<T[]>(namespace, [])) {
const paths = await this.scan(config.name);
if (!paths.length) continue;
const connection = this.connector.connection(config.name);
if (!connection) {
console.info(`${namespace} connection [%s] not exists, skip...`, config.name);
continue;
}
await callback(config, connection, paths);
}
}
/**
* @description For each migration item
*/
protected async forEachPath(
paths: string[],
callback: (file: string, name: string) => Promise<void>,
) {
for (const p of paths) await callback(p, path.basename(p));
}
/**
* @description Abstract async process method
*/
protected abstract process(): Promise<void>;
}