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.
To set up an integration with Fauna:
-
You need to have an existing Fauna database to connect to. Create a Fauna database with demo data ↗.
-
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() -
Add the Fauna database integration to your Worker:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- In Overview, select your Worker.
- Select Integrations > Fauna.
- Follow the setup flow, selecting the database created in step 1.
-
In your Worker, install the
fauna
driver to connect to your database and start manipulating data:Terminal window npm install fauna -
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);}};
To learn more about Fauna, refer to Fauna’s official documentation ↗.