Skip to content

Normalizer and object relation manager for state based systems (redux/vuex)

License

Notifications You must be signed in to change notification settings

msolvaag/state-odb

Repository files navigation

state-odb

NPM downloads NPM package

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. 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

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

About

Normalizer and object relation manager for state based systems (redux/vuex)

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published