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

Version is incrementing for dynamically added store and keeps on incrementing with every page refresh #310

Closed
irsali opened this issue Mar 13, 2022 · 16 comments
Labels
help wanted Extra attention is needed

Comments

@irsali
Copy link
Contributor

irsali commented Mar 13, 2022

I am calling createObjectStore of NgxIndexedDBService to dynamically add stores at runtime. It makes my declaration version of DBConfig behind the actual at any point of time. I guess, Due to this version mismatch, I could not find if this store is already exist.

Is there any way to provide current version for NgxIndexedDBModule.forRoot(dbConfig) ?

dbConfig: DBConfig = {
  name: 'xyz,
  version: 1,
  objectStoresMeta: [
    {
    ...
    }
  ]
};

NgxIndexedDBModule.forRoot(dbConfig),

Also, Is there any way to find if particular store exist or not. For now, I am using my own fork with below code for this but wanted to be sure if there is any elegant way already exist that i missed.

/**
  * Returns if store exist or not.
  * @param storeName The name of the store to check
  */
  isStoreExist(storeName: string): Observable<boolean> {
    return new Observable<boolean>((obs) => {
      openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
        .then((db: IDBDatabase) => {
          try {
            const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
            transaction.objectStore(storeName);
            obs.next(true);
            obs.complete();
          }
          catch (e) {
            obs.next(false);
            obs.complete();
          }

        })
        .catch((error) => {
          obs.next(false);
          obs.complete();
        });
    });
  }
@irsali
Copy link
Contributor Author

irsali commented Mar 13, 2022

I did some research and found below.

What happens if version is not given?
If version is not given and a database with that name already exists, a connection will be opened without changing the version. If version is not given and no database with that name exists, a new database will be created with version equal to 1.

So, Commentting the version check and not providing version for NgxIndexedDBModule.forRoot(dbConfig) did the hack.

// if (!dbConfig.version) {
// throw new Error('NgxIndexedDB: Please, provide the db version in the configuration');
// }

Although it works for me. I am open for elegant ways. Thank you!

@aparzi
Copy link
Collaborator

aparzi commented Apr 14, 2022

Hi @irsali,
I don't understand your problem well. Would you like a method that checks if an object store already exists?

@itzgauravsingla
Copy link

itzgauravsingla commented Nov 5, 2024

Similar thing is happening with me. When the app is opened in new tab of the same browser where it is already running, the version changes dynamically and any further transactions fails.
Is there an elegant way to fix this?

Edit: Just found the bug in my code i guess? I used deleteObjectStore when i wanted to clear the data in indexedDB, seems like it causes the version change dynamically (maybe when there is no object store to delete)

@aparzi
Copy link
Collaborator

aparzi commented Nov 5, 2024

Hi @itzgauravsingla,
any change that leads to a structural change of the DB made by another process such as a new card on the same computer leads to a version change.

@aparzi
Copy link
Collaborator

aparzi commented Nov 5, 2024

Hi @irsali,
do you still have doubts or problems? In that case I will consider closing this issue.

thanks

@itzgauravsingla
Copy link

itzgauravsingla commented Nov 6, 2024

Hi @itzgauravsingla, any change that leads to a structural change of the DB made by another process such as a new card on the same computer leads to a version change.

@aparzi If a user already have a db with different version as the config.. I am unable to delete the db as i am getting the same error of version mismatch. Is there anything I am missing?

@aparzi
Copy link
Collaborator

aparzi commented Nov 6, 2024

@itzgauravsingla As soon as possible I will do a check and see if it is possible to find a solution that would, in my opinion, go along with the concept of version integrity

@aparzi aparzi added the help wanted Extra attention is needed label Nov 12, 2024
@irsali
Copy link
Contributor Author

irsali commented Dec 24, 2024

Hi @aparzi,
There are two problems, i face.

  1. Let's say I have a DB been created with one or more stores. Now I am adding store dynamically, it means my config in code still has old version number causing issue as version number is required, so if it could be made optional that will be great for such scenarios.
  2. Yes, I want to check if store already exist

For 1st issue it is replicating in playground itself. Add one or more store then refresh browser and you see error like below:

image

@aparzi
Copy link
Collaborator

aparzi commented Dec 24, 2024

Hi @irsali,
I apologize if this problem is still pending resolution but I have been really busy these past few months. As you can see the library is always being updated and continuously improved. I promise I will try to verify the problem as soon as possible. In the meantime, if you have any ideas or possible solutions feel free to open a PR.

@irsali
Copy link
Contributor Author

irsali commented Dec 24, 2024

Below are the PRs, I have raised, Please check and merge if you see it benefitting others.

#423
#425
#424

@aparzi
Copy link
Collaborator

aparzi commented Dec 24, 2024

I will check them out calmly

@0xrgg
Copy link
Contributor

0xrgg commented Jan 11, 2025

@irsali - funnily enough I'm encountering the same issue as you, in my case each "user" (browser) could have a different version depending on how many tables have been created, which happens dynamically depending on how many times user performs action X, so basically whatever number defined in the code config is going to be incorrect for someone (I would try it all in one table, however I need to clear items and purge the table sometimes, but not delete everything from the table i.e. user config while doing this).. tried your hack fix though and I'm still getting the exception about version not being supplied

I don't know much about indexeddb but feels like I'm stuck in a catch 22 where config needs to be supplied to connect, but also need to retrieve the version number into the config to instantiate the connection 😭

@0xrgg
Copy link
Contributor

0xrgg commented Jan 12, 2025

update: @irsali - forked main, implemented your changes, built and replaced the package file and this does seem to work for me.. is there any reason it can't be merged into the codebase @aparzi ?

@aparzi
Copy link
Collaborator

aparzi commented Jan 12, 2025

Hi @0xrgg,
there is already an open PR for this issue, but I need the requested changes and I need time to test them. A new version with these fixes will be released as soon as possible. Feel free to update the PR if you want, this is an open source project.

@0xrgg
Copy link
Contributor

0xrgg commented Jan 12, 2025

@aparzi - tried to update the package.json files and commit to @irsali's feature branch but don't have permissions, so will just wait on PR author (instead of creating a duplicate), in the mean time I can continue using my local package version :)

@aparzi
Copy link
Collaborator

aparzi commented Jan 23, 2025

Issue resolved with PR #434

@aparzi aparzi closed this as completed Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants