Skip to content

fix(forum): add pagination to listForums to prevent unbounded database queries#271

Merged
omkarhole merged 1 commit into
omkarhole:mainfrom
anshul23102:fix/265-forum-pagination
Jun 2, 2026
Merged

fix(forum): add pagination to listForums to prevent unbounded database queries#271
omkarhole merged 1 commit into
omkarhole:mainfrom
anshul23102:fix/265-forum-pagination

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

Summary

Closes #265

listForums fetched every ForumTopic document with an unparameterised find(). As the forum grows, each GET /api/v1/forums loads the full collection into memory, serialises it to JSON, and sends it to the client. This causes unbounded query time, large response bodies, and elevated heap usage under concurrent requests.

What Changed

backend/controllers/forum.controller.js only:

  • Added page and limit query parameters. page defaults to 1. limit defaults to FORUM_PAGE_SIZE (20) and is capped at that value to prevent callers from bypassing the limit.
  • The response now includes { forums, page, limit, total, totalPages }.
  • A parallel countDocuments() query provides the total count for client-side pagination UI.

Testing

  1. GET /api/v1/forums returns at most 20 topics with pagination metadata.
  2. GET /api/v1/forums?page=2&limit=10 returns the correct page.
  3. A limit value larger than 20 is clamped to 20.

Type of Change

  • Bug fix (performance)

NSoC '26 contribution

Closes omkarhole#265

listForums fetched every ForumTopic document with a single
unparameterized find(). As the platform grows, each GET /api/v1/forums
request loads the entire collection into memory and transmits it to the
client.

Add page and limit query parameters capped at a FORUM_PAGE_SIZE
constant (20). The response now includes page, limit, total, and
totalPages alongside the paginated forums array.
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

@anshul23102 is attempting to deploy a commit to the omkarhole's projects Team on Vercel.

A member of the Team first needs to authorize it.

@omkarhole omkarhole merged commit 7843c9c into omkarhole:main Jun 2, 2026
0 of 2 checks passed
@omkarhole omkarhole added NSOC'26 Issues for the NSOC 2026 program level2 5 points labels Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level2 5 points NSOC'26 Issues for the NSOC 2026 program

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: GET /forums returns all forum topics with no pagination, causing unbounded database queries as topic count grows

2 participants