Skip to content

Instantly share code, notes, and snippets.

@lazyTai
Created April 29, 2018 08:47
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 lazyTai/0a4a673a65eee6117a673706cb329923 to your computer and use it in GitHub Desktop.
Save lazyTai/0a4a673a65eee6117a673706cb329923 to your computer and use it in GitHub Desktop.
export const array = [
{
"id": 1,
"title": "Blog Title",
"content": "Some really short blog content. Actually the least interesting post ever",
"created_at": "2016-01-10T23:07:43.248Z",
"updated_at": "2016-01-10T23:07:43.248Z",
"author": {
"id": 81,
"name": "Mr Shelby"
},
"comments": [
{
"id": 352,
"content": "First!",
"author": {
"id": 41,
"name": "Foo Bar"
}
}
]
}
]
import {normalize, schema, Array} from 'normalizr'
const post = new schema.Entity("post", {id: "id"})
const author = new schema.Entity("author", {id: "id"})
const postComments = new schema.Entity("postComments", {id: "id"})
post.define({
author: author,
comments: [postComments]
})
postComments.define({author: author})
var result = normalize(require('./data').array, [post])
console.log(JSON.stringify(result))
{
"entities": {
"author": {
"41": {
"id": 41,
"name": "Foo Bar"
},
"81": {
"id": 81,
"name": "Mr Shelby"
}
},
"postComments": {
"352": {
"id": 352,
"content": "First!",
"author": 41
}
},
"post": {
"1": {
"id": 1,
"title": "Blog Title",
"content": "Some really short blog content. Actually the least interesting post ever",
"created_at": "2016-01-10T23:07:43.248Z",
"updated_at": "2016-01-10T23:07:43.248Z",
"author": 81,
"comments": [
352
]
}
}
},
"result": [
1
]
}
@lazyTai
Copy link
Author

lazyTai commented Apr 29, 2018

how to use normalizr
no thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment