nodejs: nedb filename path causing problems; how to fix?

I'm an experienced programmer, but relatively new to nodejs, nedb, Mocha.

nedb is a nodejs npm module for small data usages, that replicates some of the interface used in Mongodb. I chose it because I'm building an app to run on a Beaglebone Black or Raspberry Pi and didn't want the overhead for Mongodb for my small data needs.

However, I'm having a problem with the path portion of the filename for nedb. It works fine if the myapp.js I'm working on is invoked local to the current path, but if I invoke it from a different directory, like my Mocha test directory, then nedb won't work with the same path. It won't work with relative path either; I have to specify the full path, starting with /home before I can get it to work from the Mocha test directory. Needless to say, this is both inconvenient, and prohibitave for production, as it's not running with a base of /home in production.

In the code that follows, myapp.js is the application I'm developing and testing. useMyapp.js is a utility test to use myapp.js for simplicity. These are all in the same directory, and work properly when I invoke useMyapp.js.

myapp.js is located in /home/geek/project/public/javascripts/

Here is the code for myapp.js:

var Datastore = require("nedb");
var db = new Datastore({filename: "./calEvents.db", autoload: true});

function Myapp(year, month) {
    console.log('myapp: year: ' + year + ', month: ' + month);
    this.year = year;
    this.month = month;
}

Myapp.prototype.getAllData = function() {
    db.find({}, function(err, docs) {
        if(err) {
            console.error('error: ', err);
        } else {
            console.log(" docs found: ", docs);
        }
    });
};

module.exports = Myapp;

useMyapp.js is located in the same directory at /home/geek/project/public/javascripts/

Here is the code for useMyapp.js, which works correctly, outputing the expected nedb records.

var Myapp = require("./myapp");

var myapp = new Myapp(2016, "Mar");

myapp.getAllData();

I copied useMyapp.js to my Mocha test directory, renamed it to be TestUseMyapp.js, into /home/geek/project/test/ I adjusted the require statment for relative path to myapp.js Here's the contents of TestUseMyapp.js in /home/geek/project/test/

var Myapp = require("../public/javascripts/myapp");

var myapp = new Myapp(2016, "Mar");

myapp.getAllData();

The output from running this via nodejs is an empty data set: docs found: []

THE PROBLEM: TestUseMyapp.js is able to find and load myapp.js per the 'require' statement, but the filename parameter for nedb is not working when myapp.js is invoked from a different directory. This is causing me problems between development, Mocha testing, and running in production, as the file path are all different.

I created a duplicate of myapp.js, named it myapp2.js, and kept it in the same directory as myapp.js at /home/geek/project/public/javascripts. In this, I changed the path for the nedb filename parameter. Here's the code in myapp2.js PLEASE NOTE THE ONLY CHANGE IS TO THE filename PARAMATER PATH:

var Datastore = require("nedb");
var db = new Datastore({filename: "/home/geek/project/public/javascripts/calEvents.db", autoload: true});

function Myapp(year, month) {
    console.log('myapp: year: ' + year + ', month: ' + month);
    this.year = year;
    this.month = month;
}

Myapp.prototype.getAllData = function() {
    db.find({}, function(err, docs) {
        if(err) {
            console.error('error: ', err);
        } else {
            console.log(" docs found: ", docs);
        }
    });
};

module.exports = Myapp;

This works as expected when I invoked the test against myapp2.js with the full path.

THE QUESTION: How do I resolve the filename path issue with nedb so that I don't have to change it between development, Mocha testing, and deployment to production?

Thanks!

Answers


I was able to solve this problem by inserting __dirname as part of the filename parameter.


Need Your Help

SQL Server 2005: Basic Insert / Record Logic Help Needed

asp.net sql-server-2005

I am designing a social networking site that has a "wall" feature like the others out there today. The database has an alerts table that stores some user action worthy of sharing with his friends. ...

NSXMLParser with HTML Containing Javascript and "bad" characters

xcode cocoa osx-lion

I am using NSXMLParser to parse HTML from web sites. Testing site is under my control but in operation sites will not be.