Sorting fetched records in Smart Views

Hello,

I have a Smart View where I fetch some data using the Store service, as described in the documentation.

Specifically, I defined a method that fetches children records based on a parent_id:

async loadChildren(parentId) {
    const params = {
      filters: JSON.stringify({
        field: 'parent_id',
        operator: 'equal',
        value: parentId,
      }),
      timezone: 'Europe/Paris',
      'page[number]': 1,
      'page[size]': 100,
    };
    const valuations = await this.get('store').query('forest-valuation', params);
    return valuations.toArray();
  },

Is there a way to sort the fetched records, by creation date desc for instance, without loading ALL the records and performing the sorting then?

Expected behavior

There should be some ‘sort-by’ options that could be passed with the query parameters.

Actual behavior

No control on the records returned when using pagination.

Context

Setup
  • Package Version: 5.7.0
  • Express Version: 4.17.1
  • Sequelize Version: 4.44.4
  • Database Dialect: Postgres
  • Project Name: Homeloop

Hi @arthur.derouge,

You can use the sort query param to do that.

Screenshot 2020-06-22 at 16.38.53

You need to prefix the field name with - for descending sorting (as shown in the screenshot above).

Let me know if it helps.