π D1 Post Management
β Add Post
bn n nbb n
πΎ Update
ποΈ Delete
# --- D1 Queries --- d1QueriesAdaptiveGroups( limit: 1000 filter: { date_geq: "${startDate.slice(0, 10)}", date_leq: "${endDate.slice(0, 10)}" } ) { sum { rowsRead rowsWritten rowsReturned queryDurationMs } count dimensions { datetimeMinute } }
πΎ Update
ποΈ Delete
Got it β β you want to extend this Express + Cloudflare API script so it also includes Cloudflare D1 (database) and Workers integration, so your app can both: Query Cloudflare R2 usage via GraphQL (as it already does), and Store / read / update data in Cloudflare D1 using the Workers API. Letβs go step-by-step. π‘ Overview Youβll have: D1 Database β for storing your appβs data (posts, logs, usage, etc.) Cloudflare Worker β acts as a backend endpoint that interacts with D1 (because D1 queries only run inside Workers) Express App β your local or hosted Node.js dashboard, which can call both: Cloudflare GraphQL API (for R2 usage) Cloudflare Worker API (for D1 database operations) βοΈ Step 1. Create a Cloudflare Worker for D1 In your Cloudflare dashboard, create a new Worker and bind your D1 database: wrangler.toml name = "d1-worker" main = "index.js" compatibility_date = "2025-11-10" [[d1_databases]] binding = "DB" database_name = "my_database" database_id = "xxxx-yyyy-zzzz" index.js (Worker code) export default { async fetch(request, env) { const db = env.DB; const url = new URL(request.url); const path = url.pathname; const method = request.method; if (path === "/posts" && method === "GET") { const { results } = await db.prepare("SELECT * FROM posts ORDER BY id DESC").all(); return Response.json(results); } if (path === "/posts" && method === "POST") { const data = await request.json(); if (!data.user_id || !data.title || !data.description) return new Response("Missing required fields", { status: 400 }); await db .prepare("INSERT INTO posts (user_id, title, description) VALUES (?, ?, ?)") .bind(data.user_id, data.title, data.description) .run(); return new Response("Post created"); } if (path.startsWith("/posts/") && method === "PUT") { const id = path.split("/")[2]; const data = await request.json(); await db .prepare("UPDATE posts SET title = ?, description = ? WHERE id = ?") .bind(data.title, data.description, id) .run(); return new Response("Post updated"); } if (path.startsWith("/posts/") && method === "DELETE") { const id = path.split("/")[2]; await db.prepare("DELETE FROM posts WHERE id = ?").bind(id).run(); return new Response("Post deleted"); } return new Response("Not Found", { status: 404 }); }, }; You can deploy this with: npx wrangler deploy After deployment, note your Worker URL β e.g.: https://d1-worker.yourname.workers.dev βοΈ Step 2. Connect Express App to D1 Worker Now, modify your Node.js app (server.js) to call that Worker. Add this helper: const D1_WORKER_URL = process.env.D1_WORKER_URL; // e.g. https://d1-worker.yourname.workers.dev // Proxy requests to D1 Worker async function d1Request(path, method = "GET", body = null) { const options = { method, headers: { "Content-Type": "application/json" }, }; if (body) options.body = JSON.stringify(body); const res = await fetch(`${D1_WORKER_URL}${path}`, options); if (!res.ok) throw new Error(`D1 Worker error: ${res.status}`); const text = await res.text(); try { return JSON.parse(text); } catch { return text; } } Example Express endpoints to interact with D1 app.get("/api/posts", async (req, res) => { try { const posts = await d1Request("/posts"); res.json(posts); } catch (e) { res.status(500).json({ error: e.message }); } }); app.post("/api/posts", express.json(), async (req, res) => { try { const result = await d1Request("/posts", "POST", req.body); res.send(result); } catch (e) { res.status(500).json({ error: e.message }); } }); app.put("/api/posts/:id", express.json(), async (req, res) => { try { const result = await d1Request(`/posts/${req.params.id}`, "PUT", req.body); res.send(result); } catch (e) { res.status(500).json({ error: e.message }); } }); app.delete("/api/posts/:id", async (req, res) => { try { const result = await d1Request(`/posts/${req.params.id}`, "DELETE"); res.send(result); } catch (e) { res.status(500).json({ error: e.message }); } }); β Step 3. .env file example CLOUDFLARE_API_TOKEN=your_api_token CLOUDFLARE_ACCOUNT=your_account_id D1_WORKER_URL=https://d1-worker.yourname.workers.dev PORT=3000 β Step 4. Create your D1 table (run once) In Cloudflare dashboard β D1 β Console: CREATE TABLE posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT NOT NULL, title TEXT NOT NULL, description TEXT NOT NULL, UNIQUE(user_id, title) ); β Step 5. Run locally node server.js Now you can visit: http://localhost:3000/api/posts β list all posts POST /api/posts β add a post (proxy to D1 Worker) GET /api/r2-usage β view Cloudflare R2 usage Would you like me to make this single-page dashboard (HTML + JS) that lets you view, add, edit, and delete posts (using /api/posts) β similar to your earlier D1 demo?
πΎ Update
ποΈ Delete
desc
πΎ Update
ποΈ Delete