diff --git a/db.js b/db.js index 5276c10..12c7a72 100644 --- a/db.js +++ b/db.js @@ -13,8 +13,11 @@ module.exports.CreateDB = function(meshserver) { var obj = {}; obj.dbVersion = 2; const expireLogEntrySeconds = (60 * 60 * 24 * 30); // 30 days - if (meshserver.args.mongodb) { // use MongDB - require('mongodb').MongoClient.connect(meshserver.args.mongodb, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) { + // Detect MongoDB mode robustly. The plugin is loaded by pluginHandler in a context + // where meshserver.args.mongodb may not be populated, so fall back to the parent. + var mongoUrl = meshserver.args.mongodb || (meshserver.parent && meshserver.parent.args && meshserver.parent.args.mongodb); + if (mongoUrl) { // use MongoDB + require('mongodb').MongoClient.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) { if (err != null) { console.log("Unable to connect to database: " + err); process.exit(); return; } Datastore = client; @@ -205,14 +208,14 @@ module.exports.CreateDB = function(meshserver) { } else { // use NeDb Datastore = require('nedb'); if (obj.eventsFile == null) { - obj.eventsFile = new Datastore({ filename: meshserver.getConfigFilePath('plugin-eventlog-events.db'), autoload: true }); + obj.eventsFile = new Datastore({ filename: (typeof meshserver.getConfigFilePath === 'function') ? meshserver.getConfigFilePath('plugin-eventlog-events.db') : require('path').join(meshserver.args.dataPath || '.', 'plugin-eventlog-events.db'), autoload: true }); obj.eventsFile.persistence.setAutocompactionInterval(40000); obj.eventsFile.ensureIndex({ fieldName: 'nodeid' }); obj.eventsFile.ensureIndex({ fieldName: 'TimeCreated' }); obj.eventsFile.ensureIndex({ fieldName: 'time', expireAfterSeconds: expireLogEntrySeconds }); } if (obj.settingsFile == null) { - obj.settingsFile = new Datastore({ filename: meshserver.getConfigFilePath('plugin-eventlog-settings.db'), autoload: true }); + obj.settingsFile = new Datastore({ filename: (typeof meshserver.getConfigFilePath === 'function') ? meshserver.getConfigFilePath('plugin-eventlog-settings.db') : require('path').join(meshserver.args.dataPath || '.', 'plugin-eventlog-settings.db'), autoload: true }); obj.settingsFile.persistence.setAutocompactionInterval(40000); }