indexedDB的简单使用封装
const noop = (...args: any) => undefined;
type mode = "readonly" | "readwrite";
interface Obj {
[propName: string]: any;
}
const menuList: any = {
getObjectStore(store_name: string, mode: mode): object {
const tx = this.transaction(store_name, mode);
return tx.objectStore(store_name);
},
addStore(store_name: string, jsonObj: Obj, success = noop, fail = noop) {
const store = this.getObjectStore(store_name, "readwrite");
let req;
try {
req = store.add(jsonObj);
} catch (e) {
if (e.name == "DataCloneError") throw e;
}
req.onsuccess = function () {
success();
};
req.onerror = function () {
console.error("addStore error", this.error);
fail(this.error);
};
},
updateStore(store_name: string, jsonObj: Obj, success = noop, fail = noop) {
if (!jsonObj.id) {
fail("未找到id索引");
return;
}
const store = this.getObjectStore(store_name, "readwrite");
let req;
try {
req = store.put(jsonObj);
} catch (e) {
if (e.name == "DataCloneError") throw e;
}
req.onsuccess = function () {
console.log("addStore successful");
success();
};
req.onerror = function () {
console.error("addStore error", this.error);
fail(this.error);
};
},
clearObjectStore(store_name: string, success = noop, fail = noop) {
const store = this.getObjectStore(store_name, "readwrite");
const req = store.clear();
req.onsuccess = function () {
success();
};
req.onerror = function (evt: Obj) {
fail(evt.target.errorCode);
};
},
getValueByKey(
store_name: string,
key: string,
value: string,
success = noop,
fail = noop
) {
const store = this.getObjectStore(store_name, "readwrite");
let req = store.index(key);
req = req.get(value);
req.onsuccess = function (evt: Obj) {
const record = evt.target.result;
success(record);
};
req.onerror = function (evt: Obj) {
console.error("deleteStore:", evt.target.errorCode);
fail(evt.target.errorCode);
};
},
deleteStoreById(
store_name: string,
key: string,
success = noop,
fail = noop
) {
const store = this.getObjectStore(store_name, "readwrite");
let req = store.get(key);
req.onsuccess = function (evt: Obj) {
const record = evt.target.result;
console.log("record:", record);
if (typeof record == "undefined") {
fail("No matching record found");
return;
}
req = store.delete(key);
req.onsuccess = function () {
console.log("delete successful");
success();
};
req.onerror = function (evt: Obj) {
console.error("deleteStore:", evt.target.errorCode);
fail(evt.target.errorCode);
};
};
req.onerror = function (evt: Obj) {
console.error("deleteStore:", evt.target.errorCode);
fail(evt.target.errorCode);
};
},
deleteStoreFromName(
store_name: string,
key: string,
value: string,
success = noop,
fail = noop
) {
const store = this.getObjectStore(store_name, "readwrite");
const req = store.index(key);
let that = this;
req.get(value).onsuccess = function (evt: Obj) {
if (typeof evt.target.result == "undefined") {
fail("No matching record found");
return;
}
that.deleteStoreById(evt.target.result.id, store_name, success, fail);
};
req.onerror = function (evt: Obj) {
fail(evt.target.errorCode);
};
},
getListByStore(store_name: string, success = noop, fail = noop) {
const objectStore = this.getObjectStore(store_name, "readwrite");
const open = objectStore.openCursor();
const arr: any[] = [];
open.onsuccess = function (event: Obj) {
const cursor = event.target.result;
if (cursor) {
arr.push(cursor.value);
cursor.continue();
} else {
success(arr);
}
};
open.onerror = function (err: Obj) {
fail(err);
};
},
};
function createStore(
res: Obj,
DB_STORE_NAME: string = "",
STORE_ARR: any[] = []
) {
const store = res.createObjectStore(DB_STORE_NAME, {
keyPath: "id",
autoIncrement: true,
});
STORE_ARR.forEach((key, index) => {
if (index === 0) {
store.createIndex(key, key, { unique: true });
} else {
store.createIndex(key, key, { unique: false });
}
});
return store;
}
export async function createIndexedDB(
DB_NAME: string,
DB_VERSION: number,
DB_STORE_NAME: string,
STORE_ARR: any[]
) {
const indexedDB =
window.indexedDB ||
window.webkitIndexedDB ||
window.mozIndexedDB ||
window.msIndexedDB;
if (!indexedDB) {
throw "你的浏览器不支持indexedDB";
}
return new Promise((resolve, reject) => {
const IDB = window.indexedDB.open(DB_NAME, DB_VERSION);
IDB.onsuccess = function (evt) {
console.log("openDb DONE");
const result = Object.assign(this.result, menuList);
resolve(result);
};
IDB.onerror = function (evt: Obj) {
console.error("openDb:", evt.target.errorCode);
reject(evt.target.errorCode);
};
IDB.onupgradeneeded = function (evt: Obj) {
console.log("openDb.onupgradeneeded");
const res = evt.currentTarget.result;
createStore(res, DB_STORE_NAME, STORE_ARR);
};
});
}