Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
allanhortle committed Dec 25, 2023
1 parent e2e1042 commit e89bbec
Showing 1 changed file with 26 additions and 39 deletions.
65 changes: 26 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,75 @@

Enty is a normalized cache for managing data requested from back-ends. Instead of you manually storing requested data, Enty uses schemas to describe relationships and stores the data in a normalized form.

Views declare which entities they would like, and Enty manages fetching and storing their relationships. Because the entities are stored in a normalized graph, you don't have to worry about how they are updated.
Components declare which entities they would like, and Enty manages fetching and storing their relationships. Because the entities are stored in a normalized graph, you don't have to worry about how they are updated.


## Code Samples

Enty has two parts: A Schema and an EntityApi.
Enty has two parts: Schemas and request hooks.

### 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
The first step in implementing Enty is to define your schemas. A schema is the information about how entities are related to each other. In this example a users has a list of friends which are also users.

```js
// entity/ApplicationSchema.js
import {
ObjectSchema,
ArraySchema,
EntitySchema,
} from 'enty';

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

user.shape = new ObjectSchema({
friendList: userList
});
// ./schemas.js
import {ObjectSchema, ArraySchema, EntitySchema} from 'react-enty';

export default new ObjectSchema({
user,
userList
});
export const UserSchema = new EntitySchema('user');
export const UserListSchema = new ArraySchema(user);

UserSchema.shape = new ObjectSchema({friendList: UserListSchema});
```

### 2. API
The second thing we need to do is to create our EntityApi from our schema;
The second thing we need to do is to create a request hook to fetch users

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

export default EntityApi({
user: (variables) => request('/graphql', {query: UserQuery, variables}),
userList: (variables) => request('/graphql', {query: UserListQuery, variables})
}, ApplicationSchema);
// api.js
import {createRequestHook} from 'react-enty';
import {UserSchema} from './schemas';

export const user = createRequestHook({
name: 'user',
schema: UserSchema,
request: (id) => request(`/user/${id}`)
});
```

### 3. Connect to react

```jsx
```js
// index.js
import {React} from 'react';
import Api from './EntityApi';
import {EntityProvider} from 'react-enty';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
<Api.EntityProvider>
<EntityProvider>
<App />
</Api.EntityProvider>,
</EntityProvider>,
document.getElementById('app'),
);
```

### 4. Make a Query

```jsx
```js
// ./App.js
import {React} from 'react';
import Api from './EntityApi';
import * as api from './api';
import Spinner from './components/Spinner';

export default function App(props) {
const message = Api.user.useRequest();
const message = api.user.useRequest({key: props.id});

// request a new user when props.id changes
useEffect(() => {
message.request(props.id);
}, [props.id]);

if (message.isEmpty || message.isFetching) return <Spinner />;
if (message.isEmpty || message.isPending) return <Spinner />;
if (message.isError) throw message.requestError;

return <img src={user.avatar} />;
Expand Down

0 comments on commit e89bbec

Please sign in to comment.