js-data / js-data-rethinkdb

RethinkDB adapter for js-data. Main Site: http://js-data.io, API Reference Docs: http://api.js-data.io
MIT License
32 stars 5 forks source link

orderBy multiple fields #30

Open bymaximus opened 4 years ago

bymaximus commented 4 years ago

Is there some way to use orderBy with more than one field? For example, this orderBy(r.desc('fieldA'), r.desc('fieldB')) have different results from orderBy(r.desc('fieldA')).orderBy(r.desc('fieldB'))

crobinson42 commented 4 years ago

orderBy([“user”, “updatedAt”])

Sent from my iPhone

On Nov 30, 2019, at 6:45 PM, bymaximus notifications@github.com wrote:

 Is there some way to use orderBy with more than one field? For example, this orderBy(r.desc('fieldA'), r.desc('fieldB')) have different results from orderBy(r.desc('fieldA')).orderBy(r.desc('fieldB'))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

bymaximus commented 4 years ago

orderBy([“user”, “updatedAt”])

This will order by ASC, seems the code is hardcoded to get the sort direction from the second element of the array, which makes impossible to use for example orderBy(r.desc('fieldA'), r.desc('fieldB'))

    // Sort
    if (query.orderBy) {
      if (jsData.utils.isString(query.orderBy)) {
        query.orderBy = [[query.orderBy, 'asc']];
      }
      for (var i = 0; i < query.orderBy.length; i++) {
        if (jsData.utils.isString(query.orderBy[i])) {
          query.orderBy[i] = [query.orderBy[i], 'asc'];
        }
        rql = (query.orderBy[i][1] || '').toUpperCase() === 'DESC' ? rql.orderBy(r.desc(query.orderBy[i][0])) : rql.orderBy(query.orderBy[i][0]);
      }
    }
crobinson42 commented 4 years ago

Here's your 2 options:

orderBy(['user', 'DESC'])
// or
orderBy([ ['user', 'DESC'], ['updatedAt', 'asc']])
crobinson42 commented 4 years ago

This lib looks like it needs some attention. @bymaximus if you'd like to help contribute to the code base here, I'm more than happy to help you push PR's along 👍🏽