xlited/laravel-d1

Laravel integration for Cloudflare D1.
1,053 1
Install
composer require xlited/laravel-d1
Latest Version:1.0.0
Laravel:10 · 11
License:Apache-2.0
Last Updated:Aug 7, 2024
Links: GitHub  ·  Packagist
Maintainer: icaliman

Cloudflare D1 bindings for Laravel

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Extend your PHP/Laravel application with Cloudflare D1 bindings.

🚀 Installation

You can install the package via Composer:

composer require xlited/laravel-d1

🙌 Usage

D1 with raw PDO

Though D1 is not connectable via SQL protocols, it can be used as a PDO driver via the package connector. This proxies the query and bindings to the D1's /query endpoint in the Cloudflare API.

use XliteDev\LaravelD1\D1\D1Pdo;
use XliteDev\LaravelD1\D1\D1PdoStatement;
use XliteDev\LaravelD1\CloudflareD1Connector;

$pdo = new D1Pdo(
    dsn: 'sqlite::memory:', // irrelevant
    connector: new CloudflareD1Connector(
        database: 'your_database_id',
        token: 'your_api_token',
        accountId: 'your_cf_account_id',
    ),
);

D1 with Laravel

In your config/database.php file, add a new connection:

'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', ''),
        ],
    ],
]

Then in your .env file, set up your Cloudflare credentials:

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

The d1 driver will proxy the PDO queries to the Cloudflare D1 API to run queries.

🐛 Testing

Start the built-in Worker that simulates the Cloudflare API:

cd tests/worker
npm ci
npm run start

In a separate terminal, run the tests:

vendor/bin/phpunit

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.

🎉 Credits

Related Packages

ntanduy/cloudflare-d1-database

Cloudflare D1 database driver for Laravel — full Eloquent & Query Builder suppor...

9,814 28
erimeilis/laravel-cloudflare-d1

Production-ready Cloudflare D1 database driver for Laravel with full Eloquent su...

2,490 14
aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can b...

3,339,583 456

Version History

Version Released Laravel License
1.0.0 ^10.23|^11.0 Apache-2.0
0.1.0 ^10.23 Apache-2.0