Search...
ctrl/
Light
Dark
System
Sign in

Using Gel in Cloudflare Workers

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

It covers the following:

  • Setting up a new Cloudflare Worker project

  • Configuring Gel

  • Using Gel in a Cloudflare Worker

  • Deploying the Worker to Cloudflare

You can use this project as a reference: Gel 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 Gel Cloud for a managed service or run Gel locally.

Local Gel Setup (Optional for Gel Cloud Users)

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

Copy
$ 
gel project init

It creates an gel.toml (or deprecated edgedb.toml) config file and a schema file dbschema/default.gel.

It also spins up an Gel 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 gel in your terminal to open an interactive REPL to your instance.

Copy
$ 
gel

# or
Copy
$ 
npx gel

Install the Gel npm package

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

Extend The Default Schema (Optional)

You can extend the default schema, dbschema/default.gel, 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 Gel instance:

Copy
$ 
gel migration create
Copy
$ 
gel migrate

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

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

Copy
import * as gel from "gel";

export default {
  async fetch(
    _request: Request,
    env: Env,
    ctx: ExecutionContext,
  ): Promise<Response> {
    const client = gel.createHttpClient({
      tlsSecurity: "insecure",
      dsn: "<your-gel-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>;

Gel DSN

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

Copy
$ 
gel instance credentials --insecure-dsn

tlsSecurity

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

Client Setup with Gel Cloud

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

Copy
const client = gel.createHttpClient({
  instanceName: env.GEL_INSTANCE,
  secretKey: env.GEL_SECRET_KEY,
});

Environment variables

You can obtain GEL_INSTANCE and GEL_SECRET_KEY values from the Gel Cloud dashboard.

You will need to set the GEL_INSTANCE and GEL_SECRET_KEY environment variables in your Cloudflare Worker project.

Add the following to your wrangler.toml file:

Copy
[vars]
GEL_INSTANCE = "your-gel-instance"
GEL_SECRET_KEY = "your-gel-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 gel 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 Gel with Cloudflare Workers.

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

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