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