How to present sort order-by query in Falcor?

196 views Asked by At

Suppose the model is structured as

{
   events: [
      {
         date: '2016-06-01',
         name: 'Children Day'
      },
      {
         date: '2016-01-01',
         name: 'New Year Day'
      },
      {
         date: '2016-12-25',
         name: 'Christmass'
      }
   ]
}

There could be a lot of events in our storage. From client side, we want to issue a query to get 10 events order by date in ascending order.

How to present this kind of query in Falcor?

1

There are 1 answers

0
Yasammez On

Heres what I would do: first, promote your events to entities, i.e. give them ids:

id | date       | name
 1 | 2016-06-01 | Children Day
 2 | 2016-01-01 | New Year Day
 3 | 2016-12-25 | Christmass

Then provide a route providing these events by id:

route: 'eventsById[{integers:ids}]["date","name"]'

which returns the original data. Now you can create a new route for orderings

route: 'orderedEvents['date','name']['asc','desc'][{ranges:range}]

which returns references into the eventsById route. This way your client could even request the same data sorted in different ways within the same request!

router.get(
  "orderedEvents.date.asc[0..2]",
  "orderedEvents.date.desc[0..2]");

which would return

{
  'eventsById': {
    1: {
      'date':'2016-06-01',
      'name':'Children Day' },
    2: {
      'date':'2016-01-01',
      'name':'New Year Day' },
    3: {
      'date':'2016-12-25',
      'name':'Christmass' } },
  'orderedEvents': {
    'date': {
      'asc': [
        { '$type':'ref', 'value':['eventsById',2] },
        { '$type':'ref', 'value':['eventsById',1] },
        { '$type':'ref', 'value':['eventsById',3] } ],
      'desc': [
        { '$type':'ref', 'value':['eventsById',3] },
        { '$type':'ref', 'value':['eventsById',1] },
        { '$type':'ref', 'value':['eventsById',2] } ] } }
}