Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/msolvaag/state-odb
Normalizer and object relation manager for state based systems (redux/vuex)
https://github.com/msolvaag/state-odb
db normalize redux state state-management vuex
Last synced: about 2 months ago
JSON representation
Normalizer and object relation manager for state based systems (redux/vuex)
- Host: GitHub
- URL: https://github.com/msolvaag/state-odb
- Owner: msolvaag
- License: mit
- Created: 2018-02-27T10:04:18.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-19T09:05:32.000Z (almost 7 years ago)
- Last Synced: 2024-10-31T13:34:37.958Z (about 2 months ago)
- Topics: db, normalize, redux, state, state-management, vuex
- Language: TypeScript
- Size: 60.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# state-odb
[![NPM downloads](https://img.shields.io/npm/dm/state-odb.svg?style=flat-square)](https://www.npmjs.com/package/state-odb)
[![NPM package](https://img.shields.io/npm/v/state-odb.svg?style=flat-square)](https://www.npmjs.com/package/state-odb)state-odb provides a normalized state and easy object management.
## Why
Having a normalized state is a good strategy if your data is nested in different ways. The redux documentation has a nice explanation [here](http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html).
state-odb is a port of the redux-db project with means to decouple the term redux from the logic it provides. state-odb is compatible with all single state controlled systems. ( vuex/redux/..).## How
``` javascript
import db from "./schema";export const dbReducer = db.combineReducers(
(session, action) => {
const { BlogPost, Comment, User } = session;switch (action.type) {
case "POSTS_FETCH_DONE":
case "POST_FETCH_DONE":
case "POST_UPDATE":
case "POST_INSERT":
// all these actions may be handled using just one statement.
// the upsert method accepts both single objects and arrays.
// the payload is automatically normalized and related tables are also updated.BlogPost.upsert(action.payload);
break;case "POST_DELETE": {
const { id } = action.payload;
const post = BlogPost.get(id);post.comments.delete(); // Could be skipped if cascading deletes are defined.
post.delete();// or just, BlogPost.delete( id );
break;
}
case "COMMENT_UPDATE":
case "COMMENT_INSERT": {
// assuming payload contains {id,post,author}
const { post } = action.payload;BlogPost.get(post).comments.add(action.payload);
// or just, Comment.upsert(action.payload);
break;
}
case "COMMENT_DELETE": {
const { id } = action.payload;
Comment.delete(id);
break;
}
}
}
);
```## Contact / feedback
Feel free to create issues and PR's.## Dependencies
* none