Skip to content

Commit b034841

Browse files
committed
refactor: ♻️ aggregate -> find~~ 로 변경, 필요한 데이터만 받도록 select 사용
- #406
1 parent 877225e commit b034841

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

app/src/follow/follow.service.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { FilterQuery, Model, SortOrder } from 'mongoose';
44
import { UserPreview } from 'src/common/models/common.user.model';
55
import {
66
QueryArgs,
7+
QueryOneArgs,
78
findAllAndLean,
9+
findOneAndLean,
810
} from 'src/database/mongoose/database.mongoose.query';
911
import {
1012
CursorExtractor,
@@ -34,6 +36,12 @@ export class FollowService {
3436
private readonly paginationCursorService: PaginationCursorService,
3537
) {}
3638

39+
async findOneAndLean(
40+
queryOneArgs?: QueryOneArgs<follow>,
41+
): Promise<follow | null> {
42+
return await findOneAndLean(this.followModel, queryOneArgs);
43+
}
44+
3745
async findAllAndLean(queryArgs?: QueryArgs<follow>): Promise<follow[]> {
3846
return await findAllAndLean(this.followModel, queryArgs);
3947
}
@@ -118,10 +126,12 @@ export class FollowService {
118126
aggregate.match(filter);
119127
}
120128

121-
const follower = await aggregate
122-
.match({ followId: targetId })
123-
.sort(followSort(sortOrder))
124-
.limit(limit);
129+
const follower: Pick<follow, 'userId'>[] = await this.findAllAndLean({
130+
filter: { followId: targetId },
131+
select: { _id: 0, userId: 1 },
132+
sort: followSort(sortOrder),
133+
limit,
134+
});
125135

126136
const followerUserPreview = await Promise.all(
127137
follower.map(async (follower) => {
@@ -156,10 +166,14 @@ export class FollowService {
156166
const [id, _login]: FollowListCursorField =
157167
this.paginationCursorService.toFields(after, fieldExtractor);
158168

159-
const [followAt] = await aggregate.match({
160-
userId: id,
161-
followId: targetId,
162-
});
169+
const followAt: Pick<follow, 'followAt'> | null =
170+
await this.findOneAndLean({
171+
filter: {
172+
userId: id,
173+
followId: targetId,
174+
},
175+
select: { _id: 0, followAt: 1 },
176+
});
163177

164178
if (!followAt) {
165179
return this.generateEmptyPage();
@@ -206,10 +220,12 @@ export class FollowService {
206220
aggregate.match(filter);
207221
}
208222

209-
const following = await aggregate
210-
.match({ userId: targetId })
211-
.sort(followSort(sortOrder))
212-
.limit(limit);
223+
const following: Pick<follow, 'followId'>[] = await this.findAllAndLean({
224+
filter: { userId: targetId },
225+
select: { _id: 0, followId: 1 },
226+
sort: followSort(sortOrder),
227+
limit,
228+
});
213229

214230
const followingUserPreview = await Promise.all(
215231
following.map(async (following) => {
@@ -244,10 +260,14 @@ export class FollowService {
244260
const [id, _login]: FollowListCursorField =
245261
this.paginationCursorService.toFields(after, fieldExtractor);
246262

247-
const [followAt] = await aggregate.match({
248-
userId: targetId,
249-
followId: id,
250-
});
263+
const followAt: Pick<follow, 'followAt'> | null =
264+
await this.findOneAndLean({
265+
filter: {
266+
userId: targetId,
267+
followId: id,
268+
},
269+
select: { _id: 0, followAt: 1 },
270+
});
251271

252272
if (!followAt) {
253273
return this.generateEmptyPage();

0 commit comments

Comments
 (0)