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

Difference between .getAll() and .each()? #85

Open
itjustwerks opened this issue May 2, 2019 · 6 comments
Open

Difference between .getAll() and .each()? #85

itjustwerks opened this issue May 2, 2019 · 6 comments

Comments

@itjustwerks
Copy link

Can someone explain the difference between these 2 methods? They both seem to return an array of all items in the object store. For a very large set of records, I was hoping for a way to iterate through them in a way that didn't require them all being held in memory at the same time, which is what I though .each() would do. Any ideas?

@itjustwerks
Copy link
Author

Or maybe I just don't know how to properly use the .each() method...

@bramski
Copy link
Owner

bramski commented May 2, 2019

.each() or .eachWhere is what you want. .getAll() fetches all records and returns them as an array.

@itjustwerks
Copy link
Author

Oh...I guess the syntax is

$indexedDB.openStore('storeName', function(store) {
   store.each(function(item) {
      // do synchronous stuff with item
   });
});

I was calling

store.each().then(function(items){
   // items is the entire object store array...
});

My bad!

@itjustwerks
Copy link
Author

What if I need to do async stuff within the .each loop? Specifically, changing and upserting the modified records as I go.

$indexedDB.openStore('storeName', function(store) {
   store.each(function(item) {
      item.property = 'changed';
      store.upsert(item).then(function() {
         // Go to the next item in the store...
      });
   });
});

@bramski
Copy link
Owner

bramski commented May 2, 2019

Yes that is the idea. When you open the store you get a transaction so you should launch all your tasks there. I honestly haven't touched this codebase in quite a few years so I hope that all works!

@itjustwerks
Copy link
Author

So getting back to this and digging in a bit more, I'm able to attach to the $q.notify events for the .each() function as follows:

$indexedDB.openStore('storeName', function(store) {
   store.each().then(function(allData){
      // all records have been iterated...
   },function(error){
      // an error occurred...
   },function(item) {
      // updates are notified here via the native store.openCursor().onsuccess function
   });
}).then(function() {
   // transaction is complete
});

I'm still stuck on how to do anything asynchronously within that loop. In my use case, many of the records are dependent on each other, and updates made to one record need to be saved back to indexedDb to be available when other records are processed. Is it possible to do async jobs within the openCursor lifecycle in IndexedDB?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants