From a2985251c78ab3a065c53ac8b008112a64c29f3c Mon Sep 17 00:00:00 2001 From: root Date: Wed, 19 Aug 2026 14:25:01 -0300 Subject: [PATCH] fix(db): detect MongoDB mode robustly + guard getConfigFilePath (MeshCentral 1.2.5) On MeshCentral 1.2.5 running in MongoDB mode, the plugin failed because: 1. The Mongo branch was gated behind meshserver.args.mongodb, which is not populated in the plugin load context (only the core's own db.js gets it). Now falls back to meshserver.parent.args.mongodb. 2. The NeDB fallback called meshserver.getConfigFilePath(...), a method removed in 1.2.5, causing a TypeError. Now guarded with a typeof check and falls back to path.join(dataPath, ...). Fixes ryanblenis/MeshCentral-EventLog#15 --- db.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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); }