From 121979b696795cfe628194d10e1840f5496fb5b2 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Thu, 6 Jul 2023 22:29:38 +0530 Subject: [PATCH] SQL improvement - Improved SQL query for periodic clearing of old/unauthorised data --- src/database.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/database.js b/src/database.js index bfefc95..1887477 100644 --- a/src/database.js +++ b/src/database.js @@ -479,27 +479,28 @@ DB.deleteData = function (snID, vectorClock) { DB.clearAuthorisedAppData = function (snID, app, adminID, subAdmins, timestamp) { return new Promise((resolve, reject) => { let statement = "DELETE FROM _" + snID + - " WHERE ( " + H_struct.TIME + " "?").join(", ") + ") )" : - ""); - queryResolve(statement, [timestamp, app, adminID].concat(subAdmins)) + " WHERE ( " + + H_struct.TIME + " < ? AND " + //data before deleteDelay (ie, 7 days ago) + H_struct.APPLICATION + " = ? AND " + //data of this app + T_struct.TAG + " IS NULL " + //tag field is NULL + ") AND ( " + + H_struct.RECEIVER_ID + " != ? OR " + //receiver is not admin + H_struct.SENDER_ID + " NOT IN (?) " + //sender is not subAdmin + ")"; + + queryResolve(statement, [timestamp, app, adminID, subAdmins]) .then(result => resolve(result)) .catch(error => reject(error)); }); }; -DB.clearUnauthorisedAppData = function (snID, appList, timestamp) { +DB.clearUnauthorisedAppData = function (snID, authorisedAppList, timestamp) { return new Promise((resolve, reject) => { let statement = "DELETE FROM _" + snID + - " WHERE " + H_struct.TIME + " "?").join(", ") + ")" : - ""); - queryResolve(statement, [timestamp].concat(appList)) + " WHERE " + H_struct.TIME + " < ? AND" + //data before deleteDelay (ie, 7 days ago) + H_struct.APPLICATION + " NOT IN (?)" //app not authorised + + queryResolve(statement, [timestamp, authorisedAppList]) .then(result => resolve(result)) .catch(error => reject(error)); });