Trigger Nodes
MongoDB Trigger

MongoDB Trigger

The MongoDB Trigger parses the incoming webhook payload from the MongoDB webhook triggers. In other words, the BuildShip Workflow runs every time there are any changes in the linked MongoDB Collection, Database, or Deployment. Alternatively, the workflow can also be made to run based on a given schedule (or a CRON expression); however, this documentation is for setting up a Database Trigger.

MongoDB Trigger

Prerequisites ✅

Adding MongoDB Trigger

  • Create a new BuildShip Workflow and click on the "Add Trigger" button. Select (or search for) the MongoDB Trigger and add it to the workflow.

  • Open the Instruction panel 📎 and copy the Webhook URL.

  • To test the MongoDB Trigger functionality, click on the "Add Node" button and search for the Log message to console node and add it to the workflow. For the input values, pass in the Request Body from the context menu.

  • Hit "Ship! 🚀". Read the following section to complete the testing setup.

Setting up the MongoDB Trigger

  • Navigate to your MongoDB Dashboard and navigate to the "Triggers" section.

  • Pick your desired type of trigger from Database - which will listen to changes to your database, or Scheduled - which will trigger the event at the chosen schedule. For this example, we will be setting up a Database Trigger.

  • Follow the instructions and set up your source details.

  • Under the Function section, select "Function" as the Event Type.

  • Inside the Function script editor, paste the following script. Copy the Webhook URL from the MongoDB Trigger in your BuildShip workflow, and paste it on line 5:

import axios from 'axios';
import crypto from 'crypto';
 
exports = async function (changeEvent) {
  const webhookUrl = ''; // Add your webhook URL here
  const workflowId = webhookUrl.split('/').pop();
 
  try {
    const signature = generateSignature(changeEvent, workflowId);
 
    const res = await axios.post(webhookUrl, changeEvent, {
      headers: {
        'X-Webhook-Signature': signature,
      },
    });
    console.log(JSON.stringify(changeEvent));
    return true;
  } catch (error) {
    console.error('Error sending webhook:', error);
    return false;
  }
};
 
function generateSignature(data, secret) {
  const hmac = crypto.createHmac('sha256', secret);
  hmac.update(JSON.stringify(data));
  return hmac.digest('hex');
}
  • Click on the "Add Dependency" button and add axios, then hit "Add". This will install the dependency for the function execution. Similarly, add a dependency for the crypto package.

  • Once this is done, click on the "▶︎ Run" button at the bottom. If set up correctly, this should return true as the output on the console.

MongoDB Trigger
💡

The code snippet uses the axios library to send the POST request to the webhook URL, and the crypto library to generate a signature for the webhook payload. The signature is included in the request headers to ensure the authenticity of the request.

Troubleshooting

If you encounter any issues while setting up the MongoDB Trigger, here are some common troubleshooting tips:

  • Double-check the Webhook URL: Ensure that you've copied the correct Webhook URL from BuildShip and pasted it into the MongoDB Function script.

  • Verify dependencies: Make sure you've added the required axios and crypto dependencies to the MongoDB Function.

  • Review the logs: Check the MongoDB Function logs for any error messages or helpful information that can assist in troubleshooting.

Need Help?

  • 💬
    Join BuildShip Community

    An active and large community of no-code / low-code builders. Ask questions, share feedback, showcase your project and connect with other BuildShip enthusiasts.

  • 🙋
    Hire a BuildShip Expert

    Need personalized help to build your product fast? Browse and hire from a range of independent freelancers, agencies and builders - all well versed with BuildShip.

  • 🛟
    Send a Support Request

    Got a specific question on your workflows / project or want to report a bug? Send a us a request using the "Support" button directly from your BuildShip Dashboard.

  • ⭐️
    Feature Request

    Something missing in BuildShip for you? Share on the #FeatureRequest channel on Discord. Also browse and cast your votes on other feature requests.