indexDB根据游标索引

2020-11-08  本文已影响0人  刘佳季

const db = new IndexDB('offlLineDB', getIndexedDBVersionOfNum()) // 数据库名  数据库版本

db.getIndexDataByCursor('basic_DB_assembly', keyWord, code)

--------------------------------------------------------------------------------------------

getIndexDataByCursor(storeName, storeIndex, storeKey) {

    const si = storeIndex

    const sk = storeKey

    return new Promise((resolve, reject) => {

      const request = indexedDB.open(this.dbName, this.version)

      request.onerror = function() {

        reject('IndexedDB数据库打开错误,请联系管理员。')

      }

      request.onsuccess = function(event) {

        const IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange

        const onlyKeyRange = IDBKeyRange.only(sk)

        const result = event.target.result

        const objectStore = result.transaction(storeName).objectStore(storeName).index(si).openCursor(

          onlyKeyRange)

        const dataList = []

        objectStore.onsuccess = function(event) {

          const cursor = event.target.result

          if (cursor) {

            dataList.push(cursor.value)

            cursor.continue()

          } else {

            resolve(dataList)

          }

        }

      }

    })

  }

上一篇下一篇

猜你喜欢

热点阅读