From c5e1f3e4421a0af16353f49e640dcd45163689fe Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 2 Feb 2026 20:37:56 +0400 Subject: [PATCH] Use cursor-based pagination in iterateAllThreads --- src/routes/threads/helpers.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/routes/threads/helpers.ts b/src/routes/threads/helpers.ts index dbbb0a9092..aa1bb0aed7 100644 --- a/src/routes/threads/helpers.ts +++ b/src/routes/threads/helpers.ts @@ -122,13 +122,19 @@ export async function getThreadMessages(threadId: string) { } export async function* iterateAllThreads(total: number | undefined = undefined) { - let offset = 0; const limit = 100; + let cursor: string | undefined = undefined; + let count = 0; while (true) { + const queries = [Query.limit(limit)]; + if (cursor) { + queries.push(Query.cursorAfter(cursor)); + } + const data = await databases.listDocuments( PUBLIC_APPWRITE_DB_MAIN_ID, PUBLIC_APPWRITE_COL_THREADS_ID, - [Query.offset(offset), Query.limit(limit)] + queries ); if (data.documents.length === 0) { @@ -137,12 +143,12 @@ export async function* iterateAllThreads(total: number | undefined = undefined) for (const thread of data.documents) { yield thread; + count++; + if (total !== undefined && count >= total) { + return; + } } - offset += limit; - - if (total !== undefined && offset >= total) { - break; - } + cursor = data.documents[data.documents.length - 1].$id; } }