jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.34k stars 357 forks source link

How to delete an object store? #279

Closed loxator closed 2 years ago

loxator commented 2 years ago

How do I drop the created object stores? I have tried the following but don't see the changes being reflected in the indexedDB, the function runs without any errors. Thank you for your help!

/**
 * Drop object store
 */

export const dropStore = async (storeNames: StoreNames[]) => {
  try {
    database.db = await openDB<DB>(
      environment.get('DB_NAME'),
      environment.get('IDB_VERSION'),
      {
        upgrade(db) {
          for (let i = 0; i < storeNames.length; i++) {
            if (db.objectStoreNames.contains(storeNames[i])) {
              db.deleteObjectStore(storeNames[i])
            }
          }
        },
      }
    )
  } catch (error) {
    logger.error('error', { error: error })
  }
jakearchibald commented 2 years ago

Bug reports without a minimal reproducible example will be closed (https://stackoverflow.com/help/minimal-reproducible-example). Ideally, the example should be accessible somewhere public, like jsbin, Glitch, jsfiddle, codepen etc etc.

I'll happily reopen this if you create a runnable demo of the issue, although you may discover the answer yourself while creating that demo.

loxator commented 2 years ago

@jakearchibald thanks for the idea! I tried it out and understood that the only way to make changes to the DB is to open a transaction with a larger version number than the previous one.

My use-case is that I need to drop some stores while keeping one intact with its data. Then when the application runs again, create the dropped object stores, whilst keeping the version the same. Not sure if this is possible without incrementing the version number.

jakearchibald commented 2 years ago

Yeah, you can only delete stores within a schema update. If you need to do something throughout the life of the database, maybe create another store which maintains which stores are 'active'?

loxator commented 2 years ago

Got it, thanks for taking the time out and replying!