Closed capitanfuturo closed 9 years ago
Sure. The query() portion is likely what you are looking for.
$indexedDB.openStore("foo", function (fooStore) {
query = fooStore.query();
query.findWhere(query.$index("id").$lt(5)).then( function (limitedObjects) {
... do some stuff with the items...
});
});
See https://github.com/bramski/angular-indexedDB/blob/master/angular-indexed-db.js#L660 for a bit more info on the "query" object and what you can do with it. Internally the cursor is used to iterate and return your results for most things. Implementing a pageable result should be fairly trivial.
Hi bramski, thank you for your reply. I try your code and it works well except for the line 3 where query.findWhere should be fooStore.findWhere. So the snippet is:
$indexedDB.openStore("foo", function (fooStore) {
query = fooStore.query();
store.findWhere(query.$index("id").$lt(5)).then( function (limitedObjects) {
//do some stuff with the items...
});
});
Overall this solution works well for an index that works like a sql sequence but suppose you have and index for a property like "name", I would like to use the library in order to retrieve the top 5 results and not the records with the property "name" between "a" and "b". With the IDBCursor I could use the advance() function [http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance-void-unsigned-long-count]. I'll try to modify the code.
Best regards
Right now the library does not give you access to the "continue" function of the cursor and to do essentially a "limit". I don't see that as particularly difficult but it would have to be implemented in the internals of the library.
You are welcome to fork and work on this change if it is something you need. Below is a link to the function you would need to work with to make this happen. Right now what occurs is that a "cursor" is opened and it is simply iterated until exhaustion. You could simply provide a "limit" to reduce your number of results.
https://github.com/bramski/angular-indexedDB/blob/master/src/angular-indexed-db.coffee#L217
Typically a paging solution needs an "offset" though so a proper limit would have to use a known value to begin the query from.
Cheers!
Closing this. Feel free to re open if your question isn't addressed.
Hi bramski, Thanks for your work. I'm new to angularjs and I'm trying to use this useful library in my project. My question is: I know indexeddb use cursor. Can I use it to implement a pagination function with angular-indexedDB library. I didn't found a function to access the cursor.
Best regards Giuseppe