Skip to content

Commit

Permalink
use @seald-io/nedb for node23 support
Browse files Browse the repository at this point in the history
Signed-off-by: si458 <[email protected]>
  • Loading branch information
si458 committed Nov 26, 2024
1 parent dbb5b4b commit d6a2917
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
26 changes: 16 additions & 10 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ module.exports.CreateDB = function (parent, func) {
obj.GetAllType('mesh', function (err, docs) {
if (err == null) { for (var i in docs) { count++; obj.Set(docs[i]); } }
if (obj.databaseType == DB_NEDB) { // If we are using NeDB, compact the database.
obj.file.persistence.compactDatafile();
obj.file.compactDatafile();
obj.file.on('compaction.done', function () { func(count); }); // It's important to wait for compaction to finish before exit, otherwise NeDB may corrupt.
} else {
func(count); // For all other databases, normal exit.
Expand Down Expand Up @@ -1239,8 +1239,11 @@ module.exports.CreateDB = function (parent, func) {
} else {
// Use NeDB (The default)
obj.databaseType = DB_NEDB;
try { Datastore = require('@yetzt/nedb'); } catch (ex) { } // This is the NeDB with fixed security dependencies.
if (Datastore == null) { Datastore = require('nedb'); } // So not to break any existing installations, if the old NeDB is present, use it.
try { Datastore = require('@seald-io/nedb'); } catch (ex) { } // This is the NeDB with Node 23 support.
if (Datastore == null) {
try { Datastore = require('@yetzt/nedb'); } catch (ex) { } // This is the NeDB with fixed security dependencies.
if (Datastore == null) { Datastore = require('nedb'); } // So not to break any existing installations, if the old NeDB is present, use it.
}
var datastoreOptions = { filename: parent.getConfigFilePath('meshcentral.db'), autoload: true };

// If a DB encryption key is provided, perform database encryption
Expand All @@ -1267,7 +1270,7 @@ module.exports.CreateDB = function (parent, func) {

// Start NeDB main collection and setup indexes
obj.file = new Datastore(datastoreOptions);
obj.file.persistence.setAutocompactionInterval(86400000); // Compact once a day
obj.file.setAutocompactionInterval(86400000); // Compact once a day
obj.file.ensureIndex({ fieldName: 'type' });
obj.file.ensureIndex({ fieldName: 'domain' });
obj.file.ensureIndex({ fieldName: 'meshid', sparse: true });
Expand All @@ -1276,15 +1279,15 @@ module.exports.CreateDB = function (parent, func) {

// Setup the events collection and setup indexes
obj.eventsfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-events.db'), autoload: true, corruptAlertThreshold: 1 });
obj.eventsfile.persistence.setAutocompactionInterval(86400000); // Compact once a day
obj.eventsfile.setAutocompactionInterval(86400000); // Compact once a day
obj.eventsfile.ensureIndex({ fieldName: 'ids' }); // TODO: Not sure if this is a good index, this is a array field.
obj.eventsfile.ensureIndex({ fieldName: 'nodeid', sparse: true });
obj.eventsfile.ensureIndex({ fieldName: 'time', expireAfterSeconds: expireEventsSeconds });
obj.eventsfile.remove({ time: { '$lt': new Date(Date.now() - (expireEventsSeconds * 1000)) } }, { multi: true }); // Force delete older events

// Setup the power collection and setup indexes
obj.powerfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-power.db'), autoload: true, corruptAlertThreshold: 1 });
obj.powerfile.persistence.setAutocompactionInterval(86400000); // Compact once a day
obj.powerfile.setAutocompactionInterval(86400000); // Compact once a day
obj.powerfile.ensureIndex({ fieldName: 'nodeid' });
obj.powerfile.ensureIndex({ fieldName: 'time', expireAfterSeconds: expirePowerEventsSeconds });
obj.powerfile.remove({ time: { '$lt': new Date(Date.now() - (expirePowerEventsSeconds * 1000)) } }, { multi: true }); // Force delete older events
Expand All @@ -1295,15 +1298,15 @@ module.exports.CreateDB = function (parent, func) {

// Setup the server stats collection and setup indexes
obj.serverstatsfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-stats.db'), autoload: true, corruptAlertThreshold: 1 });
obj.serverstatsfile.persistence.setAutocompactionInterval(86400000); // Compact once a day
obj.serverstatsfile.setAutocompactionInterval(86400000); // Compact once a day
obj.serverstatsfile.ensureIndex({ fieldName: 'time', expireAfterSeconds: expireServerStatsSeconds });
obj.serverstatsfile.ensureIndex({ fieldName: 'expire', expireAfterSeconds: 0 }); // Auto-expire events
obj.serverstatsfile.remove({ time: { '$lt': new Date(Date.now() - (expireServerStatsSeconds * 1000)) } }, { multi: true }); // Force delete older events

// Setup plugin info collection
if (obj.pluginsActive) {
obj.pluginsfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-plugins.db'), autoload: true });
obj.pluginsfile.persistence.setAutocompactionInterval(86400000); // Compact once a day
obj.pluginsfile.setAutocompactionInterval(86400000); // Compact once a day
}

setupFunctions(func); // Completed setup of NeDB
Expand Down Expand Up @@ -3970,8 +3973,11 @@ module.exports.CreateDB = function (parent, func) {
// Transfer NeDB data into the current database
obj.nedbtodb = function (func) {
var nedbDatastore = null;
try { nedbDatastore = require('@yetzt/nedb'); } catch (ex) { } // This is the NeDB with fixed security dependencies.
if (nedbDatastore == null) { nedbDatastore = require('nedb'); } // So not to break any existing installations, if the old NeDB is present, use it.
try { nedbDatastore = require('@seald-io/nedb'); } catch (ex) { } // This is the NeDB with Node 23 support.
if (nedbDatastore == null) {
try { nedbDatastore = require('@yetzt/nedb'); } catch (ex) { } // This is the NeDB with fixed security dependencies.
if (nedbDatastore == null) { nedbDatastore = require('nedb'); } // So not to break any existing installations, if the old NeDB is present, use it.
}

var datastoreOptions = { filename: parent.getConfigFilePath('meshcentral.db'), autoload: true };

Expand Down
2 changes: 1 addition & 1 deletion dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"@yetzt/nedb": "1.8.0",
"@seald-io/nedb": "4.0.4",
"archiver": "7.0.1",
"body-parser": "1.20.3",
"cbor": "5.2.0",
Expand Down
2 changes: 1 addition & 1 deletion meshcentral.js
Original file line number Diff line number Diff line change
Expand Up @@ -4212,7 +4212,7 @@ function mainStart() {

// Build the list of required modules
// NOTE: ALL MODULES MUST HAVE A VERSION NUMBER AND THE VERSION MUST MATCH THAT USED IN Dockerfile
var modules = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '@yetzt/nedb', '[email protected]', '[email protected]', '[email protected]', '[email protected]'];
var modules = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '@seald-io/nedb', '[email protected]', '[email protected]', '[email protected]', '[email protected]'];
if (require('os').platform() == 'win32') { modules.push('[email protected]'); modules.push('[email protected]'); if (sspi == true) { modules.push('[email protected]'); } } // Add Windows modules
if (ldap == true) { modules.push('[email protected]'); }
if (ssh == true) { modules.push('[email protected]'); }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"sample-config-advanced.json"
],
"dependencies": {
"@yetzt/nedb": "1.8.0",
"@seald-io/nedb": "4.0.4",
"archiver": "7.0.1",
"body-parser": "1.20.3",
"cbor": "5.2.0",
Expand Down

0 comments on commit d6a2917

Please sign in to comment.