Pojo Migrator
npm install pojo-migrator -S
A simple utility for migrating data stored in a a simple pojo (Plan Ol' Javascript Object).
Originally built for storing configuration values in the localStorage API, but could work with any synchronous object serialization protocol.
This module is entirely synchronous right now, because localStorage
is, but could be easily modified to suit different needs.
var Migrator = // In this example, our existing data looks like this.var persistedData = // Note the mandatory and reserved `meta` key, // with a mandatory `version` number. meta: version: 1 // All your data is held in your own structure // Under the root `data` key. data: foo: 'bar' var migrator = // Must provide a method to load existing data. // Defaults to empty object ({}) { return persistedData } // Must provide a method to persist updated data { persistedData = data } migrations: // Each `migrate` function will be run in order. // It will be passed the contents of the `data` key. // The `meta` object and version will be updated automatically. version: 1 { return dataconfig // Will error if run } version: 2 { var result = {} if datafoo resultfoos = datafoo return result } // Migrations do not need to be in order // As long as their version keys are ordered. version: 0 { var result = {} if datafoo resultfoo = data return result } // The result of `getData` is just your `data` key.// You can pretty much ignore the versioning once you're set up!var result = migrator // After making changes, be sure to tell your migrator to `saveData`.// This will automatically persist with the method you've defined in your// `setData` method, but keeping the `meta` key up to date with the version.resultfoosmigrator