2023-06-30 15:19:52 +00:00
|
|
|
class EJS_STORAGE {
|
|
|
|
constructor(dbName, storeName) {
|
|
|
|
this.dbName = dbName;
|
|
|
|
this.storeName = storeName;
|
|
|
|
}
|
|
|
|
get(key) {
|
2023-07-01 18:15:26 +00:00
|
|
|
if (!window.indexedDB) return null;
|
2023-06-30 15:19:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let openRequest = indexedDB.open(this.dbName, 1);
|
|
|
|
openRequest.onerror = () => {};
|
|
|
|
openRequest.onsuccess = () => {
|
|
|
|
let db = openRequest.result;
|
|
|
|
let transaction = db.transaction([this.storeName], "readwrite");
|
|
|
|
let objectStore = transaction.objectStore(this.storeName);
|
|
|
|
let request = objectStore.get(key);
|
|
|
|
request.onsuccess = async (e) => {
|
|
|
|
resolve(request.result);
|
|
|
|
};
|
|
|
|
request.onerror = () => resolve();
|
|
|
|
};
|
|
|
|
openRequest.onupgradeneeded = () => {
|
|
|
|
let db = openRequest.result;
|
|
|
|
if (! db.objectStoreNames.contains(this.storeName)) {
|
|
|
|
db.createObjectStore(this.storeName);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
put(key, data) {
|
2023-07-01 18:15:26 +00:00
|
|
|
if (!window.indexedDB) return null;
|
2023-06-30 15:19:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let openRequest = indexedDB.open(this.dbName, 1);
|
|
|
|
openRequest.onerror = () => {};
|
|
|
|
openRequest.onsuccess = () => {
|
|
|
|
let db = openRequest.result;
|
|
|
|
let transaction = db.transaction([this.storeName], "readwrite");
|
|
|
|
let objectStore = transaction.objectStore(this.storeName);
|
|
|
|
let request = objectStore.put(data, key);
|
|
|
|
request.onerror = () => resolve();
|
|
|
|
request.onsuccess = () => resolve();
|
|
|
|
};
|
|
|
|
openRequest.onupgradeneeded = () => {
|
|
|
|
let db = openRequest.result;
|
|
|
|
if (! db.objectStoreNames.contains(this.storeName)) {
|
|
|
|
db.createObjectStore(this.storeName);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
}
|
|
|
|
remove(key) {
|
2023-07-01 18:15:26 +00:00
|
|
|
if (!window.indexedDB) return null;
|
2023-06-30 15:19:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let openRequest = indexedDB.open(this.dbName, 1);
|
|
|
|
openRequest.onerror = () => {};
|
|
|
|
openRequest.onsuccess = () => {
|
|
|
|
let db = openRequest.result;
|
|
|
|
let transaction = db.transaction([this.storeName], "readwrite");
|
|
|
|
let objectStore = transaction.objectStore(this.storeName);
|
|
|
|
let request2 = objectStore.delete(key);
|
|
|
|
request2.onsuccess = () => resolve();
|
|
|
|
request2.onerror = () => {};
|
|
|
|
};
|
|
|
|
openRequest.onupgradeneeded = () => {
|
|
|
|
let db = openRequest.result;
|
|
|
|
if (! db.objectStoreNames.contains(this.storeName)) {
|
|
|
|
db.createObjectStore(this.storeName);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.EJS_STORAGE = EJS_STORAGE;
|