1.0.1 • Published 3 years ago

redux-object v1.0.1

Weekly downloads
8,707
License
MIT
Repository
github
Last release
3 years ago

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.

OptionDefaultDescription
eagerfalseControls lazy loading for the child relationship objects. By default, lazy loading is enabled.
ignoreLinksfalseredux-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.
includeTypefalseInclude 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

1.0.1

3 years ago

1.0.0

3 years ago

0.5.10

5 years ago

0.5.9

5 years ago

0.5.8

5 years ago

0.5.7

5 years ago

0.5.6

6 years ago

0.5.5

6 years ago

0.5.4

6 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.5

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago