fix(appwrite/new-documents): add native order and pagination support

This commit is contained in:
Ali BARIN
2024-05-15 17:50:44 +00:00
parent c122708b0b
commit 8c83b715fe

View File

@@ -52,25 +52,52 @@ export default defineTrigger({
async run($) { async run($) {
const { databaseId, collectionId } = $.step.parameters; const { databaseId, collectionId } = $.step.parameters;
const { data } = await $.http.get( const limit = 1;
`/v1/databases/${databaseId}/collections/${collectionId}/documents` let lastDocumentId = undefined;
); let offset = 0;
let documentCount = 0;
if (!data?.documents?.length) { do {
return; 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) => const { data } = await $.http.get(
a.$createdAt - b.$createdAt ? 1 : -1 `/v1/databases/${databaseId}/collections/${collectionId}/documents`,
); { params },
);
for (const document of sortedDocuments) { const documents = data?.documents;
$.pushTriggerItem({ documentCount = documents?.length;
raw: document, offset = offset + limit;
meta: { lastDocumentId = documents[documentCount - 1]?.$id;
internalId: document.$id,
}, if (!documentCount) {
}); return;
} }
for (const document of documents) {
$.pushTriggerItem({
raw: document,
meta: {
internalId: document.$id,
},
});
}
} while (documentCount === limit);
}, },
}); });