2022-06-05 21:04:16 +02:00
|
|
|
import * as plugins from './mongodump.plugins.js';
|
|
|
|
|
|
2026-05-01 15:32:05 +00:00
|
|
|
type TMongoDumpDocument = plugins.mongodb.WithId<plugins.mongodb.Document>;
|
|
|
|
|
export type TMongoDumpNameTransform = (docArg: TMongoDumpDocument) => string;
|
|
|
|
|
|
2022-06-05 21:04:16 +02:00
|
|
|
/**
|
|
|
|
|
* a MongoDump Target is a pointer to a database
|
|
|
|
|
* + exposes functions to interact with the dump target
|
|
|
|
|
*/
|
|
|
|
|
export class MongoDumpTarget {
|
2022-06-06 16:01:25 +02:00
|
|
|
public static async createAndInit(mongoDescriptorArg: plugins.tsclass.database.IMongoDescriptor) {
|
2022-06-05 21:04:16 +02:00
|
|
|
const mongoDumpTarget = new MongoDumpTarget(mongoDescriptorArg);
|
|
|
|
|
await mongoDumpTarget.init();
|
|
|
|
|
return mongoDumpTarget;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 15:32:05 +00:00
|
|
|
public readyDeferred = plugins.smartpromise.defer<void>();
|
2022-06-05 21:04:16 +02:00
|
|
|
public mongoDescriptor: plugins.tsclass.database.IMongoDescriptor;
|
2026-05-01 15:32:05 +00:00
|
|
|
public mongoDbClient!: plugins.mongodb.MongoClient;
|
|
|
|
|
public mongoDb!: plugins.mongodb.Db;
|
|
|
|
|
public smartFs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
|
|
|
|
|
|
2022-06-05 21:04:16 +02:00
|
|
|
constructor(mongoDescriptorArg: plugins.tsclass.database.IMongoDescriptor) {
|
|
|
|
|
this.mongoDescriptor = mongoDescriptorArg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* connects to the database that was specified during instance creation
|
|
|
|
|
*/
|
2026-05-01 15:32:05 +00:00
|
|
|
public async init(): Promise<void> {
|
2022-06-05 21:04:16 +02:00
|
|
|
const finalConnectionUrl = this.mongoDescriptor.mongoDbUrl
|
2026-05-01 15:32:05 +00:00
|
|
|
.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 ?? '');
|
2022-06-05 21:04:16 +02:00
|
|
|
|
|
|
|
|
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 16:01:25 +02:00
|
|
|
public async dumpCollectionToDir(
|
2026-05-01 15:32:05 +00:00
|
|
|
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>,
|
2022-06-06 16:01:25 +02:00
|
|
|
dirArg: string,
|
2026-05-01 15:32:05 +00:00
|
|
|
nameTransformFunction: TMongoDumpNameTransform = (docArg) => docArg._id.toString()
|
2022-06-06 16:01:25 +02:00
|
|
|
) {
|
2022-06-06 13:04:30 +02:00
|
|
|
const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg);
|
|
|
|
|
const collectionDir = plugins.path.join(dirPath, collectionArg.collectionName);
|
2026-05-01 15:32:05 +00:00
|
|
|
await this.smartFs.directory(collectionDir).create();
|
2022-06-06 13:04:30 +02:00
|
|
|
const cursor = collectionArg.find();
|
|
|
|
|
let value = await cursor.next();
|
2022-06-06 16:01:25 +02:00
|
|
|
while (value) {
|
2026-05-01 15:32:05 +00:00
|
|
|
const targetPath = plugins.path.join(collectionDir, `${nameTransformFunction(value)}.json`);
|
|
|
|
|
await this.smartFs.file(targetPath).encoding('utf8').write(JSON.stringify(value, null, 2));
|
2022-06-06 13:04:30 +02:00
|
|
|
value = await cursor.next();
|
|
|
|
|
}
|
2022-06-05 21:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-01 15:32:05 +00:00
|
|
|
public async dumpCollectionToTarArchiveStream(
|
|
|
|
|
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>
|
|
|
|
|
) {}
|
2022-06-06 16:01:25 +02:00
|
|
|
|
|
|
|
|
public async dumpCollectionToTarArchiveFile(
|
2026-05-01 15:32:05 +00:00
|
|
|
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>,
|
2022-06-06 16:01:25 +02:00
|
|
|
filePathArg: string
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
public async dumpAllCollectionsToDir(
|
|
|
|
|
dirArg: string,
|
2026-05-01 15:32:05 +00:00
|
|
|
nameFunctionArg?: TMongoDumpNameTransform,
|
2022-06-06 16:01:25 +02:00
|
|
|
cleanDirArg = false
|
|
|
|
|
) {
|
2026-05-01 15:32:05 +00:00
|
|
|
const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg);
|
2022-06-06 16:01:25 +02:00
|
|
|
if (cleanDirArg) {
|
2026-05-01 15:32:05 +00:00
|
|
|
if (await this.smartFs.directory(dirPath).exists()) {
|
|
|
|
|
await this.smartFs.directory(dirPath).recursive().delete();
|
|
|
|
|
}
|
|
|
|
|
await this.smartFs.directory(dirPath).create();
|
2022-06-06 16:01:25 +02:00
|
|
|
}
|
|
|
|
|
const collections = await this.getCollections();
|
|
|
|
|
for (const collection of collections) {
|
2026-05-01 15:32:05 +00:00
|
|
|
await this.dumpCollectionToDir(collection, dirPath, nameFunctionArg);
|
2022-06-06 16:01:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-05 21:04:16 +02:00
|
|
|
|
2022-06-06 16:01:25 +02:00
|
|
|
public async dumpAllCollectionsToTarArchiveStream(
|
2026-05-01 15:32:05 +00:00
|
|
|
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>,
|
2022-06-06 16:01:25 +02:00
|
|
|
filePathArg: string
|
|
|
|
|
) {}
|
2022-06-05 21:04:16 +02:00
|
|
|
}
|