承接 ntanduy/cloudflare-d1-database 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ntanduy/cloudflare-d1-database

最新稳定版本:v0.2.8

Composer 安装命令:

composer require ntanduy/cloudflare-d1-database

包简介

Easy configuration and setup for D1 Database connections in Laravel.

README 文档

README

Tests Packagist Dependency Version Latest Stable Version Total Downloads Monthly Downloads License

Integrate Cloudflare bindings into your PHP/Laravel application.

🎯 Requirements

  • PHP: >= 8.2
  • Laravel: 10.x, 11.x, or 12.x

✨ Features

This package offers support for:

🚀 Installation

composer require ntanduy/cloudflare-d1-database

👏 Usage

Integrate Cloudflare D1 with Laravel

Add a new connection in your config/database.php file:

'connections' => [
    'd1' => [
        'driver' => 'd1',
        'prefix' => '',
        'database' => env('CLOUDFLARE_D1_DATABASE_ID', ''),
        'api' => 'https://api.cloudflare.com/client/v4',
        'auth' => [
            'token' => env('CLOUDFLARE_TOKEN', ''),
            'account_id' => env('CLOUDFLARE_ACCOUNT_ID', ''),
        ],
        'timeout' => env('D1_TIMEOUT', 10),
        'connect_timeout' => env('D1_CONNECT_TIMEOUT', 5),
        'retries' => env('D1_RETRIES', 2),
        'retry_delay' => env('D1_RETRY_DELAY', 100),
    ],
]

Next, configure your Cloudflare credentials in the .env file:

CLOUDFLARE_TOKEN=your_api_token
CLOUDFLARE_ACCOUNT_ID=your_account_id
CLOUDFLARE_D1_DATABASE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Configuration Options

Option Default Description
timeout 10 Request timeout in seconds
connect_timeout 5 Connection timeout in seconds
retries 2 Max retry attempts on 5xx/429 errors
retry_delay 100 Base delay between retries (ms)

For production, you can tune these via .env:

D1_TIMEOUT=10
D1_CONNECT_TIMEOUT=5
D1_RETRIES=2
D1_RETRY_DELAY=100

The d1 driver will forward PDO queries to the Cloudflare D1 API to execute them.

⚠️ Limitations

  • No real transactions: D1 doesn't support BEGIN/COMMIT/ROLLBACK. The driver simulates transaction state for Laravel compatibility, but queries are executed immediately.
  • REST API latency: Each query is an HTTP request (~100-500ms). For low-latency needs, consider using Cloudflare Workers with native D1 bindings.

🌱 Testing

Start the built-in Worker to simulate the Cloudflare API:

cd tests/worker
npm ci
npm run start

In a separate terminal, run the tests:

vendor/bin/pest

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email contact@ntanduy.com instead of using the issue tracker.

🎉 Credits

统计信息

  • 总下载量: 4.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-22