Skip to content

harshil1712/e-com-d1-hono

Repository files navigation

D1 Read Replica with Hono

A Cloudflare Workers application built with Hono that demonstrates D1 database's global read replication functionality for an e-commerce application.

Live Demo

Live Demo

Quickstart

  1. Click on the button below to deploy the application to Cloudflare Workers:

Deploy to Cloudflare

  1. 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"}}'

Features

  • RESTful API endpoints for product management
  • D1 database integration with read replica support
  • Session-based database operations
  • Efficient product updates with partial modifications

Prerequisites

  • Node.js installed on your system
  • Cloudflare account

Get Started

  1. Clone the repository:
git clone https://github.com/harshil1712/e-com-d1-hono.git
  1. Install dependencies:
npm install
  1. Create a D1 database:
# Create a new D1 database
npx wrangler d1 create <DATABASE_NAME>
  1. Update wrangler.jsonc with the database binding:
{
  "d1_databases": [
    {
      "binding": "DB",
      "database_name": "<DATABASE_NAME>",
      "database_id": "<DATABASE_ID>"
    }
  ]
}
  1. 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"}}'
  1. Run the development server:
# Run the development server with remote D1
npm run dev
  1. Deploy to Cloudflare Workers:
# Deploy to Cloudflare Workers
npm run deploy

API Endpoints

  • GET /api/products - Retrieve all products
  • GET /api/products/:id - Retrieve a specific product
  • POST /api/products - Upsert a product

Project Structure

├── 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

Technologies Used

Type Generation

To update TypeScript types for Cloudflare bindings:

npm run cf-typegen

Contribution

Contributions are welcome! Please open an issue or submit a pull request.

License

MIT License