Search code examples
javascriptreactjsredux-orm

How to structure the Model for Redux ORM


I'm getting the response similar to the following format from server.

{"channels": [{"name":"discovery", "id":"12", 
              "details":{"src":"link", "logo":"imagelink"}}]

I'm planning to use Redux-Orm to manage the state in the store. When I'm trying to define the model, I'm having confusions. One way is to define Channel Model with name and id as attributes, details as one to one mapping and Details Model with src, logo attributes as below.

const channel = class Channel extends Model {};
channel.fields = {
   name: attr(),
   id: attr(),
   details: oneToOne('details', 'channels')
}

const details = class Details extends Model {};
details.fields = {
   src: attr(),
   logo: attr()
}

Or Should I define a single model class which represents the response as is? If so, how to define and access it?


Solution

  • If you want to have a Detail model, your backend must identify it with an id like the Channel model, and then you may do a oneToOne relation.

    That being said, using a single model or two is totally depending on how they'll interact in your app, and may grow. If your details field won't grow much more, my totally personal point of view would be to keep it in a single Channel model. you'd access it through channel.details or channel.details.src transparently.

    IMO, oneToOne simple relation like that does not need a specific model.