像MongoDB一样的JS库在客户端indexedDB中存储对象? [英] MongoDB-like JS library to store objects in client side indexedDB?
问题描述
给出以下localStorage
代码( jsfiddle ):
// my new information
var data = {character: "中", totalMistakes: 0};
var han = data.character;
// create localStorage; Unpack, update, repackage knol
/* **** THIS IS THE SECTION TO CONVERT **** */
localStorage.knol = '{}'; // create pseudo object, as string
var knol = JSON.parse(localStorage.knol)
knol[han] = data;
localStorage.knol = JSON.stringify(knol);
// Print to check if all went well.
console.log('data: ',data)
console.log('han: ',han)
console.log('knol: ',knol)
console.log('localStorage.knol: ',localStorage.knol)
console.log('localStorage.knol: ',JSON.parse(localStorage.knol))
console.log('localStorage.knol[han]: ',JSON.parse(localStorage.knol)[han])
最后,localStorage.knol
是:
{
"中": {character: "中", totalMistakes: 0}
}
我正在寻找一种类似于Mongo的js库,以将数据存储在客户端indexedDB上,其语法类似于我已经熟悉的MongoDB.
如何将上面的localStorage代码转换为类似Mongo的IndexedDB库语法,以便存储对象?
我建议使用minimongo,但是任何存储在indexedDB中的类似MongoDB的库都可以.
Dexie.js
使用 Dexie.js及其API (有使用方法在网络浏览器中
ZangoDB
(正在进行探索 https://jsfiddle.net/vb92pecv/3/)>
Given the following localStorage
code (jsfiddle) :
// my new information
var data = {character: "中", totalMistakes: 0};
var han = data.character;
// create localStorage; Unpack, update, repackage knol
/* **** THIS IS THE SECTION TO CONVERT **** */
localStorage.knol = '{}'; // create pseudo object, as string
var knol = JSON.parse(localStorage.knol)
knol[han] = data;
localStorage.knol = JSON.stringify(knol);
// Print to check if all went well.
console.log('data: ',data)
console.log('han: ',han)
console.log('knol: ',knol)
console.log('localStorage.knol: ',localStorage.knol)
console.log('localStorage.knol: ',JSON.parse(localStorage.knol))
console.log('localStorage.knol[han]: ',JSON.parse(localStorage.knol)[han])
At the end, localStorage.knol
is :
{
"中": {character: "中", totalMistakes: 0}
}
I'am looking for a Mongo-like js library to store data on client side indexedDB, with a syntax similar to MongoDB with which I'am already familiar.
How to convert localStorage code above into Mongo-like IndexedDB library syntax so to store an object ?
EDIT: I suggest minimongo, but any MongoDB-like library storing in indexedDB will do.
There is a variety of librairies available to do that.
Dexie.js
Using Dexie.js and its API (jsfiddle) :
<!-- Include dexie.js -->
<script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
<script>
var db = new Dexie('MyDatabase');
// Define a schema
db.version(1).stores({ knol: 'character, totalMistakes' });
// Open the database
db.open().catch(function(error) { alert('Uh oh : ' + error); });
// or make a new one
db.knol.put({ character: '中', totalMistakes: 8 });
// Find some old friends
var mistakes = db.knol.where('totalMistakes');
//mistakes.above(6).each (function (item) { console.log (item); });
mistakes.aboveOrEqual(0).each (function (item) { console.log (item)});
</script>
Minimongo
I don't recommend it, but there is how to use it in web browsers
ZangoDB
(Exploration ongoing https://jsfiddle.net/vb92pecv/3/ )
这篇关于像MongoDB一样的JS库在客户端indexedDB中存储对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!