我想把我的API数据存储在Browser的indexedDB中。我想尝试本地存储,但它有5MB的限制,但我的JSON数据超过7MB。我想保存在indexedDB中,以便更快地访问。我想以JSON格式保存整个数据,但不知道如何设置 IndexedDB 的方案。从数据库中取出的数据是testData
const db =new Dexie("ReactDexie");
db.version(1).stores({
test:"++id title " //Dont Know how to set scheme here for my json object
})
db.open().catch((err)=>{
console.log(err.stack || err)
})
var transaction = db.transaction([testData], IDBTransaction.READ_WRITE);
var objstore = transaction.objectStore(testData);
for (var i = 0; i < testData.length; i++) {
objstore.put(testData[i]);
}
1条答案
按热度按时间vyu0f0g11#
Follow these steps for good architecture and reusable components ( Sample project is created here ):-
1 ) Create one file lets just name it
indexDB.js
2 ) Now make one utility function that will store API data (let's assume this is in file
utility.js
)3 ) function for fetching data from indexDB (in
utility.js
file)4 ) I am considering sample JSON data as following (suppose you are getting this data in
App.js
)5 ) Store and Fetch data as following (you can call
utility.js
functions in `App.js file)The sample project is created here , you can refer to this project for further use, more about schema and useful Dexie related article you can find here .
Note*- Please clear site data, you might face multiple version issues as you were trying earlier (in that case you can change or add extraversion)