简体   繁体   中英

JavaScript: How to retrieve the latest record from IndexedDB?

I'm writing a user script which main goal it is to track watched episodes. In the IndexedDB I have an objectStore({keyPath: 'episode', autoIncrement: true}) watchedList that contains objects {episode: [the episodes id], currentTime: [episode watched till here]} . My problem is now: How do I retrieve the latest record from watchedList .

I'm using idb Promise Libary for the IndexedDB API. That are my approaches.

  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: will log the first record (lowest id)

2: will log an array with the lowest ids first

3: will log an array with the first record

How do I sort the result?

Is it a good idea to accomplish my goal with following approach?

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

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

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