jakearchibald / idb

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

"UnknownError: Database deleted by request of the user" on iOS14 #238

Closed edpatrick closed 2 years ago

edpatrick commented 2 years ago

Thanks for this package, it's great!

I'm having this error "UnknownError: Database deleted by request of the user" reported to our error reporting system and I'm having trouble replicating. I suspect the issue may be with the Safari implementation of indexedDB but posting here in case anyone has any suggestions or has encountered anything similar.

It seems to affect only users on:

I think the source of the error is this line in webkit https://github.com/WebKit/WebKit/blob/e98ff129bc8eba06ac17105f19c5f0e142aab853/Source/WebCore/Modules/indexeddb/shared/IDBError.h#L40

It seems to relate to the server connection closing.

A simplified version of our implementation:

// ./store.js
import { openDB } from 'idb'

export const upgrade = (db) => {
  if (!db?.createObjectStore) return null
  db.createObjectStore('example_store_name_1')
}

export const set = async (storeName, val, key) => {
  const dbPromise = openDB('example_db_name', 1, { upgrade })
  if (!window.indexedDB) return null
  return (await dbPromise).put(storeName, val, key)
}

export const count = async (storeName) => {
  const dbPromise = openDB('example_db_name', 1, { upgrade })
  if (!window.indexedDB) return null
  return (await dbPromise).count(storeName)
}
// ./index.js
import { set, count } from './store.js'

export const storeEvent = async (storeName, value, key) => {
  try {
    const rowCount = await count(storeName)
    // I clear the db in a separate part of the app, this count is just here here as a rough limit to 
    // ensure that I don't keep pushing data into storage if, for some reason, it is not getting cleared elsewhere
    if (rowCount < 500) {
      await set(storeName, value, key)
    }
  } catch (error) {
    // handle error
  }
}

Some of the things I have tried to replicate (on iOS 14.7.1):

Appreciate this might not be the right forum as it is likely not an issue with this package but, if anyone has any ideas or has come across this error before, any suggestions welcome!

jakearchibald commented 2 years ago

Could this happen if the user clears their storage while an IDB connection is open?

edpatrick commented 2 years ago

It could be, I couldn't replicate that though. We ended up stripping out idb and using an alternative approach. I'll close this issue now if it isn't a problem for others.