renoki-co/laravel-thermite

Laravel Thermite是一个扩展的PostgreSQL Laravel数据库驱动,用于连接到CockroachDB集群。

1.0.0 2022-02-09 22:40 UTC

README

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Laravel Thermite是一个扩展的PostgreSQL Laravel数据库驱动,用于连接到CockroachDB集群。

🤝 支持

如果您在生产应用程序、演示、爱好项目、学校项目等中使用一个或多个Renoki Co.的开源软件包,请通过GitHub Sponsors赞助我们的工作。📦

🚀 安装

您可以通过composer安装此包

composer require renoki-co/laravel-thermite

🙌 使用

该驱动程序基于Postgres,Laravel的第一方Postgres驱动程序的大部分功能在CockroachDB中可用。

// config/database.php

return [
    // ...

    'connections' => [
        // ...

        'cockroachdb' => [
            'driver' => 'cockroachdb',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '26257'),
            'database' => env('DB_DATABASE', 'defaultdb'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

    ],
];

✨ 注意事项

主键不是自增的

Postgres支持自增键,但由于CockroachDB基于全局多主架构,自增可能会导致事务争用

因此,这个扩展驱动程序提供两个您可以在迁移中调用的函数,以生成高性能的唯一ID。这些方法的区别可以在这里找到。

->id()方法替换为使用gen_random_uuid()生成随机UUID作为主键,而不是自增主键。缺点是它不可排序,与uniqueRowId()相反。

use RenokiCo\LaravelThermite\Database\Blueprint;

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
});

class User extends Model
{
    // Make sure to set key type as string.
    protected $keyType = 'string';
}

使用uniqueRowId(),它使用由unique_rowid()生成的主键。这是高度可排序的,因为它是顺序生成的。唯一的缺点是插入时的节流,由一个节点限制。

use RenokiCo\LaravelThermite\Database\Blueprint;

Schema::create('users', function (Blueprint $table) {
    $table->uniqueRowId();
    $table->string('name');
});

class User extends Model
{
    // Do not set the $keyType to string, as it is an integer.
}

与主键关联的外键

为了表示其他表中(如传递关系字段)的主键约束,请考虑使用->uuid()

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
});

Schema::create('books', function (Blueprint $table) {
    $table->id();
    $table->uuid('user_id')->index();
    $table->string('name');
});

$book = $user->books()->create(['name' => 'The Great Gatsby']);

其他注意事项

由于基于Postgres,CockroachDB从其代码中借用了功能。当涉及模式功能和可能影响您实现的反模式时,请考虑了解CockroachDB-Postgres兼容性;有关CockroachDB特有的其他注意事项,请参阅

🐛 测试

vendor/bin/phpunit

🤝 贡献

有关详细信息,请参阅CONTRIBUTING

🔒 安全

如果您发现任何安全相关的问题,请通过alex@renoki.org发送电子邮件,而不是使用问题跟踪器。

🎉 致谢