# redux-object

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

Latest version **1.0.1** (published 2021-05-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-object
pnpm add redux-object
yarn add redux-object
bun add redux-object
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-05-01 |
| First published | 2016-12-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 34.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 138 |
| Author | Yury Dymov |
| Maintainers | yury-dymov |
| Keywords | redux, normalizr, JSON, API |

## Links

- npm: https://www.npmjs.com/package/redux-object
- Repository: https://github.com/yury-dymov/redux-object
- Homepage: https://github.com/yury-dymov/redux-object#readme
- Issues: https://github.com/yury-dymov/redux-object/issues
- npm.io page: https://npm.io/package/redux-object

## Dependencies (1)

- [core-js](https://npm.io/package/core-js.md) 3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-05-01
- 1.0.0 — 2020-12-02
- 0.5.10 — 2019-06-18
- 0.5.9 — 2019-01-17
- 0.5.8 — 2019-01-17
- 0.5.7 — 2018-11-16
- 0.5.6 — 2018-03-29
- 0.5.5 — 2018-01-02
- 0.5.4 — 2017-12-05
- 0.5.3 — 2017-11-03
- 0.5.2 — 2017-09-25
- 0.5.1 — 2017-09-12
- 0.5.0 — 2017-07-13
- 0.4.5 — 2017-07-10
- 0.4.4 — 2017-05-26
- … 10 more at https://npm.io/package/redux-object/versions

## README

# redux-object

[![npm version](https://img.shields.io/npm/v/redux-object.svg?style=flat)](https://www.npmjs.com/package/redux-object)
[![Downloads](http://img.shields.io/npm/dm/redux-object.svg?style=flat-square)](https://npmjs.org/package/redux-object)
[![Build Status](https://img.shields.io/travis/yury-dymov/redux-object/master.svg?style=flat)](https://travis-ci.org/yury-dymov/redux-object)
[![Coverage Status](https://coveralls.io/repos/github/yury-dymov/redux-object/badge.svg?branch=master)](https://coveralls.io/github/yury-dymov/redux-object?branch=master)

Builds complex JS object from normalized redux store. Best works with [json-api-normalizer](https://github.com/yury-dymov/json-api-normalizer).

DEMO - [https://yury-dymov.github.io/json-api-react-redux-example/](https://yury-dymov.github.io/json-api-react-redux-example/)

Demo sources and description - [https://github.com/yury-dymov/json-api-react-redux-example](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. |


```JavaScript
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

---
_Source: https://npm.io/package/redux-object · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
