8

[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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kuan
  • 11,149
  • 23
  • 93
  • 201
  • Any updates on this? – Slartibartfast Oct 07 '16 at 03:01
  • @User528491 It has been long time( I can barely remember why I asked this question ), did you try the update part of my question. I did not get answer from the author – Kuan Oct 07 '16 at 15:51
  • did you find any solution to your question ? – Arash Nov 10 '16 at 07:44
  • @Arash Sorry, not yet. It has been long time since I asked this question, and our infrastructure has been changed to server side, no clientside DB required any more. But I think you can ask directly with Joe(author), he is a pretty helpful guy https://github.com/techfort – Kuan Nov 10 '16 at 17:46
  • Your update is in the right direction. Inside the callback `db` variable is not defined, but in general the "on ready" logic is correct. – Kamafeather Oct 13 '19 at 20:35
  • Also, might be useful for who's passing from here, it is possible to just use the `addCollection` method without checking for existence; the method will return the collection when already existing (see [related issue on the repository](https://github.com/techfort/LokiJS/issues/699)) – Kamafeather Oct 13 '19 at 22:11

2 Answers2

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

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

0

As far as I can tell its not possible to persist to a clientside DB as its difficult and generally insecure for the browser to have access to the local file system. Lokijs supports persistence within the browser's local storage. Loki uses an adapter to implement persistence to the browsers local storage, this adapter defaults to 'localStorage adapter.

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

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

MJ_Wales
  • 873
  • 2
  • 8
  • 20
  • Browser are providing the [IndexedDB API](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API), since HTML5. And it's supported by LokiJS, with a [custom adapter](https://github.com/techfort/LokiJS/wiki/LokiJS-persistence-and-adapters#example-constructing-loki-for-autoloadautosave-with-default-localstorage-adapter-). The storage limit on Chrome is something around 30-60megs (per DB; or per Collection, if partitioned). – Kamafeather Oct 13 '19 at 20:21