mondora / asteroid

An alternative client for a Meteor backend
MIT License
734 stars 101 forks source link

Sort and Limit #36

Closed willoughby closed 9 years ago

willoughby commented 9 years ago

Do you have an example of how this could be achieved?

Thanks

pscanf commented 9 years ago

Hi there,

sorting and limiting are not implemented in the reactiveQuery method. That's because the query is entirely client side: you are querying the objects that the server already sent to you when you subscribed to some publication. Therefore sorting and limiting has practically no effect on performance, because you are not restricting "how much data the server is sending you", you are only "picking from the data the server already sent you". Due to the complexity of implementing such features in the reactiveQuery methods, I preferred to delegate the task to the user of the library, which can trivially use native array methods (such as sort and filter) on a reactiveQueryInstance.result array containing the results of the query.

An example of doing so would be:

var Ceres = new Asteroid("localhost:3000");
var sub = Ceres.subscribe("myBooksSubscription");
sub.ready.then(function () {
    var Books = Ceres.getCollection("books");
    var booksRQ = Books.reactiveQuery({/* whatever */});
    var limitedAndSortedResult = booksRQ.result.sort().slice(0, 10);
});

Even from a computational standpoint, since the reactiveQuerymethod would still need to use sort and slice, there is practically no difference.

(If you want to sort and limit server-side, you need to do when you register the publication, as you can see here)

Bye :-)

willoughby commented 9 years ago

Thanks, thats what i've done, it's just a nice to have feature to combine a query with a sort, simitlar to mini mongo in Meteor.

Im using asteroid in a phone gap app which is communicating to my meteor webapp and its working really well so thanks for your lib :+1:

pscanf commented 9 years ago

Np. I'm glad you're finding it useful. :-)