Skip to content

Commit

Permalink
Limit number of fetched conversations in the API (#954)
Browse files Browse the repository at this point in the history
* Limit number of fetched conversations in the API

* Add pagination

* use var
  • Loading branch information
nsarrazin committed Mar 25, 2024
1 parent 6887755 commit e9ad67e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/routes/api/conversations/+server.ts
Expand Up @@ -2,7 +2,11 @@ import { collections } from "$lib/server/database";
import { authCondition } from "$lib/server/auth";
import type { Conversation } from "$lib/types/Conversation";

export async function GET({ locals }) {
const NUM_PER_PAGE = 300;

export async function GET({ locals, url }) {
const p = parseInt(url.searchParams.get("p") ?? "0");

if (locals.user?._id || locals.sessionId) {
const convs = await collections.conversations
.find({
Expand All @@ -14,6 +18,8 @@ export async function GET({ locals }) {
model: 1,
})
.sort({ updatedAt: -1 })
.skip(p * NUM_PER_PAGE)
.limit(NUM_PER_PAGE)
.toArray();

const res = convs.map((conv) => ({
Expand Down

0 comments on commit e9ad67e

Please sign in to comment.