Skip to content

Instantly share code, notes, and snippets.

@carbide-public
Created December 6, 2018 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbide-public/f86a7620b7621703821a3d938fb38327 to your computer and use it in GitHub Desktop.
Save carbide-public/f86a7620b7621703821a3d938fb38327 to your computer and use it in GitHub Desktop.
untitled
import { normalize } from 'normalizr';
import ProductDocument from './product-document';
const response = {
documents: [
{ record: { product: { id: 'a' } } },
{ record: { product: { id: 'b' } } },
{ record: { product: { id: 'c' } } }
]
};
const normal = normalize(response.documents, [ProductDocument]);
console.log(JSON.stringify(normal))
import { schema } from 'normalizr';
export const ENTITY_NAME = 'products';
export const idAttribute = (input) => input.record.product.id;
export const definition = {};
/**
* Extracts the product data from a document
* @param {Object} document document of product
* @returns {Object} product data
*/
export const processStrategy = ( { record: { product, ...rest } } ) => ( { ...rest, ...product } );
export const options = {
idAttribute,
processStrategy
};
export const ProductDocument = new schema.Entity( ENTITY_NAME, definition, options );
export default ProductDocument;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment