# Mongo Easy Migration

> A MongoDB plugin that simplifies writing and managing database migrations with a structured API, per-record callbacks, and built-in logging.
Published: 2024-12-04
Tags: mongodb, migration, nodejs, javascript, npm

Source: https://hugo-portfolio-teal.vercel.app/projects/easy-migration/

<!-- synced from GitHub README (scripts/sync-readmes.mjs) — edit the repo README, not this file -->

# Mongo Easy Migration Plugin

Easy Migration is a MongoDB plugin designed to simplify the process of writing and managing migrations in MongoDB databases. It streamlines the development workflow by providing a clear and structured approach to database changes.
<br/>

## Features
<ul>
<li>Intuitive API: Write migrations with ease using simple and well-documented methods.</li>
<li>Rollback Support: Provides an easy way to undo migrations (coming soon) </li>
<li>Customizable: Supports custom migration logic to suit your application needs.</li>
<li>Error Handling: Robust error reporting and logging to help debug issues.</li>
</ul>

## Setup

install library from npm using
```
npm i mongodbplugin
```
<br/>

## Usage

What you need is your <br/>
<ol>
    <li>Your mongoDB URI</li>
    <li>Your primary collection on which migration has to take data from</li>
    <li>Your list of remaining collections, or the collection on which the migration is to be performed on</li>
    <li>Callback function with logic of what is required to be done</li>
</ol>

```
const processMigration = require("mongodbplugin");

let mongoDB_URI = process.env.DB_URL

let primaryCollection = require("./models/primaryCollection");
let secondaryCollection = require("./models/secondaryCollection");
let ternaryCollection = require("./models/ternaryCollection");

let callbackFn = require("./migrations/updateNewFieldsInDB);

// the callbackFn should consists of what logic you want to impliment on per record basis, as this will be called inside a loop.

processMigration( { uri:mongoDB_URI, options: {
        // this includes further options that you want to pair up with mongodb
    }},
    primaryCollection,
    [ primaryCollection, secondaryCollection, ternaryCollection ],
    callbackFn( data, primaryCollection, secondaryCollection, ternaryCollection, writeLog ) // spread apart your collections here

    // you can use writeLog function to write your logs on system which will get saved inside a folder migrationLogs
)
/**
    writeLog(action, logContent);
    you can directly call this function inside your callback
*/

```

## Coming Soon

Improved overall Performance.<br/>
Improved logging capabilities.<br/>
Rollback support.<br/>

