溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

HTML5本地存儲之IndexedDB有什么用

發(fā)布時間:2021-07-24 13:53:02 來源:億速云 閱讀:181 作者:小新 欄目:web開發(fā)

這篇文章主要介紹HTML5本地存儲之IndexedDB有什么用,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

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就遠(yuǎn)遠(yuǎn)不能滿足需求了。這時,IndexedDB的強(qiáng)大之處就會體現(xiàn)出來了。

1、創(chuàng)建或者打開數(shù)據(jù)庫

/* 對不同瀏覽器的indexedDB進(jìn)行兼容 */
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)。當(dāng)版本號不發(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ù),當(dāng)關(guān)鍵字存在時數(shù)據(jù)不會添加
store.add(obj);
// 更新數(shù)據(jù),當(dāng)關(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(); // 迭代,游標(biāo)下移
    }
}, 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)

以上是“HTML5本地存儲之IndexedDB有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI