smartmongo/ts/index.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-05-17 16:28:09 +00:00
import { commitinfo } from './00_commitinfo_data.js';
import * as plugins from './smartmongo.plugins.js';
2021-12-20 14:58:23 +00:00
export class SmartMongo {
// STATIC
2022-05-17 16:28:09 +00:00
public static async createAndStart(replCountArg: number = 1) {
2021-12-20 14:58:23 +00:00
const smartMongoInstance = new SmartMongo();
2022-05-17 16:28:09 +00:00
await smartMongoInstance.start(replCountArg);
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 16:28:09 +00:00
public async start(countArg: number = 1) {
2022-05-18 15:35:16 +00:00
this.mongoReplicaSet = await plugins.mongoPlugin.MongoMemoryReplSet.create({ replSet: { count: countArg }, instanceOpts: [{
storageEngine: 'wiredTiger'
}]});
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.`);
2022-05-17 16:28:09 +00:00
console.log(`@pushrocks/smartmongo version ${commitinfo.version}`);
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
}
}