This project now uses Cloudflare D1 (SQLite) database for persistent theme counter storage instead of external services.
# Create the database
npx wrangler d1 create flutter-theme-counter
# This will output something like:
# database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"Update the database_id in wrangler.toml with the actual ID from step 1:
[[d1_databases]]
binding = "THEME_DB"
database_name = "flutter-theme-counter"
database_id = "your-actual-database-id-here"# Apply the initial migration
npx wrangler d1 execute flutter-theme-counter --file=./migrations/0001_create_theme_counter.sql
# For local development
npx wrangler d1 execute flutter-theme-counter --local --file=./migrations/0001_create_theme_counter.sql# Query the database to verify setup
npx wrangler d1 execute flutter-theme-counter --command="SELECT * FROM theme_counter;"
# For local testing
npx wrangler d1 execute flutter-theme-counter --local --command="SELECT * FROM theme_counter;"The worker now provides these API endpoints:
GET /api/counter- Get current theme countPOST /api/counter/increment- Increment theme count
✅ Persistent Storage: Counter survives deployments ✅ Automatic Fallback: Works offline with intelligent estimation ✅ Robust Error Handling: Graceful degradation if database is unavailable ✅ CORS Support: Proper headers for cross-origin requests ✅ Transaction Safety: Atomic increment operations ✅ Auto-initialization: Database tables created automatically
CREATE TABLE theme_counter (
id INTEGER PRIMARY KEY AUTOINCREMENT,
counter_key TEXT UNIQUE NOT NULL,
count INTEGER NOT NULL DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);For local development with D1:
# Start local development with D1 database
npx wrangler dev --local
# Or using npm script
npm run dev:localThe database configuration is automatically deployed with:
npm run deployThe service automatically handles migration from the old countapi.xyz system:
- Maintains existing counter value as starting point (12,847)
- Provides fallback estimation during transition
- Backward compatible API for existing components
Your current theme counter database will NOT be affected when deploying new features because:
- Unique Database ID: Your database has a unique ID (
34a417cb-0a43-4202-8792-2328c894da49) - Safe SQL Operations: All migrations use
CREATE TABLE IF NOT EXISTSandINSERT OR IGNORE - No Data Overwriting: Existing counter values are preserved
When deploying new features like the roadmap:
# 1. First, backup your current counter (optional but recommended)
npx wrangler d1 execute flutter-theme-counter --command="SELECT * FROM theme_counter;"
# 2. Run any new migrations (safe operations only)
npx wrangler d1 execute flutter-theme-counter --file=./migrations/0002_roadmap_deployment.sql
# 3. Deploy the application
npm run deploy0001_create_theme_counter.sql- Initial database setup0002_roadmap_deployment.sql- Roadmap feature deployment (safe)
All migration files are designed to be idempotent - they can be run multiple times safely without affecting your existing data.
# Check current counter value
npx wrangler d1 execute flutter-theme-counter --command="SELECT * FROM theme_counter;"
# Check deployment history
npx wrangler d1 execute flutter-theme-counter --command="SELECT * FROM deployment_log ORDER BY deployed_at DESC;"