swapped localdb for DexieDB
This commit is contained in:
parent
d0d446350f
commit
731b96282f
|
@ -1,10 +1,14 @@
|
||||||
import Dexie from 'dexie';
|
import Dexie from 'dexie';
|
||||||
|
|
||||||
const db = new Dexie('fast-calorie');
|
const db = new Dexie('fast');
|
||||||
|
|
||||||
db.version(1).stores({
|
db.version(1).stores({
|
||||||
catalog: `value type`,
|
catalog: '++id, value, type, target',
|
||||||
entries: `value type`
|
entries: '++id, day, type'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// db.open().catch(function (err) {
|
||||||
|
// console.error('Failed to open db: ' + (err.stack || err));
|
||||||
|
// });
|
||||||
|
|
||||||
export default db;
|
export default db;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import Litedom from '/js/litedom.es.js';
|
import Litedom from '/js/litedom.es.js';
|
||||||
import store from '/js/store.js';
|
import db from '/js/db.js';
|
||||||
|
|
||||||
Litedom({
|
Litedom({
|
||||||
el: '#app',
|
el: '#app',
|
||||||
|
@ -26,9 +26,9 @@ Litedom({
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
init() {
|
async init() {
|
||||||
// load todays history
|
// load todays history
|
||||||
this.data.history = store.get(this.data.today) || [];
|
this.entries();
|
||||||
|
|
||||||
// set focus
|
// set focus
|
||||||
document.getElementById('calories').focus();
|
document.getElementById('calories').focus();
|
||||||
|
@ -36,6 +36,9 @@ Litedom({
|
||||||
// start service worker
|
// start service worker
|
||||||
// this.worker();
|
// this.worker();
|
||||||
},
|
},
|
||||||
|
async entries() {
|
||||||
|
this.data.history = await db.entries.where({ day: this.data.today }).toArray();
|
||||||
|
},
|
||||||
details(e) {
|
details(e) {
|
||||||
// get selected index
|
// get selected index
|
||||||
let idx = e.target.getAttribute('idx');
|
let idx = e.target.getAttribute('idx');
|
||||||
|
@ -49,11 +52,11 @@ Litedom({
|
||||||
// get selected index
|
// get selected index
|
||||||
let idx = e.target.getAttribute('idx');
|
let idx = e.target.getAttribute('idx');
|
||||||
|
|
||||||
// remove entry from history
|
// delete entry
|
||||||
this.data.history.splice(idx, 1);
|
db.entries.delete(this.data.history[idx].id).then(() => {
|
||||||
|
// update entries list
|
||||||
// save history
|
this.entries();
|
||||||
this.data.history = store.set(this.data.today, this.data.history);
|
});
|
||||||
},
|
},
|
||||||
modify(e) {
|
modify(e) {
|
||||||
// get selected index
|
// get selected index
|
||||||
|
@ -66,9 +69,6 @@ Litedom({
|
||||||
let calories = document.getElementById('calories');
|
let calories = document.getElementById('calories');
|
||||||
let name = document.getElementById('name');
|
let name = document.getElementById('name');
|
||||||
|
|
||||||
// get todays latest history
|
|
||||||
let history = store.get(this.data.today) || [];
|
|
||||||
|
|
||||||
// source of calories
|
// source of calories
|
||||||
let source = (calories) => {
|
let source = (calories) => {
|
||||||
let value = Number(calories.value);
|
let value = Number(calories.value);
|
||||||
|
@ -80,16 +80,21 @@ Litedom({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// entry payload
|
// add new entry
|
||||||
history.push({
|
db.entries.add({
|
||||||
target: 'calorie',
|
|
||||||
name: String(name.value),
|
name: String(name.value),
|
||||||
|
target: 'calorie',
|
||||||
type: source(calories),
|
type: source(calories),
|
||||||
value: Number(calories.value)
|
value: Number(calories.value),
|
||||||
});
|
day: this.data.today,
|
||||||
|
created: Date.now(),
|
||||||
|
modified: Date.now(),
|
||||||
|
}).then(async () => {
|
||||||
// update todays history
|
// update todays history
|
||||||
this.data.history = store.set(this.data.today, history);
|
this.entries();
|
||||||
|
}).catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
});
|
||||||
|
|
||||||
// reset form
|
// reset form
|
||||||
document.getElementById('entry').reset();
|
document.getElementById('entry').reset();
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
const storage = {
|
|
||||||
get: (key) => {
|
|
||||||
return JSON.parse(window.localStorage.getItem(key));
|
|
||||||
},
|
|
||||||
set: (key, value) => {
|
|
||||||
window.localStorage.setItem(key, JSON.stringify(value));
|
|
||||||
|
|
||||||
return storage.get(key);
|
|
||||||
},
|
|
||||||
update: (key, json) => {
|
|
||||||
let updated = Object.assign(session.get(key) || {}, json);
|
|
||||||
|
|
||||||
window.localStorage.setItem(key, JSON.stringify(updated));
|
|
||||||
|
|
||||||
return storage.get(key);
|
|
||||||
},
|
|
||||||
remove: function(key) {
|
|
||||||
return window.localStorage.removeItem(key);
|
|
||||||
},
|
|
||||||
clear: function() {
|
|
||||||
return window.localStorage.clear();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export default storage;
|
|
Loading…
Reference in New Issue