@@ -4,7 +4,9 @@ import { FilterQuery, Model, SortOrder } from 'mongoose';
4
4
import { UserPreview } from 'src/common/models/common.user.model' ;
5
5
import {
6
6
QueryArgs ,
7
+ QueryOneArgs ,
7
8
findAllAndLean ,
9
+ findOneAndLean ,
8
10
} from 'src/database/mongoose/database.mongoose.query' ;
9
11
import {
10
12
CursorExtractor ,
@@ -34,6 +36,12 @@ export class FollowService {
34
36
private readonly paginationCursorService : PaginationCursorService ,
35
37
) { }
36
38
39
+ async findOneAndLean (
40
+ queryOneArgs ?: QueryOneArgs < follow > ,
41
+ ) : Promise < follow | null > {
42
+ return await findOneAndLean ( this . followModel , queryOneArgs ) ;
43
+ }
44
+
37
45
async findAllAndLean ( queryArgs ?: QueryArgs < follow > ) : Promise < follow [ ] > {
38
46
return await findAllAndLean ( this . followModel , queryArgs ) ;
39
47
}
@@ -118,10 +126,12 @@ export class FollowService {
118
126
aggregate . match ( filter ) ;
119
127
}
120
128
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
+ } ) ;
125
135
126
136
const followerUserPreview = await Promise . all (
127
137
follower . map ( async ( follower ) => {
@@ -156,10 +166,14 @@ export class FollowService {
156
166
const [ id , _login ] : FollowListCursorField =
157
167
this . paginationCursorService . toFields ( after , fieldExtractor ) ;
158
168
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
+ } ) ;
163
177
164
178
if ( ! followAt ) {
165
179
return this . generateEmptyPage ( ) ;
@@ -206,10 +220,12 @@ export class FollowService {
206
220
aggregate . match ( filter ) ;
207
221
}
208
222
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
+ } ) ;
213
229
214
230
const followingUserPreview = await Promise . all (
215
231
following . map ( async ( following ) => {
@@ -244,10 +260,14 @@ export class FollowService {
244
260
const [ id , _login ] : FollowListCursorField =
245
261
this . paginationCursorService . toFields ( after , fieldExtractor ) ;
246
262
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
+ } ) ;
251
271
252
272
if ( ! followAt ) {
253
273
return this . generateEmptyPage ( ) ;
0 commit comments