Last active
December 8, 2020 06:58
-
-
Save ryandabler/28d7b7932415e1e4b23ddb56d80bd0c9 to your computer and use it in GitHub Desktop.
A complete working example of all the uses of cursor for this article: https://itnext.io/searching-in-your-indexeddb-database-d7cbf202a17
This file contains hidden or 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 invoices = [ | |
{ invoiceId: "c1-5b", vendor: "GE", paid: true }, | |
{ invoiceId: "a", vendor: "GE", paid: false }, | |
{ invoiceId: "-7c", vendor: "Whirlpool", paid: true }, | |
{ invoiceId: "b", vendor: "Bosch", paid: true }, | |
{ invoiceId: "9b54-dbdd", vendor: "Bosch", paid: false }, | |
{ invoiceId: "2cac788-", vendor: "Whirlpool", paid: true }, | |
{ invoiceId: "99cd9c14", vendor: "Bosch", paid: false }, | |
{ invoiceId: "a1854b-14b", vendor: "Frigidaire", paid: false }, | |
{ invoiceId: "43b76a75", vendor: "Bosch", paid: true }, | |
{ invoiceId: "95b971959", vendor: "GE", paid: false } | |
]; | |
const invoiceItems = [ | |
{ invoiceId: "c1-5b", row: 0, item: "Table", price: 11822 }, | |
{ invoiceId: "c1-5b", row: 1, item: "Recliner", price: 5296 }, | |
{ invoiceId: "a", row: 0, item: "Table", price: 13162 }, | |
{ invoiceId: "a", row: 1, item: "Dryer", price: 4129 }, | |
{ invoiceId: "a", row: 2, item: "Refrigerator", price: 6930 }, | |
{ invoiceId: "-7c", row: 0, item: "Dishwasher", price: 746 }, | |
{ invoiceId: "-7c", row: 1, item: "Refrigerator", price: 12209 }, | |
{ invoiceId: "-7c", row: 2, item: "Dryer", price: 12981 }, | |
{ invoiceId: "-7c", row: 3, item: "Dryer", price: 6852 }, | |
{ invoiceId: "b", row: 0, item: "Oven", price: 13695 }, | |
{ invoiceId: "b", row: 1, item: "Oven", price: 12427 }, | |
{ invoiceId: "9b54-dbdd", row: 0, item: "Oven", price: 9830 }, | |
{ invoiceId: "9b54-dbdd", row: 1, item: "Recliner", price: 6244 }, | |
{ invoiceId: "9b54-dbdd", row: 2, item: "Dryer", price: 6946 }, | |
{ invoiceId: "2cac788-", row: 0, item: "Recliner", price: 115 }, | |
{ invoiceId: "2cac788-", row: 1, item: "Dishwasher", price: 1377 }, | |
{ invoiceId: "2cac788-", row: 2, item: "Dryer", price: 6627 }, | |
{ invoiceId: "99cd9c14", row: 0, item: "Recliner", price: 1794 }, | |
{ invoiceId: "99cd9c14", row: 1, item: "Dishwasher", price: 7142 }, | |
{ invoiceId: "99cd9c14", row: 2, item: "Dryer", price: 2253 }, | |
{ invoiceId: "99cd9c14", row: 3, item: "Oven", price: 13509 }, | |
{ invoiceId: "a1854b-14b", row: 0, item: "Refrigerator", price: 3373 }, | |
{ invoiceId: "a1854b-14b", row: 1, item: "Dryer", price: 7821 }, | |
{ invoiceId: "a1854b-14b", row: 2, item: "Oven", price: 12542 }, | |
{ invoiceId: "43b76a75", row: 0, item: "Oven", price: 5569 }, | |
{ invoiceId: "43b76a75", row: 1, item: "Refrigerator", price: 13791 }, | |
{ invoiceId: "43b76a75", row: 2, item: "Dishwasher", price: 6762 }, | |
{ invoiceId: "43b76a75", row: 3, item: "Dishwasher", price: 9957 }, | |
{ invoiceId: "95b971959", row: 0, item: "Dryer", price: 5341 } | |
]; | |
const request = window.indexedDB.open('database', 1); | |
// Create database | |
request.onupgradeneeded = event => { | |
const db = event.target.result; | |
const invoiceStore = db.createObjectStore('invoices', { keyPath: 'invoiceId' }); | |
invoiceStore.createIndex('VendorIndex', 'vendor'); | |
invoices.forEach(invoice => invoiceStore.add(invoice)); | |
const itemStore = db.createObjectStore('invoice-items', { keyPath: ['invoiceId', 'row'] }); | |
itemStore.createIndex('InvoiceIndex', 'invoiceId'); | |
invoiceItems.forEach(invoiceItem => itemStore.add(invoiceItem)); | |
}; | |
request.onsuccess = () => { | |
const db = request.result; | |
const transaction = db.transaction(['invoices', 'invoice-items'], 'readwrite'); | |
const invoiceStore = transaction.objectStore('invoices'); | |
const itemStore = transaction.objectStore('invoice-items'); | |
const vendorIndex = invoiceStore.index('VendorIndex'); | |
const invIndex = itemStore.index('InvoiceIndex'); | |
// Get cursor on invoice store simply to print all invoices | |
const getCursorRequest = invoiceStore.openCursor(); | |
getCursorRequest.onsuccess = e => { | |
const cursor = e.target.result; | |
if (cursor) { | |
console.log(cursor.value); | |
cursor.continue(); | |
} else { | |
console.log('Exhausted all documents'); | |
} | |
} | |
// Get cursor on vendor index of invoice store to update all 'GE' vendors to 'P&GE' | |
const keyRng = IDBKeyRange.only('GE'); | |
const updateCursorRequestIndex = vendorIndex.openCursor(keyRng); | |
updateCursorRequestIndex.onsuccess = e => { | |
const cursor = e.target.result; | |
if (cursor) { | |
const invoice = cursor.value; | |
invoice.vendor = 'P&GE'; | |
cursor.update(invoice); | |
cursor.continue(); | |
} | |
} | |
// Get cursor to delete all Frigidaire invoices | |
const deleteInvoiceCursorRequest = vendorIndex.openCursor(IDBKeyRange.only('Frigidaire')); | |
deleteInvoiceCursorRequest.onsuccess = e => { | |
const invCursor = e.target.result; | |
if (invCursor) { | |
// Get invoice item cursor | |
const invItemCursorRequest = invIndex.openCursor(IDBKeyRange.only(invCursor.value.invoiceId)); | |
invItemCursorRequest.onsuccess = e => { | |
const invItemCursor = e.target.result; | |
if (invItemCursor) { | |
invItemCursor.delete(); | |
invItemCursor.continue(); | |
} | |
} | |
invCursor.delete(); | |
invCursor.continue(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment