2.0.0 • Published 1 year ago

enty v2.0.0

Weekly downloads
81
License
MIT
Repository
github
Last release
1 year ago

Enty

enty npm enty circle

Introduction

Enty is a framework for managing data requested from back-ends and APIs. Instead of you manually storing requested data, Enty uses schemas to describe relationships and stores the data as normalized entities.

  • Views can declare what data they need.
  • There is practically no data fetching code.
  • Data given to views is always up to date.
  • Bad relationships in data become clear.
  • pairs wonderfully with graphql

Purpose

Any webapp that involves both a back and a front end will create entities. Unique pieces of data that are known by an id. The back end might call them models, the front end might call them application state, let's call them entities.

When the client side thinks of storing these entities in terms of endpoints and stores (or even actions and reducers) it's another set of hands touching the data. It allows more places for shady hacks to creep in. It allows more places for code to become brittle. It allows more places for the code to break.

On top of this you force the front end to recreate relationships between entities. Storing data by type in isolated stores logically makes sense, but when a view visually combines two entities (post with comments) you create a point where the front end needs to know exactly how to reconstruct this. This is not an insurmountable problem but as the code base grows so will the places where the front end has to know some specific detail and the places where things can go wrong.

In reality the front end doesn't care where the data came from or how it is stored. It just knows that it wants a certain number of entities and information about whether they have arrived yet.

Enty lets you describe the relationships of your entities through schemas. It is then able to store them in a normalized state. This means that they are not stored by request but by the unique id that they were given by the back-end.

Getting Started

Enty has two parts: A Schema and an EntityApi.

1. Schema

The first step in implementing Enty is to define your schema. This defines what relationships your entities have. A user might have a list of friends which are also users. So we can define that as a user

// entity/ApplicationSchema.js
import {
    MapSchema,
    ListSchema,
    EntitySchema,
} from 'enty';

var user = EntitySchema('user');
var userList = ListSchema(user);

user.define(MapSchema({
    friendList: userList
}))

export default MapSchema({
   user,
   userList
});

2. API

The second thing we need to do is to create our EntityApi from our schema;

// entity/EntityApi.js
import {EntityApi} from 'enty';
import ApplicationSchema from './ApplicationSchema';

const Api = EntityApi(ApplicationSchema, {
    core: payload => request('/graphql', payload)
});

export const {
    EntityStore,
    CoreQueryHock,
    CoreMutationHock,
} = Api;

3. Connect to react

// client.jsx
import {React} from 'react';
import {Provider} from 'react-redux';
import ReactDOM from 'react-dom';
import {EntityStore} from './entity/EntityApi';


ReactDOM.render(
    <Provider store={EntityStore}>
        <App />
    </Provider>,
    document.getElementById('app'),
);

4. Make a Query

// component/User.jsx
import {React} from 'react';
import {CoreQueryHock} from '../entity/EntityApi';

function User(props) {
    const {user} = props;
    return <img src={user.get('avatar')} />;
}

const withData = CoreQueryHock(props => {
    return {
        query: UserDataQuery,
        variables: {
            id: props.id
        }
    };
}, ['id']);

export default withData(User);

Entity Flow

  1. Props Change / OnMutate Triggered
    The Enty data flow begins when either a QueryHocked components props change or a MutationHocked component fires its onMutate callback. When this happens the corresponding promise creator in the API is fired.

  2. Data Request / Recieve
    The data request actions is triggered and the corresponding queryRequestState becomes a FetchingState. If the promise rejects the Error action is triggered, the requestState becomes an error and the flow finishes. If the promise resolves the receive action is triggered, the requestState becomes a SuccessState.

  3. Normalize
    The payload is passed into schema.normalize, which will in turn call schema.normalize recursively on its children as defined. Entities are stored under their schema type key and the result of their id attribute. Each entity is also passed through their constructor function which is given the current entity and the previous version if it exists.

  4. Results & Entities Stored
    The normalised entities are shallow merged with the previous state. The normalised result object is stored under its resultKey.

  5. Views Updated
    The update in state triggers a rerender. All hocked views select their data based on their result key. Schema.denormalize is given the new entity state and the normalised result object that matches their result key. As the result object is traversed denormalizeFilter is called on each entity. Any that fail the test will not be returned.

Entity Types

FAQ

What if I am using two Query/Mutation hocks

Use the options override!

const withQuery = CoreQueryHock(
    props => ({
        query: UserQuery, 
        variables: {
            id: props.id
        }
    }),
    {
        queryRequestStateProp: 'userRequestState'
    }
);

How do I load things?

Why is react-redux a peer dependency (it's not yet... but it should be)

How do I handle endpoints that return arrays?

We have found the cleanest way is to add a new service to your api and modify the data before it is given to Enty

// EntityApi.js
const Api = EntityApi(ApplicationSchema, {
    core: payload => request('/graphql', payload),
    userList: payload => request('/user', payload).then(data => ({userList: data}))
});

Do I have to export an MapSchema from my EntityApi?

3.0.0-alpha.1

1 year ago

3.0.0-alpha.0

1 year ago

3.0.0-alpha.3

1 year ago

3.0.0-alpha.2

1 year ago

2.0.0

1 year ago

2.0.0-alpha.16

2 years ago

2.0.0-alpha.7

2 years ago

2.0.0-alpha.9

2 years ago

2.0.0-alpha.11

2 years ago

2.0.0-alpha.10

2 years ago

2.0.0-alpha.6

2 years ago

2.0.0-alpha.15

2 years ago

2.0.0-alpha.14

2 years ago

2.0.0-alpha.13

2 years ago

2.0.0-alpha.12

2 years ago

2.0.0-alpha.3

3 years ago

2.0.0-alpha.4

3 years ago

2.0.0-alpha.5

3 years ago

2.0.0-alpha.1

4 years ago

2.0.0-alpha.2

4 years ago

1.1.0

4 years ago

1.1.0-alpha.7

4 years ago

1.1.0-alpha.6

4 years ago

1.1.0-alpha.5

4 years ago

1.1.0-alpha.4

4 years ago

1.1.0-alpha.3

4 years ago

1.1.0-alpha.2

4 years ago

1.1.0-alpha.1

4 years ago

1.1.0-alpha.0

4 years ago

2.0.0-alpha.0

4 years ago

1.0.0

5 years ago

1.0.0-alpha.16

5 years ago

1.0.0-alpha.15

5 years ago

1.0.0-alpha.14

5 years ago

1.0.0-alpha.13

5 years ago

1.0.0-alpha.12

5 years ago

1.0.0-alpha.9

5 years ago

1.0.0-alpha.8

5 years ago

1.0.0-alpha.7

5 years ago

1.0.0-alpha.6

5 years ago

1.0.0-alpha.5

5 years ago

1.0.0-alpha.4

5 years ago

0.50.1

5 years ago

1.0.0-alpha.3

5 years ago

1.0.0-alpha.2

5 years ago

1.0.0-alpha.1

5 years ago

1.0.0-alpha.0

5 years ago

0.50.0

6 years ago

0.50.0-alpha.2

6 years ago

0.50.0-alpha.1

6 years ago

0.50.0-alpha.0

6 years ago

0.49.1

6 years ago

0.49.0

6 years ago

0.49.0-6

6 years ago

0.49.0-5

6 years ago

0.49.0-4

6 years ago

0.49.0-3

6 years ago

0.49.0-2

6 years ago

0.49.0-1

6 years ago

0.48.2

6 years ago

0.49.0-0

6 years ago

0.48.1

6 years ago

0.48.0

6 years ago

0.48.0-2

6 years ago

0.48.0-1

6 years ago

0.48.0-0

6 years ago

0.47.1

6 years ago

0.47.0

6 years ago

0.46.0

7 years ago

0.45.1

7 years ago

0.45.0

7 years ago

0.44.0

7 years ago

0.44.0-2

7 years ago

0.44.0-1

7 years ago

0.43.2

7 years ago

0.44.0-0

7 years ago

0.43.1

7 years ago

0.43.0

7 years ago

0.42.0

7 years ago

0.41.0

7 years ago

0.40.0

7 years ago

0.39.0

7 years ago

0.38.1

7 years ago

0.38.0

7 years ago

0.37.2

7 years ago

0.37.1

7 years ago

0.37.0

7 years ago

0.36.1

7 years ago

0.36.0

7 years ago

0.35.0

7 years ago

0.34.0

7 years ago

0.33.1

7 years ago

0.33.0

7 years ago

0.32.3

7 years ago

0.32.2

7 years ago

0.32.1

7 years ago

0.32.0

7 years ago

0.32.0-10

7 years ago

0.32.0-9

7 years ago

0.32.0-8

8 years ago

0.32.0-7

8 years ago

0.32.0-6

8 years ago

0.32.0-5

8 years ago

0.32.0-4

8 years ago

0.32.0-3

8 years ago

0.32.0-2

8 years ago

0.32.0-1

8 years ago

0.31.1

8 years ago

0.32.0-0

8 years ago

0.31.0

8 years ago

0.30.1

8 years ago

0.30.0

8 years ago

0.29.0

8 years ago

0.28.0

8 years ago

0.27.0

8 years ago

0.26.0

8 years ago

0.25.0

8 years ago

0.24.0

8 years ago

0.23.0

8 years ago

0.22.0

8 years ago

0.21.1

8 years ago

0.21.0

8 years ago

0.20.0

8 years ago

0.19.1

8 years ago

0.19.0

8 years ago