JSFiddle - React, Tailwind, and code Playground

by megaadriano

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lokijs/1.5.2/lokijs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lokijs/1.5.2/loki-indexed-adapter.min.js"></script>
<div id="fn"></div>

JavaScript

var idbAdapter = new LokiIndexedAdapter('loki2');
var db = new loki('loki.db',{
 autoload: true,
  autoloadCallback : loadHandler,
	adapter: idbAdapter,
  autosave: true, 
  autosaveInterval: 4000
});

function loadHandler() {
      // if database did not exist it will be empty so I will intitialize here
       people = db.getCollection('people');
      if (people === null) {
        people = db.addCollection('people');
        initPeople();
      }
      
       cats = db.getCollection('cats');
      if (cats === null) {
        cats = db.addCollection('cats');
          initCats();
      }
        address = db.getCollection('address');
      if (address === null) {
        address = db.addCollection('address');
          initAddress();
      }
      testaLoki();
    }



function initPeople(){
  people.insert({ id: 0, name: 'Mary' });
  people.insert({ id: 1, name: 'John' });
  people.insert({ id: 2, name: 'Marta' });
  people.insert({ id: 3, name: 'Robert' });
  people.insert({ id: 4, name: 'Antonio' });
  alert('a')
  db.saveDatabase(function(err) {
    if (err) {
      console.log("error : " + err);
    }
    else {
      console.log("database saved.");
    }
  })

}

function initAddress(){
  address.insert({ id: 0, street: 'Av Santa Maria' });
  address.insert({ id: 1, street: 'Rua Simoes' });
  address.insert({ id: 2, street: 'Rua Martanias' });
  address.insert({ id: 3, street: 'Rua Roger' });
  address.insert({ id: 4, street: 'Rua Santo Antonio' });
  alert('a')
  db.saveDatabase(function(err) {
    if (err) {
      console.log("error : " + err);
    }
    else {
      console.log("database saved.");
    }
  })

}


function initCats(){
  cats.insert({ owner: 0, name: 'Betsy' });
  cats.insert({ owner: 1, name: 'Bingo' });
  cats.insert({ owner: 1, name: 'Banjo' });
  cats.insert({ owner: 2, name: 'Fifi' });
  cats.insert({ owner: 3, name: 'Fuzzy' });
  cats.insert({ owner: 4, name: 'Gizmo' });
   db.saveDatabase(function(err) {
    if (err) {
     ...