Guides

Cloudflare Workers

Run City directly on Cloudflare Workers and D1 with @downcity/city, without waiting for a separate edge package.

If you want to deploy Downcity on Cloudflare Workers, the recommended path is not a separate edge package. Wire @downcity/city directly into the Workers runtime.

The public repository includes templates/edgefed as a developer starter. Downcity's private official Worker implementation lives outside the public repository.

What this path actually solves

The main difference from Node.js is not the HTTP entry. It is the runtime resource model:

  • the database usually comes from a binding such as env.DB
  • environment values are not just a local .env file
  • the request origin may need to be synchronized into services on every request
  • Worker isolates are reused, so runtime cache behavior must be managed explicitly

That is why this is better explained as a guide than as an empty npm package.

The minimum shape

Federation needs a D1 Database Adapter:

import { Federation } from "@downcity/federation";
import { Database } from "@downcity/database-d1";

export interface Env {
  DB: D1Database;
}

export default {
  async fetch(request: Request, env: Env) {
    const database = new Database({ binding: env.DB });
    const base = new Federation({ database });

    await base.health();
    return base.fetch(request);
  },
};

The adapter owns the D1 runtime differences:

  • only @downcity/database-d1 receives the D1 binding
  • the adapter combines Drizzle with snapshot conflict retries and atomic commits
  • City and services never receive the raw D1 binding
  • the current request origin can be synchronized before calls that need OAuth callback URLs

Use templates/edgefed as a starter

The starter in templates/edgefed/src/index.ts keeps only the boundaries required to connect Federation to a Worker:

  • wrap env.DB with @downcity/database-d1
  • reuse the Federation instance within a Worker isolate
  • delegate the HTTP request to Federation

Product services, models, and providers should be composed by the product and do not belong in the Edge runtime template.

Boundary with the Node path

Worker and Node use the same mental model: create the appropriate Database Adapter, then pass it to new Federation({ database }).

  • Node.js local projects use @downcity/database-sqlite
  • Node.js production deployments can use @downcity/database-postgresql
  • Workers / D1 use @downcity/database-d1

Next

  • If you still need the City mental model first, read City
  • If you need to manage provider keys, continue with Provider environment
  • For the minimal Worker integration, open templates/edgefed/src/index.ts