TinyWebDB deployment for Cloudflare Workers with D1 (SQLite) storage.
- SQL database at the edge: Full SQLite database capabilities
- Strong consistency: No eventual consistency delays (unlike KV)
- ACID transactions: Reliable data integrity
- SQL queries: Rich querying capabilities
- Global replication: Automatic replication across Cloudflare's network
- Free tier: 5 million reads/day and 100,000 writes/day
Choose D1 when:
- You need strong consistency
- You want SQL querying capabilities
- You need transactions
- You have relational data
Choose KV when:
- You need the absolute lowest latency (sub-millisecond reads)
- You're okay with eventual consistency
- Simple key-value operations are sufficient
- A Cloudflare account (free tier works!)
- Wrangler CLI installed:
npm install -g wrangler
wrangler logincd packages/cloudflare-d1
npm run db:createThis will output:
✅ Successfully created DB 'tinywebdb'
[[d1_databases]]
binding = "TINYWEBDB_D1"
database_name = "tinywebdb"
database_id = "abc123-def456-ghi789"
Copy the database_id from the output and update wrangler.toml:
[[d1_databases]]
binding = "TINYWEBDB_D1"
database_name = "tinywebdb"
database_id = "YOUR_D1_DATABASE_ID" # Replace with your actual IDnpm run db:initThis creates the stored_data table in your D1 database.
npm run deployYour TinyWebDB service will be deployed to:
https://tinywebdb-d1.YOUR_SUBDOMAIN.workers.dev
# First, initialize local database
npm run db:init:local
# Then start dev server
npm run devThis starts a local development server with a local D1 database.
npm run build# Query remote database
wrangler d1 execute tinywebdb --command "SELECT * FROM stored_data LIMIT 10"
# Query local database
wrangler d1 execute tinywebdb --local --command "SELECT COUNT(*) FROM stored_data"To modify the schema:
- Create a new SQL file with your changes
- Execute it against your database:
wrangler d1 execute tinywebdb --file=./migrations/001_add_column.sql
# Export data
wrangler d1 export tinywebdb --output=backup.sql
# Restore data
wrangler d1 execute tinywebdb --file=backup.sqlOnce deployed, your service will have these endpoints:
curl -X POST https://tinywebdb-d1.YOUR_SUBDOMAIN.workers.dev/storeavalue \
-H "Content-Type: application/json" \
-d '{"tag":"mykey","value":"myvalue"}'curl -X POST https://tinywebdb-d1.YOUR_SUBDOMAIN.workers.dev/getvalue \
-H "Content-Type: application/json" \
-d '{"tag":"mykey"}'curl -X POST https://tinywebdb-d1.YOUR_SUBDOMAIN.workers.dev/deleteentry \
-H "Content-Type: application/json" \
-d '{"tag":"mykey"}'[env.production]
name = "tinywebdb-d1-production"
[[env.production.d1_databases]]
binding = "TINYWEBDB_D1"
database_name = "tinywebdb-production"
database_id = "YOUR_PRODUCTION_D1_ID"
[env.staging]
name = "tinywebdb-d1-staging"
[[env.staging.d1_databases]]
binding = "TINYWEBDB_D1"
database_name = "tinywebdb-staging"
database_id = "YOUR_STAGING_D1_ID"Deploy to a specific environment:
wrangler deploy --env productionCloudflare D1 pricing (as of 2024):
Free tier (Workers Free plan):
- 5 million read queries/day
- 100,000 write queries/day
- 5 GB storage
Paid plans (Workers Paid - $5/month):
- First 25 billion reads free, then $0.001 per million
- First 50 million writes free, then $1.00 per million
- First 5 GB storage free, then $0.75 per GB-month
See Cloudflare D1 pricing for details.
- Database size limit: 10 GB per database (contact Cloudflare for higher limits)
- Query timeout: 30 seconds per query
- Max databases: 50 per account (free), 500 per account (paid)
- SQL features: Most SQLite features supported, some limitations on certain functions
| Storage | Read Latency | Write Latency | Consistency |
|---|---|---|---|
| KV | <1ms | 1-60s propagation | Eventual |
| D1 | ~5ms | Immediate | Strong |
Make sure you've created the D1 database and updated the ID in wrangler.toml.
The schema is already initialized. Skip the db:init step.
Run npm run db:init to initialize the schema.
Ensure you're running migrations against the correct database (local vs remote).
MIT