Skip to content

Instantly share code, notes, and snippets.

@rozeappletree
Created July 2, 2020 10:21
Show Gist options
  • Save rozeappletree/c04232c34a0602e39f45d8398370fc39 to your computer and use it in GitHub Desktop.
Save rozeappletree/c04232c34a0602e39f45d8398370fc39 to your computer and use it in GitHub Desktop.
Boilerplate
// Load IndexedDB
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.tx = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB.")
}
// Operations
function operation(type){
const dbName = "myDB";
var request = indexedDB.open(dbName, 3), db, tx, store, index; // globals
// doesn't execute while retreiving data
request.onupgradeneeded = function(e) {
let db = request.result,
store = db.createObjectStore('logStore', {keyPath: 'id'}),
index = store.createIndex('date', 'date', {unique: true}); /* ('logStore', {autoIncrement: true}) */
};
request.onerror = function(e) {
console.log("ERROR: ", e.target.errorCode)
};
request.onsuccess = function(e) {
db = request.result;
tx = db.transaction('logStore', 'readwrite');
store = tx.objectStore('logStore');
dateIndex = store.index('date')
db.onerror = function(e){
console.log("DB ERROR: ", e.target.errorCode)
}
if (type=='putData'){
_putData();
} else if (type == 'getData'){
_getData();
}
tx.oncomplete = function(){
db.close()
}
}
function _putData(){
// put in info
store.put({
id : 1,
date : '21 Mar 2020 - 6pm',
head : 'head1',
log : 'log1'
});
store.put({
id : 2,
date : '22 Mar 2020 - 7pm',
head : 'head2xx',
log : 'log2'
});
}
function _getData(){
// get data
let atId1 = store.get(1); // key i.e id = 1
let atDateIdx = dateIndex.get('22 Mar 2020 - 7pm');
// as asynchronous, use
atId1.onsuccess = function(){
console.log(atId1.result);
console.log(atId1.result.head, atId1.result.log);
};
atDateIdx.onsuccess = function(){
console.log(atDateIdx.result);
console.log(atDateIdx.result.head, atId1.result.log);
};
}
}
// MAIN
// ----
// comment one of two
// 1.
//operation(type="putData")
// 2. After put data / if existing db
operation(type="getData")
@rozeappletree
Copy link
Author

list all databases

indexedDB.databases().then(r => console.log(r))

deleteDatabase

indexedDB.deleteDatabase('exactDatabaseName') // get name from indexedDB.databases().then(r => console.log(r))

list all object stores

request.onsuccess = function(e) {
        db          = request.result;
        console.log(db.objectStoreNames)
}

delete object store

request.onsuccess = function(e) {
        db          = request.result;
        console.log(db.deleteObjectStore( xxx ))
}

Note: These are just APIs. Got these info by checking data-members and methods in console for corresponding object. (__proto__)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment