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')
// 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'),