Kurounin/Pagination

Transform sort

Closed this issue · 3 comments

Hello, thank you for the great package.

There are a couple of functions to transform filters and options.
But how to transform sorting options? For example my collection is connected to another one by id and I want to sort the first one by name field in the second.

I assume something like transform_sort would be nice to have.

Hi,

Since sort is included in the options, you can do something like this on server side:

publishPagination(MyCollection, {
    transform_options: function (filters, options) {
        options.sort = _.extend({ name: 1 }, options.sort);
        return options;
    }
});

@Kurounin it seems like this solution doesn't solve my problem :-(
I need a field from another collection to sort by.
Is there a way to use observeChanges for example?

Mongo does not provide a method to sort a collection using a value of a referred document from another collection. If you have a 1 to 1 relationship of the documents, you can modify your application logic by doing it differently:

  • get documents from collection 2, sorted by name and paginated
  • get documents from collection 1 where foreignKeyId is in the returned documents from collection 2
  • sort the array of received documents from collection 1 on client side, using a custom function