A module for creating and managing a local MongoDB instance for testing purposes.
Go to file
2024-04-14 18:00:24 +02:00
.gitea/workflows fix(core): update 2023-08-08 16:59:47 +02:00
.vscode fix(core): update 2021-12-20 15:58:23 +01:00
test fix(core): update 2023-08-08 16:59:47 +02:00
ts fix(core): update 2023-08-08 16:59:47 +02:00
.gitignore fix(core): update 2021-12-20 15:58:23 +01:00
license fix(core): update 2021-12-20 15:58:23 +01:00
npmextra.json update tsconfig 2024-04-14 18:00:24 +02:00
package.json update tsconfig 2024-04-14 18:00:24 +02:00
pnpm-lock.yaml fix(core): update 2023-08-08 14:07:05 +02:00
readme.hints.md update tsconfig 2024-04-14 18:00:24 +02:00
readme.md update tsconfig 2024-04-14 18:00:24 +02:00
tsconfig.json update npmextra.json: githost 2024-04-01 21:36:43 +02:00

@push.rocks/smartmongo

create a local mongodb for testing

Install

To start using @push.rocks/smartmongo in your project, you first need to install it via npm. You can do this by running the following command in your terminal:

npm install @push.rocks/smartmongo --save-dev

This will add @push.rocks/smartmongo as a development dependency to your project because it's typically used for testing purposes.

Usage

The @push.rocks/smartmongo package provides a convenient way to spin up a local MongoDB instance, primarily for testing purposes. It's designed to simplify the process of configuring and managing a MongoDB replica set during development or in CI/CD pipelines. Below, we present a comprehensive guide on how to utilize the full feature set of this module, employing ESM syntax and TypeScript.

Setting Up

To get started, you must first import the SmartMongo class from the package. This class is responsible for handling the MongoDB instances.

import { SmartMongo } from '@push.rocks/smartmongo';

Creating and Starting a MongoDB Instance

With SmartMongo, you can easily create and start a MongoDB replica set. You can specify the number of replica instances; however, if not specified, it defaults to 1.

async function setupMongoDB() {
  const smartMongoInstance = await SmartMongo.createAndStart(1); // Number of replicas is optional
  return smartMongoInstance;
}

const myDbInstance = await setupMongoDB();

After invoking createAndStart, an instance of MongoDB is spun up and is ready for use. The createAndStart function returns a SmartMongo instance which can be interacted with for further operations.

Accessing MongoDB Connection Information

After instantiation, you might want to connect your application or test suite to the MongoDB instance. The getMongoDescriptor method facilitates this by providing essential connection details.

const mongoDescriptor = await myDbInstance.getMongoDescriptor();
console.log(mongoDescriptor.mongoDbUrl); // Use this URL to connect with Mongoose or MongoDB clients.

Stopping and Cleaning Up

Once your tests have completed or you're done using the MongoDB instance, its crucial to properly stop and clean up the resources. @push.rocks/smartmongo provides two methods for this purpose:

  1. stop(): Stops the MongoDB instance without persisting any data.

    await myDbInstance.stop();
    
  2. stopAndDumpToDir(dirPath): Stops the MongoDB instance and persists the data to the specified directory. This is useful if you need to examine the data post-test or reuse it in subsequent runs.

    await myDbInstance.stopAndDumpToDir('./path/to/dump');
    

Advanced Usage

@push.rocks/smartmongo also provides advanced features for dumping the database and configuring MongoDB replica sets. These features can be particularly useful for complex testing scenarios or when specific MongoDB behaviors need to be emulated.

Dumping Data

To dump the MongoDB data for inspection or backup purposes, use the stopAndDumpToDir method. This method optionally takes a function to customize the naming scheme of the dumped files based on the document content.

await myDbInstance.stopAndDumpToDir('./path/to/dump', (doc) => {
  return `customNameBasedOnDoc-${doc._id}.bson`;
});

Using @push.rocks/smartmongo significantly simplifies the process of managing MongoDB instances for local testing environments. It abstracts away the complexity of starting, operating, and tearing down MongoDB replica sets, allowing developers to focus on building and testing their applications.

Conclusion

@push.rocks/smartmongo serves as a powerful tool in a developer's arsenal for efficiently configuring, running, and managing MongoDB instances in testing scenarios. By following the above guide, developers can leverage MongoDB in their projects with minimal setup and gain valuable insights into their applications' data interactions in a controlled and reproducible environment.

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.