richan-fongdasen / turso-laravel
为 Laravel 提供的 Turso/LibSQL 数据库驱动程序
Requires
- php: ^8.2
- illuminate/bus: ^11.0
- illuminate/console: ^11.0
- illuminate/contracts: ^11.0
- illuminate/database: ^11.0
- illuminate/filesystem: ^11.0
- illuminate/http: ^11.0
- illuminate/queue: ^11.0
- illuminate/support: ^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
README
此包为 Laravel 提供了 Turso 数据库驱动程序,允许您在 Laravel 应用程序中使用 Turso 作为数据库后端。该驱动程序使用 HTTP 客户端与 Turso 数据库服务器进行通信。
您可以在 richan-fongdasen/pingcrm 存储库中找到一个使用此 Turso 数据库驱动程序的演示应用程序。
要求
- PHP 8.2 或更高版本
- Laravel 11.0 或更高版本
- Node.js 16 或更高版本
安装
您可以通过 Composer 安装此包
composer require richan-fongdasen/turso-laravel
要在 Laravel 中使用 Turso 作为数据库驱动程序,请将以下配置追加到您的 config/database.php
文件中的 connections
数组
'turso' => [ 'driver' => 'turso', 'db_url' => env('DB_URL', 'http://localhost:8080'), 'access_token' => env('DB_ACCESS_TOKEN'), 'db_replica' => env('DB_REPLICA'), 'database' => null, // Leave this null 'prefix' => env('DB_PREFIX', ''), 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), 'sticky' => env('DB_STICKY', true), ],
发布配置和同步脚本
通过运行以下命令发布配置文件和同步脚本
php artisan vendor:publish --provider="RichanFongdasen\Turso\TursoLaravelServiceProvider"
上述命令发布了以下文件
config/turso-laravel.php
turso-sync.mjs
config/turso-laravel.php
文件的内容应如下所示
return [ 'client' => [ 'connect_timeout' => env('TURSO_CONNECT_TIMEOUT', 2), 'timeout' => env('TURSO_REQUEST_TIMEOUT', 5), ], 'sync_command' => [ 'node_path' => env('NODE_PATH'), // Full path to the node executable. E.g: /usr/bin/node 'script_filename' => 'turso-sync.mjs', 'script_path' => realpath(__DIR__ . '/..'), 'timeout' => 60, ], ];
您可能需要将 NODE_PATH
环境变量设置为 Node.js 可执行文件的路径。这是运行同步脚本所必需的。
安装 Node.js 依赖项
Turso 数据库驱动程序需要 Node.js 来运行同步脚本。通过运行以下命令安装 Node.js 依赖项
npm install @libsql/client
配置
在 Laravel 应用程序中,数据库驱动程序配置存储在您的 .env
文件中。以下是 Turso 数据库驱动程序的可用配置
DB_CONNECTION=turso DB_URL=http://localhost:8080 DB_ACCESS_TOKEN= DB_REPLICA= DB_PREFIX= DB_FOREIGN_KEYS=true DB_STICKY=true
用法
对于本地开发,您可以使用 Turso 团队提供的本地 Turso 数据库服务器。有关运行本地 Turso 数据库服务器的说明,请参阅Turso CLI 文档。
Turso 数据库驱动程序应与 Laravel 的 Query Builder 和 Eloquent ORM 正常工作。以下是一些示例
use App\Models\User; use Illuminate\Support\Facades\DB; // Using Query Builder $users = DB::table('users')->orderBy('name')->get(); // Using Eloquent ORM $users = User::with('posts')->orderBy('name')->get();
嵌入式副本支持
该驱动程序支持嵌入式副本功能。如果您不熟悉此功能,请参阅Turso 嵌入式副本文章以获取更多信息。
从 artisan 命令运行同步脚本
使用以下 Artisan 命令手动运行同步脚本
php artisan turso:sync
如果副本数据库的路径不存在,您可能会遇到错误。当副本数据库尚未创建时,这是预期的。
以编程方式运行同步脚本
使用以下代码以编程方式运行同步脚本
use Illuminate\Support\Facades\DB; use RichanFongdasen\Turso\Facades\Turso; if ( DB::hasModifiedRecords() ) { // Run the sync script immediately Turso::sync(); // Run the sync script in the background Turso::backgroundSync(); }
调试
要调试 Turso 数据库客户端发送和接收的 HTTP 请求和响应,请按照以下方式启用调试功能
Turso::enableQueryLog(); DB::table('users')->get(); // Get the query log $logs = Turso::getQueryLog();
更新日志
有关最近更改的更多信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
请参阅我们的安全策略了解如何报告安全漏洞。
致谢
许可证
MIT许可证(MIT)。更多信息请参阅许可证文件。