Search code examples
javascriptreactjsreduxfluxnormalizr

Normalizr - not resulting expected


import { normalize, Schema, arrayOf } from 'normalizr';

var ListA = [
    {
        id:1, 
        text: "text1",
        comments : [
            {
                id: 232,
                text: "asfasd"
            },
            {
                id: 333,
                text: "abcsss"
            }
        ]
    }, 
    {id:2, text:"text2", comments:[]}, 
    {id:3, text:"text3", comments:[]}
    ]

I am trying to normalize this simple response. I am not sure what is wrong on what I am doing or I haven't understood normalizr documentation.

const post = new Schema('posts');
// const comment = new Schema('comments');
// const collection = new Schema('collections');

// post.define({
//  comments : comment,
//  collections : arrayOf(collection)
// });
ListA = normalize(ListA, {
    posts: arrayOf(post)
});

console.log(ListA);

This just results same response on "result" object, and entities object is empty. Can someone please help me. First I am trying to normalize just the Post and after then would be comment aswell.. But I haven't been able to cross the first step.


Solution

  • Few examples of using normalizr

    1) Normalize simple object
    just for example

    import { Schema, arrayOf } from 'normalizr';
    const postSchema = new Schema('posts');
    
    // simple post object
    var post = {
      id:1,
      title: "some title"
    };
    
    console.log(normalize(post, postSchema));
    

    result will be

    {
       "entities":{
          "posts":{
             "1":{
                "id":1,
                "title":"some title"
             }
          }
       },
       "result":1
    }
    

    2) Normalize array of objects

    import { Schema, arrayOf } from 'normalizr';
    const postSchema = new Schema('posts');
    
    // array of posts
    var posts = [
      {
        id:1,
        title: "foo"
      },
      {
        id:2,
        title: "far"
      },
      {
        id:3,
        title: "baz"
      }
    ];
    
    console.log(normalize(posts, arrayOf(postSchema)));
    

    result will be

    {
       "entities":{
          "posts":{
             "1":{
                "id":1,
                "title":"foo"
             },
             "2":{
                "id":2,
                "title":"far"
             },
             "3":{
                "id":3,
                "title":"baz"
             }
          }
       },
       "result":[
          1,
          2,
          3
       ]
    }
    

    3) Normalize complex object

    const postSchema = new Schema('posts');
    const authorSchema = new Schema('authors');
    const commentSchema = new Schema('comments');
    
    postSchema.define({
      author: authorSchema,
      comments: arrayOf(commentSchema)
    });
    
    // complex post object
    var post = {
      id:1,
      title: "foo",
      author: {
        id: 201,
        name: "Bar Baz"
      },
      comments: [
        {
          id: 1002,
          body: "Some content"
        },
        {
          id: 1003,
          body: "Some content 1003"
        },
        {
          id: 1004,
          body: "Some content 1004"
        }
      ]
    };
    
    console.log(normalize(post, postSchema));
    

    result will be

    {
      "entities":{
        "posts":{
          "1":{
            "id":1,
              "title":"foo",
              "author":201,
              "comments":[
              1002,
              1003,
              1004
            ]
          }
        },
        "authors":{
          "201":{
            "id":201,
              "name":"Bar Baz"
          }
        },
        "comments":{
          "1002":{
            "id":1002,
              "body":"Some content"
          },
          "1003":{
            "id":1003,
              "body":"Some content 1003"
          },
          "1004":{
            "id":1004,
              "body":"Some content 1004"
          }
        }
      },
      "result":1
    }
    

    So you can try

    ListA = normalize(ListA, arrayOf(post));
    

    instead of

     ListA = normalize(ListA, {
        posts: arrayOf(post)
     });