mongodump/ts/mongodump.classes.mongodumptarget.ts

107 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-06-05 19:04:16 +00:00
import * as plugins from './mongodump.plugins.js';
/**
* a MongoDump Target is a pointer to a database
* + exposes functions to interact with the dump target
*/
export class MongoDumpTarget {
2022-06-06 14:01:25 +00:00
public static async createAndInit(mongoDescriptorArg: plugins.tsclass.database.IMongoDescriptor) {
2022-06-05 19:04:16 +00:00
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;
constructor(mongoDescriptorArg: plugins.tsclass.database.IMongoDescriptor) {
this.mongoDescriptor = mongoDescriptorArg;
}
/**
* connects to the database that was specified during instance creation
*/
public async init(): Promise<any> {
const finalConnectionUrl = this.mongoDescriptor.mongoDbUrl
.replace('<USERNAME>', this.mongoDescriptor.mongoDbUser)
.replace('<username>', this.mongoDescriptor.mongoDbUser)
.replace('<USER>', this.mongoDescriptor.mongoDbUser)
.replace('<user>', this.mongoDescriptor.mongoDbUser)
.replace('<PASSWORD>', this.mongoDescriptor.mongoDbPass)
.replace('<password>', this.mongoDescriptor.mongoDbPass)
.replace('<DBNAME>', this.mongoDescriptor.mongoDbName)
.replace('<dbname>', 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
*/
2022-06-06 14:01:25 +00:00
public async dumpCollectionToDir(
collectionArg: plugins.mongodb.Collection,
dirArg: string,
nameTransformFunction = (doc: any) => doc._id
) {
2022-06-06 11:04:30 +00:00
const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg);
const collectionDir = plugins.path.join(dirPath, collectionArg.collectionName);
await plugins.smartfile.fs.ensureDir(collectionDir);
const cursor = collectionArg.find();
let value = await cursor.next();
2022-06-06 14:01:25 +00:00
while (value) {
await plugins.smartfile.memory.toFs(
JSON.stringify(value, null, 2),
plugins.path.join(collectionDir, `${nameTransformFunction(value)}.json`)
);
2022-06-06 11:04:30 +00:00
value = await cursor.next();
}
2022-06-05 19:04:16 +00:00
}
2022-06-06 14:01:25 +00:00
public async dumpCollectionToTarArchiveStream(collectionArg: plugins.mongodb.Collection) {}
public async dumpCollectionToTarArchiveFile(
collectionArg: plugins.mongodb.Collection,
filePathArg: string
) {}
public async dumpAllCollectionsToDir(
dirArg: string,
nameFunctionArg?: (docArg: any) => string,
cleanDirArg = false
) {
if (cleanDirArg) {
await plugins.smartfile.fs.ensureEmptyDir(dirArg);
}
const collections = await this.getCollections();
for (const collection of collections) {
await this.dumpCollectionToDir(collection, dirArg, nameFunctionArg);
}
}
2022-06-05 19:04:16 +00:00
2022-06-06 14:01:25 +00:00
public async dumpAllCollectionsToTarArchiveStream(
collectionArg: plugins.mongodb.Collection,
filePathArg: string
) {}
2022-06-05 19:04:16 +00:00
}