WebSQL threw an error [Error: Error code 1: no such table: document-store]

WebSQL threw an error [Error: Error code 1: no such table: document-store]

我们在 RxDB 的应用程序中使用 react-naive-sqlite-2 并开始偶尔出现此错误。它似乎发生在我们删除数据库之后。我很惊讶地看到这是一个 WebSQL 错误,因为我们使用的是 react-native 并且 WebSQL 已被弃用。我没有很好的调试方法,但我的直觉是我们有一些代码仍在尝试访问现在死掉的数据库。

这是我们用来设置数据库的代码:

import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite'
import SQLite from 'react-native-sqlite-2'
import { addRxPlugin, createRxDatabase } from 'rxdb'
import { RxDBReplicationGraphQLPlugin } from 'rxdb/plugins/replication-graphql'

import type { DatabaseType } from '../generated'

/**
 * SQLITE SETUP
 */
const SQLiteAdapter = SQLiteAdapterFactory(SQLite)
addRxPlugin(SQLiteAdapter)
addRxPlugin(require('pouchdb-adapter-http'))

/**
 * Other plugins
 */
addRxPlugin(RxDBReplicationGraphQLPlugin)

export const getRxDB = async () => {
  return await createRxDatabase<DatabaseType>({
    name: 'gatherdatabase',
    adapter: 'react-native-sqlite', // the name of your adapter
    multiInstance: false,
  })

问题发生在我们注销并尝试重新登录后。当我们注销时,我们调用 removeRxDatabase。有没有人运行以前遇到过这种问题或知道调试方法?

对于后代来说,问题是我们在状态管理库 (Zustand) 中有一个对数据库的引用,该数据库在过去注销时一直保留着。当我们再次尝试登录时,我们的 getOrCreateDatabase 函数没有生成一个新函数,但它无效,因为我们在 rxdb 中有 运行 database.remove()。我们最终只是清除了 Zustand 数据库并在一处调用 database.remove()

export const useRxDB = create<UseRxDBType>((set, get) => ({
  database: undefined,
  /**
   * we set the database to ready in the LocalDocument store once local docs are loaded into the store
   */
  databaseIsReady: false,
  getOrCreateDatabase: async () => {
    let database = get().database

    if (!database) {
      database = await createRxDatabase()

      if (!Rx.isRxDatabase(database)) throw new Error(`database isnt a valid RxDB database.`)

      set({ database })
    }

    return database
  },
  resetDatabase: async () => {
    const database = get().database

    if (database) {
      await database.remove()
      set({ database: undefined })
    }
  },
}))