fix(mongodb): modernize MongoDB dump handling and filesystem integration

This commit is contained in:
2026-05-01 15:32:05 +00:00
parent 7ee4a48ee2
commit a4950ef358
15 changed files with 2671 additions and 13638 deletions
+1 -1
View File
@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/mongodump',
version: '1.1.0',
version: '1.1.1',
description: 'A tool to create and manage dumps of MongoDB databases, supporting data export and import.'
}
+4 -4
View File
@@ -3,7 +3,7 @@ import { MongoDumpTarget } from './mongodump.classes.mongodumptarget.js';
export class MongoDump {
public mongoTargetObjectMap = new plugins.lik.ObjectMap<MongoDumpTarget>();
constructor() {}
public async addMongoTargetByMongoDescriptor(
@@ -15,8 +15,8 @@ export class MongoDump {
}
public async stop() {
await this.mongoTargetObjectMap.forEach(async (mongoTargetArg) => {
await mongoTargetArg.mongoDbClient.close();
})
await Promise.all(
this.mongoTargetObjectMap.getArray().map((mongoTargetArg) => mongoTargetArg.mongoDbClient.close())
);
}
}
+34 -25
View File
@@ -1,5 +1,8 @@
import * as plugins from './mongodump.plugins.js';
type TMongoDumpDocument = plugins.mongodb.WithId<plugins.mongodb.Document>;
export type TMongoDumpNameTransform = (docArg: TMongoDumpDocument) => string;
/**
* a MongoDump Target is a pointer to a database
* + exposes functions to interact with the dump target
@@ -11,10 +14,12 @@ export class MongoDumpTarget {
return mongoDumpTarget;
}
public readyDeferred = plugins.smartpromise.defer();
public readyDeferred = plugins.smartpromise.defer<void>();
public mongoDescriptor: plugins.tsclass.database.IMongoDescriptor;
public mongoDbClient: plugins.mongodb.MongoClient;
public mongoDb: plugins.mongodb.Db;
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;
}
@@ -22,16 +27,16 @@ export class MongoDumpTarget {
/**
* connects to the database that was specified during instance creation
*/
public async init(): Promise<any> {
public async init(): Promise<void> {
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);
.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,
@@ -60,47 +65,51 @@ export class MongoDumpTarget {
* dumps a collection to a directory
*/
public async dumpCollectionToDir(
collectionArg: plugins.mongodb.Collection,
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>,
dirArg: string,
nameTransformFunction = (doc: any) => doc._id
nameTransformFunction: TMongoDumpNameTransform = (docArg) => docArg._id.toString()
) {
const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg);
const collectionDir = plugins.path.join(dirPath, collectionArg.collectionName);
await plugins.smartfile.fs.ensureDir(collectionDir);
await this.smartFs.directory(collectionDir).create();
const cursor = collectionArg.find();
let value = await cursor.next();
while (value) {
await plugins.smartfile.memory.toFs(
JSON.stringify(value, null, 2),
plugins.path.join(collectionDir, `${nameTransformFunction(value)}.json`)
);
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 dumpCollectionToTarArchiveStream(
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>
) {}
public async dumpCollectionToTarArchiveFile(
collectionArg: plugins.mongodb.Collection,
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>,
filePathArg: string
) {}
public async dumpAllCollectionsToDir(
dirArg: string,
nameFunctionArg?: (docArg: any) => string,
nameFunctionArg?: TMongoDumpNameTransform,
cleanDirArg = false
) {
const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg);
if (cleanDirArg) {
await plugins.smartfile.fs.ensureEmptyDir(dirArg);
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, dirArg, nameFunctionArg);
await this.dumpCollectionToDir(collection, dirPath, nameFunctionArg);
}
}
public async dumpAllCollectionsToTarArchiveStream(
collectionArg: plugins.mongodb.Collection,
collectionArg: plugins.mongodb.Collection<plugins.mongodb.Document>,
filePathArg: string
) {}
}
+10 -12
View File
@@ -2,34 +2,32 @@
import * as path from 'path';
export {
path
}
path,
};
// pushrocks scope
import * as lik from '@push.rocks/lik';
import * as smartfile from '@push.rocks/smartfile';
import * as smartjson from '@push.rocks/smartjson';
import * as smartfs from '@push.rocks/smartfs';
import * as smartpath from '@push.rocks/smartpath';
import * as smartpromise from '@push.rocks/smartpromise';
export {
lik,
smartfile,
smartjson,
smartfs,
smartpath,
smartpromise
}
smartpromise,
};
// tsclass
import * as tsclass from '@tsclass/tsclass';
export {
tsclass
}
tsclass,
};
// third party scope
import * as mongodb from 'mongodb';
export {
mongodb
}
mongodb,
};