Connect

The data-fetching decorator for Lore

findAll

This blueprint mirrors the find blueprint, but adds a helper to automatically include data that matches the where object.

This is useful if you want to display a list of data (such as tweets created by a user) and then automatically have new tweets they create show up at the top of that list.

Usage

Example usage is below:

import { connect } from 'lore-hook-connect';

connect((getState, props) => {
  return {
    tweets: getState('tweet.findAll', {
      where: {
        user: 1
      }
    })
  }
})

When invoked, this blueprint will create a API call, and then automatically merge data from the Redux Store into the object it returns, as long as that data matches all fields provided in the where attribute.

To illustrate, imagine the query above returned 3 tweets (meaning the API has 3 tweets created by the user with the id of 1, who we will pretend is the current user):

  1. First tweet
  2. Second tweet
  3. Third tweet

Now imagine the current user composes another tweet, and sends it. Because this tweet is now in our Redux Store, and the user attribute is equal to 1, it will automatically be included in the response from this blueprint, and the data returned from the blueprint will now look like this:

  1. NEW tweet
  2. First tweet
  3. Second tweet
  4. Third tweet

In other words, this blueprint returns the original cached response from the API plus any additional data that lives in the Redux Store matching that criteria.

Blueprint

import find from './find';

export default _.defaultsDeep({

  defaults: {
    include: {
      where: function(model, params) {
        if (_.keys(params.where).length > 0) {
          return _.isMatch(model.data, params.where);
        }

        return true;
      }
    }
  }

}, find);