Download the PHP package ntanduy/cloudflare-d1-database without Composer

On this page you can find all versions of the php package ntanduy/cloudflare-d1-database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package cloudflare-d1-database

Cloudflare D1 Database Driver for Laravel

codecov Tests PHP Laravel Latest Stable Version Total Downloads Monthly Downloads License

Use Cloudflare D1 as a native Laravel database driver — full Eloquent ORM, Query Builder, and Migration support.

🎯 Requirements

✨ Features

🚀 Installation

👏 Usage

Step 1: Publish Configuration

This creates config/d1-database.php with all available options.

Step 2: Choose a Driver

This package supports two drivers to connect Laravel with Cloudflare D1:

Driver How it works Latency Setup
REST (default) Calls Cloudflare D1 REST API directly Higher (extra HTTP hop) API Token only
Worker Routes queries through your own Cloudflare Worker Lower (co-located with D1) Requires deploying a Worker

Driver 1: REST API (Default)

The simplest setup — no extra infrastructure needed. Queries are sent to Cloudflare's REST API.

Add to your .env:

How to get these values:

  1. API Token — Go to Cloudflare Dashboard → API Tokens → Create Token → use the "Edit Cloudflare D1" template
  2. Account ID — Found on your Cloudflare Dashboard overview page (right sidebar)
  3. Database ID — Go to Workers & Pages → D1 → click your database → copy the Database ID

That's it! Your Laravel app can now use D1.


Driver 2: Worker (Low Latency)

For production apps that need lower latency, deploy a Cloudflare Worker as a proxy between Laravel and D1.

Add to your .env:

Deploy the Worker

A ready-to-deploy Worker template is included in the Worker/ directory. To deploy:

Before deploying, update wrangler.jsonc with your D1 database binding:

Important: Set WORKER_SECRET using npx wrangler secret put WORKER_SECRET — never put secrets in wrangler.jsonc. This secret must match the CF_D1_WORKER_SECRET in your Laravel .env.

HMAC Request Signing (Optional)

For additional security, enable HMAC request signing. Each request gets a unique signature that prevents body tampering and provides per-isolate replay detection.

Laravel .env:

Worker (optional enforcement):

When enabled, the PHP driver adds three headers to every request:

The Worker verifies these when present, rejects expired timestamps, and tracks seen nonces to prevent replay within the same isolate. The per-request nonce ensures that two identical requests within the same second produce different signatures and are not falsely rejected as replays. Without HMAC_REQUIRED=true, unsigned requests still work (backward compatible).

Note: Replay detection is per-isolate — it resets on Worker cold starts and is separate per Cloudflare colo. For stricter guarantees, consider using Durable Objects for global nonce storage.

Worker Endpoints

The Worker exposes these endpoints:

Endpoint Method Auth Description
/health GET Health check
/query POST ✅ Bearer Execute a single SQL query
/batch POST ✅ Bearer Execute multiple statements atomically
/exec POST ✅ Bearer Execute raw DDL/migration SQL
/raw POST ✅ Bearer Execute a query and return raw array-of-arrays

Step 3: Set as Default Connection

To use D1 as the default database, add to your .env:

Step 4: Verify Connection

Run the built-in health check to verify your setup:

Step 5: Run Migrations

📖 Examples

Eloquent ORM

Query Builder

Query Logger

Monitor queries for debugging or performance analysis:

Runtime Driver Detection

Batch Queries

Execute multiple SQL statements in a single HTTP round-trip. On the Worker driver, this uses D1's native batch() for atomic execution.

If any statement fails, a D1BatchException is thrown with the index of the failing statement:

Driver Feature Matrix

Feature REST Worker
Bulk Insert
Sessions / Read Replication ❌ Not supported ✅ Full support
Auto Read/Write Splitting ❌ Not supported ✅ Full support
Import (d1:import) ✅ (via REST credentials)
Time Travel (d1:time-travel) ✅ (via REST credentials)
Schema Dump ✅ (via REST credentials)
Database Info (d1:info) ✅ Full metadata ✅ Query test + REST metadata
Batch Queries
Circuit Breaker
Automatic Retries

Bulk Insert

Insert multiple rows efficiently using D1 batch execution:

Sessions / Read Replication (Worker Driver Only)

D1 supports global read replication — read queries can be served by nearby replicas for lower latency. The Sessions API ensures sequential consistency across queries.

Important: Sessions are only available with the Worker driver. The REST API does not support D1 Sessions — this is a Cloudflare platform limitation.

Enable via Config

Add to your .env:

This automatically enables sessions for all queries on the Worker driver.

Enable Programmatically

Session Modes

Mode First Query Use When
first-unconstrained Any instance (primary or replica) Lowest latency, eventual consistency OK
first-primary Primary database Need the latest data for first query
<bookmark> At least as fresh as the bookmark Continuing from a previous session

How It Works

  1. PHP sends a session parameter with each query to the Worker
  2. Worker calls env.DB.withSession(param) to create a D1 session
  3. Worker returns a bookmark in the response
  4. PHP stores the bookmark and uses it for the next query
  5. This ensures sequential consistency across HTTP calls

Worker Template

The Worker template in Worker/ already includes session support. If you're upgrading from a previous version, redeploy the Worker:

Auto Read/Write Splitting (Worker Driver Only)

Automatically route SELECT queries to D1 replicas and INSERT/UPDATE/DELETE to the primary — zero code changes required.

Once configured, Laravel handles everything:

Database Info

Inspect your D1 database metadata and connection status:

Displays database name, UUID, size, table count, read replication mode, R/W splitting status, circuit breaker state, and runs a query test.

Uses the D1 REST API for metadata. Worker-only users see table count and query test but need REST credentials for full metadata.

Import

Import a SQL file into your D1 database:

The command handles the full import flow automatically:

  1. Computes MD5 hash and sends init request to get a presigned upload URL
  2. Uploads the SQL file to R2
  3. Triggers ingestion
  4. Polls until import is complete

Note: Like d1:schema-dump, the import command always uses the REST API. Worker-only users must also set CF_D1_API_TOKEN, CF_D1_ACCOUNT_ID, and CF_D1_DATABASE_ID in their .env.

Time Travel

D1 automatically creates restore points (bookmarks) for up to 30 days. Use d1:time-travel to get the current bookmark or restore your database to any point in time:

To restore the database to a previous state:

Warning: Restore is a destructive operation — it overwrites the database in place. In-flight queries will be cancelled. The command will prompt for confirmation before proceeding. The previous bookmark is shown after restore so you can undo if needed.

Schema Dump

Export your D1 database schema (and optionally data) as a SQL file:

This uses the D1 export REST API with polling mode. The dump is saved to database/schema/{connection}-schema.sql.

Options

Note: d1:schema-dump always uses the REST API for export, even when the Worker driver is your primary connection. Worker-only users must also set CF_D1_API_TOKEN, CF_D1_ACCOUNT_ID, and CF_D1_DATABASE_ID in their .env for the dump command to work.

Circuit Breaker

Prevents cascading failures when Cloudflare Workers experience cold starts or sustained outages. Instead of blocking for 30s+ on retries, the circuit breaker fails fast after consecutive failures.

States:

Enable in your config (config/database.php or config/d1-database.php):

Handle the exception:

Note: Use file or redis as the cache_driver. Avoid database to prevent a dependency loop when D1 itself is down.

Retry & Backoff

The driver automatically retries failed requests with exponential backoff and jitter:

Backoff formula: delay × 2^(attempt-1) + random jitter (0-100ms)

Attempt Base Delay (100ms)
1 ~100-200ms
2 ~200-300ms

⚙️ Configuration Reference

Manual Setup (Alternative)

Instead of publishing the config, you can add the connection directly to config/database.php:

Options Reference

Option Default Description
d1_driver rest Connection driver: rest (Cloudflare REST API) or worker (custom Worker)
database Your Cloudflare D1 Database ID
api https://api.cloudflare.com/client/v4 Cloudflare API base URL (REST driver only)
auth.token Cloudflare API Token (REST driver only)
auth.account_id Cloudflare Account ID (REST driver only)
worker_url Your Worker URL (Worker driver only)
worker_secret Shared secret for Worker auth (Worker driver only)
hmac false Enable HMAC request signing for body-tamper protection and replay detection (Worker driver only)
timeout 10 HTTP request timeout in seconds
connect_timeout 5 HTTP connection timeout in seconds
retries 2 Max retry attempts on 5xx/429 errors
retry_delay 100 Base delay between retries in milliseconds
transaction_mode silent How transaction APIs are handled: silent (no-op), log (warn once), exception (throw)
session.enabled false Enable D1 sessions for read replication (Worker driver only)
session.mode first-unconstrained Session mode: first-primary or first-unconstrained
read_write_splitting.enabled false Route SELECT to read replicas, writes to primary (Worker driver only)
read_write_splitting.sticky true After a write, route subsequent reads to write connector for consistency
read_write_splitting.read_mode first-unconstrained Session mode for the read connector
read_write_splitting.write_mode first-primary Session mode for the write connector
circuit_breaker.enabled false Enable circuit breaker for fail-fast behavior
circuit_breaker.threshold 5 Consecutive failures before opening the circuit
circuit_breaker.cooldown 30 Seconds before allowing a probe request
circuit_breaker.cache_driver file Laravel cache driver for circuit state (file, redis)

Environment Variables

⚠️ Limitations

🌱 Testing

PHP Tests

Worker Tests (Vitest)

Local Development with Worker

Start the built-in Worker to test against a local D1 instance:

🤝 Contributing

Contributions are welcome! Please open an issue or pull request on GitHub.

🔒 Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

🎉 Credits


All versions of cloudflare-d1-database with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
ext-pdo Version *
ext-pdo_sqlite Version *
guzzlehttp/psr7 Version ^2.4
illuminate/cache Version ^10.0|^11.0|^12.0|^13.0
illuminate/contracts Version ^10.0|^11.0|^12.0|^13.0
illuminate/database Version ^10.0|^11.0|^12.0|^13.0
illuminate/http Version ^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^10.0|^11.0|^12.0|^13.0
saloonphp/saloon Version ^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ntanduy/cloudflare-d1-database contains the following files

Loading the files please wait ...