petersirka / nosql

NoSQL embedded database for small node.js projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Access to custom() returns null consistently

octplane opened this issue · comments

I'm note sure I'm using the library correctly, but this code:

var fs = require("fs"),
  path = require("path");

var db_path = "/tmp";
var db_file = path.join(db_path, "db.nosql");

var db = require('nosql').load(db_file);

console.log("Loaded %s db", db_file);
console.log(db.custom());

if(!db.custom() || !db.custom().configured) {
  console.log("Setting flag to True");
  db.custom({configured:true});
}

Writes configured:true every time it's run, although the database on the disk is:

{"version":"v2.0.2","views":{},"stored":{},"description":"","created":"2013-12-01T22:38:08.194Z","custom":{"configured":true}}

I believe it should not be the case and that it should do that only once.

Also, the console.log(db.custom()) always prints null as if the DB was not yet loaded...

Hi @octplane,

I updated NoSQL version (update version from NPM) and I added new event database.on('load', function() {});.

var fs = require("fs"),
path = require("path");

var db_path = "/tmp";
var db_file = path.join(db_path, "db.nosql");

var db = require('nosql').load(db_file);

console.log("Loaded %s db", db_file);
console.log(db.custom());

// OLD VERSION:
setTimeout(function() {
    if(!db.custom() || !db.custom().configured) {
        console.log("Setting flag to True");
        db.custom({configured:true});
    }
}, 200);

// NEW VERSION:
db.on('load', function() {
    var db = this;
    if(!db.custom() || !db.custom().configured) {
        console.log("Setting flag to True");
        db.custom({configured:true});
    }   
});

Thanks!

Nice, it works as expected, thank you :-). This event was not in the doc, so I didn't dig any further. I should have ! :)

👍