From 921187a2aaa15196094c9fe500e7636871ad47d9 Mon Sep 17 00:00:00 2001 From: Hippo Date: Tue, 11 Jan 2022 12:48:28 +0530 Subject: [PATCH] Count totals using database instead of manually This is way more efficient --- server/index.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/server/index.js b/server/index.js index 010317c..5cb18ff 100644 --- a/server/index.js +++ b/server/index.js @@ -161,15 +161,10 @@ router.get('/', async (req, res) => { if (DEBUG) console.debug('Returning home page') // count people - // TODO: optimise to do using SQL only - let total_people = 0 - let total_rupees = 0 - let result = await Pledge.fetchAll() + let { total_people, total_rupees } = (await knex('pledges') + .sum('amount as total_rupees') + .count('* as total_people'))[0] - for (let pledge of result.models) { - total_people += 1 - total_rupees += pledge.get('amount') - } twing.render('index.htm.twig', { 'goal_rupees': Number(goalRupees).toLocaleString('en-IN'),