Guest User

Untitled

a guest
Oct 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. const _ = require('lodash');
  2. const { ORM, Model, attr } = require('redux-orm');
  3.  
  4. // define model
  5. class Event extends Model {
  6. }
  7.  
  8. Event.modelName = 'Event';
  9. Event.fields = {
  10. title: attr(),
  11. };
  12.  
  13. // prepare orm and session
  14. const orm = new ORM();
  15. orm.register(Event);
  16. const session = orm.session(orm.getEmptyState());
  17.  
  18. // generate sample data
  19. _.range(100).forEach(index => {
  20. session.Event.create({
  21. title: `Event ${index % 2 === 0 ? '!' : '?'}`
  22. });
  23. });
  24.  
  25.  
  26. // filter now accepts function callbacks
  27. const result = session.Event.query.filter(data => {
  28. // data is a POJO directly from store
  29. global.console.warn(data);
  30. return data.title.includes('!');
  31. });
  32.  
  33. global.console.warn(result.count());
  34.  
  35.  
  36. // filter can also be a POJO with attribute spec
  37. const count = session.Event.query.filter({ title: 'Event !' }).count();
  38. global.console.warn(count);
Add Comment
Please, Sign In to add comment