smartmongo/ts/index.ts
2022-05-17 17:16:12 +02:00

35 lines
993 B
TypeScript

import * as plugins from './smartmongo.plugins';
export class SmartMongo {
// STATIC
public static async createAndStart() {
const smartMongoInstance = new SmartMongo();
await smartMongoInstance.start();
return smartMongoInstance;
}
// INSTANCE
private _readyDeferred = plugins.smartpromise.defer();
public readyPromise = this._readyDeferred.promise;
public mongoReplicaSet: plugins.mongoPlugin.MongoMemoryReplSet;
constructor() {
}
public async start(countArg: number = 4) {
this.mongoReplicaSet = await plugins.mongoPlugin.MongoMemoryReplSet.create({ replSet: { count: countArg } });
this._readyDeferred.resolve();
console.log(`mongoReplicaSet with ${countArg} replicas started.`);
}
public async getMongoDescriptor(): Promise<plugins.smartdata.IMongoDescriptor> {
await this.readyPromise;
return {
mongoDbUrl: this.mongoReplicaSet.getUri(),
}
}
public async stop() {
await this.mongoReplicaSet.stop(true);
}
}