Skip to content

Deploy to Cloudflare

Goal: a Worker with a D1 database and an R2 bucket, deployed with Wrangler.

  1. Declare the bindings in a wrangler.jsonc in the app root. Hamur merges it into .output/wrangler.json and keeps your entries. @hamur/db uses the D1 binding DB and @hamur/blob the R2 binding BLOB on this preset, and a binding missing from the file fails the build with a message that names it:
jsonc
{
  "name": "my-app",
  "d1_databases": [
    { "binding": "DB", "database_name": "my-app", "database_id": "<the id wrangler d1 create printed>" },
  ],
  "r2_buckets": [{ "binding": "BLOB", "bucket_name": "my-app-uploads" }],
}
  1. Create the database, build, set the app key as a secret, apply the migrations of server/db/migrations/ and deploy. Wrangler owns D1 migrations, so db.migrate is not used here. hamur deploy --prebuilt runs wrangler deploy --config .output/wrangler.json on what was just built:
sh
bunx wrangler d1 create my-app
hamur build --preset cloudflare
bunx wrangler secret put HAMUR_KEY --config .output/wrangler.json
bunx wrangler d1 migrations apply my-app --remote --config .output/wrangler.json
hamur deploy --prebuilt
  1. Mind two limits. Hashing a password costs CPU on Workers, and a Free plan cannot hash at the default count; see Passwords. The fs blob driver and the SQLite connectors do not run on Workers, which is why the sample uses D1 and R2.

See also: Cloudflare Workers, hamur deploy, A SQLite database.