From 3c09f68d21af9fb03534c5b4686be2d8aa4ea582 Mon Sep 17 00:00:00 2001 From: niamu01 Date: Sat, 30 Dec 2023 19:36:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20:recycle:=20aggregate=20->=20find~~?= =?UTF-8?q?=20=EB=A1=9C=20=EB=B3=80=EA=B2=BD,=20=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=A7=8C=20=EB=B0=9B?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20select=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - #406 --- app/src/follow/follow.service.ts | 52 ++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/app/src/follow/follow.service.ts b/app/src/follow/follow.service.ts index b0c36203..631ea9b0 100644 --- a/app/src/follow/follow.service.ts +++ b/app/src/follow/follow.service.ts @@ -4,7 +4,9 @@ import { FilterQuery, Model, SortOrder } from 'mongoose'; import { UserPreview } from 'src/common/models/common.user.model'; import { QueryArgs, + QueryOneArgs, findAllAndLean, + findOneAndLean, } from 'src/database/mongoose/database.mongoose.query'; import { CursorExtractor, @@ -34,6 +36,12 @@ export class FollowService { private readonly paginationCursorService: PaginationCursorService, ) {} + async findOneAndLean( + queryOneArgs?: QueryOneArgs, + ): Promise { + return await findOneAndLean(this.followModel, queryOneArgs); + } + async findAllAndLean(queryArgs?: QueryArgs): Promise { return await findAllAndLean(this.followModel, queryArgs); } @@ -118,10 +126,12 @@ export class FollowService { aggregate.match(filter); } - const follower = await aggregate - .match({ followId: targetId }) - .sort(followSort(sortOrder)) - .limit(limit); + const follower: Pick[] = await this.findAllAndLean({ + filter: { followId: targetId }, + select: { _id: 0, userId: 1 }, + sort: followSort(sortOrder), + limit, + }); const followerUserPreview = await Promise.all( follower.map(async (follower) => { @@ -156,10 +166,14 @@ export class FollowService { const [id, _login]: FollowListCursorField = this.paginationCursorService.toFields(after, fieldExtractor); - const [followAt] = await aggregate.match({ - userId: id, - followId: targetId, - }); + const followAt: Pick | null = + await this.findOneAndLean({ + filter: { + userId: id, + followId: targetId, + }, + select: { _id: 0, followAt: 1 }, + }); if (!followAt) { return this.generateEmptyPage(); @@ -206,10 +220,12 @@ export class FollowService { aggregate.match(filter); } - const following = await aggregate - .match({ userId: targetId }) - .sort(followSort(sortOrder)) - .limit(limit); + const following: Pick[] = await this.findAllAndLean({ + filter: { userId: targetId }, + select: { _id: 0, followId: 1 }, + sort: followSort(sortOrder), + limit, + }); const followingUserPreview = await Promise.all( following.map(async (following) => { @@ -244,10 +260,14 @@ export class FollowService { const [id, _login]: FollowListCursorField = this.paginationCursorService.toFields(after, fieldExtractor); - const [followAt] = await aggregate.match({ - userId: targetId, - followId: id, - }); + const followAt: Pick | null = + await this.findOneAndLean({ + filter: { + userId: targetId, + followId: id, + }, + select: { _id: 0, followAt: 1 }, + }); if (!followAt) { return this.generateEmptyPage();