npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nx-mongo-migrate

v0.0.9

Published

This library is a custom Nx plugin that manages database migrations for any project using mongoose. The code was originally based on the popular migrate-mongo library but completely re-written in typescript and specifically for Nx workspaces.

Downloads

22

Readme

npm version License: MIT

Quick Start

This library is a custom Nx plugin that manages database migrations for any project using mongoose. The code was originally based on the popular migrate-mongo library but completely re-written in typescript and specifically for Nx workspaces.

  • simple to use
  • easily write scripts for automated data management via CI/CD pipelines
  • supports any NoSQL database that supports Mongoose

Attention: this is a node.js library. It is supposed to be used as a backend/server-side library and will definitely not work within a browser.

Installation

npm:

npm install nx-mongo-migrate --save-dev

yarn:

yarn add -D nx-mongo-migrate

pnpm:

pnpm add -D nx-mongo-migrate

How to Use Nx-Mongo-Migrate

How to - Initialize Migrations for a New Project:

The first step in managing data for a specific project is to initialize migrations for the project. This can be done by

  1. right-clicking and selecting 'Nx generate...'
  2. Select the 'nx-mongo-migrate - init' option
  3. Follow the generator prompt

This should create a migrations directory in the desired project along with modifying the project configuration to include new migration commands for performing the actual management of database migrations


How to - Create Migrations:

The second step in managing data for a specific project is to create a new migration for the project. This can be done by

  1. right-clicking and selecting 'Nx generate...'
  2. Select the 'nx-mongo-migrate - create' option
  3. Follow the generator prompt

This should generate a new database migration file which contains two methods. The first is the 'up' method which is responsible for applying the specified database changes. The second method is the 'down' method which is resopnsible for reversing the changes created in the 'up' method.

It is strongly recommended that all migrations be written in an idempotent manner


How to - Apply Migrations:

After creating a new migration, the next step is to apply the migration to the database. This can be done by running the nx mongo-migrate-up <project> command. The database migration engine will keep track of your migrations, including the contents of each migration file to ensure that your database stays in sync.


How to - Revert Migrations:

It may be neccessary to revert a migration from time to time. This can be done by running the nx mongo-migrate-down <project> command. This command will revert the last applied migration for the project.


How to - Get Migration Status:

Nx mongo-migrate contains a convenience function that will display the most recent migration for a specific project. This can be done by running the nx mongo-migrate-status <project> command.


Additional Considerations

All migrations are applied linearly in time, each marked with a timestamp in both the name and database. This means that in order to apply and revert migrations successfully all migrations must be reverted and applied linearly in time as well. This ordinarily will not cause issues as the migration engine will handle this for you, but any manual modification to the migration database collection could cause migrations to fall out of sync.

This is why it is strongly recommended to NEVER manually modify the database collection unless you are absolutely sure what you are doing as doing so may result in irrepairable damage!

The database migration engine does track migration history and migration contents. Therefore, it only runs migrations once. However, as a best practice, it is still strongly recommended to write migrations in an idempotent manner. This is especially important when writing migrations for collections where data already exists that is not managed by mongo-migrate.

The database migration engine also tracks the contents of each database migration. Therefore, if any of the migration scripts which have already been applied get modified, then any future migrations will fail. This is a safe guard to protect the integrity of the database.