Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to page? #14

Closed
capitanfuturo opened this issue Dec 2, 2014 · 4 comments
Closed

How to page? #14

capitanfuturo opened this issue Dec 2, 2014 · 4 comments
Labels

Comments

@capitanfuturo
Copy link

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

@bramski bramski changed the title How to paging with cursor How to page? Dec 2, 2014
@bramski
Copy link
Owner

bramski commented Dec 2, 2014

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.

@capitanfuturo
Copy link
Author

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

@bramski
Copy link
Owner

bramski commented Dec 4, 2014

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!

@bramski
Copy link
Owner

bramski commented Mar 11, 2015

Closing this. Feel free to re open if your question isn't addressed.

@bramski bramski closed this as completed Mar 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants