ConfigMigration

May 15, 2020 · View on GitHub

version Maintenance MIT size dep Known Vulnerabilities Build Status

SlimIO Configuration Migration. This package is used in SlimIO when addon Schema has been updated, so we need to migrate the Configuration Content to not break the startup.

Requirements

Features

  • Support both add and remove action.

Replace is not supported because it's considered dangerous and a bad practices to update keys!

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/config-migration
# or
$ yarn add @slimio/config-migration

Usage example

const { strictEqual } = require("assert");
const ConfigMigrator = require("@slimio/config-migration");

// Original Schema
const originalSchema = {
    properties: {
        hostname: { type: "string" },
        release: { type: "string" }
    }
};

// Migrated Schema (remove >> release, add >> version)
const migratedSchema = {
    properties: {
        hostname: { type: "string" },
        version: {
            type: "string",
            default: "v1.0.0"
        }
    }
}

const cfgMigrator = new ConfigMigrator(originalSchema, migratedSchema);
const migratedPayload = cfgMigrator.migrate({
    hostname: "localhost",
    release: "v1.0.0"
});

const expected = { hostname: "localhost", version: "v1.0.0" };
strictEqual(JSON.stringify(migratedPayload), JSON.stringify(expected));

API

Schema and payload should be plain JavaScript object.

{} || Object.create(null);

constructor(originalSchema: Object, migrateSchema: Object);

Create a new Configuration migrator.

migrate(payload: Object): Object

Migrate a given payload. The payload will be cloned deep (the original copy shall not be updated).

Dependencies

NameRefactoringSecurity RiskUsage
@slimio/isMinorLowType checker
fast-json-patch⚠️MajorHighInteract with JSON file
lodash.clonedeepMinorLowClone deep Objects
lodash.getMinorLowGet deep a value
lodash.setMinorLowSet deep a value

License

MIT