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