0

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.

Josh
  • 17,834
  • 7
  • 50
  • 68
Hugo LOPEZ
  • 605
  • 8
  • 21
  • 1
    So, you're looking for a tutorial on how to use minimongo? – devius Feb 20 '18 at 17:14
  • (That's available online. I would like to document on Stackoverflow various approach for **MongoDB-like indexedDB librairies**: minimongo, Dexie.js, ZangoDB, etc. A 101 kick starter. I'am at the beggining of my journey indeed. Feel free to submit something. All ways are welcome.) – Hugo LOPEZ Feb 20 '18 at 17:16

1 Answers1

2

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/ )

Community
  • 1
  • 1
Hugo LOPEZ
  • 605
  • 8
  • 21
  • ZangoDB seems to be abandoned... The author last commented on issues in the GitHub repo in 2019. – Denis Aug 17 '23 at 06:15