diff --git a/wco/apx/apx.js b/wco/apx/apx.js index 10acf94..73516a5 100644 --- a/wco/apx/apx.js +++ b/wco/apx/apx.js @@ -514,19 +514,19 @@ apx.ready(apx.update); //2nd param optional=> true mean does not wait same if ap apx.indexedDB = apx.indexedDB || {}; -apx.indexedDB.set = async (db, storeName, keyPath, key, value) => { +apx.indexedDB.set = async (db, storeName, value) => { return new Promise((resolve, reject) => { const request = indexedDB.open(db, 1); request.onupgradeneeded = (event) => { const db = event.target.result; - if (window.threadsObj) { - for (const threadId of Object.keys(window.threadsObj)) { - if (!db.objectStoreNames.contains(threadId)) { - db.createObjectStore(threadId, { keyPath: keyPath }); - }; - }; + if (!db.objectStoreNames.contains("threads")) { + db.createObjectStore("threads", { keyPath: "uuid" }); + }; + + if (!db.objectStoreNames.contains("messages")) { + db.createObjectStore("messages", { keyPath: "privatriid" }); }; }; @@ -539,8 +539,8 @@ apx.indexedDB.set = async (db, storeName, keyPath, key, value) => { const transaction = db.transaction(storeName, "readwrite"); const store = transaction.objectStore(storeName); - - const putRequest = store.put({ timestamp: key, value: value }); + + const putRequest = store.put(value); putRequest.onsuccess = () => resolve(); putRequest.onerror = (error) => reject(error); }; @@ -549,35 +549,22 @@ apx.indexedDB.set = async (db, storeName, keyPath, key, value) => { }); }; -apx.indexedDB.get = async (db, storeName, keyPath, key) => { +apx.indexedDB.get = async (db, storeName, key) => { return new Promise((resolve, reject) => { const request = indexedDB.open(db, 1); - request.onupgradeneeded = (event) => { - const db = event.target.result; - - if (window.threadsObj) { - for (const threadId of Object.keys(window.threadsObj)) { - if (!db.objectStoreNames.contains(threadId)) { - db.createObjectStore(threadId, { keyPath: keyPath }); - }; - }; - }; - }; - request.onsuccess = (event) => { const db = event.target.result; - if (!db.objectStoreNames.contains(storeName)) { return resolve(null); - }; - + } const transaction = db.transaction(storeName, "readonly"); const store = transaction.objectStore(storeName); + const getRequest = store.get(key); - + getRequest.onsuccess = () => { - resolve(getRequest.result ? getRequest.result.value : null); + resolve(getRequest.result || null); }; getRequest.onerror = () => resolve(null); @@ -587,22 +574,10 @@ apx.indexedDB.get = async (db, storeName, keyPath, key) => { }); }; -apx.indexedDB.del = async (db, storeName, keyPath, key) => { +apx.indexedDB.del = async (db, storeName, key) => { return new Promise((resolve, reject) => { const request = indexedDB.open(db, 1); - request.onupgradeneeded = (event) => { - const db = event.target.result; - - if (window.threadsObj) { - for (const threadId of Object.keys(window.threadsObj)) { - if (!db.objectStoreNames.contains(threadId)) { - db.createObjectStore(threadId, { keyPath: keyPath }); - }; - }; - }; - }; - request.onsuccess = (event) => { const db = event.target.result;