From 8c83b715fefa945389084d6953cd8383505673d4 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Wed, 15 May 2024 17:50:44 +0000 Subject: [PATCH] fix(appwrite/new-documents): add native order and pagination support --- .../appwrite/triggers/new-documents/index.js | 61 +++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/packages/backend/src/apps/appwrite/triggers/new-documents/index.js b/packages/backend/src/apps/appwrite/triggers/new-documents/index.js index 97ac66d2..6c5a7e5f 100644 --- a/packages/backend/src/apps/appwrite/triggers/new-documents/index.js +++ b/packages/backend/src/apps/appwrite/triggers/new-documents/index.js @@ -52,25 +52,52 @@ export default defineTrigger({ async run($) { const { databaseId, collectionId } = $.step.parameters; - const { data } = await $.http.get( - `/v1/databases/${databaseId}/collections/${collectionId}/documents` - ); + const limit = 1; + let lastDocumentId = undefined; + let offset = 0; + let documentCount = 0; - if (!data?.documents?.length) { - return; - } + do { + const params = { + queries: [ + JSON.stringify({ + method: 'orderDesc', + atttribute: '$createdAt' + }), + JSON.stringify({ + method: 'limit', + values: [limit] + }), + // An invalid cursor shouldn't be sent. + lastDocumentId && JSON.stringify({ + method: 'cursorAfter', + values: [lastDocumentId] + }) + ].filter(Boolean), + }; - const sortedDocuments = data.documents.sort((a, b) => - a.$createdAt - b.$createdAt ? 1 : -1 - ); + const { data } = await $.http.get( + `/v1/databases/${databaseId}/collections/${collectionId}/documents`, + { params }, + ); - for (const document of sortedDocuments) { - $.pushTriggerItem({ - raw: document, - meta: { - internalId: document.$id, - }, - }); - } + const documents = data?.documents; + documentCount = documents?.length; + offset = offset + limit; + lastDocumentId = documents[documentCount - 1]?.$id; + + if (!documentCount) { + return; + } + + for (const document of documents) { + $.pushTriggerItem({ + raw: document, + meta: { + internalId: document.$id, + }, + }); + } + } while (documentCount === limit); }, });