简体   繁体   中英

How to detect PouchDB IndexedDB adapter for existing DB

I have an existing user base using 'idb' adapter. For new users, I want to create DB with 'indexeddb' adapter. I need to find a way to figure out what adapter is used for the existing DB, as it may be either of those two.

To find the current adapter for an existing PouchDB database, you need to use the native IndexedDB API. Using PouchDB directly will not work because it creates the database when it does not exists, and fail when it is of another version.

The following code should to the trick (TypeScript).

async function getCurrentAdapterForDb(dbname: string): Promise<string> {
  return new Promise<undefined | "idb" | "indexeddb">((resolve, reject) => {
    // To detect whether the database is using idb or indexeddb, we look at the version property of the indexeddb
    // If the database does not exists ==> onupgradeneeded is called with oldVersion = 0 (requesting version 1)
    // If the database exists, a version below Math.pow(10, 13) is considered idb. A version above is considered indexeddb.
    let request = window.indexedDB.open(`_pouch_${dbname}`);
    request.onupgradeneeded = function (e: IDBVersionChangeEvent): void {
      // Since no version has been requested, this mean the database do not exists.
      if (e.oldVersion === 0) {
        resolve(undefined); // Database does not exists
      }

      // We do not want the database to be created or migrated here, this is only a check. Therefore, abort the transaction.
      request.transaction.abort();
    };

    request.onerror = function (err: Event): void {
      if (request.error.code !== request.error.ABORT_ERR) {
        // Abort Error has been triggered above. Ignoring since we already resolved the promise.
        reject(request.error);
      }
    };

    request.onsuccess = function (): void {
      // When the database is successfully opened, we detect the adapter using the version
      // If we are dealing with a new database, oldVersion will be 0.
      // If we are dealing with an existing database, oldVersion will be less than indexedDbStartVersion.
      // indexedDbStartVersion is taken from pouchdb-adapter-indexeddb/index.es.js -- v7.2.2
      let indexedDbStartVersion = Math.pow(10, 13);

      if (request.result.version < indexedDbStartVersion) {
        resolve("idb");
      } else {
        resolve("indexeddb");
      }

      // Closes the database
      request.result.close();
    };
  })
}

请同时在浏览器中查看 PouchDB

console.log(yourPouchDBinstance.adapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM