smartmongo/ts/index.ts

35 lines
993 B
TypeScript
Raw Normal View History

2021-12-20 14:58:23 +00:00
import * as plugins from './smartmongo.plugins';
export class SmartMongo {
// STATIC
2021-12-20 16:02:19 +00:00
public static async createAndStart() {
2021-12-20 14:58:23 +00:00
const smartMongoInstance = new SmartMongo();
2021-12-20 16:02:19 +00:00
await smartMongoInstance.start();
2021-12-20 14:58:23 +00:00
return smartMongoInstance;
}
// INSTANCE
private _readyDeferred = plugins.smartpromise.defer();
public readyPromise = this._readyDeferred.promise;
2022-05-17 15:16:12 +00:00
public mongoReplicaSet: plugins.mongoPlugin.MongoMemoryReplSet;
2021-12-20 14:58:23 +00:00
constructor() {
}
2022-05-17 15:16:12 +00:00
public async start(countArg: number = 4) {
this.mongoReplicaSet = await plugins.mongoPlugin.MongoMemoryReplSet.create({ replSet: { count: countArg } });
2021-12-20 14:58:23 +00:00
this._readyDeferred.resolve();
2022-05-17 15:16:12 +00:00
console.log(`mongoReplicaSet with ${countArg} replicas started.`);
2021-12-20 14:58:23 +00:00
}
2021-12-20 15:24:04 +00:00
public async getMongoDescriptor(): Promise<plugins.smartdata.IMongoDescriptor> {
2021-12-20 14:58:23 +00:00
await this.readyPromise;
return {
2022-05-17 15:16:12 +00:00
mongoDbUrl: this.mongoReplicaSet.getUri(),
2021-12-20 14:58:23 +00:00
}
}
public async stop() {
2022-05-17 15:16:12 +00:00
await this.mongoReplicaSet.stop(true);
2021-12-20 14:58:23 +00:00
}
}