How to reload the Loki database and collect it while saving

[UPDATE] I later found out some example:

        this.db = new Loki("viewsaving", {
            autosave: true,
            autosaveInterval: 5000,
            autoload: true,
            autoloadCallback: function(){
                db_ready = true;
                if(db.getCollection("namedviews") == null ){
                    this.namedviews = db.addCollection("namedviews");
                }
                if(db.getCollection("timedviews") == null ){
                    this.timedviews = db.addCollection("timedviews");
                }
            }
        });

It basically works on my side. so I just use it, not sure if this is correct or not, please report.


Everything:

I'm new to Lokijs, I wonder how can I reload the database and collection that were saved?

Let's say that I create a database and a collection and then save it (for example, click a button to invoke the save process):

var db = new Loki("mydb");
var users = db.addCollection('users');
// we bind this to a button click event
function saveUser(){
    users.insert({
      name: 'joe'
    });
    users.insert({
      name: 'john'
    });
    users.insert({
      name: 'jack'
    });
    db.saveDatabase();
}

Then, when I refresh this page, how can I load "mydb" and "users" from persistence and not create a new one (since it will go through again var db = new Loki("mydb");), is there an API to check for the presence of a database?

thanks

+5
1

, , , , . Lokijs . Loki , localStorage.

var db = new Loki("test.db", {
  autoload: true,
  autoloadCallback : databaseInitialize,
  autosave: true, 
  autosaveInterval: 4000,
 //adapter: 'default already set'
});

https://rawgit.com/techfort/LokiJS/master/jsdoc/tutorial-Persistence%20Adapters.html

0

Source: https://habr.com/ru/post/1625004/


All Articles