smartmongo/ts/index.ts

54 lines
1.7 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
2022-05-19 06:48:43 +00:00
constructor() {}
2021-12-20 14:58:23 +00:00
2022-05-17 16:28:09 +00:00
public async start(countArg: number = 1) {
2022-05-19 06:48:43 +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(),
2022-05-19 06:48:43 +00:00
};
2021-12-20 14:58:23 +00:00
}
public async stop() {
2022-06-06 14:51:41 +00:00
await this.mongoReplicaSet.stop();
await this.mongoReplicaSet.cleanup();
}
public async stopAndDumpToDir(dirArg: string) {
const dumpDir = plugins.smartpath.transform.makeAbsolute(dirArg);
const mongodumpInstance = new plugins.mongodump.MongoDump();
const mongodumpTarget = await mongodumpInstance.addMongoTargetByMongoDescriptor(await this.getMongoDescriptor());
await mongodumpTarget.dumpAllCollectionsToDir(dumpDir);
await mongodumpInstance.stop();
await this.stop();
2021-12-20 14:58:23 +00:00
}
2022-05-19 06:48:43 +00:00
}