optimizing DB projections

This commit is contained in:
Knee Cola
2025-11-23 11:59:06 +01:00
parent fcf3c447d1
commit 1995ad9de9
4 changed files with 41 additions and 21 deletions

View File

@@ -7,7 +7,11 @@ export async function GET(request: Request, { params:{ id } }: { params: { id:st
const dbClient = await getDbClient();
const location = await dbClient.collection<BillingLocation>("lokacije")
.findOne({ _id: locationID });
.findOne({ _id: locationID }, {
projection: {
utilBillsProofOfPaymentAttachment: 1,
}
});
if(!location?.utilBillsProofOfPaymentAttachment) {
notFound();

View File

@@ -381,24 +381,24 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu
"yearMonth.year": year,
},
},
{
$addFields: {
bills: {
$map: {
input: "$bills",
as: "bill",
in: {
_id: "$$bill._id",
name: "$$bill.name",
paid: "$$bill.paid",
billedTo: "$$bill.billedTo",
payedAmount: "$$bill.payedAmount",
hasAttachment: { $ne: ["$$bill.attachment", null] },
},
},
},
},
},
// DUPLICATION of block below ... probably added by AI {
// DUPLICATION of block below ... probably added by AI $addFields: {
// DUPLICATION of block below ... probably added by AI bills: {
// DUPLICATION of block below ... probably added by AI $map: {
// DUPLICATION of block below ... probably added by AI input: "$bills",
// DUPLICATION of block below ... probably added by AI as: "bill",
// DUPLICATION of block below ... probably added by AI in: {
// DUPLICATION of block below ... probably added by AI _id: "$$bill._id",
// DUPLICATION of block below ... probably added by AI name: "$$bill.name",
// DUPLICATION of block below ... probably added by AI paid: "$$bill.paid",
// DUPLICATION of block below ... probably added by AI billedTo: "$$bill.billedTo",
// DUPLICATION of block below ... probably added by AI payedAmount: "$$bill.payedAmount",
// DUPLICATION of block below ... probably added by AI hasAttachment: { $ne: ["$$bill.attachment", null] },
// DUPLICATION of block below ... probably added by AI },
// DUPLICATION of block below ... probably added by AI },
// DUPLICATION of block below ... probably added by AI },
// DUPLICATION of block below ... probably added by AI },
// DUPLICATION of block below ... probably added by AI },
{
$addFields: {
_id: { $toString: "$_id" },
@@ -412,10 +412,10 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu
paid: "$$bill.paid",
billedTo: "$$bill.billedTo",
payedAmount: "$$bill.payedAmount",
hasAttachment: "$$bill.hasAttachment",
hasAttachment: { $ne: ["$$bill.attachment", null] },
},
},
},
}
}
},
{
@@ -433,6 +433,9 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu
// "bills.attachment": 0,
// "bills.notes": 0,
// "bills.hub3aText": 1,
// project only file name - leave out file content so that
// less data is transferred to the client
"utilBillsProofOfPaymentAttachment.fileName": 1,
},
},
{
@@ -494,6 +497,7 @@ export const fetchLocationById = async (locationID:string) => {
projection: {
// don't include the attachment binary data in the response
"bills.attachment.fileContentsBase64": 0,
"utilBillsProofOfPaymentAttachment.fileContentsBase64": 0,
},
}
);

View File

@@ -39,6 +39,9 @@ export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }:
return({
// copy all the properties from the previous location
...prevLocation,
// clear properties specific to the month
seenByTenant: undefined,
utilBillsProofOfPaymentAttachment: undefined,
// assign a new ID
_id: (new ObjectId()).toHexString(),
yearMonth: {

View File

@@ -38,6 +38,15 @@ export const fetchBarcodeDataForPrint = withUser(async (user: AuthenticatedUser,
userId, // ensure data belongs to authenticated user
"yearMonth.year": year,
"yearMonth.month": month
}, {
// project only necessary fields
projection: {
name: 1,
bills: 1,
barcodeImage: 1,
hub3aText: 1,
payedAmount: 1
}
})
.toArray();