IDBWrapper
IDBWrapper copied to clipboard
Unique DB with multiple stores
I refactored the way IDBWrapper works with stores. Instead of creating one DB for each store, I think the right way is to create one DB with multiple stores. My mains changes consist of receiving the name of the DB and an array of params to create the stores. To accomplish this, I created an internal class called Store to group the store handling that was directly in the main class. The result is an object created just like before, but instead of using it like: idb.put, now it is idb.store1.put, for example.
The refactor is extensive and I might have left something behind, but I ask you to please consider merging to your repository. If you find anything that needs to be changed, just tell me and I can do it :)
Thanks for the PR!
Wow, that's quite some work, awesome :) I'll need some time to go through this, but one question upfront: What was your reasoning behind this change? I mean, what are the benefits of this architecture?
I am implementing an app that has some parts that need to work offline, that's the reason I started using IndexedDB and looked for a nice lib that simplifies the complicated original API and found IDBWrapper ;)
With the requisite of working offline, I have to store quite some data in the client side in about 5 stores.
Also, different users might access the app from the same browser, and one user should not see other user's data.
So, I need to separate they're data. I know there is the storePrefix
param that I could use to create separate stores for different users, but it gets a little bit messy.
5 stores are creating 5 DBs for 1 user, with 2 users, it goes to 10 and so on.
With the ability to have multiple stores in one DB, it gets more organized (just like a SQL database doesn't create on DB for each table).
My app is built with Angular, so I created a service that initializes the stores before any controller or directive. If all stores are initialized with only one call, it gets much easier to return a promise to wait the stores to get initialized. It also makes testing much easier.
Thanks for taking the time to analyze my PRs :D