首页 > 解决方案 > JavaScript:如何从 IndexedDB 中检索最新记录?

问题描述

我正在编写一个用户脚本,其主要目标是跟踪观看的剧集。在 IndexedDB 我有一个 objectStore({keyPath: 'episode', autoIncrement: true})watchedList包含 objects {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());
  })

标签: javascriptindexeddb

解决方案


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


推荐阅读