A Cloudflare Workers application built with Hono that demonstrates D1 database's global read replication functionality for an e-commerce application.
- Click on the button below to deploy the application to Cloudflare Workers:
- Enable Read Replication:
# Enable Read replication
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id}" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"read_replication": {"mode": "auto"}}'
- RESTful API endpoints for product management
- D1 database integration with read replica support
- Session-based database operations
- Efficient product updates with partial modifications
- Node.js installed on your system
- Cloudflare account
- Clone the repository:
git clone https://github.com/harshil1712/e-com-d1-hono.git
- Install dependencies:
npm install
- Create a D1 database:
# Create a new D1 database
npx wrangler d1 create <DATABASE_NAME>
- Update
wrangler.jsonc
with the database binding:
- Enable Read replication:
# Enable Read replication
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id}" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"read_replication": {"mode": "auto"}}'
- Run the development server:
# Run the development server with remote D1
npm run dev
- Deploy to Cloudflare Workers:
# Deploy to Cloudflare Workers
npm run deploy
GET /api/products
- Retrieve all productsGET /api/products/:id
- Retrieve a specific productPOST /api/products
- Upsert a product
├── src/
│ └── index.ts # Main application code
├── public/
│ └── index.html # Product listing page
│ └── product-details.html # Product details page
├── package.json # Project dependencies
└── wrangler.jsonc # Cloudflare Workers configuration
- Hono - Fast, Lightweight, Web-standards Web Framework
- Cloudflare Workers - Serverless platform
- D1 Database - SQLite database at the edge
To update TypeScript types for Cloudflare bindings:
npm run cf-typegen
Contributions are welcome! Please open an issue or submit a pull request.
MIT License