Created
December 12, 2023 04:46
-
-
Save tylerchilds/bb4e43253fd6fffe9e589504340f0c4c to your computer and use it in GitHub Desktop.
Indexed DB Key/Value - Hello World
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 databaseName = 'braid'; | |
| const storeName = 'wg'; | |
| const database = new Promise(function initialize(resolve, reject) { | |
| const request = indexedDB.open(databaseName, 1); | |
| request.onupgradeneeded = function(event) { | |
| const database = event.target.result; | |
| database.createObjectStore(storeName, { keyPath: 'schema', autoIncrement: false }); | |
| }; | |
| request.onsuccess = function(event) { | |
| resolve(event.target.result); | |
| }; | |
| }); | |
| export async function load(keys) { | |
| const db = await database; | |
| const transaction = db.transaction(storeName); | |
| const objectStore = transaction.objectStore(storeName); | |
| const rows = await new Promise(function loadFromDatabase(resolve, reject) { | |
| const rows = []; | |
| const read = objectStore.openCursor(); | |
| read.onsuccess = function(event) { | |
| let cursor = event.target.result; | |
| if (cursor) { | |
| if(keys.includes(cursor.key)) { | |
| rows.push(cursor.value); | |
| } | |
| cursor.continue(); | |
| } else { | |
| resolve(rows) | |
| } | |
| }; | |
| read.onerror = reject; | |
| }); | |
| return rows; | |
| } | |
| export async function save(schema, data) { | |
| const db = await database; | |
| const record = { schema, data }; | |
| const transaction = db.transaction(storeName, 'readwrite'); | |
| const objectStore = transaction.objectStore(storeName); | |
| let request; | |
| return new Promise(function saveToDatabase(resolve, reject) { | |
| try { | |
| request = objectStore.get(schema); | |
| request.onsuccess = function(event) { | |
| const request = objectStore.put(record); | |
| request.onsuccess = resolve; | |
| } | |
| } catch (e) { | |
| const request = objectStore.add(record); | |
| request.onsuccess = resolve; | |
| request.onerror = reject; | |
| } | |
| }); | |
| } | |
| export default { | |
| save, | |
| load | |
| } | |
| save('hello-world', { hello: 'world'}) | |
| load('hello-world').then(function restoreFromCache(rows) { | |
| rows.map(({schema, data}) => console.log({ schema, data })); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment