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

Unique DB with multiple stores #72

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

andrehil
Copy link
Contributor

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 :)

… returns "undefined" (keep compatibility) or "true".

Because IndexedDB is limited on filtering, I had to use iterate and manually check each item with custom filters.
But I still need to limit the quantity of records returned, so I don't iterate the whole store, I know that there is the param "limit" the problem is that IDBWrapper doesn't know how many records passed my filter.
To solve this, I changed the "onItem" function to return a boolean and increment "recordCount" only if it returns "undefined" (keep compatibility) or "true".
…n array of stores.

Creation of a class called "Store" which contains the specific attributes of a store.
Main class changed to receive the param dbName and an array of store params to create each store inside the DB.
With these changes the user is able to create one DB with multiple stores.
Tests changed to comply with the new behavior.
@jensarps
Copy link
Owner

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?

@andrehil
Copy link
Contributor Author

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 storePrefixparam 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

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

Successfully merging this pull request may close these issues.

None yet

2 participants