Skip to content

Commit

Permalink
refactor: parallelize queries for better performance (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinwhisman authored Dec 26, 2023
1 parent 7d5a217 commit b5b4020
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/lib/monthly-overview.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
export const getMonthlyOverview = async (supabase, year, month) => {
const currentMonth = new Date(year, month, 1).toDateString();
const nextMonth = new Date(year, month + 1, 1).toDateString();
const getExpenses = async (supabase, currentMonth, nextMonth) => {
const { data: expenses } = await supabase
.from('expenses')
.select('id,date,category,description,amount')
.gte('date', currentMonth)
.lt('date', nextMonth);

return expenses;
};

const getIncome = async (supabase, currentMonth, nextMonth) => {
const { data: income } = await supabase
.from('income')
.select('id,date,category,description,amount')
.gte('date', currentMonth)
.lt('date', nextMonth);

return income;
};

const getSavings = async (supabase, currentMonth, nextMonth) => {
const { data: savings } = await supabase
.from('savings')
.select('id,date,category,description,amount')
.gte('date', currentMonth)
.lt('date', nextMonth);

return savings;
};

const getDebt = async (supabase, currentMonth, nextMonth) => {
const { data: debt } = await supabase
.from('debt')
.select('id,date,category,description,amount,interest_rate,minimum_payment')
.gte('date', currentMonth)
.lt('date', nextMonth);

return debt;
};

export const getMonthlyOverview = async (supabase, year, month) => {
const currentMonth = new Date(year, month, 1).toDateString();
const nextMonth = new Date(year, month + 1, 1).toDateString();
const [expenses, income, savings, debt] = await Promise.all([
getExpenses(supabase, currentMonth, nextMonth),
getIncome(supabase, currentMonth, nextMonth),
getSavings(supabase, currentMonth, nextMonth),
getDebt(supabase, currentMonth, nextMonth),
]);

return {
expenses,
income,
Expand Down

0 comments on commit b5b4020

Please sign in to comment.