项目作者: verbanent

项目描述 :
Ordered binary UUID in Laravel / Eloquent based on UUID version 1
高级语言: PHP
项目地址: git://github.com/verbanent/eloquent-binary-uuid.git
创建时间: 2020-08-07T12:42:01Z
项目社区:https://github.com/verbanent/eloquent-binary-uuid

开源协议:MIT License

下载


Ordered binary UUID in Laravel / Eloquent

Quality Gate Status
Downloads
StyleCI
CodeFactor
Maintainability Rating
Lines of Code
Coverage
Packagist PHP Version Support
Packagist License

Based on articles about the optimization of UUID storage in databases, I decided to write a simple library that allows this in my projects. I based on the information available here:
https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/
https://www.percona.com/community-blog/2018/10/12/generating-identifiers-auto_increment-sequence/

The package currently only supports MySQL.

Installation

Please install the package via Composer:

  1. composer require verbanent/eloquent-binary-uuid

Migration

Your model will use an ordered binary UUID, if you prepare a migration:

  1. Schema::create('table_name', function (Blueprint $table) {
  2. $table->uuid('id');
  3. });

Or if you want to use a custom column name for the primary key:

  1. Schema::create('table_name', function (Blueprint $table) {
  2. $table->uuid('uuid');
  3. $table->primary('uuid');
  4. });

Using UUID in models

All what you have to do, is use a new trait in models with UUID as a primary key:

  1. use Illuminate\Database\Eloquent\Model;
  2. use Verbanent\Uuid\Traits\BinaryUuidSupportableTrait;
  3. class Book extends Model
  4. {
  5. use BinaryUuidSupportableTrait;
  6. }

The above example works for the column id. If you use custom name for UUID column, you need to define it:

  1. use Illuminate\Database\Eloquent\Model;
  2. use Verbanent\Uuid\Traits\BinaryUuidSupportableTrait;
  3. class Book extends Model
  4. {
  5. use BinaryUuidSupportableTrait;
  6. public $uuidColumn = 'uuid';
  7. }

Abstract model for model with UUID

For your convenience you can extend your model with AbstractModel:

  1. use Verbanent\Uuid\AbstractModel;
  2. class Lang extends AbstractModel
  3. {
  4. //
  5. }

The above example works for the column id. If you use custom name for UUID column, you need to define it:

  1. use Verbanent\Uuid\AbstractModel;
  2. class Lang extends AbstractModel
  3. {
  4. public $uuidColumn = 'uuid';
  5. protected $primaryKey = 'uuid';
  6. protected $fillable = ['uuid'];
  7. }

Foreign binary UUID

If you would like to use UUID as a foreign key, use another trait and set $uuidable property for this model:

  1. use Verbanent\Uuid\AbstractModel;
  2. use Verbanent\Uuid\Traits\ForeignBinaryUuidSupportableTrait;
  3. class LangTranslation extends AbstractModel
  4. {
  5. use ForeignBinaryUuidSupportableTrait;
  6. private $uuidable = [
  7. 'lang',
  8. 'one_lang_bucket',
  9. ];
  10. }

Getting a string form of UUID

The library is kept as simple as possible, so if you want to get a string form of UUID, just use a method:

  1. $book = new \App\Book;
  2. $book->save();
  3. dd($book->uuid());
  4. // Output: "11e947f9-a1bd-f844-88d8-6030d483c5fe"

or use a property, if you need a binary value:

  1. # If you use the default primary key:
  2. dd($book->id);
  3. // Output: b"\x11éGù¡½øDˆØ`0ÔƒÅþ"
  4. # If you use `uuid` as a primary key:
  5. dd($book->uuid);
  6. // Output: b"\x11éGù¡½øDˆØ`0ÔƒÅþ"

Finding by primary UUID

For primary keys finding rows is simple and always return a model:

  1. $lang = Lang::find('11e947f9-a1bd-f844-88d8-6030d483c5fe');
  2. dd($lang->uuid());
  3. // Output: "11e947f9-a1bd-f844-88d8-6030d483c5fe"

Finding by foreign UUID

For foreign keys finding rows requires a column name and returns collection of model:

  1. $langTranslation = LangTranslation::findByUuid('lang', '11e947f9-a1bd-f844-88d8-6030d483c5fe');
  2. dd($langTranslation[0]->uuid(), $langTranslation[1]->uuid(), $langTranslation[2]->uuid());
  3. // Output: "11e94805-b94c-68e0-8720-6030d483c5fe"
  4. // "11e94805-b955-4e2e-b089-6030d483c5fe"
  5. // "11e94805-b957-af02-8bf8-6030d483c5fe"

Getting a foreign UUID string

You can print string form of your foreign UUID keys:

  1. $translation = LangTranslation::findByUuid('lang', '11e947f9-a1bd-f844-88d8-6030d483c5fe')->first();
  2. dd($translation->foreignUuid('lang'));
  3. // Output: "11e947f9-a1bd-f844-88d8-6030d483c5fe"

Because trying to have access to the property directly will print binary form of UUID:

  1. dd($translation->lang);
  2. // Output: b"\x11éGù¡½øDˆØ`0ÔƒÅþ"

Unit tests

Run this command if you want to check unit tests:

  1. ./vendor/bin/phpunit

Or if you want to check code coverage:

  1. phpdbg -qrr vendor/bin/phpunit --coverage-html coverage tests