Created
October 1, 2021 15:34
-
-
Save GautamPanickar/610507a38860e4f7e16b8641b9d5ab56 to your computer and use it in GitHub Desktop.
JS code to read data in a paginated format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const connection = indexedDB.open('gautam', 1); | |
const startIndex = 1; | |
const stopIndex = 5; | |
connection.onsuccess = (e) => { | |
let results = []; | |
let totalCount = 0; | |
let advanced = startIndex === 1; | |
let counter = startIndex; | |
const db = e.target.result; | |
const transaction = db.transaction('contacts', 'readonly'); | |
transaction.oncomplete = event => resolve({ contacts: results, total: totalCount }); | |
transaction.onerror = event => reject(event.target); | |
const store = transaction.objectStore('contacts'); | |
store.count().onsuccess = event => { | |
totalCount = event.target.result; | |
console.log(`TOTAL COUNT << ${totalCount}`); | |
}; | |
store.openCursor().onsuccess = event => { | |
const cursor = event.target.result; | |
if (!cursor) { | |
console.log(results); | |
return; | |
} | |
if (advanced) { | |
counter++; | |
results.push(cursor.value); | |
if (counter > stopIndex) { | |
console.log(results); | |
return; | |
} | |
cursor.continue(); | |
} else { | |
advanced = true; | |
cursor.advance(startIndex - 1); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment