import * as plugins from './mongodump.plugins.js'; type TMongoDumpDocument = plugins.mongodb.WithId; export type TMongoDumpNameTransform = (docArg: TMongoDumpDocument) => string; /** * a MongoDump Target is a pointer to a database * + exposes functions to interact with the dump target */ export class MongoDumpTarget { public static async createAndInit(mongoDescriptorArg: plugins.tsclass.database.IMongoDescriptor) { const mongoDumpTarget = new MongoDumpTarget(mongoDescriptorArg); await mongoDumpTarget.init(); return mongoDumpTarget; } public readyDeferred = plugins.smartpromise.defer(); public mongoDescriptor: plugins.tsclass.database.IMongoDescriptor; public mongoDbClient!: plugins.mongodb.MongoClient; public mongoDb!: plugins.mongodb.Db; public smartFs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode()); constructor(mongoDescriptorArg: plugins.tsclass.database.IMongoDescriptor) { this.mongoDescriptor = mongoDescriptorArg; } /** * connects to the database that was specified during instance creation */ public async init(): Promise { const finalConnectionUrl = this.mongoDescriptor.mongoDbUrl .replace('', this.mongoDescriptor.mongoDbUser ?? '') .replace('', this.mongoDescriptor.mongoDbUser ?? '') .replace('', this.mongoDescriptor.mongoDbUser ?? '') .replace('', this.mongoDescriptor.mongoDbUser ?? '') .replace('', this.mongoDescriptor.mongoDbPass ?? '') .replace('', this.mongoDescriptor.mongoDbPass ?? '') .replace('', this.mongoDescriptor.mongoDbName ?? '') .replace('', this.mongoDescriptor.mongoDbName ?? ''); this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, { maxPoolSize: 100, maxIdleTimeMS: 10, }); this.mongoDb = this.mongoDbClient.db(this.mongoDescriptor.mongoDbName); console.log(`Connected to database ${this.mongoDescriptor.mongoDbName}`); this.readyDeferred.resolve(); } /** * gets all collections */ public async getCollections() { await this.readyDeferred.promise; const collections = await this.mongoDb.collections(); console.log(`The follwing collections have been found:`); for (const collection of collections) { console.log(collection.collectionName); } return collections; } /** * dumps a collection to a directory */ public async dumpCollectionToDir( collectionArg: plugins.mongodb.Collection, dirArg: string, nameTransformFunction: TMongoDumpNameTransform = (docArg) => docArg._id.toString() ) { const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg); const collectionDir = plugins.path.join(dirPath, collectionArg.collectionName); await this.smartFs.directory(collectionDir).create(); const cursor = collectionArg.find(); let value = await cursor.next(); while (value) { const targetPath = plugins.path.join(collectionDir, `${nameTransformFunction(value)}.json`); await this.smartFs.file(targetPath).encoding('utf8').write(JSON.stringify(value, null, 2)); value = await cursor.next(); } } public async dumpCollectionToTarArchiveStream( collectionArg: plugins.mongodb.Collection ) {} public async dumpCollectionToTarArchiveFile( collectionArg: plugins.mongodb.Collection, filePathArg: string ) {} public async dumpAllCollectionsToDir( dirArg: string, nameFunctionArg?: TMongoDumpNameTransform, cleanDirArg = false ) { const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg); if (cleanDirArg) { if (await this.smartFs.directory(dirPath).exists()) { await this.smartFs.directory(dirPath).recursive().delete(); } await this.smartFs.directory(dirPath).create(); } const collections = await this.getCollections(); for (const collection of collections) { await this.dumpCollectionToDir(collection, dirPath, nameFunctionArg); } } public async dumpAllCollectionsToTarArchiveStream( collectionArg: plugins.mongodb.Collection, filePathArg: string ) {} }