IndexedDB 簡單封裝

2021-09-07 20:05:46 字數 2749 閱讀 9668

indexeddb 瀏覽器資料庫,是乙個非關係型資料庫,資料形式使用的是json,indexeddb適合儲存大量資料,它的api是非同步呼叫的,當然他的api 也相對複雜。

當然瀏覽器資料儲存 還有localstorage,cookies,web sql等 為什麼還再來乙個indexeddb。

之前我在開發過程中使用的是web sql,可以直接寫sql查詢資料。

localstorage,用key-value鍵值模式儲存資料,而且localstorage就是專門為小數量資料設計的。

cookies只能是字串 而且空間有限。

如果你在實際應用中希望儲存大量資料時,indexeddb會明顯的更適合,indexeddb也能提供你更為複雜的查詢資料的方式,還能建立索引,他的索引還是挺666的。

下面是自己看了阮一峰的 文章

簡單的學習了下 indexeddb 對這個瀏覽器資料庫有個大概的了解,下面是個人對indexeddb 簡單的封裝。

const dbname = "test";

const dbversion = 1;

export

default

] },

student: ]}},

async initdb() ;

request.onsuccess = function

() ;

request.onupgradeneeded = function

(event) );

for (let i = 0; i < that.store[t].cursorindex.length; i++) );}}

}};

},//

開啟資料庫

opendb: function

() ;

request.onsuccess = function

(event) ;

});},

//刪除表 deletedb: function

(table) ;

deletequest.onsuccess = function

() ;

},//

關閉資料庫

closedb: async function

(db)

let closequest =d.closedb();

return

new promise(resolve =>;

closequest.onsuccess = function

() ;

});}

catch

(error)

},//

新增資料,add新增新值

insert: async function

(table, data) ;

request.onsuccess = function

() ;

});}

catch

(error)

},//

更新 update: async function

(table, data) ;

request.onsuccess = function

() ;

});}

catch

(error)

},//

刪除資料

delete: async function

(table, keyvalue) ;

request.onsuccess = function

() ;

});}

catch

(error)

},//

清空資料

clear: async function

(table) ,

//查詢資料 表名 索引值 索引 key 沒有value key為key 而不是索引

get: async function

(table, keyvalue, indexcursor) ;

request.onsuccess = function

(event)

else

} else

};});

} catch

(error)

},//

通過游標運算元據, callback中要有游標移動方式

handledatabycursor: async function

(table, keyrange) ;

request.onsuccess = function

(event) ;

});}

catch

(error)

},//

通過索引游標運算元據, callback中要有游標移動方式

handledatabyindex: async function

(table, keyrange, sursorindex) ;

request.onsuccess = function

(event)

};});

} catch

(error)

},//

建立游標索引

createcursorindex: async function

(table, cursorindex, unique) );

return

promise.resolve();

}};

如果對您有幫助請動動滑鼠右下方給我來個贊,您的支援是我最大的動力。

IndexedDB 簡單封裝

indexeddb 瀏覽器資料庫,是乙個非關係型資料庫,資料形式使用的是json,indexeddb適合儲存大量資料,它的api是非同步呼叫的,當然他的api 也相對複雜。當然瀏覽器資料儲存 還有localstorage,cookies,web sql等 為什麼還再來乙個indexeddb。之前我在...

indexedDB 簡單使用

indexeddb 打造靠譜 web 離線資料庫 前端資料庫 indexeddb 簡介 資料庫配置 let db 資料倉儲物件 資料表 const tablename myobjectstore 暫時沒有什麼用處 模式封裝 const mode 開啟資料庫 引數 dbname,dbver let r...

Linux Epoll簡單封裝

1 單獨起執行緒進行epoll時間的偵聽 2 接收到事件後的預處理 printerr file line handler on recv and send socket event err,null,0 m cache.set usedbuf t buf close event data.fd re...