我有一个对象数组,它可以有数百万行。我试图将其存储在indexDB,我可以成功地做到这一点,但它有一些性能问题的"放"方法。原因是因为我必须遍历每个索引并将其单独放在indexDB中。有没有一种方法可以简单地将整个数组转储到这里?这是我的代码…
var indexDB;
indexDB = window.indexedDB;
var open = indexedDB.open("HistoricalDB");
open.onupgradeneeded = function(){
let db = open.result;
let store = db.createObjectStore("HistoricalTable", {keyPath: "id"});
let index = store.createIndex("CIndex", ["time", "value"]);
};
open.onsuccess = function(){
let db = open.result;
let tx = db.transaction("HistoricalTable", "readwrite");
let store = tx.objectStore("HistoricalTable");
let index = store.index("CIndex");
for (let i=0; i<allHistoricalData.length-1; i++){
store.put(allHistoricalData[i]);
}
let getAll = store.getAll();
getAll.onsuccess = function(){
console.log(getAll.result)
}
tx.oncomplete = function(){
db.close();
};
}
这就是为什么要杀我....
for (let i=0; i<allHistoricalData.length-1; i++){
store.put(allHistoricalData[i]);
}
是否有一种方法来做store.put(allHistoricalData)而不是store。把(allHistoricalData[i]在一个循环?
IndexDB可以存储任何类型的javascript可克隆数据,因此您应该能够简单地将您的数组作为一个整体存储。
我看到你正在创建一个索引,并使用它来存储值。IndexDB索引用于查找存储中的数据,但不应该用于存储数据本身。
你要做的是像这样把数据直接放到存储中:
var open = window.indexedDB.open('HistoricalDB');
open.onupgradeneeded = function () {
let db = open.result;
db.createObjectStore('HistoricalTable');
};
open.onsuccess = function () {
let db = open.result;
let tx = db.transaction('HistoricalTable', 'readwrite');
let store = tx.objectStore('HistoricalTable');
let request = store.put(allHistoricalData, 'DATA');
request.onsuccess = function () {
console.log('success!');
};
request.onerror = function () {
console.log(request.error);
};
};