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.
Prerequisites
Sign up for a Cloudflare account to later deploy your worker.
Ensure you have the following installed:
Setup and configuration
Initialize a New Cloudflare Worker Project
Use the create-cloudflare package to create a new Cloudflare Worker project.
$
npm create cloudflare@latest # or pnpm, yarn, bun
# or
$
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.
Configure Gel
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:
$
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.
$
gel
# or
$
npx gel
Install the Gel npm package
$
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:
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:
$
gel migration create
$
gel migrate
Using Gel in a Cloudflare Worker
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:
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:
$
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:
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:
[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:
node_compat = true
To run the worker locally, use the following command:
$
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:
$
npm run deploy # or pnpm, yarn, bun
This will deploy the worker to Cloudflare and provide you with a URL to access your worker.
Wrapping up
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.