承接 richan-fongdasen/turso-laravel 相关项目开发

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

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

richan-fongdasen/turso-laravel

Composer 安装命令:

composer require richan-fongdasen/turso-laravel

包简介

A Turso/LibSQL database driver for Laravel

README 文档

README

Latest Version on Packagist License: MIT PHPStan Unit Tests Code Style codecov Total Downloads

This package provides a Turso database driver for Laravel, allowing you to use Turso as your database backend in Laravel applications. The driver communicates with the Turso database server using an HTTP client.

You can find a demo application that uses this Turso database driver in the richan-fongdasen/pingcrm repository.

Requirements

  • PHP 8.2 or higher
  • Laravel 11.0 or higher
  • Node.js 18 or higher

Installation

You can install the package via Composer:

composer require richan-fongdasen/turso-laravel

To use Turso as your database driver in Laravel, append the following configuration to the connections array in your config/database.php file:

'turso' => [
    'driver'                  => 'turso',
    'db_url'                  => env('DB_URL', 'http://localhost:8080'),
    'access_token'            => env('DB_ACCESS_TOKEN'),
    'db_replica'              => env('DB_REPLICA'),
    'prefix'                  => env('DB_PREFIX', ''),
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    'sticky'                  => env('DB_STICKY', true),
],

Publishing Configuration and Sync Script

Publish the configuration file and sync script by running the following command:

php artisan vendor:publish --provider="RichanFongdasen\Turso\TursoLaravelServiceProvider"

The above command publishes the following files:

  • config/turso-laravel.php
  • turso-sync.mjs

The content of the config/turso-laravel.php file should look like this:

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,
    ],
];

You may need to set the NODE_PATH environment variable to the path of your Node.js executable. This is required to run the sync script.

Installing Node.js Dependencies

The Turso database driver requires Node.js to run the sync script. Install the Node.js dependencies by running the following command:

npm install @libsql/client

Configuration

In Laravel applications, the database driver configuration is stored in your .env file. Here are the available configurations for the Turso database driver:

DB_CONNECTION=turso
DB_URL=http://localhost:8080
DB_ACCESS_TOKEN=
DB_REPLICA=
DB_PREFIX=
DB_FOREIGN_KEYS=true
DB_STICKY=true
ENV Variable Name Description
DB_URL The Turso database server URL. E.g: https://[databaseName]-[organizationName].turso.io
DB_ACCESS_TOKEN (Optional) The access token to access the Turso database server.
DB_REPLICA (Optional) The full path to the local embedded replica database file. E.g: /tmp/turso.sqlite
DB_PREFIX (Optional) The database table prefix.
DB_FOREIGN_KEYS Enable or disable foreign key constraints, default is true.
DB_STICKY Enable or disable sticky connections while performing write operations, default is true.

Usage

For local development, you can use the local Turso database server provided by the Turso team. Refer to the Turso CLI documentation for instructions on running the local Turso database server.

The Turso database driver should work as expected with Laravel's Query Builder and Eloquent ORM. Here are some examples:

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();

Embedded Replica Support

The driver supports the embedded replica feature. If you're unfamiliar with this feature, refer to the Turso embedded replica article for more information.

Running the sync script from artisan command

Run the sync script manually using the following Artisan command:

php artisan turso:sync <connectionName?>

You may encounter an error if the path to the replica database does not exist. This is expected when the replica database has not been created yet.

Running the sync script programmatically

Run the sync script programmatically using the following code:

use Illuminate\Support\Facades\DB;
use RichanFongdasen\Turso\Facades\Turso;

if ( DB::hasModifiedRecords() ) {
    // Run the sync script immediately
    DB::sync();

    // Run the sync script in the background
    DB::backgroundSync();
}

// Sync on the specific connection
DB::connection('turso')->sync();
DB::connection('turso')->backgroundSync();

// Sync on all of the turso database connections
Turso::sync();
Turso::backgroundSync();

Debugging

To debug the HTTP requests and responses sent and received by the Turso database client, enable the debugging feature as follows:

// Enabling query log on default database connection
DB::enableQueryLog();

// Enabling query log on specific connection
DB::connection('turso')->enableQueryLog();

// Perform some queries
DB::table('users')->get();

// Get the query log for default database connection
DB::getQueryLog();

// Get the query log for specific connection
DB::connection('turso')->getQueryLog();

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

richan-fongdasen/turso-laravel 适用场景与选型建议

richan-fongdasen/turso-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.36k 次下载、GitHub Stars 达 108, 最近一次更新时间为 2024 年 04 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「turso」 「libsql」 「Richan Fongdasen」 「turso-laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 richan-fongdasen/turso-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 richan-fongdasen/turso-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 richan-fongdasen/turso-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 108
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 108
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-11