2019-02-03 02:40:24 +01:00
|
|
|
let idbKeyval = (() => {
|
2019-02-02 20:13:06 +01:00
|
|
|
let db;
|
|
|
|
|
|
|
|
|
|
function getDB() {
|
|
|
|
|
if (!db) {
|
2019-02-04 03:09:15 +01:00
|
|
|
// console.log('CREO DB STORAGE JS !')
|
2019-02-02 20:13:06 +01:00
|
|
|
db = new Promise((resolve, reject) => {
|
|
|
|
|
const openreq = indexedDB.open('mydb3', 11);
|
|
|
|
|
|
|
|
|
|
openreq.onerror = () => {
|
|
|
|
|
reject(openreq.error);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
openreq.onupgradeneeded = () => {
|
|
|
|
|
// First time setup: create an empty object store
|
|
|
|
|
openreq.result.createObjectStore('todos', { keyPath: '_id' });
|
|
|
|
|
openreq.result.createObjectStore('sync_todos', { keyPath: '_id' });
|
2019-02-04 03:09:15 +01:00
|
|
|
openreq.result.createObjectStore('sync_todos_patch', { keyPath: '_id' });
|
2019-02-02 20:13:06 +01:00
|
|
|
openreq.result.createObjectStore('delete_todos', { keyPath: '_id' });
|
|
|
|
|
openreq.result.createObjectStore('config', { keyPath: '_id' });
|
2019-02-04 03:09:15 +01:00
|
|
|
openreq.result.createObjectStore('swmsg', { keyPath: '_id' });
|
2019-02-02 20:13:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
openreq.onsuccess = () => {
|
|
|
|
|
resolve(openreq.result);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function withStore(type, table, callback, ) {
|
|
|
|
|
const db = await getDB();
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const transaction = db.transaction(table, type);
|
|
|
|
|
transaction.oncomplete = () => resolve();
|
|
|
|
|
transaction.onerror = () => reject(transaction.error);
|
|
|
|
|
callback(transaction.objectStore(table));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
async get(key) {
|
|
|
|
|
let req;
|
2019-02-02 21:16:49 +01:00
|
|
|
await withStore('readonly', 'keyval', store => {
|
2019-02-02 20:13:06 +01:00
|
|
|
req = store.get(key);
|
|
|
|
|
});
|
|
|
|
|
return req.result;
|
|
|
|
|
},
|
2019-02-11 02:58:53 +01:00
|
|
|
|
|
|
|
|
// jsonCopy(src) {
|
|
|
|
|
// return JSON.parse(JSON.stringify(src));
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
|
|
// contains(a, b) {
|
|
|
|
|
// // array matches
|
|
|
|
|
// if (Array.isArray(b)) {
|
|
|
|
|
// return b.some(x => a.indexOf(x) > -1);
|
|
|
|
|
// }
|
|
|
|
|
// // string match
|
|
|
|
|
// return a.indexOf(b) > -1;
|
|
|
|
|
// },
|
|
|
|
|
|
2019-02-02 21:16:49 +01:00
|
|
|
async getdata(table, key) {
|
|
|
|
|
let req;
|
2019-02-03 00:51:58 +01:00
|
|
|
|
2019-02-02 21:16:49 +01:00
|
|
|
await withStore('readonly', table, store => {
|
2019-02-11 02:58:53 +01:00
|
|
|
// console.log('store', store, 'key', key)
|
2019-02-02 21:16:49 +01:00
|
|
|
req = store.get(key);
|
|
|
|
|
});
|
2019-02-11 02:58:53 +01:00
|
|
|
|
2019-02-02 21:16:49 +01:00
|
|
|
return req.result;
|
|
|
|
|
},
|
2019-02-02 20:13:06 +01:00
|
|
|
async getalldata(table) {
|
|
|
|
|
let req;
|
|
|
|
|
await withStore('readonly', table, store => {
|
|
|
|
|
req = store.getAll();
|
|
|
|
|
});
|
|
|
|
|
return req.result;
|
|
|
|
|
},
|
2019-02-02 21:16:49 +01:00
|
|
|
async set(key, value) {
|
2019-02-03 00:51:58 +01:00
|
|
|
let req;
|
|
|
|
|
await withStore('readwrite', 'keyval', store => {
|
|
|
|
|
req = store.put(value, key);
|
2019-02-02 20:13:06 +01:00
|
|
|
});
|
2019-02-03 00:51:58 +01:00
|
|
|
return req.result;
|
2019-02-02 20:13:06 +01:00
|
|
|
},
|
2019-02-03 00:51:58 +01:00
|
|
|
async setdata(table, value) {
|
|
|
|
|
let req;
|
2019-02-11 02:58:53 +01:00
|
|
|
// console.log('setdata', table, value)
|
2019-02-02 21:16:49 +01:00
|
|
|
|
2019-02-03 00:51:58 +01:00
|
|
|
await withStore('readwrite', table, store => {
|
|
|
|
|
req = store.put(value);
|
2019-02-02 20:13:06 +01:00
|
|
|
});
|
2019-02-03 00:51:58 +01:00
|
|
|
return req.result;
|
2019-02-02 20:13:06 +01:00
|
|
|
},
|
2019-02-02 21:16:49 +01:00
|
|
|
async delete(key) {
|
|
|
|
|
return await withStore('readwrite', 'keyval', store => {
|
2019-02-02 20:13:06 +01:00
|
|
|
store.delete(key);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
async deletedata(table, key) {
|
|
|
|
|
return await withStore('readwrite', table, store => {
|
|
|
|
|
store.delete(key);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
async clearalldata(table) {
|
2019-02-12 12:06:01 +01:00
|
|
|
console.log('clearalldata', table)
|
2019-02-02 20:13:06 +01:00
|
|
|
return await withStore('readwrite', table, store => {
|
|
|
|
|
store.clear();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
// iOS add-to-homescreen is missing IDB, or at least it used to.
|
|
|
|
|
// I haven't tested this in a while.
|
|
|
|
|
if (!self.indexedDB) {
|
|
|
|
|
idbKeyval = {
|
|
|
|
|
get: key => Promise.resolve(localStorage.getItem(key)),
|
|
|
|
|
set: (key, val) => Promise.resolve(localStorage.setItem(key, val)),
|
|
|
|
|
delete: key => Promise.resolve(localStorage.removeItem(key))
|
|
|
|
|
};
|
|
|
|
|
}
|