I have a database inside indexedDB filled using an Emberjs adapter. I have set the keypath like this:
this.addModel(App.Device, { keyPath: \'key\' });
Use objectStore.openKeyCursor with the direction "prev":
const openCursorRequest = store.openKeyCursor(null, 'prev')
openCursorRequest.onsuccess = () => {
const cursor = openCursorRequest.result
const maxKey = cursor && cursor.key
// use maxKey...
}
Using Promise:
const getMaxKey = (db, storeName) => new Promise((resolve, reject) => {
const tx = db.transaction(storeName, 'readonly')
const store = tx.objectStore(storeName)
const openCursorRequest = store.openKeyCursor(null, 'prev')
openCursorRequest.onsuccess = () => {
const cursor = openCursorRequest.result
resolve(cursor && cursor.key)
}
openCursorRequest.onerror = () => {
reject(openCursorRequest.error)
}
})