jetcod / eloquent-keygen
A Laravel package providing seamless integration with Snowflake ID generation for Eloquent models
Installs: 1 223
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
- godruoyi/php-snowflake: ^2.0|^3.0
Requires (Dev)
- fakerphp/faker: ^1.23
- friendsofphp/php-cs-fixer: ^3.49
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0|^10.0|^11.0
This package is not auto-updated.
Last update: 2024-12-24 07:43:49 UTC
README
This is a Laravel package providing seamless integration with Snowflake ID generation for Eloquent models. Simplify primary key management with automatic generation using a highly distributed unique identifier system. Enhance your application's scalability and efficiency with this easy-to-use package for Laravel Eloquent models.
Installation
To install jetcod/deloquent-keygen
, you can use Composer, the dependency manager for PHP. Run the following command in your terminal:
composer require jetcod/eloquent-keygen
Then you need to publish the config file:
php artisan vendor:publish --provider="eloquent-key-generator-config"
How to use?
To use the package, follow these steps:
- Extend Your Model:
Instead of extending Illuminate\Database\Eloquent\Model, extend Jetcod\Eloquent\Model in your Eloquent model. This will enable automatic ID generation using the Snowflake algorithm.
<?php namespace App\Models; use Jetcod\Eloquent\Model as EloquentModel; class YourModel extends EloquentModel { // Your model code here }
- Using Trait:
If you don't want to extend the Eloquent model, or you want to apply the Snowflake ID generation to one model, you can use the Snowflake trait in your model class.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model as EloquentModel; use Jetcod\Eloquent\Traits\Snowflake; class YourModel extends EloquentModel { use Snowflake; }
- Disable Snowflake Generation for a Model:
If you want to disable the Snowflake ID generation for a particular model, you can add a snowflake method to that model returning false.
<?php namespace App\Models; use Jetcod\Eloquent\Model as EloquentModel; class AnotherModel extends EloquentModel { protected function snowflake(): bool { return false; } // Your model code here }
The snowflake method returns a boolean value indicating whether the Snowflake ID generation should be enabled or disabled for that model.
- Configuration for distributed system:
The package uses the following configuration to generate the distributed unique identifier:
<?php use Godruoyi\Snowflake\LaravelSequenceResolver; return [ 'attributes' => [ /* |------------------------------------------------------------------ | This represents the datacenter id used to generate snowflake ids. |------------------------------------------------------------------ */ 'datacenter' => env('SNOWFLAKE_DATACENTER_ID', 1), /* |-------------------------------------------------------------- | This represents the worker id used to generate snowflake ids. |-------------------------------------------------------------- */ 'worker' => env('SNOWFLAKE_WORKER_ID', 1), /* |-------------------------------------------------------------------------------------------------------- | This represents the sequence resolver class. It is to ensure that sequence-number generated in the same | millisecond of the same node is unique. | | Avaialable resolvers: | 1. Godruoyi\Snowflake\RandomSequenceResolver | 2. Godruoyi\Snowflake\FileLockResolver | 3. Godruoyi\Snowflake\LaravelSequenceResolver |-------------------------------------------------------------------------------------------------------- */ 'sequence_resolver' => env('SNOWFLAKE_SEQUENCE_RESOLVER', LaravelSequenceResolver::class), 'file_lock_directory' => env('SNOWFLAKE_File_LOCK_DIRECTORY', null), // Default is null, means use <app_path>/storage/snowflake directory. ], ];
License
This project is licensed under the MIT License - see the LICENSE file for details.