optimizing DB projections
This commit is contained in:
@@ -7,7 +7,11 @@ export async function GET(request: Request, { params:{ id } }: { params: { id:st
|
|||||||
|
|
||||||
const dbClient = await getDbClient();
|
const dbClient = await getDbClient();
|
||||||
const location = await dbClient.collection<BillingLocation>("lokacije")
|
const location = await dbClient.collection<BillingLocation>("lokacije")
|
||||||
.findOne({ _id: locationID });
|
.findOne({ _id: locationID }, {
|
||||||
|
projection: {
|
||||||
|
utilBillsProofOfPaymentAttachment: 1,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if(!location?.utilBillsProofOfPaymentAttachment) {
|
if(!location?.utilBillsProofOfPaymentAttachment) {
|
||||||
notFound();
|
notFound();
|
||||||
|
|||||||
@@ -381,24 +381,24 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu
|
|||||||
"yearMonth.year": year,
|
"yearMonth.year": year,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
// DUPLICATION of block below ... probably added by AI {
|
||||||
$addFields: {
|
// DUPLICATION of block below ... probably added by AI $addFields: {
|
||||||
bills: {
|
// DUPLICATION of block below ... probably added by AI bills: {
|
||||||
$map: {
|
// DUPLICATION of block below ... probably added by AI $map: {
|
||||||
input: "$bills",
|
// DUPLICATION of block below ... probably added by AI input: "$bills",
|
||||||
as: "bill",
|
// DUPLICATION of block below ... probably added by AI as: "bill",
|
||||||
in: {
|
// DUPLICATION of block below ... probably added by AI in: {
|
||||||
_id: "$$bill._id",
|
// DUPLICATION of block below ... probably added by AI _id: "$$bill._id",
|
||||||
name: "$$bill.name",
|
// DUPLICATION of block below ... probably added by AI name: "$$bill.name",
|
||||||
paid: "$$bill.paid",
|
// DUPLICATION of block below ... probably added by AI paid: "$$bill.paid",
|
||||||
billedTo: "$$bill.billedTo",
|
// DUPLICATION of block below ... probably added by AI billedTo: "$$bill.billedTo",
|
||||||
payedAmount: "$$bill.payedAmount",
|
// DUPLICATION of block below ... probably added by AI payedAmount: "$$bill.payedAmount",
|
||||||
hasAttachment: { $ne: ["$$bill.attachment", null] },
|
// 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: {
|
$addFields: {
|
||||||
_id: { $toString: "$_id" },
|
_id: { $toString: "$_id" },
|
||||||
@@ -412,10 +412,10 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu
|
|||||||
paid: "$$bill.paid",
|
paid: "$$bill.paid",
|
||||||
billedTo: "$$bill.billedTo",
|
billedTo: "$$bill.billedTo",
|
||||||
payedAmount: "$$bill.payedAmount",
|
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.attachment": 0,
|
||||||
// "bills.notes": 0,
|
// "bills.notes": 0,
|
||||||
// "bills.hub3aText": 1,
|
// "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: {
|
projection: {
|
||||||
// don't include the attachment binary data in the response
|
// don't include the attachment binary data in the response
|
||||||
"bills.attachment.fileContentsBase64": 0,
|
"bills.attachment.fileContentsBase64": 0,
|
||||||
|
"utilBillsProofOfPaymentAttachment.fileContentsBase64": 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }:
|
|||||||
return({
|
return({
|
||||||
// copy all the properties from the previous location
|
// copy all the properties from the previous location
|
||||||
...prevLocation,
|
...prevLocation,
|
||||||
|
// clear properties specific to the month
|
||||||
|
seenByTenant: undefined,
|
||||||
|
utilBillsProofOfPaymentAttachment: undefined,
|
||||||
// assign a new ID
|
// assign a new ID
|
||||||
_id: (new ObjectId()).toHexString(),
|
_id: (new ObjectId()).toHexString(),
|
||||||
yearMonth: {
|
yearMonth: {
|
||||||
|
|||||||
@@ -38,6 +38,15 @@ export const fetchBarcodeDataForPrint = withUser(async (user: AuthenticatedUser,
|
|||||||
userId, // ensure data belongs to authenticated user
|
userId, // ensure data belongs to authenticated user
|
||||||
"yearMonth.year": year,
|
"yearMonth.year": year,
|
||||||
"yearMonth.month": month
|
"yearMonth.month": month
|
||||||
|
}, {
|
||||||
|
// project only necessary fields
|
||||||
|
projection: {
|
||||||
|
name: 1,
|
||||||
|
bills: 1,
|
||||||
|
barcodeImage: 1,
|
||||||
|
hub3aText: 1,
|
||||||
|
payedAmount: 1
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user