Skip to content

Fauna

Fauna is a is a distributed document-relational database that combines the flexibility of documents with the power of a relational, ACID compliant database that scales across regions, clouds or the globe.

Database Integrations allow you to connect to a database from your Worker by getting the right configuration from your database provider and adding it as secrets to your Worker.

Set up an integration with Fauna

To set up an integration with Fauna:

  1. You need to have an existing Fauna database to connect to. Create a Fauna database with demo data.

  2. Once your database is created with demo data, you can query it directly using the Shell tab in the Fauna dashboard:

    Terminal window
    Customer.all()
  3. Add the Fauna database integration to your Worker:

    1. Log in to the Cloudflare dashboard and select your account.
    2. In Account Home, select Workers & Pages.
    3. In Overview, select your Worker.
    4. Select Integrations > Fauna.
    5. Follow the setup flow, selecting the database created in step 1.
  4. In your Worker, install the fauna driver to connect to your database and start manipulating data:

    Terminal window
    npm install fauna
  5. The following example shows how to make a query to your Fauna database in a Worker. The credentials needed to connect to Fauna have been automatically added as secrets to your Worker through the integration.

    import { Client, fql } from "fauna";
    export default {
    async fetch(request, env) {
    const fauna = new Client({
    secret: env.FAUNA_SECRET,
    });
    const query = fql`Customer.all()`;
    const result = await fauna.query(query);
    return Response.json(result.data);
    },
    };
  6. You can manage the Cloudflare Fauna integration from the Fauna Dashboard:

    • To view Fauna keys for an integrated Cloudflare Worker, select your database and click the Keys tab.

      Keys for a Cloudflare Worker integration are prepended with _cloudflare_key_.

      You can delete the key to disable the integration.

    • When you connect a Cloudflare Worker to your database, Fauna creates an OAuth client app in your Fauna account.

      To view your account’s OAuth apps, go to Account Settings > OAuth Apps in the Fauna Dashboard.

      OAuth apps in Fauna

      You can delete the app to disable the integration.

To learn more about Fauna, refer to Fauna’s official documentation.