Skip to content

Commit 94b6071

Browse files
committed
feature(SeparatedConnection): added option hooks to change default connection
1 parent 890f3d0 commit 94b6071

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

src/injection.tokens.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export interface Config {
3737
useNewUrlParser: boolean;
3838
};
3939
};
40+
hooks?: {
41+
connection: <T = any>() => Promise<T>;
42+
down: (fileName: string) => Promise<any>;
43+
up: (fileName: string) => Promise<any>;
44+
status: (fileNames: string[]) => Promise<ReturnType[]>;
45+
};
4046
dateTimeFormat?: () => string;
4147
outDir: string;
4248
migrationsDir?: string;

src/services/migration/migration.service.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export class MigrationService {
2424
) {}
2525

2626
async connect() {
27+
if (this.configService.config.hooks) {
28+
return this.configService.config.hooks.connection() as never;
29+
}
2730
await this.database.mongooseConnect();
2831
return this.database.connect();
2932
}
@@ -62,14 +65,22 @@ export class MigrationService {
6265
});
6366
throw error;
6467
}
65-
const collection = client
66-
.db()
67-
.collection(this.configService.config.changelogCollectionName);
68+
6869
const { fileName } = item;
6970
const appliedAt = new Date();
7071

7172
try {
72-
await collection.insertOne({ fileName, appliedAt });
73+
if (
74+
this.configService.config.hooks &&
75+
typeof this.configService.config.hooks.up === 'function'
76+
) {
77+
await this.configService.config.hooks.up(fileName);
78+
} else {
79+
const collection = client
80+
.db()
81+
.collection(this.configService.config.changelogCollectionName);
82+
await collection.insertOne({ fileName, appliedAt });
83+
}
7384
} catch (err) {
7485
await logger.error({
7586
migrated,
@@ -135,11 +146,20 @@ export class MigrationService {
135146
});
136147
throw error;
137148
}
138-
const collection = client
139-
.db()
140-
.collection(this.configService.config.changelogCollectionName);
149+
141150
try {
142-
await collection.deleteOne({ fileName: lastAppliedItem.fileName });
151+
if (
152+
this.configService.config.hooks &&
153+
typeof this.configService.config.hooks.down === 'function'
154+
) {
155+
await this.configService.config.hooks.down(lastAppliedItem.fileName);
156+
} else {
157+
const collection = client
158+
.db()
159+
.collection(this.configService.config.changelogCollectionName);
160+
await collection.deleteOne({ fileName: lastAppliedItem.fileName });
161+
}
162+
143163
const res: ReturnType = {
144164
fileName: lastAppliedItem.fileName,
145165
appliedAt: new Date(),
@@ -230,6 +250,12 @@ export class MigrationService {
230250

231251
async statusInternal() {
232252
const fileNames = await this.migrationsResolver.getFileNames();
253+
if (
254+
this.configService.config.hooks &&
255+
typeof this.configService.config.hooks.status === 'function'
256+
) {
257+
return this.configService.config.hooks.status(fileNames);
258+
}
233259
const client = await this.connect();
234260
const collection = client
235261
.db()

xmigrate.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@ export default async () => {
33
defaultTemplate: 'typescript',
44
outDir: './.xmigrate',
55
typescript: true,
6+
hooks: {
7+
connection: () => {
8+
return {
9+
db: () => ({ collection: () => ({ updateOne: () => null }) }),
10+
};
11+
},
12+
up: (fileName: string) => {
13+
/* Save up migration */
14+
// Migrations.save({ fileName, createdAt: new Date() });
15+
console.log('up');
16+
return {};
17+
},
18+
down: (fileName: string) => {
19+
/* Remove migration from changelog */
20+
// Migrations.remove(fileName);
21+
console.log('down');
22+
return {};
23+
},
24+
status: (fileNames: string[]) => {
25+
/* Get all migrations from database */
26+
const prevMigrations = [
27+
{ fileName: '2020102412521-pesho.ts', appliedAt: new Date() },
28+
];
29+
return fileNames.map((fileName) => {
30+
const itemInLog = prevMigrations.find((m) => m.fileName === fileName);
31+
const appliedAt = itemInLog
32+
? (itemInLog.appliedAt as Date).toJSON()
33+
: 'PENDING';
34+
return { fileName, appliedAt, result: null };
35+
});
36+
},
37+
},
638
// dateTimeFormat: () => new Date().toISOString(),
739
mongodb: {
840
url: 'mongodb://localhost:27017',

0 commit comments

Comments
 (0)