From 59ce61d3a6e3f16aa26126a698c51e784a1004e3 Mon Sep 17 00:00:00 2001 From: Akhil Reddy Date: Tue, 21 Jan 2025 14:31:28 +0530 Subject: [PATCH] improve query performance --- src/permit/permit.service.ts | 54 ++++++++++++++++++++++++++++-------- src/rts/rts.controller.ts | 2 +- src/rts/rts.service.ts | 52 +++++++++++++++++++++++++++------- 3 files changed, 86 insertions(+), 22 deletions(-) diff --git a/src/permit/permit.service.ts b/src/permit/permit.service.ts index 8ece40e..3ce0123 100644 --- a/src/permit/permit.service.ts +++ b/src/permit/permit.service.ts @@ -22,9 +22,9 @@ export async function getPermit(permitId: string, tenantId: string) { .findOne({ $and: [{ tenantId: tenantId }, { pid: permitId }], }) - .populate("county") - .populate("client") - .populate("assignedTo"); + .populate({ path: "county", select: "pid name avatar" }) + .populate({ path: "client", select: "pid name avatar" }) + .populate({ path: "assignedTo", select: "pid name avatar" }); } export async function listPermits(params: PageQueryParams, tenantId: string) { @@ -55,7 +55,7 @@ export async function listPermits(params: PageQueryParams, tenantId: string) { }, { $lookup: { - from: "permits", + from: "users", localField: "assignedTo", foreignField: "_id", as: "assignedRec", @@ -63,15 +63,47 @@ export async function listPermits(params: PageQueryParams, tenantId: string) { }, { $project: { - _id: 0, + _id: 1, pid: 1, permitNumber: 1, - county: { $arrayElemAt: ["$countyRec", 0] }, - client: { $arrayElemAt: ["$clientRec", 0] }, permitDate: 1, stage: 1, status: 1, - assignedTo: { $arrayElemAt: ["$assignedRec", 0] }, + county: { + $let: { + vars: { county: { $arrayElemAt: ["$countyRec", 0] } }, + in: { + _id: "$$county._id", + pid: "$$county.pid", + name: "$$county.name", + type: "$$county.type", + avatar: "$$county.avatar", + }, + }, + }, + client: { + $let: { + vars: { client: { $arrayElemAt: ["$clientRec", 0] } }, + in: { + _id: "$$client._id", + pid: "$$client.pid", + name: "$$client.name", + type: "$$client.type", + avatar: "$$client.avatar", + }, + }, + }, + assignedTo: { + $let: { + vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } }, + in: { + _id: "$$assigned._id", + pid: "$$assigned.pid", + name: "$$assigned.name", + avatar: "$$assigned.avatar", + }, + }, + }, }, }, { @@ -112,9 +144,9 @@ export async function updatePermit( { ...input, updatedAt: new Date() }, { new: true } ) - .populate("county") - .populate("client") - .populate("assignedTo"); + .populate({ path: "county", select: "pid name avatar" }) + .populate({ path: "client", select: "pid name avatar" }) + .populate({ path: "assignedTo", select: "pid name avatar" }); return updatePermitResult; } diff --git a/src/rts/rts.controller.ts b/src/rts/rts.controller.ts index b702c3e..5eaf23e 100644 --- a/src/rts/rts.controller.ts +++ b/src/rts/rts.controller.ts @@ -53,7 +53,7 @@ export async function updateRtsHandler(req: FastifyRequest, res: FastifyReply) { const updatedRts = await updateRts(rtsId, input, req.user.tenantId); if (!updatedRts) return res.code(404).send({ error: "resource not found" }); - return res.code(200).send(updateRts); + return res.code(200).send(updatedRts); } catch (err) { return err; } diff --git a/src/rts/rts.service.ts b/src/rts/rts.service.ts index feee853..5eab2e7 100644 --- a/src/rts/rts.service.ts +++ b/src/rts/rts.service.ts @@ -43,6 +43,8 @@ export async function createRts( export async function getRts(id: string, tenantId: string) { return await rtsModel .findOne({ pid: id, tenantId: tenantId }) + .populate({ path: "county", select: "pid name avatar" }) + .populate({ path: "client", select: "pid name avatar" }) .populate({ path: "createdBy", select: "pid name avatar" }); } @@ -62,7 +64,6 @@ export async function listRts(params: PageQueryParams, tenantId: string) { localField: "county", foreignField: "_id", as: "countyRec", - pipeline: [{ $project: { _id: 1, pid: 1, name: 1, type: 1 } }], }, }, { @@ -71,7 +72,6 @@ export async function listRts(params: PageQueryParams, tenantId: string) { localField: "client", foreignField: "_id", as: "clientRec", - pipeline: [{ $project: { _id: 1, pid: 1, name: 1, type: 1 } }], }, }, { @@ -80,18 +80,47 @@ export async function listRts(params: PageQueryParams, tenantId: string) { localField: "createdBy", foreignField: "_id", as: "createdRec", - pipeline: [{ $project: { _id: 1, pid: 1, name: 1, avatar: 1 } }], }, }, { $project: { _id: 1, pid: 1, - county: { $arrayElemAt: ["$countyRec", 0] }, - client: { $arrayElemAt: ["$clientRec", 0] }, statusPipeline: 1, createdAt: 1, - createdBy: { $arrayElemAt: ["$createdRec", 0] }, + county: { + $let: { + vars: { county: { $arrayElemAt: ["$countyRec", 0] } }, + in: { + _id: "$$county._id", + pid: "$$county.pid", + name: "$$county.name", + type: "$$county.type", + }, + }, + }, + client: { + $let: { + vars: { client: { $arrayElemAt: ["$clientRec", 0] } }, + in: { + _id: "$$client._id", + pid: "$$client.pid", + name: "$$client.name", + type: "$$client.type", + }, + }, + }, + createdBy: { + $let: { + vars: { created: { $arrayElemAt: ["$createdRec", 0] } }, + in: { + _id: "$$created._id", + pid: "$$created.pid", + name: "$$created.name", + avatar: "$$created.avatar", + }, + }, + }, }, }, { @@ -124,10 +153,13 @@ export async function updateRts( input: UpdateRtsInput, tenantId: string ) { - return await rtsModel.findOneAndUpdate( - { pid: id, tenantId: tenantId }, - input - ); + const updatedRts = await rtsModel + .findOneAndUpdate({ pid: id, tenantId: tenantId }, input, { new: true }) + .populate({ path: "createdBy", select: "pid name avatar" }) + .populate({ path: "county", select: "pid name avatar" }) + .populate({ path: "client", select: "pid name avatar" }); + + return updatedRts; } export async function deleteRts(id: string, tenantId: string) {