1

is it possible to load existing database(maybe synchronously) and export collection from it?

i.e.

lokijs_data.js

var loki = require('lokijs');
var testDb = new loki('test-db.json', { autosave: true, autoload: true });

if(!testDb.getCollection('coll1')) {
    testDb.addCollection('coll1')
}   

module.exports = {
    coll1: testDb.getCollection('coll1')
}

app.js

var coll1 = require('./lokijs_data').coll1;
console.log(coll1); //prints 'null'

Thank You!

shkipper
  • 1,403
  • 3
  • 21
  • 35
  • it should be perfectly feasible. Have you tried? I can't see a reason why it wouldn't work. – Joe Minichino Sep 19 '15 at 07:53
  • it seems like collection checks(get/add) should be placed in autoloadCallback, otherwise collection gets always recreated. But if the check is placed in callback, how to exports collections ? – shkipper Sep 19 '15 at 20:46
  • have you tried to put the module.exports statement in the autoloadCallback? – Joe Minichino Sep 21 '15 at 12:43
  • thx for reply. it is not possible to put module.exports in callback, edited question a bit. what I want to do is to load test-db.json synchronously, then add collections if they do not exist and export them. if this code if(!testDb.getCollection('coll1')) { testDb.addCollection('coll1') } is used outside autoloadCallback testDb.getCollection('coll1') will always return null and testDb.addCollection('coll1') will recreate collection, erasing all existing data. But if I place this code in autoloadCallback the code in module.exports coll1: testDb.getCollection('coll1') will return null – shkipper Sep 21 '15 at 14:27
  • yeah the problem is that i should really change autoloadCallback to be a promise, I'll have a think about this, head over to github and open an issue maybe, so i can fix up a permanent solution – Joe Minichino Sep 21 '15 at 14:34
  • or maybe add possibility to load databases synchronously :) – shkipper Sep 22 '15 at 06:56

0 Answers0