Skip to content

Instantly share code, notes, and snippets.

@suthaharan
Created March 13, 2024 18:29
Show Gist options
  • Save suthaharan/3a4e2d30b272689da056627681d3e2f8 to your computer and use it in GitHub Desktop.
Save suthaharan/3a4e2d30b272689da056627681d3e2f8 to your computer and use it in GitHub Desktop.
IndexedDB: Read all data
var connection = indexedDB.open('<DBNAME>', <Version#>);
connection.onsuccess = (event) => {
const db = event.target.result;
// get all contacts
getAllContacts(db);
};
function getAllContacts(db) {
const txn = db.transaction('record', "readonly");
const objectStore = txn.objectStore('record');
//console.log(objectStore);
objectStore.openCursor().onsuccess = (event) => {
let cursor = event.target.result;
if (cursor) {
let contact = cursor.value;
console.log(contact);
// continue next record
cursor.continue();
}
};
// close the database connection
txn.oncomplete = function () {
db.close();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment