An easy method of composing normalizr strategies
npm:
npm install normalizr-strategies --save
Yarn (note that yarn add
automatically saves the package to the dependencies
in package.json
):
yarn add normalizr-strategies
Use with Node.js, Browserify, or webpack:
import {schema} from 'normalizr';
import strategies, {renameKeys, requiredFields} from 'normalizr-strategies';
const MySchema = new schema.Entity('myschema', {}, {
processStrategy: strategies(
renameKeys({
server_id: 'id', // names 'server_id' to 'id'
}),
requiredFields('server_id', 'anotherRequiredField'),
)
});
A strategy is a function that returns an object in the following structure
{
strategy: function (config, value, parent, key) {
// ...apply strategy
return value;
}
}
let addHardCodedProps = () => {
return {
prop1: 1,
prop2: 1,
strategy: function (config, value) {
return {
...value,
prop1: config.prop1,
prop2: config.prop2,
}
}
}
}
// Usage
const MySchema = new schema.Entity('myschema', {}, {
processStrategy: strategies(
renameKeys({
server_id: 'id', // names 'server_id' to 'id'
}),
addHardCodedProps(),
)
});