From 5344b79cdb73c17c53e290c3d3a217e090679934 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Sat, 8 Oct 2022 19:13:50 +0530 Subject: [PATCH] Minor Syntax improvements - Replaced convertDateToString() to new Date() as SQL queries accepts js-object Date - Pass nested array for bulk insert in backup-restore --- .gitignore | 1 + src/backup/slave.js | 23 +++++++++++------------ src/coupling.js | 2 +- src/market.js | 2 +- src/set_globals.js | 10 ---------- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 6446ac4..e067b5b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /args/param.json /args/keys*.json *test* +*.tmp* diff --git a/src/backup/slave.js b/src/backup/slave.js index 3bb1621..00df6e5 100644 --- a/src/backup/slave.js +++ b/src/backup/slave.js @@ -83,7 +83,7 @@ const requestInstance = { checksum_count_down: 0 }; -requestInstance.open = function(ws = null) { +requestInstance.open = function (ws = null) { const self = this; //Check if there is an active request if (self.request) { @@ -109,7 +109,7 @@ requestInstance.open = function(ws = null) { }).catch(error => console.error(error)) } -requestInstance.close = function() { +requestInstance.close = function () { const self = this; if (self.onetime) self.ws.close(); @@ -252,8 +252,8 @@ function storeBackupData(cache_promises, checksum_ref) { resolve(true); else verifyChecksum(checksum_ref) - .then(result => resolve(result)) - .catch(error => reject(error)) + .then(result => resolve(result)) + .catch(error => reject(error)) }); }) }) @@ -276,7 +276,7 @@ function storeBackupData(cache_promises, checksum_ref) { } -storeBackupData.commit = function(data, result) { +storeBackupData.commit = function (data, result) { let promises = []; for (let i = 0; i < data.length; i++) switch (result[i].status) { @@ -319,16 +319,15 @@ function updateTableData(table, data) { return new Promise((resolve, reject) => { if (!data.length) return resolve(null); - let cols = Object.keys(data[0]), - _mark = "(" + Array(cols.length).fill('?') + ")"; - let values = data.map(r => cols.map(c => validateValue(r[c]))).flat(); - let statement = `INSERT INTO ${table} (${cols}) VALUES ${Array(data.length).fill(_mark)}` + + let cols = Object.keys(data[0]); + let values = data.map(r => cols.map(c => validateValue(r[c]))); + let statement = `INSERT INTO ${table} (${cols}) VALUES ?` + " ON DUPLICATE KEY UPDATE " + cols.map(c => `${c}=VALUES(${c})`).join(); - DB.query(statement, values).then(_ => resolve(true)).catch(error => reject(error)); + DB.query(statement, [values]).then(_ => resolve(true)).catch(error => reject(error)); }) } -const validateValue = val => (typeof val === "string" && /\.\d{3}Z$/.test(val)) ? global.convertDateToString(val) : val; +const validateValue = val => (typeof val === "string" && /\.\d{3}Z$/.test(val)) ? new Date(val) : val; function verifyChecksum(checksum_ref) { return new Promise((resolve, reject) => { @@ -399,7 +398,7 @@ function verifyHash(hashes) { //Data to be deleted (incorrect data will be added by resync) let id_end = result[t].value[1].map(i => i * HASH_N_ROW); //eg if i=2 AND H_R_C = 5 then id_end = 2 * 5 = 10 (ie, range 6-10) Promise.allSettled(id_end.map(i => - DB.query(`DELETE FROM ${tables[t]} WHERE id BETWEEN ${i - HASH_N_ROW + 1} AND ${i}`))) //eg, i - HASH_N_ROW + 1 = 10 - 5 + 1 = 6 + DB.query(`DELETE FROM ${tables[t]} WHERE id BETWEEN ${i - HASH_N_ROW + 1} AND ${i}`))) //eg, i - HASH_N_ROW + 1 = 10 - 5 + 1 = 6 .then(_ => null); } else console.error(result[t].reason); diff --git a/src/coupling.js b/src/coupling.js index 0cfdc05..994c3df 100644 --- a/src/coupling.js +++ b/src/coupling.js @@ -195,7 +195,7 @@ function processOrders(seller_best, buyer_best, asset, cur_rate, quantity) { })); txQueries.push([ "INSERT INTO TradeTransactions (seller, buyer, asset, quantity, unitValue, tx_time, txid) VALUES (?, ?, ?, ?, ?, ?, ?)", - [seller_best.floID, buyer_best.floID, asset, quantity, cur_rate, global.convertDateToString(time), hash] + [seller_best.floID, buyer_best.floID, asset, quantity, cur_rate, new Date(time), hash] ]); return txQueries; diff --git a/src/market.js b/src/market.js index 8f96f7d..05a231d 100644 --- a/src/market.js +++ b/src/market.js @@ -325,7 +325,7 @@ function transferToken(sender, receivers, token) { })); txQueries.push([ "INSERT INTO TransferTransactions (sender, receiver, token, totalAmount, tx_time, txid) VALUE (?, ?, ?, ?, ?, ?)", - [sender, JSON.stringify(receivers), token, totalAmount, global.convertDateToString(time), hash] + [sender, JSON.stringify(receivers), token, totalAmount, new Date(time), hash] ]); DB.transaction(txQueries) .then(result => resolve(hash)) diff --git a/src/set_globals.js b/src/set_globals.js index ae8f846..f402c37 100644 --- a/src/set_globals.js +++ b/src/set_globals.js @@ -2,16 +2,6 @@ //fetch for node js (used in floBlockchainAPI.js) global.fetch = require("node-fetch"); -global.convertDateToString = function(timestamp) { - let date = new Date(timestamp); - return date.getFullYear() + '-' + - ('00' + (date.getMonth() + 1)).slice(-2) + '-' + - ('00' + date.getDate()).slice(-2) + ' ' + - ('00' + date.getHours()).slice(-2) + ':' + - ('00' + date.getMinutes()).slice(-2) + ':' + - ('00' + date.getSeconds()).slice(-2); -} - //Set browser paramaters from param.json (or param-default.json) var param; try {