Count totals using database instead of manually

This is way more efficient
This commit is contained in:
Hippo 2022-01-11 12:48:28 +05:30
parent faa53a3a7e
commit 921187a2aa

View file

@ -161,15 +161,10 @@ router.get('/', async (req, res) => {
if (DEBUG) console.debug('Returning home page') if (DEBUG) console.debug('Returning home page')
// count people // count people
// TODO: optimise to do using SQL only let { total_people, total_rupees } = (await knex('pledges')
let total_people = 0 .sum('amount as total_rupees')
let total_rupees = 0 .count('* as total_people'))[0]
let result = await Pledge.fetchAll()
for (let pledge of result.models) {
total_people += 1
total_rupees += pledge.get('amount')
}
twing.render('index.htm.twig', { twing.render('index.htm.twig', {
'goal_rupees': Number(goalRupees).toLocaleString('en-IN'), 'goal_rupees': Number(goalRupees).toLocaleString('en-IN'),