I was interested to learn more about the developer experience of Cloudflare’s D1 serverless SQL database offering. I started with this tutorial. Using wrangler you can scaffold a Worker and create a D1 database. The docs were straightforward up until the Write queries within your Worker section. For me, wrangler scaffolded a worker with a different structure than the docs discuss. I was able to progress through the rest of the tutorial by doing the following:

Add the following to worker-configuration.d.ts:

export interface Env {
    DB D1Database;
}

Modify worker.ts to make the SQL query:

        ...

        if (url.pathname.startsWith('/api/')) {
            if (url.pathname === "/api/beverages") {
                const { results } = await env.DB.prepare(
                  "SELECT * FROM Customers WHERE CompanyName = ?"
                )
                  .bind("Bs Beverages")
                  .all();
                return Response.json(results);
              }

            return apiRouter.handle(request);
        }

        ...

Run

wrangler dev --local

wrangler dev --local --persist yields an error for my version of wrangler:

❯ wrangler version
 ⛅️ wrangler 3.1.0

Validate the worker queries the database:

❯ curl http://127.0.0.1:8787/api/beverages | jq .
[
  {
    "CustomerId": 11,
    "CompanyName": "Bs Beverages",
    "ContactName": "Victoria Ashworth"
  },
  {
    "CustomerId": 13,
    "CompanyName": "Bs Beverages",
    "ContactName": "Random Name"
  }
]

I ran npm run deploy to start a Cloudflare working in production. The CLI outputted a URL when my worker was up and running. I tried curling the API route that accesses the database, but unfortunately got an error (“Error 1101”). I started streaming realtime logs from the Cloudflare dashboards for the working, the hit the same route again. The logs registered the exception but I wasn’t able to find much additional information:

{
    "exceptions": [
        {
        "name": "Error",
        "message": "D1_ERROR",
        "timestamp": 1687098711996
        }
    ],
    ...
}

Overall, it was easy to get things working locally and in production (for the worker at least). It’s not clear if D1 can be easily used outside the Cloudflare serverless ecosystem – for example, I’m not sure if I could connect to a D1 database from a Python app. Currently, D1 seems dependent on Cloudflare’s tooling and serverless platform. I stopped once I got stuck on the D1_ERROR.