Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload Loki database and collection from persistence

Tags:

lokijs

[UPDATE] I laterly found out some example which is like:

        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 advise.


All:

I am pretty new to Lokijs, I wonder how can I reload the database and collection which has been persisted?

Say that I build a database and collection, then I persist it( like click a button to trigger persistence 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 rather than create new one( cos it will go thru var db = new Loki("mydb"); again ), is there API to check if a database exists?

like image 397
Kuan Avatar asked Jan 19 '16 17:01

Kuan


1 Answers

const db = new loki('example.json', {
 env: 'BROWSER',
 autosave: true,
 autosaveInterval: 500,
 autoload: true })

You need to assign the 'env' property to 'BROWSER'

like image 179
Tirthankar Nayak Avatar answered Oct 18 '22 04:10

Tirthankar Nayak