Last active
November 9, 2019 12:02
-
-
Save takahirohonda/26d063f30963c60326bf072fe88b4afe to your computer and use it in GitHub Desktop.
replacing-local-storage-with-indexeddb-2.js
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 exampleData = [ | |
{data: [{categoryName: 'To do', listName: 'Personal'}, | |
{categoryName: 'To do', listName: 'Work'}]}, | |
{data: [{listName: 'Personal'}, {listName: 'Work'}]} | |
] | |
const key = ['category', 'list'] | |
let db | |
let request = indexedDB.open('mtd', 1); | |
request.onupgradeneeded = e => { | |
db = e.target.result | |
const store = db.createObjectStore('mtd-data') | |
console.log('created store: ', store) | |
// const store = db.createObjectStore('mtd-data', | |
// {keyPath: 'key', autoIncrement: true}); | |
// store.createIndex('context', 'context', { unique: true }) | |
// store.createIndex('data', 'data', { unique: false }) | |
} | |
request.onsuccess = e => { | |
db = e.target.result; | |
console.log('opened indexedDb...', db) | |
if (!db.objectStoreNames.contains('mtd-data')) { | |
const transaction = db.transaction(["mtd-data"], 'readwrite') | |
const store = transaction.objectStore('mtd-data') | |
exampleData.forEach((data, index) => { | |
store.put(data, key[index]) | |
console.log('Upserted data: ', data) | |
}) | |
db.close() | |
} | |
else { | |
console.log('mtd-data store already exists') | |
} | |
} | |
request.onerror = e => { | |
console.error('Failed to open db: ', e.target.error) | |
} | |
// reopen index db | |
request = indexedDB.open('mtd', 1) | |
request.onsuccess = e => { | |
db = e.target.result | |
const transaction = db.transaction(["mtd-data"], 'readonly') | |
const store = transaction.objectStore('mtd-data') | |
const categoryData = store.get('category') | |
categoryData.onsuccess= e => { | |
console.log('category data retrived: ', e.target.result.data) | |
} | |
const listData = store.get('list') | |
listData.onsuccess = e => { | |
console.log('listData retrieved: ', e.target.result.data) | |
} | |
db.close() | |
} | |
request.onerror = e => { | |
console.error('Failed to open db second time: ', e.target.error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment