smartmongo/ts/index.ts
2022-06-06 17:38:35 +02:00

53 lines
1.7 KiB
TypeScript

import { commitinfo } from './00_commitinfo_data.js';
import * as plugins from './smartmongo.plugins.js';
export class SmartMongo {
// STATIC
public static async createAndStart(replCountArg: number = 1) {
const smartMongoInstance = new SmartMongo();
await smartMongoInstance.start(replCountArg);
return smartMongoInstance;
}
// INSTANCE
private _readyDeferred = plugins.smartpromise.defer();
public readyPromise = this._readyDeferred.promise;
public mongoReplicaSet: plugins.mongoPlugin.MongoMemoryReplSet;
constructor() {}
public async start(countArg: number = 1) {
this.mongoReplicaSet = await plugins.mongoPlugin.MongoMemoryReplSet.create({
replSet: { count: countArg },
instanceOpts: [
{
storageEngine: 'wiredTiger',
},
],
});
this._readyDeferred.resolve();
console.log(`mongoReplicaSet with ${countArg} replicas started.`);
console.log(`@pushrocks/smartmongo version ${commitinfo.version}`);
}
public async getMongoDescriptor(): Promise<plugins.smartdata.IMongoDescriptor> {
await this.readyPromise;
return {
mongoDbUrl: this.mongoReplicaSet.getUri(),
};
}
public async stop() {
await this.mongoReplicaSet.stop();
await this.mongoReplicaSet.cleanup();
}
public async stopAndDumpToDir(dirArg: string, nameFunctionArg?: (doc: any) => string, emptyDirArg = true) {
const mongodumpInstance = new plugins.mongodump.MongoDump();
const mongodumpTarget = await mongodumpInstance.addMongoTargetByMongoDescriptor(await this.getMongoDescriptor());
await mongodumpTarget.dumpAllCollectionsToDir(dirArg, nameFunctionArg, emptyDirArg);
await mongodumpInstance.stop();
await this.stop();
}
}