feat(appwrite): add new documents trigger

This commit is contained in:
Rıdvan Akca
2024-01-25 16:02:43 +03:00
committed by Ali BARIN
parent 9dc82290b5
commit 6e5c0cc0c7
9 changed files with 163 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
import newDocuments from './new-documents/index.js';
export default [newDocuments];

View File

@@ -0,0 +1,76 @@
import defineTrigger from '../../../../helpers/define-trigger.js';
export default defineTrigger({
name: 'New documents',
key: 'newDocuments',
pollInterval: 15,
description: 'Triggers when a new document is created.',
arguments: [
{
label: 'Database',
key: 'databaseId',
type: 'dropdown',
required: true,
description: '',
variables: true,
source: {
type: 'query',
name: 'getDynamicData',
arguments: [
{
name: 'key',
value: 'listDatabases',
},
],
},
},
{
label: 'Collection',
key: 'collectionId',
type: 'dropdown',
required: true,
dependsOn: ['parameters.databaseId'],
description: '',
variables: true,
source: {
type: 'query',
name: 'getDynamicData',
arguments: [
{
name: 'key',
value: 'listCollections',
},
{
name: 'parameters.databaseId',
value: '{parameters.databaseId}',
},
],
},
},
],
async run($) {
const { databaseId, collectionId } = $.step.parameters;
const { data } = await $.http.get(
`/v1/databases/${databaseId}/collections/${collectionId}/documents`
);
if (!data?.documents?.length) {
return;
}
const sortedDocuments = data.documents.sort((a, b) =>
a.$createdAt - b.$createdAt ? 1 : -1
);
for (const document of sortedDocuments) {
$.pushTriggerItem({
raw: document,
meta: {
internalId: document.$id,
},
});
}
},
});