Search
ctrl/
Ask AI
Light
Dark
System

Using EdgeDB in Cloudflare Workers

This guide demonstrates how to integrate EdgeDB with Cloudflare Workers to build serverless applications that can interact with EdgeDB.

It covers the following:

  • Setting up a new Cloudflare Worker project

  • Configuring EdgeDB

  • Using EdgeDB in a Cloudflare Worker

  • Deploying the Worker to Cloudflare

You can use this project as a reference: EdgeDB Cloudflare Workers Example.

Sign up for a Cloudflare account to later deploy your worker.

Ensure you have the following installed:

Use the create-cloudflare package to create a new Cloudflare Worker project.

Copy
$ 
npm create cloudflare@latest # or pnpm, yarn, bun

# or
Copy
$ 
npx create-cloudflare@latest

Answer the prompts to create a new project. Pick the “Hello World” Worker template to get started.

You’ll be asked if you want to put your project on Cloudflare. If you say yes, you’ll need to sign in (if you haven’t already). If you don’t want to deploy right away, switch to the project folder you just made to start writing your code. When you’re ready to deploy your project on Cloudflare, you can run npx wrangler deploy to push it.

Using Wrangler CLI

If you prefer using Wrangler to set up your worker, you can use the wrangler generate command to create a new project.

You can use EdgeDB Cloud for a managed service or run EdgeDB locally.

Local EdgeDB Setup (Optional for EdgeDB Cloud Users)

If you’re running EdgeDB locally, you can use the following command to create a new instance:

Copy
$ 
edgedb project init

It creates an edgedb.toml config file and a schema file dbschema/default.esdl.

It also spins up an EdgeDB instance and associates it with the current directory. As long as you’re inside the project directory, all CLI commands will be executed against this instance.

You can run edgedb in your terminal to open an interactive REPL to your instance.

Copy
$ 
edgedb

Install the EdgeDB npm package

Copy
$ 
npm install edgedb # or pnpm, yarn, bun

Extend The Default Schema (Optional)

You can extend the default schema, dbschema/default.esdl, to define your data model, and then try it out in the Cloudflare Worker code.

Add new types to the schema file:

Copy
module default {
  type Movie {
    required title: str {
      constraint exclusive;
    };
    multi actors: Person;
  }

  type Person {
    required name: str;
  }
}

Then apply the schema schema to your EdgeDB instance:

Copy
$ 
edgedb migration create
Copy
$ 
edgedb migrate

Open the index.ts file from the src directory in your project, and remove the default code.

To interact with your local EdgeDB instance, use the following code:

Copy
import * as edgedb from "edgedb";

export default {
  async fetch(
    _request: Request,
    env: Env,
    ctx: ExecutionContext,
  ): Promise<Response> {
    const client = edgedb.createHttpClient({
      tlsSecurity: "insecure",
      dsn: "<your-edgedb-dsn>",
    });
    const movies = await client.query(`select Movie { title }`);
    return new Response(JSON.stringify(movies, null, 2), {
      headers: {
        "content-type": "application/json;charset=UTF-8",
      },
    });
  },
} satisfies ExportedHandler<Env>;

EdgeDB DSN

Replace <your-edgedb-dsn> with your EdgeDB DSN. You can obtain your EdgeDB DSN from the command line by running:

Copy
$ 
edgedb instance credentials --insecure-dsn

tlsSecurity

The tlsSecurity option is set to insecure to allow connections to a local EdgeDB instance. This lets you test your Cloudflare Worker locally. Don’t use this option in production.

Client Setup with EdgeDB Cloud

If you’re using EdgeDB Cloud, you can instead use the following code to set up the client:

Copy
const client = edgedb.createHttpClient({
  instanceName: env.EDGEDB_INSTANCE,
  secretKey: env.EDGEDB_SECRET_KEY,
});

Environment variables

You can obtain EDGEDB_INSTANCE and EDGEDB_SECRET_KEY values from the EdgeDB Cloud dashboard.

You will need to set the EDGEDB_INSTANCE and EDGEDB_SECRET environment variables in your Cloudflare Worker project.

Add the following to your wrangler.toml file:

Copy
[vars]
EDGEDB_INSTANCE = "your-edgedb-instance"
EDGEDB_SECRET_KEY = "your-edgedb-secret-key"

Next, you can run wrangler types to generate the types for your environment variables.

Running the Worker

Adding polyfills for Node.js

The edgedb package currently uses Node.js built-in modules that are not available in the Cloudflare Worker environment. You have to add the following line to your wrangler.toml file to include the polyfills:

Copy
node_compat = true

To run the worker locally, use the following command:

Copy
$ 
npm run dev # or pnpm, yarn, bun

This will start a local server at http://localhost:8787. Run curl http://localhost:8787 to see the response.

Deploying the Worker to Cloudflare

To deploy the worker to Cloudflare, use the following command:

Copy
$ 
npm run deploy # or pnpm, yarn, bun

This will deploy the worker to Cloudflare and provide you with a URL to access your worker.

Congratulations! You have successfully integrated EdgeDB with Cloudflare Workers.

Here’s a minimal starter project that you can use as a reference: EdgeDB Cloudflare Workers Example.

Check out the Cloudflare Workers documentation for more information and to learn about the various features and capabilities of Cloudflare Workers.