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

@oliasoft-open-source/node-json-migrator

v2.3.10

Published

A library for JSON migrations

Downloads

2,782

Readme

Node JSON Migrator

A simple JSON change management tool for Node.js

  • Lightweight JSON change management tool for Node.js applications
  • Written in JavaScript
  • Pre-transpiled (no extra build steps)
  • Not tied to any framework
  • Open source (MIT License)

Features

  • Versioning of JSON payloads
  • Write change scripts (migrations) in JavaScript
  • Apply changes to JSON payloads using a convenient migrate() function
  • Works great in a team setting (multiple developers can track and handle changes)
  • Supports automated change deployment across multiple environments
  • Automatically tracks and applies only new (pending) change scripts
  • Automatically detects and warns when previous scripts have been altered
  • Optionally track version history in a PostgreSQL database (pg-promise interface)

How to use

Installation and setup

The database (if used) must already exist and have permissions granted.

Install the tool directly from Gitlab (published NPM packages may be available in future):

npm install --save git+https://gitlab.com/oliasoft-open-source/node-json-migrator.git

Defining migrations (change scripts)

Use the createMigration() function to generate a skeleton migration script, for example:

import {createMigration} from 'node-json-migrator';

const directory = 'json-migrations';
const description = 'add-panda';
await createMigration(directory, description);

This will create and populate skeleton files:

  • add-panda/add-panda.js is the skeleton change script (alter this to describe the desired payload change)
  • plan.json is the execution plan (defines the execution order / sequence for migration scripts)

Migration scripts export default functions that receive a payload and return the next payload. They must use immutable update patterns, and Immer is recommended for this. For example, after editing, the scripts might look like this:

json-migrations/add-panda/add-panda.js (adds a property to the payload)

import produce from 'immer';

export default (payload) = produce(state, (draft) => {
   draft.panda = 'Elliot';
});

json-migrations/plan.json (defines the migration execution plan)

[
  {
    "fileHash": "70fd80fd19828435b8656b8d01f99b840381050ded4aec2dd126e2f0c9864c88",
    "fileName": "add-panda.js",
    "sequence": "1"
  }
]

Note: the fileHash property is the SHA256 hash of the migration file content. You don't have to manually maintain that, the tool will sync it automatically for you.

Running migrations

In your application source code, import and call the migrate() function. The simplest usage (runs all migrations) is:

import {migrate} from 'node-json-migrator';

const {nextPayload} = await migrate({
  payload: {},
  config: {
    directory: 'json-migrations'
  }
});

Normally you want to track payload versions, and only execute pending migrations. To do this, you need to connect to a PostgreSQL database and pass a connected pg-promise database object. Then it looks like this:

import pgPromise from 'pg-promise';
import {migrate} from 'node-json-migrator';

//connect to the database somewhere in the database layer
const pgp = pgPromise({});
const pgpHelpers = pgp.helpers;
const database = pgp(/*connection string*/);
database.connect();

//if we know the current payload version, we can run only the newer, pending migration scripts
const currentPayloadVersion = undefined;

const {nextPayload, nextVersion} = await migrate({
  payload: {},
  config: {
    directory: 'json-migrations',
    database, //connected database object
    pgpHelpers, //pg-promise helpers: https://vitaly-t.github.io/pg-promise/helpers.html
    version: currentPayloadVersion
  }
});

Other config options

config = {
  dry: true, //execute without writing to database tables or files
  force: true, //execute bypassing some validation warnings
}

Usage Rules

  • files in the plan must exist
  • filenames must be unique and follow formatting rules
  • sequences must be unique
  • not allowed to change sequence numbers after releasing (merging) a migration
  • not allowed to remove released files from the plan
  • should release only one migration script at a time
  • not allowed to alter/modify released migration files (instead, write a new migration to replace it)
  • migration scripts should be repeatable (not fail if they run twice on the same payload)

Backfitting bugfixes

If you release a bad migration script, there is an escape-hatch for backfitting bugfixes:

  • rename the faulty script to .skip.js (or .skip2.js if it happened more than once)
  • add a new migration file with the original filename, to replace it (fix bug in new file)
  • the replacement script will not re-execute on payloads that already had the original script applied

Or, you can backfit a script earlier in the sequence

  • add a new migration file
  • set its sequence to for example 1.1 (subsequence number)
  • the backfitted script will execute one time on all payloads

How to contribute

Contribution is welcome via issue tickets and merge requests.

  • coding style (ESLint and prettier) is mandatory (applied via pre-commit hook)
  • to test: npm run test
    • database is mocked with pg-mem (in-memory database)
    • filesystem is mocked mock-fs
    • coverage reports are in test/coverage (not committed)
  • to build: npm run buld (this transpile a production build to the dist directory)
    • the build dist directory should be committed