0.1.5 • Published 3 years ago
@twogate/migrason v0.1.5
migrason
migrason (/maigréisɒn/) is a migration tool for JSON.
Run
# Run migration script
# $ node <migrate script> <old input json> <output filename>
node 001_add_properties.js 001.json ./out.json
Example
Adding properties
Assume 001.json as input json. When you want to migrate it to 002.json, you can write a migrate file like 001_add_properties.js.
change(prev) {
// The previous JSON can be accessed via `prev`
console.log(prev)
// Apply the default values
// Properties with default values that are not in the previous JSON will be added
// If the previous JSON already has a property same as the default property, the value of the previous JSON is used.
return this.applyDefaults(prev)
}
Removing properties
To remove properties, use the delete
operator of JavaScript. An example is 002_delete_and_rename_props.js.
change(prev) {
// assign object which is applied default values
const next = this.applyDefaults(prev)
// copying property
next.tabs.layout = prev.tabs.tabs
// removing property
delete next.theme.appTheme.accent
delete next.tabs.tabs
return next
}
Customizing (de)serializer
To customize (de)serializer, you can override (de)serializer methods. 001_add_properties_yaml.js is an example that overrides (de)serializer to read/write yaml files.
deserialize(input) {
return yaml.safeLoad(input)
}
serialize(next) {
return yaml.safeDump(next)
}
Hint
- If you write a default value which is not existing on old JSON, the default value is used for output.
- Properties that are not in the default value are not removed from the old JSON and must be removed explicitly.