日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

indexedDB复合索引

發(fā)布時間:2023/12/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 indexedDB复合索引 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目的:通過構建聚合索引,根據其中一個索引值匹配另一個索引值(不會對indexedDB數據進行深拷貝,只獲取索引的值),提供性能優(yōu)化

// index.html <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title> </head><body><button id="add">添加</button><input type="text" placeholder="請輸入ID" id="idInput" /><button id="search">復合索引</button><script src="./db.js"></script> </body></html> // db.js //indexDB是瀏覽器本地數據庫,用于存儲大量數據,存儲格式為json var db; // 打開創(chuàng)建數據庫: function openDB(name, version = 1) {if (!window.indexedDB) {alert("您的瀏覽器不支持indexDB");return;}var indexDBRequest = window.indexedDB.open(name, version);indexDBRequest.onerror = function(e) {console.log("Open Error!");};indexDBRequest.onsuccess = function(e) {db = indexDBRequest.result; //這里才是 indexedDB對象console.log("創(chuàng)建/打開數據庫成功。db:%o", db);};indexDBRequest.onupgradeneeded = function(e) {console.log("DB version change to " + version);db = indexDBRequest.result;// 有了數據庫后我們自然希望創(chuàng)建一個表用來存儲數據,但indexedDB中沒有表的概念,而是叫 objectStore ,一個數據庫中可以包含多個objectStore,objectStore是一個靈活的數據結構,可以存放多種類型數據。也就是說一個objectStore相當于一張表,里面存儲的每條數據和一個鍵相關聯(lián)。if (!db.objectStoreNames.contains("students")) {var store = db.createObjectStore("students", { keyPath: "id" });//刪除objectStore// db.deleteObjectStore('storeName');// 創(chuàng)建索引// 在indexedDB中有兩種索引,一種是自增長的int值,一種是keyPath:自己指定索引列store.createIndex("nameIndex", "name", { unique: true });store.createIndex("ageIndex", "age", { unique: false });// 1. 創(chuàng)建復合索引store.createIndex("id_age", ["id", "age"], { unique: false });console.log("第一次創(chuàng)建數據庫或者更新數據庫。db:%o", db);}}; } window.onload = function() {openDB("dbname1"); };function saveData(storeName, data) {//創(chuàng)建事務var transaction = db.transaction([storeName], "readwrite");//訪問事務中的objectStorevar store = transaction.objectStore(storeName);//data為對象var addRequest = store.add(data);addRequest.onsuccess = function(event) {console.log("save data done...", store);};addRequest.onerror = function(event) {console.log("數據寫入失敗", store);}; } document.getElementById("add").onclick = function() {saveData("students", { id: "3", name: "張三3", age: 18 });saveData("students", { id: "1", name: "張三1", age: 27 });saveData("students", { id: "4", name: "張三4", age: 35 }); }; /** 通過 id 獲取 age 的值* 通過構建聚合索引,根據其中一個索引值匹配另一個索引值(不會對indexedDB數據進行深拷貝,只獲取索引的值) */ function getIndexByKey(indexName, indexRangeArr) {return new Promise((resolve) => {//創(chuàng)建事務var transaction = db.transaction(["students"], "readwrite");//訪問事務中的objectStorevar store = transaction.objectStore("students");const index = store.index(indexName);const indexRange = IDBKeyRange.bound(...indexRangeArr);const req = index.openKeyCursor(indexRange);req.onsuccess = function(ev) {const cursor = ev.target.result;if (!cursor) {resolve(null);} else {resolve({key: cursor.primaryKey,index: cursor.key,});}};}); } document.getElementById("search").onclick = async function() {const id = document.getElementById("idInput").value;if (id) {const res = await getIndexByKey("id_age", [[id], // id_age 復合索引[id + "_"], // id_age 復合索引,id + '_'一定大于 idfalse, // 包含上下限false, // 包含上下限]);console.log("search resulte...",res,"年齡是...",res ? res.index[1] : res);} else {console.log("請選輸入ID...");} };

總結

以上是生活随笔為你收集整理的indexedDB复合索引的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。