Skip to content

Commit

Permalink
refactor: ♻️ aggregate -> find~~ 로 변경, 필요한 데이터만 받도록 select 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu01 committed Feb 20, 2024
1 parent 53dac2c commit 3c09f68
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions app/src/follow/follow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,6 +36,12 @@ export class FollowService {
private readonly paginationCursorService: PaginationCursorService,
) {}

async findOneAndLean(
queryOneArgs?: QueryOneArgs<follow>,
): Promise<follow | null> {
return await findOneAndLean(this.followModel, queryOneArgs);
}

async findAllAndLean(queryArgs?: QueryArgs<follow>): Promise<follow[]> {
return await findAllAndLean(this.followModel, queryArgs);
}
Expand Down Expand Up @@ -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<follow, 'userId'>[] = await this.findAllAndLean({
filter: { followId: targetId },
select: { _id: 0, userId: 1 },
sort: followSort(sortOrder),
limit,
});

const followerUserPreview = await Promise.all(
follower.map(async (follower) => {
Expand Down Expand Up @@ -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<follow, 'followAt'> | null =
await this.findOneAndLean({
filter: {
userId: id,
followId: targetId,
},
select: { _id: 0, followAt: 1 },
});

if (!followAt) {
return this.generateEmptyPage();
Expand Down Expand Up @@ -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<follow, 'followId'>[] = await this.findAllAndLean({
filter: { userId: targetId },
select: { _id: 0, followId: 1 },
sort: followSort(sortOrder),
limit,
});

const followingUserPreview = await Promise.all(
following.map(async (following) => {
Expand Down Expand Up @@ -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<follow, 'followAt'> | null =
await this.findOneAndLean({
filter: {
userId: targetId,
followId: id,
},
select: { _id: 0, followAt: 1 },
});

if (!followAt) {
return this.generateEmptyPage();
Expand Down

0 comments on commit 3c09f68

Please sign in to comment.