mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-27 16:02:16 +03:00
First version with working PostgreSQL support.
This commit is contained in:
parent
11ff538ec5
commit
244399c329
48
db.js
48
db.js
@ -343,10 +343,10 @@ module.exports.CreateDB = function (parent, func) {
|
||||
}
|
||||
if (obj.databaseType == 6) {
|
||||
// Postgres
|
||||
sqlDbQuery('DELETE FROM Main WHERE ((extra LIKE (\'mesh/%\')) AND (extra NOT IN ($1)))', [meshlist], func);
|
||||
sqlDbQuery('DELETE FROM Main WHERE ((extra != NULL) AND (extra LIKE (\'mesh/%\')) AND (extra != ANY ($1)))', [meshlist], function (err, response) { });
|
||||
} else if ((obj.databaseType == 4) || (obj.databaseType == 5)) {
|
||||
// MariaDB
|
||||
sqlDbQuery('DELETE FROM Main WHERE (extra LIKE ("mesh/%") AND (extra NOT IN ?)', [meshlist], func);
|
||||
sqlDbQuery('DELETE FROM Main WHERE (extra LIKE ("mesh/%") AND (extra NOT IN ?)', [meshlist], function (err, response) { });
|
||||
} else if (obj.databaseType == 3) {
|
||||
// MongoDB
|
||||
obj.file.deleteMany({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
|
||||
@ -1073,7 +1073,10 @@ module.exports.CreateDB = function (parent, func) {
|
||||
if (func) try { func(error); } catch (ex) { console.log('SQLERR4', ex); }
|
||||
} else {
|
||||
var docs = [];
|
||||
if (results.command == 'SELECT') { for (var i in results.rows) {if (results.rows[i].doc) { if (typeof results.rows[i].doc == 'string') { docs.push(JSON.parse(results.rows[i].doc)); } else { docs.push(results.rows[i].doc); } } } }
|
||||
if ((results.command == 'INSERT') && (results.rows != null) && (results.rows.length == 1)) { docs = results.rows[0]; }
|
||||
else if (results.command == 'SELECT') {
|
||||
for (var i in results.rows) { if (results.rows[i].doc) { if (typeof results.rows[i].doc == 'string') { docs.push(JSON.parse(results.rows[i].doc)); } else { docs.push(results.rows[i].doc); } } }
|
||||
}
|
||||
if (func) { try { func(null, docs); } catch (ex) { console.log('SQLERR5', ex); } }
|
||||
}
|
||||
});
|
||||
@ -1111,7 +1114,7 @@ module.exports.CreateDB = function (parent, func) {
|
||||
.catch(function (err) { conn.release(); if (func) { try { func(err); } catch (ex) { console.log(ex); } } });
|
||||
})
|
||||
.catch(function (err) { if (func) { try { func(err); } catch (ex) { console.log(ex); } } });
|
||||
} else if ((obj.databaseType == 5) || (obj.databaseType == 6)) { // MySQL or Postgres SQL
|
||||
} else if ((obj.databaseType == 5) || (obj.databaseType == 6)) { // MySQL
|
||||
var Promises = [];
|
||||
for (var i in queries) { if (typeof queries[i] == 'string') { Promises.push(Datastore.query(queries[i])); } else { Promises.push(Datastore.query(queries[i][0], queries[i][1])); } }
|
||||
Promise.all(Promises)
|
||||
@ -1146,24 +1149,27 @@ module.exports.CreateDB = function (parent, func) {
|
||||
obj.GetAllTypeNoTypeField = function (type, domain, func) { sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2', [type, domain], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); }); };
|
||||
obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
|
||||
if (id && (id != '')) {
|
||||
sqlDbQuery('SELECT doc FROM main WHERE id = $1 AND type = $2 AND domain = $3 AND extra IN ($4)', [id, type, domain, meshes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
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) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
} else {
|
||||
if (extrasids == null) {
|
||||
sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2 AND extra IN ($3)', [type, domain, meshes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra = ANY ($3))', [type, domain, meshes], function (err, docs) {
|
||||
if (err == null) { for (var i in docs) { delete docs[i].type } }
|
||||
func(err, performTypedRecordDecrypt(docs));
|
||||
}, true);
|
||||
} else {
|
||||
sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2 AND (extra IN ($3) OR id IN ($4))', [type, domain, meshes, extrasids], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
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) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
}
|
||||
}
|
||||
};
|
||||
obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
|
||||
if (id && (id != '')) {
|
||||
sqlDbQuery('SELECT doc FROM main WHERE id = $1 AND type = $2 AND domain = $3 AND extra IN ($4)', [id, type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
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) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
} else {
|
||||
sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2 AND extra IN ($3)', [type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2 AND (extra = ANY ($3))', [type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
|
||||
}
|
||||
};
|
||||
obj.GetAllType = function (type, func) { sqlDbQuery('SELECT doc FROM main WHERE type = $1', [type], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
|
||||
obj.GetAllIdsOfType = function (ids, domain, type, func) { sqlDbQuery('SELECT doc FROM main WHERE id IN ($1) AND domain = $2 AND type = $3', [ids, domain, type], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
|
||||
obj.GetAllIdsOfType = function (ids, domain, type, func) { sqlDbQuery('SELECT doc FROM main WHERE (id = ANY ($1)) AND domain = $2 AND type = $3', [ids, domain, type], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
|
||||
obj.GetUserWithEmail = function (domain, email, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
|
||||
obj.GetUserWithVerifiedEmail = function (domain, email, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
|
||||
obj.Remove = function (id, func) { sqlDbQuery('DELETE FROM main WHERE id = $1', [id], func); };
|
||||
@ -1183,22 +1189,22 @@ module.exports.CreateDB = function (parent, func) {
|
||||
obj.GetAllEvents = function (func) { sqlDbQuery('SELECT doc FROM events', null, func); };
|
||||
obj.StoreEvent = function (event, func) {
|
||||
obj.dbCounters.eventsSet++;
|
||||
var batchQuery = [['INSERT INTO events VALUES ($1, $2, $3, $4, $5, $6, $7)', [null, event.time, ((typeof event.domain == 'string') ? event.domain : null), event.action, event.nodeid ? event.nodeid : null, event.userid ? event.userid : null, event]]];
|
||||
for (var i in event.ids) { if (event.ids[i] != '*') { batchQuery.push(['INSERT INTO eventids VALUES (LAST_INSERT_ID(), $1)', [event.ids[i]]]); } }
|
||||
sqlDbBatchExec(batchQuery, function (err, docs) { if (func != null) { func(err, docs); } });
|
||||
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, event], function (err, docs) {
|
||||
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]]); } } }
|
||||
});
|
||||
};
|
||||
obj.GetEvents = function (ids, domain, func) {
|
||||
if (ids.indexOf('*') >= 0) {
|
||||
sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC', [domain], func);
|
||||
} else {
|
||||
sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND target IN ($2)) GROUP BY id ORDER BY time DESC', [domain, ids], func);
|
||||
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);
|
||||
}
|
||||
};
|
||||
obj.GetEventsWithLimit = function (ids, domain, limit, func) {
|
||||
if (ids.indexOf('*') >= 0) {
|
||||
sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC LIMIT $2', [domain, limit], func);
|
||||
} else {
|
||||
sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND target IN ($2)) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, ids, limit], func);
|
||||
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);
|
||||
}
|
||||
};
|
||||
obj.GetUserEvents = function (ids, domain, username, func) {
|
||||
@ -1206,22 +1212,22 @@ module.exports.CreateDB = function (parent, func) {
|
||||
if (ids.indexOf('*') >= 0) {
|
||||
sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC', [domain, userid], func);
|
||||
} else {
|
||||
sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND target IN ($3)) GROUP BY id ORDER BY time DESC', [domain, userid, ids], func);
|
||||
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);
|
||||
}
|
||||
};
|
||||
obj.GetUserEventsWithLimit = function (ids, domain, username, limit, func) {
|
||||
const userid = 'user/' + domain + '/' + username.toLowerCase();
|
||||
if (ids.indexOf('*') >= 0) {
|
||||
sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT ?', [domain, userid, limit], func);
|
||||
sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT $3', [domain, userid, limit], func);
|
||||
} else {
|
||||
sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND target IN ($3)) GROUP BY id ORDER BY time DESC LIMIT ?', [domain, userid, ids, limit], func);
|
||||
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);
|
||||
}
|
||||
};
|
||||
obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
|
||||
if (ids.indexOf('*') >= 0) {
|
||||
sqlDbQuery('SELECT doc FROM events WHERE ((domain = $1) AND (time BETWEEN $2 AND ?)) ORDER BY time', [domain, start, end], func);
|
||||
} else {
|
||||
sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE ((domain = $1) AND (target IN ($2)) AND (time BETWEEN $3 AND $4)) GROUP BY id ORDER BY time', [domain, ids, start, end], func);
|
||||
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);
|
||||
}
|
||||
};
|
||||
//obj.GetUserLoginEvents = function (domain, username, func) { } // TODO
|
||||
@ -1235,7 +1241,7 @@ module.exports.CreateDB = function (parent, func) {
|
||||
// Database actions on the power collection
|
||||
obj.getAllPower = function (func) { sqlDbQuery('SELECT doc FROM power', null, func); };
|
||||
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); };
|
||||
obj.getPowerTimeline = function (nodeid, func) { sqlDbQuery('SELECT doc FROM power WHERE ((nodeid = $1) OR (nodeid = "*")) ORDER BY time ASC', [nodeid], func); };
|
||||
obj.getPowerTimeline = function (nodeid, func) { sqlDbQuery('SELECT doc FROM power WHERE ((nodeid = $1) OR (nodeid = \'*\')) ORDER BY time ASC', [nodeid], func); };
|
||||
obj.removeAllPowerEvents = function () { sqlDbQuery('DELETE FROM power', null, function (err, docs) { }); };
|
||||
obj.removeAllPowerEventsForNode = function (nodeid) { sqlDbQuery('DELETE FROM power WHERE nodeid = $1', [nodeid], function (err, docs) { }); };
|
||||
|
||||
@ -1283,7 +1289,7 @@ module.exports.CreateDB = function (parent, func) {
|
||||
|
||||
// Plugin operations
|
||||
if (obj.pluginsActive) {
|
||||
obj.addPlugin = function (plugin, func) { sqlDbQuery('INSERT INTO plugin VALUES ($1, $2)', [null, value], func); }; // Add a plugin
|
||||
obj.addPlugin = function (plugin, func) { sqlDbQuery('INSERT INTO plugin VALUES (DEFAULT, $2)', [value], func); }; // Add a plugin
|
||||
obj.getPlugins = function (func) { sqlDbQuery('SELECT doc FROM plugin', null, func); }; // Get all plugins
|
||||
obj.getPlugin = function (id, func) { sqlDbQuery('SELECT doc FROM plugin WHERE id = $1', [id], func); }; // Get plugin
|
||||
obj.deletePlugin = function (id, func) { sqlDbQuery('DELETE FROM plugin WHERE id = $1', [id], func); }; // Delete plugin
|
||||
|
@ -729,6 +729,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
||||
|
||||
// Request a list of all nodes
|
||||
db.GetAllTypeNoTypeFieldMeshFiltered(links, extraids, domain.id, 'node', command.id, function (err, docs) {
|
||||
|
||||
//console.log(docs);
|
||||
|
||||
if (docs == null) { docs = []; }
|
||||
parent.common.unEscapeAllLinksFieldName(docs);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user