From db3b4abed56a15f9bb71348b7340ada19d0e384e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Thu, 8 Feb 2024 15:15:10 +0100 Subject: [PATCH] optimization: not fetching binary data if not needed --- app/attachment/[id]/route.tsx | 2 +- app/lib/actions/billActions.ts | 17 +++++++++++++++-- app/lib/actions/locationActions.ts | 27 ++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/app/attachment/[id]/route.tsx b/app/attachment/[id]/route.tsx index b153845..b8099f8 100644 --- a/app/attachment/[id]/route.tsx +++ b/app/attachment/[id]/route.tsx @@ -4,7 +4,7 @@ import { notFound } from 'next/navigation'; export async function GET(request: Request, { params:{ id } }: { params: { id:string } }) { const [locationID, billID] = id.split('-'); - const [location, bill] = await fetchBillById(locationID, billID) ?? []; + const [location, bill] = await fetchBillById(locationID, billID, true) ?? []; if(!bill?.attachment) { notFound(); diff --git a/app/lib/actions/billActions.ts b/app/lib/actions/billActions.ts index d5ed282..ea8e3fb 100644 --- a/app/lib/actions/billActions.ts +++ b/app/lib/actions/billActions.ts @@ -196,14 +196,27 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI } }) -export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => { +export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string, includeAttachmentBinary:boolean = false) => { const { id: userId } = user; const dbClient = await getDbClient(); + // don't include the attachment binary data in the response + // if the attachment binary data is not needed + const projection = includeAttachmentBinary ? {} : { + "bills.attachment.fileContentsBase64": 0, + }; + // find a location with the given locationID - const billLocation = await dbClient.collection("lokacije").findOne({ _id: locationID, userId }) + const billLocation = await dbClient.collection("lokacije").findOne( + { + _id: locationID, + userId + }, + { + projection + }) if(!billLocation) { console.log(`Location ${locationID} not found`); diff --git a/app/lib/actions/locationActions.ts b/app/lib/actions/locationActions.ts index e094c9d..f523eae 100644 --- a/app/lib/actions/locationActions.ts +++ b/app/lib/actions/locationActions.ts @@ -105,10 +105,18 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu // fetch all locations for the given year const locations = await dbClient.collection("lokacije") - .find({ - userId, - "yearMonth.year": year, - }) + .find( + { + userId, + "yearMonth.year": year, + }, + { + projection: { + // don't include the attachment binary data in the response + "bill.attachment.fileContentsBase64": 0, + }, + } + ) .sort({ "yearMonth.year": -1, "yearMonth.month": -1, @@ -130,7 +138,16 @@ export const fetchLocationById = withUser(async (user:AuthenticatedUser, locatio const { id: userId } = user; // find a location with the given locationID - const billLocation = await dbClient.collection("lokacije").findOne({ _id: locationID, userId}); + const billLocation = await dbClient.collection("lokacije") + .findOne( + { _id: locationID, userId }, + { + projection: { + // don't include the attachment binary data in the response + "bill.attachment.fileContentsBase64": 0, + }, + } + ); if(!billLocation) { console.log(`Location ${locationID} not found`);