question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createObjectStore bumps db version

See original GitHub issue

Everytime createObjectStore is called, the database version is incremented. Just trying to figure out if this is intended behaviour or am I doing something wrong?

I have the following config in my app.module, which is passed to the ngx-indexed-db module forRoot method:

const dbConfig: DBConfig = { name: 'appDb', version: 1, objectStoresMeta: [ ] };

Which creates a new database (version 1) with no stores, which is fine.

Then, in the user authentication service, I’m creating new object stores when a user logs in:

private createDataStores(): void {
    const offlineCacheSchema: ObjectStoreMeta = {
      store: 'offlineCache-' + this.user.id,
      storeConfig: { keyPath: 'id', autoIncrement: true },
      storeSchema: [
        { name: 'endpoint', keypath: 'endpoint', options: { unique: false } },
        { name: 'ulid', keypath: 'ulid', options: { unique: true } },
        { name: 'data', keypath: 'data', options: { unique: false } }
      ]
    }

    const apiCacheSchema: ObjectStoreMeta = {
      store: 'apiCache-' + this.user.id,
      storeConfig: { keyPath: 'id', autoIncrement: true },
      storeSchema: [
        { name: 'endpoint', keypath: 'endpoint', options: { unique: true } },
        { name: 'timestamp', keypath: 'timestamp', options: { unique: false } },
        { name: 'data', keypath: 'data', options: { unique: false } }
      ]
    };

    this.dbService.createObjectStore(offlineCacheSchema);
    this.dbService.createObjectStore(apiCacheSchema);
  }

The stores are created fine, but the database version bumps to 3. Reloading the app gives the following error: image

image

image

Since version is a required property, and the module requires DbConfig. I’m not sure how to use createObjectStore to create dynamic stores if the version increments every time.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
LukeMcDonnellcommented, May 11, 2021
0reactions
assuncaocharlescommented, May 13, 2021

That is a good catch for the lib to handler in the future, for now I would say the consumer will have to handle the dbVersion control. Here is a example of workaround https://codesandbox.io/s/floral-thunder-thbnf?file=/src/app/app.component.ts

Read more comments on GitHub >

github_iconTop Results From Across the Web

IDBDatabase.createObjectStore() - Web APIs - MDN Web Docs
The createObjectStore() method of the IDBDatabase interface ... 4); // This handler is called when a new version of the database // is ......
Read more >
Creating and reading database stores in indexeddb
To trigger it to run, let's bump our version number from 1 to 2 . // Open a database let ... createObjectStore() method...
Read more >
assuncaocharles/ngx-indexed-db - GitHub
A service that wraps IndexedDB database in an Angular service. It exposes very simple observables API to enable the usage of IndexedDB without...
Read more >
Cannot create second objectStore IndexedDB - Stack Overflow
Bump it up to version 3 in your above code and it will work. If not, add a openRequest.onblocked handler and add console.log...
Read more >
How to use idb, a 1kb package that makes IndexedDB easy
If the DB doesn't exist in the browser, the user's version will be ... Now let's do demo10, where we bump the version...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found