D1Client
A lightweight Cloudflare D1 HTTP client for Luau.
Features
- Connects to any Cloudflare D1 database via the REST API
- Promise-style initialization chain (
.andThen/.catch) - Generic, fully-typed query results
- Structured error handling: no bare
pcallboilerplate in your code
Installation
bash
embr add mayari/d1client
embr iExample
lua
local d1 = require("d1client")
local db = d1.new(ACCOUNT_ID, DATABASE_ID, API_TOKEN)
:andThen(function()
print("Connected to D1!")
end)
:catch(function(err)
error(err)
end)
-- run a query
local res = db:query([[
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
email TEXT UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
]])
if not res.success then
for _, err in res.errors do
error(`[{err.code}]: {err.message}`)
end
end