IndexedDB 是一種低級API,用于客戶端存儲大量結(jié)構(gòu)化數(shù)據(jù)(包括, 文件/ blobs)。該API使用索引來實現(xiàn)對該數(shù)據(jù)的高性能搜索。
最近有一項業(yè)務(wù)需求,就是可以離線存儲數(shù)據(jù),等到有網(wǎng)絡(luò)信號的時候可以上傳表單和圖片。所以研究了一下HTML5的IndexedDB。
對于只存儲某些字段的需求來說,可以使用Local Storage和 Session Storage來完成。但是一旦存儲大量的數(shù)據(jù),Local Storage和 Session Storage就遠遠不能滿足需求了。這時,IndexedDB的強大之處就會體現(xiàn)出來了。
1、創(chuàng)建或者打開數(shù)據(jù)庫
/* 對不同瀏覽器的indexedDB進行兼容 */
const indexeddb = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
/* 創(chuàng)建或連接數(shù)據(jù)庫 */
const request = indexeddb.open(name, version); // name:數(shù)據(jù)庫名,version:數(shù)據(jù)庫版本號
因為indexedDB在不同的瀏覽器上有兼容性,所以我們需要些一個兼容函數(shù)來兼容indexedDB。
2、連接到數(shù)據(jù)庫的回調(diào)函數(shù)
request.addEventListener('success', function(event){
// 打開或創(chuàng)建數(shù)據(jù)庫成功
}, false);
request.addEventListener('error', function(event){
// 打開或創(chuàng)建數(shù)據(jù)庫失敗
}, false);
request.addEventListener('upgradeneeded', function(event){
// 更新數(shù)據(jù)庫時執(zhí)行
}, false);
在連接到數(shù)據(jù)庫后,request會監(jiān)聽三種狀態(tài):
- success:打開或創(chuàng)建數(shù)據(jù)庫成功
- error:打開或創(chuàng)建數(shù)據(jù)庫失敗
- upgradeneeded:更新數(shù)據(jù)庫
upgradeneeded狀態(tài)是在indexedDB創(chuàng)建新的數(shù)據(jù)庫時和indexeddb.open(name, version) version(數(shù)據(jù)庫版本號)發(fā)生變化時才能監(jiān)聽到此狀態(tài)。當版本號不發(fā)生變化時,不會觸發(fā)此狀態(tài)。數(shù)據(jù)庫的ObjectStore的創(chuàng)建、刪除等都是在這個監(jiān)聽事件下執(zhí)行的。
3、創(chuàng)建、刪除ObjectStore
在indexedDB中,ObjectStore類似于數(shù)據(jù)庫的表。
request.addEventListener('upgradeneeded', function(event){
// 創(chuàng)建數(shù)據(jù)庫實例
const db = event.target.result;
// 關(guān)閉數(shù)據(jù)庫
db.close();
// 判斷是否有ObjectStore
db.objectStoreNames.contains(objectStoreName);
// 刪除ObjectStore
db.deleteObjectStore(objectStoreName);
}, false);
可以用如下方法創(chuàng)建一個ObjectStore
request.addEventListener('upgradeneeded', function(event){
// 創(chuàng)建數(shù)據(jù)庫實例
const db = event.target.result;
// 判斷是否有ObjectStore
if(!db.objectStoreNames.contains(objectStoreName)){
const store = db.createObjectStore(objectStoreName, {
keyPath: keyPath // keyPath 作為ObjectStore的搜索關(guān)鍵字
});
// 為ObjectStore創(chuàng)造索引
store.createIndex(name, // 索引
index, // 鍵值
{
unique: unique // 索引是否唯一
});
}
}, false);
4、數(shù)據(jù)的增刪改查
request.addEventListener('success', function(event){
// 創(chuàng)建數(shù)據(jù)庫實例
const db = event.target.result;
// 查找一個ObjectStore
db.transaction(objectStoreName, wa);
// wa為'readwrite'時,數(shù)據(jù)可以讀寫
// wa為'readonly'時,數(shù)據(jù)只讀
const store = transaction.objectStore(objectStoreName);
}, false);
數(shù)據(jù)庫的增刪改查:
// 添加數(shù)據(jù),當關(guān)鍵字存在時數(shù)據(jù)不會添加
store.add(obj);
// 更新數(shù)據(jù),當關(guān)鍵字存在時覆蓋數(shù)據(jù),不存在時會添加數(shù)據(jù)
store.put(obj);
// 刪除數(shù)據(jù),刪除指定的關(guān)鍵字對應(yīng)的數(shù)據(jù)
store.delete(value);
// 清除ObjectStore
store.clear();
// 查找數(shù)據(jù),根據(jù)關(guān)鍵字查找指定的數(shù)據(jù)
const g = store.get(value);
g.addEventListener('success', function(event){
// 異步查找后的回調(diào)函數(shù)
}, false);
按索引查找數(shù)據(jù)
const index = store.index(indexName);
const cursor = index.openCursor(range);
cursor.addEventListener('success', function(event){
const result = event.target.result;
if(result){
result.value // 數(shù)據(jù)
result.continue(); // 迭代,游標下移
}
}, false);
按索引的范圍查找數(shù)據(jù)
const index = store.index(indexName);
const cursor = index.openCursor(range);
/**
* range為null時,查找所有數(shù)據(jù)
* range為指定值時,查找索引滿足該條件的對應(yīng)的數(shù)據(jù)
* range為IDBKeyRange對象時,根據(jù)條件查找滿足條件的指定范圍的數(shù)據(jù)
*/
// 大于或大于等于
range = IDBKeyRange.lowerBound(value, true) // (value, +∞),> value
range = IDBKeyRange.lowerBound(value, false) // [value, +∞),>= value
// 小于或小于等于,isOpen:true,開區(qū)間;false,閉區(qū)間
range = IDBKeyRange.upperBound(value, isOpen)
// 大于或大于等于value1,小于或小于等于value2
IDBKeyRange.bound(value1, value2, isOpen1, isOpen2)
最后,自己封裝了一個indexedDB的庫,可以參考一下:duan602728596/IndexedDB
以上所述是小編給大家介紹的HTML5本地存儲之IndexedDB,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!