Skip to content

Schemas

Michael Niday edited this page Jun 6, 2018 · 2 revisions

Schemas help to define the shape of the normalized records

Nested associations inside the records returned in item/items

Redux-capacitor can hydrate records with any nested associations if it has the schema set up here.

For example, if record.js defines both the people and planet schemas, you can configure the record to have homeworld with a record corresponding to the fetched planets in the store

import { schemasFromFieldDefinitions } from 'redux-capacitor'

const fieldDefinitions = {
  people: {
    id: null,
    url: null,
    name: null,
    homeworld: null
  },
  planet: {
    id: null,
    name: null
  }
}

const schemas = schemasFromFieldDefinitions(fieldDefinitions)

schemas.people.define({
  homeworld: schemas.planet
})

Now despite the api request for people returning homeworld as an id collection.item.homeworld if that homeworld is in the store, collection.item.homeworld will be hydrated with that record.

Setting which ids to use for keys in the store

Modifying which attribute gets used as the ID on a record

schemas.people = new normalizr.schema.Entity('people', {}, { idAttribute: record => record.url })

This is often not needed if the API returns records that have a unique ID, but could be utilized for getting a specific key structure in the store

Clone this wiki locally