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 🙏

© 2026 – Pkg Stats / Ryan Hefner

generic-migrations

v0.2.2

Published

A promise-based generic migration library

Readme

Node Generic Migrations

A promise-based generic migration library for Node.js >= 6.

Why?

After looking for a simple migration library for a while, all I found were very opinionated libraries with no support for promises that either forced a storage system or a directory structure on you. Instead, generic-migrations gives you a public interface with which you can define how you load and store state, how you set up and tear down your environment and all your migrations. Whether you use separate files in a directory or not, store state in a file or in a database, or any other detail is completely up to you.

Installation

$ npm install --save generic-migrations

Example

import migrations from 'generic-migrations';

const migr = migrations({
    async setup (context) {
        /* connect to your DB, etc. */
        context.db = await connectToDatabase();
    },

    async teardown (context) {
        /* close DB connection, etc */
        await context.db.close();
    },

    async loadState (context) {
        /* load your state */
        return await context.db.findOne(/* ... */);
    },

    async saveState (context, state) {
        /* save your state */
        await context.db.replaceOne(/* ... */);
    },
});

/* For each version ... */
migr.define(1, 'first-migration', {
    async upgrade (context) {
        /* Upgrade to version 1 */
    },

    async downgrade (context) {
        /* Downgrade from version 1 */
    },
});

/* Run the migrations */
(async () => {
    migr.upgrade();
});

API

createMigration(functions) (default export)

Creates a new empty Migration with the given lifecycle functions.

  • functions: Object with the following attributes:
    • setup(context): (Optional) Function that sets up your environment, returns a Promise. The context attribute is an object that will be passed around for all user-defined functions; modify it at will.
    • teardown(context): (Optional) Function that tears down your environment, returns a Promise. The context attribute is the same as the one given to setup.
    • loadState(context): (Optional) Function that loads the state, returns a Promise to the state. The promise might resolve to null or undefined if no state is available. The context attribute is the same as the one given to setup.
    • saveState(context, state): (Optional) Function that saves the state, returns a Promise. The context attribute is the same as the one given to setup; the state argument should be stored unmodified in whatever storage system you want.

Migration#define(version, name, functions)

Defines a new version for your migration.

  • version: Integer defining the version. Must be unique for this migration.
  • name: Migration name. Used only for logging and other human-oriented processes.
  • functions: Object with the following attributes:
    • upgrade(context): Function that performs an upgrade to this version, returns a Promise. The context attribute is the same as the one given to setup.
    • downgrade(context): Function that performs a downgrade from this version, returns a Promise. The context attribute is the same as the one given to setup.

Migration#upgrade(targetVersion = +Infinity)

Performs an upgrade to targetVersion. Returns a promise that will resolve to the last version that was upgraded to. See the section on error handling for more information.

  • targetVersion: Target version for the upgrade. If not provided, upgrades to the latest defined version.

    Note that the saved state will remember only the last version up to the latest defined one. For example, if only versions 1 and 5 are available and you call upgrade(10), the current version will be 5, and later adding versions 3 and 7 will trigger an upgrade for version 7 but not 3.

Migration#downgrade(targetVersion = -Infinity)

Performs a downgrade to targetVersion. Returns a promise that will resolve to one less than the last version that was downgraded from. See the section on error handling for more information.

  • targetVersion: Target version for the downgrade. If not provided, downgrades to the earliest defined version.

    Note that the saved state will remember only the last version up to the earliest defined one. For example, if only versions 4 and 6 are available and you call downgrade(0), the current version will be 3, and later adding versions 1 and 5 will trigger an downgrade for version 1 but not 6.

Error Handling

TODO