normalizr-strategies
v0.0.4
Published
An easy way to chain normalizr strategies in a verbose way
Readme
Normalize-Strategies
An easy method of composing normalizr strategies
npm:
npm install normalizr-strategies --saveYarn (note that yarn add automatically saves the package to the dependencies in package.json):
yarn add normalizr-strategiesUsage
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'),
)
});Built in strategies
renameKeys
requiredFields
hasParentId
Custom strategies
A strategy is a function that returns an object in the following structure
{
strategy: function (config, value, parent, key) {
// ...apply strategy
return value;
}
}Example
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(),
)
});