# react-entity-getter

> A helper class to query redux state for entities

Latest version **0.0.8** (published 2017-06-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-entity-getter
pnpm add react-entity-getter
yarn add react-entity-getter
bun add react-entity-getter
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2017-06-08 |
| First published | 2016-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Known vulnerabilities | 0 (+10 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | The Gnar Company, Inc. |
| Maintainers | mikestone14 |
| Keywords | react, orm, redux, entityGetter, entity-getter, entities |

## Links

- npm: https://www.npmjs.com/package/react-entity-getter
- Repository: https://github.com/TheGnarCo/react-entity-getter
- Issues: https://github.com/TheGnarCo/react-entity-getter/issues
- npm.io page: https://npm.io/package/react-entity-getter

## Dependencies (9)

- [expect](https://npm.io/package/expect.md) ^1.14.0
- [lodash](https://npm.io/package/lodash.md) 4.16.4
- [babel-core](https://npm.io/package/babel-core.md) ^6.4.5
- [babel-loader](https://npm.io/package/babel-loader.md) ^6.2.1
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^6.3.19
- [babel-preset-es2015](https://npm.io/package/babel-preset-es2015.md) ^6.14.0
- [babel-preset-stage-0](https://npm.io/package/babel-preset-stage-0.md) ^6.5.0
- [babel-plugin-transform-runtime](https://npm.io/package/babel-plugin-transform-runtime.md) ^6.4.3
- [babel-plugin-add-module-exports](https://npm.io/package/babel-plugin-add-module-exports.md) ^0.1.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2017-06-08
- 0.0.7 — 2017-06-08
- 0.0.6 — 2017-03-30
- 0.0.5 — 2016-12-07
- 0.0.4 — 2016-12-07
- 0.0.3 — 2016-11-02
- 0.0.2 — 2016-09-10

## README

# React Entity Getter

[![npm version](https://badge.fury.io/js/react-entity-getter.svg)](https://badge.fury.io/js/react-entity-getter)
[![CircleCI](https://circleci.com/gh/TheGnarCo/react-entity-getter.svg?style=svg)](https://circleci.com/gh/TheGnarCo/react-entity-getter)

The React Entity Getter is a helper class with functions that assist in retrieving redux entitites from state.

This is particularly helpful for connected React components in the `mapStateToProps` function.

## Getting Started

1) Add this package to your package.json file.

```
$ npm install --save react-entity-getter
```

2) Create a utility file to hold your state entity getter.

3) In the utility file, create a function that returns a path to your entities in redux state.

```js
const pathToEntity = (entityName) => {
  return `api.data.${entityName}.data`;
};
```

4) In the utility file, import this package and create a new instance of the `EntityGetter` class, passing in your path to entities in redux state.

```js
// ./utilities/entityGetter.js
import entityGetter from 'react-entity-getter';

const pathToEntity = (entityName) => {
  return `api.data.${entityName}.data`;
};

export default entityGetter(pathToEntity);
```

5) In your connected React components, import your `entityGetter` utility.

```js
// ./pages/HomePage.jsx
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import entityGetter from '../utilities/entityGetter';

class HomePage extends Component {
  static propTypes = {
    dispatch: PropTypes.func,
  };

  render () {
    const { user } = this.props;

    return (
      <div>Hello, {user.firstName}</div>
    );
  }
}

const mapStateToProps = (state) => {
  const user = entityGetter(state).get('users').findBy({ isCurrentUser: true });

  return { user };
};

export default connect(mapStateToProps)(HomePage);
```

## Attributes

### entities

* Returns all entities of the `entityName` in redux state.

Example:

```js
const users = stateEntityGetter(state).get('users').entities;
```

## Functions

### findBy

```
findBy<A, B, C>(filters:A, options:B?) -> C?
```

* Returns the first entity matching the attributes passed to the function.

#### Options
* ignoreCase: matches the attribute value of an entity regardless of the case

#### Examples:

```js
const user = stateEntityGetter(state).get('users').findBy({ isCurrentUser: true }); // returns a single User entity
const post = stateEntityGetter(state).get('posts').findBy({
  title: 'My post',
  published: true,
}); // returns a single Post entity
```

```js
const users = stateEntityGetter(state).get('users');
const user = users.findBy({ first_name: 'mike' }, { ignoreCase: true }); // returns a single User entity
```

### where

```
where<A,B, C>(filters:A, options:B?) -> [C]
```

* Returns an array of all entities matching the attributes passed to the function.

#### Options
* ignoreCase: matches the attribute value of an entity regardless of the case

#### Examples:

```js
const entities = stateEntityGetter(state);
const user = entities.get('users').findBy({ isCurrentUser: true });
const post = entities.get('posts').findBy({ title: 'My post' });
const comments = entities.get('comments').where({
  post_id: post.id,
  user_id: user.id,
}); // returns an array of comments related to the post and user
const billUsers = entities.get('users').where({ first_name: 'bill' }, { ignoreCase: true });
```

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