JavaScript:如何从IndexedDB检索最新记录?

时间:2019-05-06 20:42:21

标签: javascript indexeddb

我正在编写一个用户脚本,该脚本的主要目的是跟踪观看的剧集。在IndexedDB中,我有一个objectStore({keyPath:'episode',autoIncrement:true})watchedList,其中包含对象{episode: [the episodes id], currentTime: [episode watched till here]}。现在的问题是:如何从watchedList检索最新记录。

我将idb Promise Libary用于IndexedDB API。那是我的方法。

  db.get('watchedList', IDBKeyRange.lowerBound(1)).then(data => {
    console.log(data);
  })
  db.getAll('watchedList').then(data => {
    console.log(data);
  })
  db.getAll('watchedList', null, 1).then(data => {
    console.log(data);
  })

1:将记录第一条记录(最低ID)

2:将首先记录ID最低的数组

3:将记录第一条记录的数组

如何对结果进行排序?

采用以下方法来实现我的目标是一个好主意吗?

  db.getAll('watchedList').then(data => {
    console.log(data.pop());
  })

1 个答案:

答案 0 :(得分:2)

尝试使用openCursor并将direction参数指定为prev