First pass at adding SQLite3 database support (#4398)
Ylian Saint-Hilaire committed
Aug 15, 2022 at 00:33 UTC
1680138e8d38ec2c62a3c8573c46d3369c08b9c5
3 files changed
+295
-4
MeshCentralServer.njsproj
+2
@@ -710,6 +710,7 @@
710
<Folder Include="typings\globals\pg-pool\" />
711
<Folder Include="typings\globals\split2\" />
712
<Folder Include="typings\globals\sprintf-js\" />
713
+ <Folder Include="typings\globals\sqlite3\" />
714
<Folder Include="typings\globals\type-check\" />
715
<Folder Include="typings\globals\underscore\" />
716
<Folder Include="typings\globals\uuid\" />
@@ -751,6 +752,7 @@
752
<TypeScriptCompile Include="typings\globals\pg-pool\index.d.ts" />
753
<TypeScriptCompile Include="typings\globals\split2\index.d.ts" />
754
<TypeScriptCompile Include="typings\globals\sprintf-js\index.d.ts" />
755
+ <TypeScriptCompile Include="typings\globals\sqlite3\index.d.ts" />
756
<TypeScriptCompile Include="typings\globals\type-check\index.d.ts" />
757
<TypeScriptCompile Include="typings\globals\underscore\index.d.ts" />
758
<TypeScriptCompile Include="typings\globals\uuid\index.d.ts" />
db.js
+291
-4
@@ -125,6 +125,8 @@ module.exports.CreateDB = function (parent, func) {
125
});
126
});
127
});
128
+ } else if (obj.databaseType == 8) { // SQLite3
129
+ // TODO
130
}
131
obj.removeInactiveDevices();
132
}
@@ -387,7 +389,10 @@ module.exports.CreateDB = function (parent, func) {
389
if (meshChange) { obj.Set(docs[i]); }
390
}
391
}
390
- if (obj.databaseType == 7) {
392
+ if (obj.databaseType == 8) {
393
+ // SQLite
394
+
395
+ } else if (obj.databaseType == 7) {
396
// AceBase
397
398
} else if (obj.databaseType == 6) {
@@ -663,7 +668,48 @@ module.exports.CreateDB = function (parent, func) {
668
});
669
}
670
666
- if (parent.args.acebase) {
671
+ if (parent.args.sqlite3) {
672
+ // SQLite3 database setup
673
+ obj.databaseType = 8;
674
+ const sqlite3 = require('sqlite3');
675
+ obj.file = new sqlite3.Database(parent.path.join(parent.datapath, 'meshcentral.sqlite'), sqlite3.OPEN_READWRITE, function (err) {
676
+ if (err && (err.code == 'SQLITE_CANTOPEN')) {
677
+ // Database needs to be created
678
+ obj.file = new sqlite3.Database(parent.path.join(parent.datapath, 'meshcentral.sqlite'), function(err) {
679
+ if (err) { console.log("SQLite Error: " + err); exit(1); return; }
680
+ obj.file.exec(`
681
+ CREATE TABLE main (id VARCHAR(256) PRIMARY KEY NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON);
682
+ CREATE TABLE events(id SERIAL PRIMARY KEY, time TIMESTAMP, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON);
683
+ CREATE TABLE eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT);
684
+ CREATE TABLE serverstats (time TIMESTAMP PRIMARY KEY, expire TIMESTAMP, doc JSON);
685
+ CREATE TABLE power (id SERIAL PRIMARY KEY, time TIMESTAMP, nodeid CHAR(255), doc JSON);
686
+ CREATE TABLE smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON);
687
+ CREATE TABLE plugin (id SERIAL PRIMARY KEY, doc JSON);
688
+ CREATE INDEX ndxtypedomainextra ON main (type, domain, extra);
689
+ CREATE INDEX ndxextra ON main (extra);
690
+ CREATE INDEX ndxextraex ON main (extraex);
691
+ CREATE INDEX ndxeventstime ON events(time);
692
+ CREATE INDEX ndxeventsusername ON events(domain, userid, time);
693
+ CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time);
694
+ CREATE INDEX ndxeventids ON eventids(target);
695
+ CREATE INDEX ndxserverstattime ON serverstats (time);
696
+ CREATE INDEX ndxserverstatexpire ON serverstats (expire);
697
+ CREATE INDEX ndxpowernodeidtime ON power (nodeid, time);
698
+ CREATE INDEX ndxsmbiostime ON smbios (time);
699
+ CREATE INDEX ndxsmbiosexpire ON smbios (expire);
700
+ `, function (err) {
701
+ // Completed setup of SQLite3
702
+ setupFunctions(func);
703
+ }
704
+ );
705
+ });
706
+ return;
707
+ } else if (err) { console.log("SQLite Error: " + err); exit(1); return; }
708
+
709
+ // Completed setup of SQLite3
710
+ setupFunctions(func);
711
+ });
712
+ } else if (parent.args.acebase) {
713
// AceBase database setup
714
obj.databaseType = 7;
715
const { AceBase } = require('acebase');
@@ -1145,7 +1191,12 @@ module.exports.CreateDB = function (parent, func) {
1191
1192
// Query the database
1193
function sqlDbQuery(query, args, func) {
1148
- if (obj.databaseType == 4) { // MariaDB
1194
+ if (obj.databaseType == 8) { // SQLite
1195
+ obj.file.all(query, args, function (err, docs) {
1196
+ if (docs != null) { for (var i in docs) { if (typeof docs[i].doc == 'string') { docs[i] = JSON.parse(docs[i].doc); } } }
1197
+ if (func) { func(err, docs); }
1198
+ });
1199
+ } else if (obj.databaseType == 4) { // MariaDB
1200
Datastore.getConnection()
1201
.then(function (conn) {
1202
conn.query(query, args)
@@ -1225,7 +1276,243 @@ module.exports.CreateDB = function (parent, func) {
1276
}
1277
1278
function setupFunctions(func) {
1228
- if (obj.databaseType == 7) {
1279
+ if (obj.databaseType == 8) {
1280
+ // Database actions on the main collection. SQLite3: https://www.linode.com/docs/guides/getting-started-with-nodejs-sqlite/
1281
+ obj.Set = function (value, func) {
1282
+ obj.dbCounters.fileSet++;
1283
+ var extra = null, extraex = null;
1284
+ value = common.escapeLinksFieldNameEx(value);
1285
+ if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
1286
+ if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
1287
+ if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
1288
+ sqlDbQuery('INSERT INTO main VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (id) DO UPDATE SET type = $2, domain = $3, extra = $4, extraex = $5, doc = $6;', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, JSON.stringify(performTypedRecordEncrypt(value))], func);
1289
+ }
1290
+ obj.SetRaw = function (value, func) {
1291
+ obj.dbCounters.fileSet++;
1292
+ var extra = null, extraex = null;
1293
+ if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
1294
+ if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
1295
+ if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
1296
+ sqlDbQuery('INSERT INTO main VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (id) DO UPDATE SET type = $2, domain = $3, extra = $4, extraex = $5, doc = $6;', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, JSON.stringify(performTypedRecordEncrypt(value))], func);
1297
+ }
1298
+ obj.Get = function (_id, func) {
1299
+ sqlDbQuery('SELECT doc FROM main WHERE id = $1', [_id], function (err, docs) {
1300
+ if ((docs != null) && (docs.length > 0)) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1301
+ func(err, performTypedRecordDecrypt(docs));
1302
+ });
1303
+ }
1304
+ obj.GetAll = function (func) {
1305
+ sqlDbQuery('SELECT domain, doc FROM main', null, function (err, docs) {
1306
+ if ((docs != null) && (docs.length > 0)) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1307
+ func(err, performTypedRecordDecrypt(docs));
1308
+ });
1309
+ }
1310
+ obj.GetHash = function (id, func) {
1311
+ sqlDbQuery('SELECT doc FROM main WHERE id = $1', [id], function (err, docs) {
1312
+ if ((docs != null) && (docs.length > 0)) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1313
+ func(err, performTypedRecordDecrypt(docs));
1314
+ });
1315
+ }
1316
+ obj.GetAllTypeNoTypeField = function (type, domain, func) {
1317
+ sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2', [type, domain], function (err, docs) {
1318
+ if ((docs != null) && (docs.length > 0)) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1319
+ func(err, performTypedRecordDecrypt(docs));
1320
+ });
1321
+ };
1322
+ obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
1323
+ if (id && (id != '')) {
1324
+ sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra = ANY ($4))', [id, type, domain, meshes], function (err, docs) {
1325
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1326
+ func(err, performTypedRecordDecrypt(docs));
1327
+ });
1328
+ } else {
1329
+ if (extrasids == null) {
1330
+ sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra = ANY ($3))', [type, domain, meshes], function (err, docs) {
1331
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1332
+ func(err, performTypedRecordDecrypt(docs));
1333
+ }, true);
1334
+ } else {
1335
+ sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND ((extra = ANY ($3)) OR (id = ANY ($4)))', [type, domain, meshes, extrasids], function (err, docs) {
1336
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1337
+ func(err, performTypedRecordDecrypt(docs));
1338
+ });
1339
+ }
1340
+ }
1341
+ };
1342
+ obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
1343
+ if (id && (id != '')) {
1344
+ sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra = ANY ($4))', [id, type, domain, nodes], function (err, docs) {
1345
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1346
+ func(err, performTypedRecordDecrypt(docs));
1347
+ });
1348
+ } else {
1349
+ sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra = ANY ($3))', [type, domain, nodes], function (err, docs) {
1350
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1351
+ func(err, performTypedRecordDecrypt(docs));
1352
+ });
1353
+ }
1354
+ };
1355
+ obj.GetAllType = function (type, func) {
1356
+ sqlDbQuery('SELECT doc FROM main WHERE type = $1', [type], function (err, docs) {
1357
+ if (docs != null) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1358
+ func(err, performTypedRecordDecrypt(docs));
1359
+ });
1360
+ }
1361
+ obj.GetAllIdsOfType = function (ids, domain, type, func) {
1362
+ sqlDbQuery('SELECT doc FROM main WHERE (id = ANY ($1)) AND domain = $2 AND type = $3', [ids, domain, type], function (err, docs) {
1363
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1364
+ func(err, performTypedRecordDecrypt(docs));
1365
+ });
1366
+ }
1367
+ obj.GetUserWithEmail = function (domain, email, func) {
1368
+ sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) {
1369
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1370
+ func(err, performTypedRecordDecrypt(docs));
1371
+ });
1372
+ }
1373
+ obj.GetUserWithVerifiedEmail = function (domain, email, func) {
1374
+ sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) {
1375
+ if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1376
+ func(err, performTypedRecordDecrypt(docs));
1377
+ });
1378
+ }
1379
+ obj.Remove = function (id, func) { sqlDbQuery('DELETE FROM main WHERE id = $1', [id], func); };
1380
+ obj.RemoveAll = function (func) { sqlDbQuery('DELETE FROM main', null, func); };
1381
+ obj.RemoveAllOfType = function (type, func) { sqlDbQuery('DELETE FROM main WHERE type = $1', [type], func); };
1382
+ obj.InsertMany = function (data, func) { var pendingOps = 0; for (var i in data) { pendingOps++; obj.SetRaw(data[i], function () { if (--pendingOps == 0) { func(); } }); } }; // Insert records directly, no link escaping
1383
+ obj.RemoveMeshDocuments = function (id, func) { sqlDbQuery('DELETE FROM main WHERE extra = $1', [id], function () { sqlDbQuery('DELETE FROM main WHERE id = $1', ['nt' + id], func); }); };
1384
+ obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
1385
+ obj.DeleteDomain = function (domain, func) { sqlDbQuery('DELETE FROM main WHERE domain = $1', [domain], func); };
1386
+ obj.SetUser = function (user) { if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
1387
+ obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
1388
+ obj.getLocalAmtNodes = function (func) {
1389
+ sqlDbQuery('SELECT doc FROM main WHERE (type = \'node\') AND (extraex IS NOT NULL)', null, function (err, docs) {
1390
+ if (docs != null) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1391
+ var r = []; if (err == null) { for (var i in docs) { if (docs[i].host != null) { r.push(docs[i]); } } } func(err, r);
1392
+ });
1393
+ };
1394
+ obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) {
1395
+ sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extraex = $2', [domainid, 'uuid/' + uuid], function (err, docs) {
1396
+ if (docs != null) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
1397
+ func(err, docs);
1398
+ });
1399
+ };
1400
+ obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { sqlDbExec('SELECT COUNT(id) FROM main WHERE domain = $1 AND type = $2', [domainid, type], function (err, response) { func((response['COUNT(id)'] == null) || (response['COUNT(id)'] > max), response['COUNT(id)']) }); } }
1401
+
1402
+ // Database actions on the events collection
1403
+ obj.GetAllEvents = function (func) {
1404
+ sqlDbQuery('SELECT doc FROM events', null, func);
1405
+ };
1406
+ obj.StoreEvent = function (event, func) {
1407
+ obj.dbCounters.eventsSet++;
1408
+ /* TODO!!!
1409
+ sqlDbQuery('INSERT INTO events VALUES (DEFAULT, $1, $2, $3, $4, $5, $6) RETURNING id', [event.time, ((typeof event.domain == 'string') ? event.domain : null), event.action, event.nodeid ? event.nodeid : null, event.userid ? event.userid : null, JSON.stringify(event)], function (err, docs) {
1410
+ if (docs.id) { for (var i in event.ids) { if (event.ids[i] != '*') { sqlDbQuery('INSERT INTO eventids VALUES ($1, $2)', [docs.id, event.ids[i]]); } } }
1411
+ });
1412
+ */
1413
+ };
1414
+ obj.GetEvents = function (ids, domain, func) {
1415
+ if (ids.indexOf('*') >= 0) {
1416
+ sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC', [domain], func);
1417
+ } else {
1418
+ sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))) GROUP BY id ORDER BY time DESC', [domain, ids], func);
1419
+ }
1420
+ };
1421
+ obj.GetEventsWithLimit = function (ids, domain, limit, func) {
1422
+ if (ids.indexOf('*') >= 0) {
1423
+ sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC LIMIT $2', [domain, limit], func);
1424
+ } else {
1425
+ sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, ids, limit], func);
1426
+ }
1427
+ };
1428
+ obj.GetUserEvents = function (ids, domain, userid, func) {
1429
+ if (ids.indexOf('*') >= 0) {
1430
+ sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC', [domain, userid], func);
1431
+ } else {
1432
+ sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))) GROUP BY id ORDER BY time DESC', [domain, userid, ids], func);
1433
+ }
1434
+ };
1435
+ obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
1436
+ if (ids.indexOf('*') >= 0) {
1437
+ sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT $3', [domain, userid, limit], func);
1438
+ } else {
1439
+ sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))) GROUP BY id ORDER BY time DESC LIMIT $4', [domain, userid, ids, limit], func);
1440
+ }
1441
+ };
1442
+ obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
1443
+ if (ids.indexOf('*') >= 0) {
1444
+ sqlDbQuery('SELECT doc FROM events WHERE ((domain = $1) AND (time BETWEEN $2 AND $3)) ORDER BY time', [domain, start, end], func);
1445
+ } else {
1446
+ sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE ((domain = $1) AND (target = ANY ($2)) AND (time BETWEEN $3 AND $4)) GROUP BY id ORDER BY time', [domain, ids, start, end], func);
1447
+ }
1448
+ };
1449
+ //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO
1450
+ obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) ORDER BY time DESC LIMIT $3', [nodeid, domain, limit], func); };
1451
+ obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) ORDER BY time DESC LIMIT $4', [nodeid, domain, userid, limit], func); };
1452
+ obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
1453
+ obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND nodeid = $2', [domain, nodeid], function (err, docs) { }); };
1454
+ obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND userid = $2', [domain, userid], function (err, docs) { }); };
1455
+ obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) { sqlDbQuery('SELECT COUNT(*) FROM events WHERE action = \'authfail\' AND domain = $1 AND userid = $2 AND time > $3', [domainid, userid, lastlogin], function (err, response, raw) { func(err == null ? parseInt(raw.rows[0].count) : 0); }); }
1456
+
1457
+ // Database actions on the power collection
1458
+ obj.getAllPower = function (func) { sqlDbQuery('SELECT doc FROM power', null, func); };
1459
+ obj.storePowerEvent = function (event, multiServer, func) { obj.dbCounters.powerSet++; if (multiServer != null) { event.server = multiServer.serverid; } sqlDbQuery('INSERT INTO power VALUES (DEFAULT, $1, $2, $3)', [event.time, event.nodeid ? event.nodeid : null, event], func); };
1460
+ obj.getPowerTimeline = function (nodeid, func) { sqlDbQuery('SELECT doc FROM power WHERE ((nodeid = $1) OR (nodeid = \'*\')) ORDER BY time ASC', [nodeid], func); };
1461
+ obj.removeAllPowerEvents = function () { sqlDbQuery('DELETE FROM power', null, function (err, docs) { }); };
1462
+ obj.removeAllPowerEventsForNode = function (nodeid) { if (nodeid == null) return; sqlDbQuery('DELETE FROM power WHERE nodeid = $1', [nodeid], function (err, docs) { }); };
1463
+
1464
+ // Database actions on the SMBIOS collection
1465
+ obj.GetAllSMBIOS = function (func) { sqlDbQuery('SELECT doc FROM smbios', null, func); };
1466
+ obj.SetSMBIOS = function (smbios, func) { var expire = new Date(smbios.time); expire.setMonth(expire.getMonth() + 6); sqlDbQuery('INSERT INTO smbios VALUES ($1, $2, $3, $4) ON CONFLICT (id) DO UPDATE SET time = $2, expire = $3, doc = $4', [smbios._id, smbios.time, expire, JSON.stringify(smbios)], func); };
1467
+ obj.RemoveSMBIOS = function (id) { sqlDbQuery('DELETE FROM smbios WHERE id = $1', [id], function (err, docs) { }); };
1468
+ obj.GetSMBIOS = function (id, func) { sqlDbQuery('SELECT doc FROM smbios WHERE id = $1', [id], func); };
1469
+
1470
+ // Database actions on the Server Stats collection
1471
+ obj.SetServerStats = function (data, func) { sqlDbQuery('INSERT INTO serverstats VALUES ($1, $2, $3) ON CONFLICT (time) DO UPDATE SET expire = $2, doc = $3', [data.time, data.expire, JSON.stringify(data)], func); };
1472
+ obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); sqlDbQuery('SELECT doc FROM serverstats WHERE time > $1', [t], func); }; // TODO: Expire old entries
1473
+
1474
+ // Read a configuration file from the database
1475
+ obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
1476
+
1477
+ // Write a configuration file to the database
1478
+ obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
1479
+
1480
+ // List all configuration files
1481
+ obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
1482
+
1483
+ // Get all configuration files (TODO: This is not SQL)
1484
+ obj.getAllConfigFiles = function (password, func) {
1485
+ obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
1486
+ if (err != null) { func(null); return; }
1487
+ var r = null;
1488
+ for (var i = 0; i < docs.length; i++) {
1489
+ var name = docs[i]._id.split('/')[1];
1490
+ var data = obj.decryptData(password, docs[i].data);
1491
+ if (data != null) { if (r == null) { r = {}; } r[name] = data; }
1492
+ }
1493
+ func(r);
1494
+ });
1495
+ }
1496
+
1497
+ // Get database information (TODO: Complete this)
1498
+ obj.getDbStats = function (func) {
1499
+ obj.stats = { c: 4 };
1500
+ sqlDbQuery('SELECT COUNT(*) FROM main', null, function (err, response, raw) { obj.stats.meshcentral = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1501
+ sqlDbQuery('SELECT COUNT(*) FROM serverstats', null, function (err, response, raw) { obj.stats.serverstats = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1502
+ sqlDbQuery('SELECT COUNT(*) FROM power', null, function (err, response, raw) { obj.stats.power = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1503
+ sqlDbQuery('SELECT COUNT(*) FROM smbios', null, function (err, response, raw) { obj.stats.smbios = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1504
+ }
1505
+
1506
+ // Plugin operations
1507
+ if (obj.pluginsActive) {
1508
+ obj.addPlugin = function (plugin, func) { sqlDbQuery('INSERT INTO plugin VALUES (DEFAULT, $2)', [JSON.stringify(value)], func); }; // Add a plugin
1509
+ obj.getPlugins = function (func) { sqlDbQuery('SELECT doc FROM plugin', null, func); }; // Get all plugins
1510
+ obj.getPlugin = function (id, func) { sqlDbQuery('SELECT doc FROM plugin WHERE id = $1', [id], func); }; // Get plugin
1511
+ obj.deletePlugin = function (id, func) { sqlDbQuery('DELETE FROM plugin WHERE id = $1', [id], func); }; // Delete plugin
1512
+ obj.setPluginStatus = function (id, status, func) { obj.getPlugin(id, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].status = status; obj.updatePlugin(id, docs[0], func); } }); };
1513
+ obj.updatePlugin = function (id, args, func) { delete args._id; sqlDbQuery('INSERT INTO plugin VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET doc = $2', [id, JSON.stringify(args)], func); };
1514
+ }
1515
+ } else if (obj.databaseType == 7) {
1516
// Database actions on the main collection. AceBase: https://github.com/appy-one/acebase
1517
obj.Set = function (data, func) {
1518
data = common.escapeLinksFieldNameEx(data);
meshcentral.js
+2
@@ -1205,6 +1205,7 @@ function CreateMeshCentralServer(config, args) {
1205
config2['mongodbcol'] = config['mongodbcol'];
1206
config2['dbencryptkey'] = config['dbencryptkey'];
1207
config2['acebase'] = config['acebase'];
1208
+ config2['sqlite3'] = config['sqlite3'];
1209
1210
// We got a new config.json from the database, let's use it.
1211
config = obj.config = config2;
@@ -3896,6 +3897,7 @@ function mainStart() {
3897
if (config.settings.postgres != null) { modules.push('pg@8.7.1'); modules.push('pgtools@0.3.2'); } // Add Postgres, Postgres driver.
3898
if (config.settings.mariadb != null) { modules.push('mariadb'); } // Add MariaDB, official driver.
3899
if (config.settings.acebase != null) { modules.push('acebase'); } // Add AceBase, official driver.
3900
+ if (config.settings.sqlite3 != null) { modules.push('sqlite3'); } // Add sqlite3, official driver.
3901
if (config.settings.vault != null) { modules.push('node-vault'); } // Add official HashiCorp's Vault module.
3902
if (config.settings.plugins != null) { modules.push('semver'); } // Required for version compat testing and update checks
3903
if ((config.settings.plugins != null) && (config.settings.plugins.proxy != null)) { modules.push('https-proxy-agent'); } // Required for HTTP/HTTPS proxy support