Skip to content

Instantly share code, notes, and snippets.

@fuyi
Last active January 12, 2017 12:54
Show Gist options
  • Save fuyi/d60c464024b358c1913402b19ffcec30 to your computer and use it in GitHub Desktop.
Save fuyi/d60c464024b358c1913402b19ffcec30 to your computer and use it in GitHub Desktop.
Model normalize/denormalize flow

Flow

  1. Define nested Schema
  2. Saga middleware detect a _REQUEST Action, which trigger a API request
  3. API request success trigger a _REQUEST_SUCCESS Action, api client get API response, map it with model to be normalized to, if the match found, normalizer flatten the nested JSON objects into flat entities structure, then send to store
  4. Redux store puts the normalized entities in place automatically
  5. ?? We need to manually update associated key to parent object, can this be automated?
  6. When passing data from Store to Container, we denormalize the objects by recursively looking into the key -> object mapping, reassemble the parent object. If any child object is missing, we report a error in console

Interesting files to look into:

  • redux/sagas/client/client.js - normalize object according to schema definition
  • redux/models/index.js - recursive schema definition
  • redux/helpers/denormalizer.js - recursive denormalize object from Redux store to nested object structure

Pitfalls:

  • New association need to be defined and exported from schema file
  • model to be normalized to needs to be defined with ACTION object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment