Search code examples
normalizr

Try to understand normalizr's schema.entity VS Array and Object


All:

I am trying to understand the relationship between Entity Array and Object:

  1. Are they just different format to describe diff structure of data? Or Entity is quite diff from the rest two?
  2. The normalized data result has a structure like {result:,entities:}, are the data structures only defined with schema.Entity put inside entities or so can schema.Array and Object? When I define a schema only use Object and Array, it seems nothing put in entities, I am not sure if it is my schema def fault or this is how normalizr work?
  3. If only schema.Entity() defined data can put into entities, then how can I put an data array into it, something like {0:.., 1:..,2:,}?

For exmaple, I have data like:

var data = [
    {
        id:"0",
        items:[
            {
                id: "0",
                data: {name:"data-0-0"}
            },
            {
                id: "1",
                data: {name:"data-0-1"}
            }
        ]
    },
    {
        id:"1",
        items:[
            {
                id: "0",
                data: {name:"data-1-0"}
            },
            {
                id: "1",
                data: {name:"data-1-1"}
            }
        ]
    }
]

const normalizedData = normalize(data, [{items:[{data:{}}]}]);

And the normalized data is like:

{
    "entities": {},
    "result": {
        "0": {
            "id": "0",
            "items": [
                {
                    "id": "0",
                    "data": {
                        "name": "data-1-0"
                    }
                }
            ]
        }
    }
}

Thanks


Solution

  • Question: Are they just different format to describe diff structure of data? Or Entity is quite diff from the rest two?

    Answer: Yes. An Entity is a singular object that has a unique identifier associated with it. Array and Object are more generic structures that can't be uniquely identified. In your case, it looks like you only need to use Array and Entity for the data you're describing.

    Question: Are the data structures only defined with schema? Entity put inside entities?

    Answer: Yes.