ngx-indexed-db icon indicating copy to clipboard operation
ngx-indexed-db copied to clipboard

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

Open irsali opened this issue 2 years ago • 2 comments

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 avatar Mar 13 '22 03:03 irsali

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!

irsali avatar Mar 13 '22 04:03 irsali

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

aparzi avatar Apr 14 '22 12:04 aparzi