Dexie.js: query phrase combined sortBy and limit

Created on 16 Jan 2016  路  2Comments  路  Source: dfahlander/Dexie.js

Hi,
Here's my schema:"++id,from,to,type,time,msg", I would like to query the latest several messages which 'from' or 'to' equals to a value, and the following phrase I got works:

db.Msg
.where('from')
.equals(id)
.or('to')
.equals(id)
.sortBy("time", function(array) {
deferred.resolve(array.slice(-limit));
})

But I think this is not the best choice, could you give me some advice. Thanks

Most helpful comment

I would recommend using

db.table.orderBy('time').and(x => x.from===id || x.to===id).reverse().limit(limit).toArray()

You then don't have to query the entire result and sort it in memory and the query would perform equally no matter how large the table would grow.

All 2 comments

I would recommend using

db.table.orderBy('time').and(x => x.from===id || x.to===id).reverse().limit(limit).toArray()

You then don't have to query the entire result and sort it in memory and the query would perform equally no matter how large the table would grow.

Yeah, quit helpful, thanks!

Was this page helpful?
0 / 5 - 0 ratings