Interested in knowing how to store domain(remote) state in Redux? Here is my 60 lines of code gist to replace minimongo

The reducer will take the data(either in array or object form) and parse it into a key value store. We call this store “cache”.

To insert one/many docoments:

insert("users", {user});
insert("posts", [array of posts])

To get any document from the store is as simple as calling state.cache.collections[id].

const user = cache.users["dkljfdfdfjhdf"];
const post = cache.posts["kd9kf9dkd9"];

If you want to index by a different field, you can pass it an index option:

insertOne("users", user, {index: "username"});
insertMany("posts", user, {index: "slug"});

Now you can get the by the index you named:

const user = cache.users["some_username"];
const post = cache.posts["some-title-slug"];

To join data in components:

const post = cache.posts["some-title-slug"];
post.user = cache.users[post.user_id];

gist here:

2 Likes

Yeah that’s really cool. Your idea to use key / value pairs made my life in Redux so much easier than trying to use Arrays inside the Reducer.

For me personally, I think I would forget which collection is indexed by which property, I’d prefer to have them all keyed by index. Inside my components, I should be able to look up by property value by using the underscore _.where function http://underscorejs.org/#where , correct ?

yep. if you need a single doc, like findOne you can use _.findWhere, if you need multiple you can use _.find

Chaining is also possible and fun:

const posts = _.chain(cache.posts)
  .where({ user_id: user.id })
  .filter(post => {
    const query = new RegExp(this.state.searchInput, 'i');
    return query.test(post.title) || query.test(post.description);
  })
  .sortBy('publishedAt')
  .reverse()
  .value();