diff --git a/packages/backend/src/apps/firebase/triggers/index.js b/packages/backend/src/apps/firebase/triggers/index.js index 3f46f9b4..760f76bb 100644 --- a/packages/backend/src/apps/firebase/triggers/index.js +++ b/packages/backend/src/apps/firebase/triggers/index.js @@ -1,3 +1,7 @@ +import newChildObjectInFirebaseRealtimeDatabase from './new-child-object-in-a-firebase-realtime-database/index.js'; import newDocumentsWithinFirestoreCollection from './new-documents-within-firestore-collection/index.js'; -export default [newDocumentsWithinFirestoreCollection]; +export default [ + newChildObjectInFirebaseRealtimeDatabase, + newDocumentsWithinFirestoreCollection, +]; diff --git a/packages/backend/src/apps/firebase/triggers/new-child-object-in-a-firebase-realtime-database/index.js b/packages/backend/src/apps/firebase/triggers/new-child-object-in-a-firebase-realtime-database/index.js new file mode 100644 index 00000000..6215deda --- /dev/null +++ b/packages/backend/src/apps/firebase/triggers/new-child-object-in-a-firebase-realtime-database/index.js @@ -0,0 +1,75 @@ +import Crypto from 'crypto'; +import defineTrigger from '../../../../helpers/define-trigger.js'; + +export default defineTrigger({ + name: 'New child object in a firebase realtime database', + key: 'newChildObjectInFirebaseRealtimeDatabase', + pollInterval: 15, + description: + 'Triggers when a new child object is generated within a specific path.', + arguments: [ + { + label: 'Path', + key: 'path', + type: 'string', + required: true, + description: + "Indicate the path to the key of the object where the child objects to be queried are located, for example, 'foo/bar/here.json'.", + variables: true, + }, + { + label: 'Order', + key: 'order', + type: 'string', + required: false, + description: + 'The key or path of the child that should be utilized for comparing objects/records. If unspecified, the order of $key is used.', + variables: true, + }, + { + label: 'Location of newest objects', + key: 'locationOfNewestObjects', + type: 'dropdown', + required: false, + description: + 'Specifies whether the new 100 records are positioned at the "top" or the "bottom" of the ordering. If left unspecified, the assumption is that the bottom/last result represents the "newest objects" (limitToLast).', + variables: true, + options: [ + { label: 'Top of results', value: 'limitToFirst' }, + { label: 'Bottom of results', value: 'limitToLast' }, + ], + }, + ], + + async run($) { + const { path, order, locationOfNewestObjects } = $.step.parameters; + + const params = {}; + + if (order) { + params.orderBy = `"${order}"`; + } + + if (locationOfNewestObjects) { + params[`${locationOfNewestObjects}`] = 100; + } + + const { data } = await $.http.get(path, { + params, + additionalProperties: { + setFirestoreBaseUrl: false, + }, + }); + + if (!data) { + return; + } + + $.pushTriggerItem({ + raw: data, + meta: { + internalId: Crypto.randomUUID(), + }, + }); + }, +}); diff --git a/packages/docs/pages/apps/firebase/triggers.md b/packages/docs/pages/apps/firebase/triggers.md index 234fab10..516a3bdc 100644 --- a/packages/docs/pages/apps/firebase/triggers.md +++ b/packages/docs/pages/apps/firebase/triggers.md @@ -1,6 +1,8 @@ --- favicon: /favicons/firebase.svg items: + - name: New child object in a firebase realtime database + desc: Triggers when a new child object is generated within a specific path. - name: New documents within a firestore collection desc: Triggers when a new document is added within a Cloud Firestore collection. ---