This Gist was automatically created by Carbide, a free online programming environment.
Created
December 6, 2018 03:34
-
-
Save carbide-public/f86a7620b7621703821a3d938fb38327 to your computer and use it in GitHub Desktop.
untitled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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