Dexie.js
Fiddle with Dexie.js
by dshilkret
HTML
<!-- Include dexie.js -->
<script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
<h3>Log</h3>
<textarea id="log"></textarea>
<!-- Just a simple log function... -->
CSS
textarea#log
{
width: 100%;
height: 1000px;
}
JavaScript
//
// Define database
//
function log(txt) {
document.getElementById('log').value += txt + "\n";
console.log(txt)
}
//alert(JSON.stringify(Dexie.exists("DocIntDB")));
localStorage.setItem("token_ol", "MyTestOLToken");
var tokenLS = localStorage.getItem("token_ol");
if(tokenLS){
localStorage.setItem("in_db", true);
}
var inDB = localStorage.getItem("in_db");
var db = new Dexie("DocIntDB");
db.version(1).stores({
token: '++id,token,token_exp'
});
//log ("Using Dexie v" + Dexie.semVer);
//
// Query Database
//
db.open().then(function() {
return db.token.add({
token: tokenLS,
token_exp: "123456",
});
}).then(function() {
return db.token
.orderBy("id")
//.where('token_exp')
//.between(40,65)
.toArray();
}).then(function(token) {
//db.token.add({token: "MyToken", token_exp: 45})
//log("token Type " + typeof(db.token));
var tokenLength = token.length;
var tokenVal = token[0];
var myTokenItem = tokenVal.id;
db.token.where('id').above(myTokenItem).delete();
//alert(myTokenItem);
//if()
log("token_exp: " + JSON.stringify(tokenVal.token_exp));
log("id: " + JSON.stringify(tokenVal.id));
log("Found token: " + JSON.stringify(token, null, 2));
log("Token Length: " + JSON.stringify(tokenLength, null, 2));
//log("getID: " + db.token.get({id: 166}));
// return db.token.where('id').above(myTokenItem).delete();
/* }).then(function(){
return db.delete(); */ // So you can experiment again and again...
}).catch(Dexie.MissingAPIError, function(e) {
log("Couldn't find indexedDB API");
}).catch('SecurityError', function(e) {
log("SeurityError - This browser doesn't support indexedDB.");
}).catch(function(e) {
log(e);
});