Skip to content

Commit dc2aa3b

Browse files
committed
refactor: pass db object to mongo db store
BREAKING CHANGE: init no longer accepts a database uri, pass a db object instead
1 parent 00f3038 commit dc2aa3b

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

src/MigrationStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface MigrationModel {
77
}
88

99
export interface MigrationStore {
10-
init(options: Record<string, unknown>): Promise<void>;
10+
init(options: Record<string, unknown>): Promise<void> | void;
1111
getAppliedMigrations(): Promise<MigrationModel[]>;
1212
insertMigration(migration: Migration): Promise<void>;
1313
}

src/MongoMigrationStore.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
import { Collection, MongoClient, MongoClientOptions } from 'mongodb';
1+
import { Collection, Db } from 'mongodb';
22

33
import { Migration } from './Migration';
44
import { MigrationModel, MigrationStore } from './MigrationStore';
55

66
export class MongoMigrationStore implements MigrationStore {
77
private collection?: Collection<MigrationModel>;
88

9-
async init({
10-
uri,
11-
database,
12-
migrationsCollection,
13-
options,
14-
}: {
15-
uri: string;
16-
database: string;
17-
migrationsCollection: string;
18-
options?: MongoClientOptions;
19-
}): Promise<void> {
20-
try {
21-
const client = await MongoClient.connect(uri, options);
22-
const db = client.db(database);
23-
this.collection = db.collection(migrationsCollection);
24-
} catch (e) {
25-
throw new Error(e);
26-
}
9+
init({ db, migrationsCollection }: { db: Db; migrationsCollection: string }): void {
10+
this.collection = db.collection(migrationsCollection);
2711
}
2812

2913
getAppliedMigrations(): Promise<MigrationModel[]> {

0 commit comments

Comments
 (0)