improve query performance
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user