Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Commit

Permalink
Remove need to have id attribute in state
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyw committed Jan 14, 2017
1 parent d664934 commit 2b49d0b
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 197 deletions.
189 changes: 84 additions & 105 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ npm install denormalize-with-state --save

```js
import { normalize, Schema, arrayOf } from 'normalizr';
import { denormalizeWithState } from './src/index';
import { denormalizeWithState } from 'denormalize-with-state';

export const postSchema = new Schema('posts');
export const postListSchema = arrayOf(postSchema);
Expand All @@ -31,64 +31,43 @@ postSchema.define({
});


export const normalizedData = normalize([
{
id: 1,
title: 'post A',
comments: [
{
id: 1,
text: 'comment A',
author: {
id: 1,
text: "author A's message",
contact: {
id: 1,
email: 'hello@abc.com',
},
},
},
{
id: 2,
text: 'comment B',
author: {
id: 2,
text: "author B's message",
},
},
],
eexport const entities = {
posts: {
1: {
id: 1,
title: 'post A',
comments: [1, 2],
},
2: {
id: 2,
title: 'post B',
comments: [3, 4],
},
},
{
id: 2,
title: 'post B',
comments: [
{
id: 3,
text: 'comment C',
author: {
id: 3,
text: "author C's message",
},
},
{
id: 4,
text: 'comment D',
author: {
id: 4,
text: "author D's message",
},
},
],
comments: {
1: { id: 1, text: 'comment A', author: 1 },
2: { id: 2, text: 'comment B', author: 2 },
3: { id: 3, text: 'comment C', author: 3 },
4: { id: 4, text: 'comment D', author: 4 },
},
], postListSchema);
author: {
1: { id: 1, text: "author A's message", contact: 1 },
2: { id: 2, text: "author B's message" },
3: { id: 3, text: "author C's message" },
4: { id: 4, text: "author D's message" },
},
contact: {
1: { id: 1, email: 'hello@abc.com' },
},
};


const state = {
Post: {
list: {
result: {
1: { id: 1, isLoading: false, tag: 'cool' },
2: { id: 2, isLoading: true, tag: 'super' },
1: { isLoading: false, tag: 'cool' },
2: { isLoading: true, tag: 'super' },
},
},
view: {
Expand All @@ -101,94 +80,94 @@ const state = {
Comment: {
list: {
result: {
1: { id: 1, isLoading: false },
2: { id: 2, isLoading: true },
1: { isLoading: false },
2: { isLoading: true },
},
},
},
Author: {
list: {
result: {
3: { id: 3, name: 'cool author C' },
4: { id: 4, name: 'cool author D' },
3: { name: 'cool author C' },
4: { name: 'cool author D' },
},
},
},
Contact: {
list: {
result: {
1: { id: 1, name: 'The one' },
1: { name: 'The one' },
},
},
},
};


const posts = denormalizeWithState(state.Post.list.result, normalizedData.entities, postListSchema, {
const posts = denormalizeWithState(state.Post.list.result, entities, postListSchema, {
posts: state.Post.list.result,
comments: state.Comment.list.result,
author: state.Author.list.result,
contact: contact => ({ ...contact, email: contact.email.toUpperCase() }),
});

console.log(posts);
console.log(JSON.stringify(posts, null, 2));
// [
// {
// id: 1,
// isLoading: false,
// tag: 'cool',
// title: 'post A',
// comments: [
// "isLoading": false,
// "tag": "cool",
// "id": 1,
// "title": "post A",
// "comments": [
// {
// id: 1,
// isLoading: false,
// text: 'comment A',
// author: {
// id: 1,
// text: "author A's message",
// contact: {
// id: 1,
// email: "HELLO@ABC.COM",
// },
// },
// "isLoading": false,
// "id": 1,
// "text": "comment A",
// "author": {
// "id": 1,
// "text": "author A's message",
// "contact": {
// "id": 1,
// "email": "HELLO@ABC.COM"
// }
// }
// },
// {
// id: 2,
// isLoading: true,
// text: 'comment B',
// author: {
// id: 2,
// text: "author B's message",
// },
// },
// ],
// "isLoading": true,
// "id": 2,
// "text": "comment B",
// "author": {
// "id": 2,
// "text": "author B's message"
// }
// }
// ]
// },
// {
// id: 2,
// isLoading: true,
// tag: 'super',
// title: 'post B',
// comments: [
// "isLoading": true,
// "tag": "super",
// "id": 2,
// "title": "post B",
// "comments": [
// {
// id: 3,
// text: 'comment C',
// author: {
// id: 3,
// name: 'cool author C',
// text: "author C's message",
// },
// "id": 3,
// "text": "comment C",
// "author": {
// "name": "cool author C",
// "id": 3,
// "text": "author C's message"
// }
// },
// {
// id: 4,
// text: 'comment D',
// author: {
// id: 4,
// name: 'cool author D',
// text: "author D's message",
// },
// },
// ],
// },
// "id": 4,
// "text": "comment D",
// "author": {
// "name": "cool author D",
// "id": 4,
// "text": "author D's message"
// }
// }
// ]
// }
// ]
```

Expand Down
93 changes: 36 additions & 57 deletions demo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { normalize, Schema, arrayOf } from 'normalizr';
import { denormalizeWithState } from './src/index';

export const postSchema = new Schema('posts');
export const postListSchema = arrayOf(postSchema);
export const commentSchema = new Schema('comments');
Expand All @@ -19,64 +18,44 @@ postSchema.define({
comments: arrayOf(commentSchema),
});

export const normalizedData = normalize([
{
id: 1,
title: 'post A',
comments: [
{
id: 1,
text: 'comment A',
author: {
id: 1,
text: "author A's message",
contact: {
id: 1,
email: 'hello@abc.com',
},
},
},
{
id: 2,
text: 'comment B',
author: {
id: 2,
text: "author B's message",
},
},
],

const entities = {
posts: {
1: {
id: 1,
title: 'post A',
comments: [1, 2],
},
2: {
id: 2,
title: 'post B',
comments: [3, 4],
},
},
{
id: 2,
title: 'post B',
comments: [
{
id: 3,
text: 'comment C',
author: {
id: 3,
text: "author C's message",
},
},
{
id: 4,
text: 'comment D',
author: {
id: 4,
text: "author D's message",
},
},
],
comments: {
1: { id: 1, text: 'comment A', author: 1 },
2: { id: 2, text: 'comment B', author: 2 },
3: { id: 3, text: 'comment C', author: 3 },
4: { id: 4, text: 'comment D', author: 4 },
},
], postListSchema);
author: {
1: { id: 1, text: "author A's message", contact: 1 },
2: { id: 2, text: "author B's message" },
3: { id: 3, text: "author C's message" },
4: { id: 4, text: "author D's message" },
},
contact: {
1: { id: 1, email: 'hello@abc.com' },
},
};


const state = {
Post: {
list: {
result: {
1: { id: 1, isLoading: false, tag: 'cool' },
2: { id: 2, isLoading: true, tag: 'super' },
1: { isLoading: false, tag: 'cool' },
2: { isLoading: true, tag: 'super' },
},
},
view: {
Expand All @@ -89,30 +68,30 @@ const state = {
Comment: {
list: {
result: {
1: { id: 1, isLoading: false },
2: { id: 2, isLoading: true },
1: { isLoading: false },
2: { isLoading: true },
},
},
},
Author: {
list: {
result: {
3: { id: 3, name: 'cool author C' },
4: { id: 4, name: 'cool author D' },
3: { name: 'cool author C' },
4: { name: 'cool author D' },
},
},
},
Contact: {
list: {
result: {
1: { id: 1, name: 'The one' },
1: { name: 'The one' },
},
},
},
};


const posts = denormalizeWithState(state.Post.list.result, normalizedData.entities, postListSchema, {
const posts = denormalizeWithState(state.Post.list.result, entities, postListSchema, {
posts: state.Post.list.result,
comments: state.Comment.list.result,
author: state.Author.list.result,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "denormalize-with-state",
"version": "0.1.3",
"version": "0.1.4",
"description": "denormalize-with-state takes data denormalized by denormalizr and merges in extra state.",
"keywords": [
"normalizr",
Expand Down
Loading

0 comments on commit 2b49d0b

Please sign in to comment.