← FaunaDB

New or Removed Documents in a Collection from FaunaDB API

Pipedream makes it easy to connect APIs for FaunaDB and 900+ other apps remarkably fast.

Trigger workflow on
New or Removed Documents in a Collection from the FaunaDB API
Next, do this
Connect to 900+ APIs using code and no-code building blocks
No credit card required
Into to Pipedream
Watch us build a workflow
Watch us build a workflow
7 min
Watch now ➜
Trusted by 250,000+ developers from startups to Fortune 500 companies:
Trusted by 250,000+ developers from startups to Fortune 500 companies

Developers Pipedream

Getting Started

Trigger a workflow on New or Removed Documents in a Collection with FaunaDB API. When you configure and deploy the workflow, it will run on Pipedream's servers 24x7 for free.

  1. Configure the New or Removed Documents in a Collection trigger
    1. Configure timer
    2. Connect your FaunaDB account
    3. Select a Collection
    4. Optional- Configure Emit changes as a single event
  2. Add steps to connect to 900+ APIs using code and no-code building blocks
  3. Deploy the workflow
  4. Send a test event to validate your setup
  5. Turn on the trigger

Integrations

Send Message with Discord Webhook API on New or Removed Documents in a Collection from FaunaDB API
FaunaDB + Discord Webhook
 
Try it
Add Multiple Rows with Google Sheets API on New or Removed Documents in a Collection from FaunaDB API
FaunaDB + Google Sheets
 
Try it
Get Film with SWAPI API on New or Removed Documents in a Collection from FaunaDB API
FaunaDB + SWAPI - Star Wars
 
Try it
Create Multiple Records with Airtable API on New or Removed Documents in a Collection from FaunaDB API
FaunaDB + Airtable
 
Try it
Custom Request with HTTP / Webhook API on New or Removed Documents in a Collection from FaunaDB API
FaunaDB + HTTP / Webhook
 
Try it

Details

This is a pre-built, open source component from Pipedream's GitHub repo. The component is developed by Pipedream and the community, and verified and maintained by Pipedream.

To contribute an update to an existing component or create a new component, create a PR on GitHub. If you're new to Pipedream component development, you can start with quickstarts for trigger span and action development, and then review the component API reference.

New or Removed Documents in a Collection on FaunaDB
Description:Emit new event each time you add or remove a document from a specific collection, with the details of the document.
Version:0.0.7
Key:faunadb-changes-to-collection

Code

import fauna from "../../faunadb.app.mjs";
import _ from "lodash";

export default {
  type: "source",
  key: "faunadb-changes-to-collection",
  name: "New or Removed Documents in a Collection",
  description: "Emit new event each time you add or remove a document from a specific collection, with the details of the document.",
  version: "0.0.7",
  dedupe: "unique", // Dedupe events based on the concatenation of event + document ref id
  props: {
    timer: {
      type: "$.interface.timer",
      default: {
        intervalSeconds: 5 * 60,
      },
    },
    db: "$.service.db",
    fauna,
    collection: {
      propDefinition: [
        fauna,
        "collections",
      ],
    },
    emitEventsInBatch: {
      type: "boolean",
      label: "Emit changes as a single event",
      description:
        "If `true`, all events are emitted as an array, within a single Pipedream event. Defaults to `false`, emitting each event in Fauna as its own event in Pipedream",
      optional: true,
      default: false,
    },
  },
  async run() {
    // As soon as the script runs, mark the start time so we can fetch changes
    // since this time on the next run. Fauna expects epoch ms as its cursor.
    const ts = +new Date() * 1000;
    const cursor = this.db.get("cursor") || ts;

    const events = await this.fauna.getEventsInCollectionAfterTs(
      this.collection,
      cursor,
    );

    if (!events.length) {
      console.log(`No new events in collection ${this.collection}`);
      this.db.set("cursor", ts);
      return;
    }

    console.log(`${events.length} new events in collection ${this.collection}`);

    // Batched emits do not take advantage of the built-in deduper
    if (this.emitEventsInBatch) {
      this.$emit({
        events,
      }, {
        summary: `${events.length} new event${events.length > 1
          ? "s"
          : ""}`,
        id: cursor,
      });
    } else {
      for (const event of events) {
        this.$emit(event, {
          summary: `${event.action.toUpperCase()} - ${event.document.id}`,
          id: `${event.action}-${event.document.id}`, // dedupes events based on this ID
        });
      }
    }

    // Finally, set cursor for the next run to the max timestamp of the changed events, ensuring we
    // get all events after that on the next run. We need to add 1 since the timestamp filter in
    // Fauna is inclusive: https://docs.fauna.com/fauna/current/api/fql/functions/paginate
    const maxEventTs = _.maxBy(events, (event) => event.ts).ts + 1;

    this.db.set("cursor", maxEventTs);
  },
};

Configuration

This component may be configured based on the props defined in the component code. Pipedream automatically prompts for input values in the UI and CLI.
LabelPropTypeDescription
timer$.interface.timer
N/Adb$.service.dbThis component uses $.service.db to maintain state between component invocations.
FaunaDBfaunaappThis component uses the FaunaDB app.
CollectioncollectionstringSelect a value from the drop down menu.
Emit changes as a single eventemitEventsInBatchboolean

If true, all events are emitted as an array, within a single Pipedream event. Defaults to false, emitting each event in Fauna as its own event in Pipedream

Authentication

FaunaDB uses API keys for authentication. When you connect your FaunaDB account, Pipedream securely stores the keys so you can easily authenticate to FaunaDB APIs in both code and no-code steps.

Create a key in the Security section of your FaunaDB dashboard, then enter the Secret below.

Read more about Keys and Fauna Security.

About FaunaDB

The database built for serverless, featuring native GraphQL

More Ways to Use FaunaDB

Actions

Execute GraphQL Query with the FaunaDB API

Performs an arbitrary authorized GraphQL query. See docs here

 
Try it
Import GraphQL schema with the FaunaDB API

Import graphQL schema to a database. See docs here

 
Try it
Read From FaunaDB Collection with the FaunaDB API

Reads all documents from a given FaunaDB collection. See docs here

 
Try it