TinyBase logoTinyBase

getResultSortedRowIds

The getResultSortedRowIds method returns the Ids of every ResultRow in the ResultTable of the given query, sorted according to the values in a specified ResultCell.

getResultSortedRowIds(
  queryId: Id,
  cellId?: any,
  descending?: boolean,
  offset?: number,
  limit?: number,
): Ids
TypeDescription
queryIdId

The Id of a query.

cellId?any

The Id of the ResultCell whose values are used for the sorting, or undefined to by sort the ResultRow Id itself.

descending?boolean

Whether the sorting should be in descending order.

offset?number

The number of ResultRow Ids to skip for pagination purposes, if any.

limit?number

The maximum number of ResultRow Ids to return, or undefined for all.

returnsIds

An array of the sorted Ids of every ResultRow in the result of the query.

This has the same behavior as a Store's getSortedRowIds method. For example, if the query Id is invalid, the method returns an empty array. Similarly, the sorting of the rows is alphanumeric, and you can indicate whether it should be in descending order. The offset and limit parameters are used to paginate results, but default to 0 and undefined to return all available ResultRow Ids if not specified.

Note that every call to this method will perform the sorting afresh - there is no caching of the results - and so you are advised to memoize the results yourself, especially when the ResultTable is large. For a performant approach to tracking the sorted ResultRow Ids when they change, use the addResultSortedRowIdsListener method.

Example

This example creates a Queries object, a single query definition, and then calls this method on it (as well as a non-existent definition) to get the ResultRow Ids.

const store = createStore().setTable('pets', {
  fido: {species: 'dog', color: 'brown'},
  felix: {species: 'cat', color: 'black'},
  cujo: {species: 'dog', color: 'black'},
});

const queries = createQueries(store).setQueryDefinition(
  'dogColors',
  'pets',
  ({select, where}) => {
    select('color');
    where('species', 'dog');
  },
);

console.log(queries.getResultSortedRowIds('dogColors', 'color'));
// -> ['cujo', 'fido']

console.log(queries.getResultSortedRowIds('catColors', 'color'));
// -> []

Since

v2.0.0