.vscode | ||
test | ||
ts | ||
.gitignore | ||
.gitlab-ci.yml | ||
license | ||
npmextra.json | ||
package-lock.json | ||
package.json | ||
pnpm-lock.yaml | ||
readme.hints.md | ||
readme.md | ||
tsconfig.json |
@push.rocks/mongodump
a tool to handle dumps of MongoDB databases
Install
To use @push.rocks/mongodump in your project, run:
npm install @push.rocks/mongodump --save
Usage
This guide provides an overview of how to work with @push.rocks/mongodump to handle dumps of MongoDB databases efficiently.
Setting up a MongoDB Dump Target
First, you'll need to describe your MongoDB database using an interface provided by the module. Here's a sample descriptor:
import { IMongoDescriptor } from '@tsclass/tsclass';
import { MongoDump, MongoDumpTarget } from '@push.rocks/mongodump';
const myMongoDescriptor: IMongoDescriptor = {
mongoDbName: '<database_name>',
mongoDbUser: '<database_user>',
mongoDbPass: '<database_password>',
mongoDbUrl: 'mongodb+srv://<user>:<password>@<cluster_url>/<dbname>?retryWrites=true&w=majority',
};
Creating and Using the MongoDump Instance
To interact with MongoDB for dumping purposes, you'll use the MongoDump
class:
async function setupMongoDump() {
const mongoDump = new MongoDump();
const mongoDumpTarget = await mongoDump.addMongoTargetByMongoDescriptor(myMongoDescriptor);
// mongoDumpTarget can now be used for further operations
}
setupMongoDump();
Dumping Collections to a Directory
To dump the collections of a database into a directory with files representing each document, you can use the dumpCollectionToDir
method. This method is useful for creating backups or migrating data:
async function dumpCollections() {
const mongoDump = new MongoDump();
const mongoDumpTarget = await mongoDump.addMongoTargetByMongoDescriptor(myMongoDescriptor);
await mongoDumpTarget.dumpAllCollectionsToDir('./path/to/dumpDir', null, true);
// This dumps all collections to the specified directory, cleaning the directory before dumping.
}
dumpCollections();
Advanced Dumping Options
For more control over the dumping process, including naming conventions for dumped files or handling specific collections, you can explore methods like dumpCollectionToDir
for individual collections and advanced configurations concerning directory cleanliness and document naming.
Shutting Down Properly
It's important to close down database connections properly once your dumping operations are complete:
async function shutDownMongoDump() {
const mongoDump = new MongoDump();
await mongoDump.stop();
// Closes all open database connections gracefully
}
shutDownMongoDump();
Conclusion
The @push.rocks/mongodump module provides a flexible approach to handling MongoDB database dumps, whether it's for backup, migration, or other purposes. By leveraging TypeScript and modern async patterns, it integrates smoothly into modern Node.js applications focused on MongoDB interactions.
License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.
Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.