feathersjs-ecosystem / feathers-redux

Integrate Feathers with your Redux store
MIT License
114 stars 23 forks source link

Added filters to onCreated #48

Closed Fiorello closed 6 years ago

Fiorello commented 6 years ago

Added filters on the onCreated method based on feathers-vuex implementation.

Es: Filter onCreated realtime event with same query criteria of find (if query params is passed).

const query = { 
  $select: [ 'username' ],
  $limit: 10,
  $sort: {
    _id: -1
  },
  username: {
    $regex: 'admin',
  }
  ...
};

dispatch(services.users.find({query}));

const users = app.service('users');
users.on('created', (data) => {
  dispatch(services.users.onCreated(data, query));
});
eddyystop commented 6 years ago

Presently onCreated it just an adapter for Feathers serviceName.on('created', ...). This PR adds non_Feathers features to it.

What is the use-case? Why do this for onCreate and not for onUpdate, nor onPatch, nor onRemove?

Fiorello commented 6 years ago

Use case Realtime data receiving from socket is not filtered by query params, so i have to apply the same filters. Example Query for all posts in a collection filtered by {active: false}. When another user create a new post with state active, feathers tells me from socket that another post was created, but I don't want it to be added to the collection.

Motivation Initially I tried to apply the filter directly to the 'created' event. If the data passed correctly the filter I added it to the collection. The problem is that if a certain $sort, or $limit was applied, the onCreated method would obviously incorrectly add the data. I could have done everything outside feathers-redux, filter the whole date set and use the store action dispatch to overwrite the entire store, but I'm worried that in a realtime application with thousands of updates this would be poorly performing.

But you're right, you should add it to the other methods too (onUpdate, onPatch, onRemove). If you think this can be done without changing the feathers-redux core, please tell me.

eddyystop commented 6 years ago

There is a difference between an event handler like the method you're using and a cache of records. This discussion has been discussed before and they should be different repos.

Have you looked at Feathers-offline-realtime? https://auk.docs.feathersjs.com/guides/offline-first/readme.html