0.0.4 • Published 7 years ago

normalizr-strategies v0.0.4

Weekly downloads
83
License
Apache-2.0
Repository
github
Last release
7 years ago

Normalize-Strategies

An easy method of composing normalizr strategies

Install with npm or Yarn:

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

Usage

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(),
    )
});