Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

redux-object

yury-dymov97.1kMIT1.0.1

Builds complex JS object from normalized redux store. Best works with json-api-normalizer

redux, normalizr, JSON, API

readme

redux-object

npm version Downloads Build Status Coverage Status

Builds complex JS object from normalized redux store. Best works with json-api-normalizer.

DEMO - https://yury-dymov.github.io/json-api-react-redux-example/

Demo sources and description - https://github.com/yury-dymov/json-api-react-redux-example

API

Library provides build function, which takes 4 parameters: redux state part, object type, ID or an array of IDs or null, and options.

If ID is provided in a form of array, multiple objects are fetched. If ID is null, all objects of selected type are fetched.

Option Default Description
eager false Controls lazy loading for the child relationship objects. By default, lazy loading is enabled.
ignoreLinks false redux-object doesn't support remote objects. This option suppresses the exception thrown in case user accesses a property, which is not loaded to redux store yet.
includeType false Include the record type as a property 'type' on each result. This is particularly useful for identifying the record type returned by a polymorphic relationship.
import build from 'redux-object';

/*
state:
{
  data: {
    post: {
      "2620": {
        attributes: {
          "text": "hello",
          "id": 2620
        },
        relationships: {
          daQuestion: {
            id: "295",
            type: "question"
          },
          liker: [{
              id: "1",
              type: "user"
            }, {
              id: "2",
              type: "user",
            }, {
              id: "3",
              type: "user"
            }
          ],
          comments: []
        }
      }
    },
    question: {
      "295": {
        attributes: {
          text: "hello?"
        }
      }
    },
    user: {
      "1": {
        attributes: {
          id: 1,
          name: "Alice"
        }
      },
      "2": {
        attributes: {
          id: 2,
          name: "Bob"
        }
      },
      "3": {
        attributes: {
          id: 3,
          text: "Jenny"
        }
      }
    },
    meta: {
      'posts/me': {
        data: {
          post: '2620'
        }
      }
    }
  }
};
*/

const post = build(state.data, 'post', '2620');

console.log(post.id); // -> 2620
console.log(post.text); // -> hello
console.log(post.daQuestion); // -> { id: 295, text: "hello?" }
console.log(post.liker.length); //-> 3
console.log(post.liker[0]); // -> { id: 1, name: "Alice" }

// Other examples

const post = build(state.data, 'post', '2620', { eager: true });
const post = build(state.data, 'post', '2620', { eager: false, ignoreLinks: true });

Child objects are lazy loaded unless eager option is explicitly provided.

License

MIT (c) Yury Dymov

changelog

Version 1.0.1 (30th April 2021)

IE 11 backward compatibility support via core-js 3 polyfilling

Version 1.0.0 (2nd December 2020)

Deps update

Version 0.5.10 (17th June 2019)

Updated vulnerable deps

Version 0.5.9 (17th January 2019)

Deps update; istanbul -> nyc for code coverage reporting

Version 0.5.8 (16th January 2019)

Introduced resolved property (https://github.com/yury-dymov/redux-object/pull/38)

Version 0.5.7 (16th November 2018)

Added support for links (https://github.com/yury-dymov/redux-object/pull/37). Updated vulnerable deps

Version 0.5.6 (29th March 2018)

Added support for immutable.js (https://github.com/yury-dymov/redux-object/pull/34)

Version 0.5.5 (2nd January 2018)

Fixes bug, when object doesn't have any attributes (https://github.com/yury-dymov/redux-object/pull/32)

Version 0.5.4 (5th November 2017)

Private cache attributes are not enumerable for object returned by build (https://github.com/yury-dymov/redux-object/pull/31)

Version 0.5.3 (3d November 2017)

Object properties are enumerable (https://github.com/yury-dymov/redux-object/pull/25)

Version 0.5.2 (25th September 2017)

Added 'meta' support per spec (https://github.com/yury-dymov/redux-object/issues/22)

Version 0.5.1 (12th September 2017)

Fixed returning empty array for relationships without data when ignoreLinks is true (https://github.com/yury-dymov/redux-object/issues/20)

Version 0.5.0 (13th July 2017)

Accessing related object not in the store returning id instead of null if related object is not loaded but id is available (https://github.com/yury-dymov/redux-object/issues/18)

Version 0.4.5 (09th July 2017)

Type is optionally propogated to objects (https://github.com/yury-dymov/redux-object/issues/16)

Version 0.4.4 (26th May 2017)

Added unminified version for development https://github.com/yury-dymov/redux-object/issues/15

Version 0.4.2 (6th May 2017)

Removed lodash from dependencies

Version 0.4.1 (5th May 2017)

Added options to disable lazy loading behavior for relationships and to suppress throwing error for remote relationships