Skip to content

Commit 661312e

Browse files
authored
Merge pull request #133 from cuappdev/ashley/debug-pagination
Debugging pagination
2 parents 4ffe1b6 + 29de8f6 commit 661312e

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/repositories/PostRepository.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,27 @@ export class PostRepository extends AbstractRepository<PostModel> {
1818
.getMany();
1919
}
2020

21-
public async getAllPostsPaginated(skip:number,limit:number): Promise<PostModel[]> {
21+
public async getAllPostsPaginated(skip: number, limit: number): Promise<PostModel[]> {
22+
// Step 1: Get paginated post IDs
23+
const postIds = await this.repository
24+
.createQueryBuilder("post")
25+
.select("post.id")
26+
.where("post.archive = false")
27+
.orderBy("post.createdAt", "DESC")
28+
.skip(skip)
29+
.take(limit)
30+
.getMany();
31+
32+
const ids = postIds.map(post => post.id);
33+
if (ids.length === 0) return [];
34+
35+
// Step 2: Fetch full posts with relations
2236
return await this.repository
2337
.createQueryBuilder("post")
2438
.leftJoinAndSelect("post.user", "user")
2539
.leftJoinAndSelect("post.categories", "categories")
26-
.where("post.archive = false")
27-
.orderBy("post.created", "DESC")
28-
.skip(skip) // Skip previous pages
29-
.take(limit)
30-
40+
.where("post.id IN (:...ids)", { ids })
41+
.orderBy("post.createdAt", "DESC")
3142
.getMany();
3243
}
3344

0 commit comments

Comments
 (0)