subiabre/doctrine-snowflakes

Custom id generator implementing snowflake algorithm

2.0 2023-11-13 19:04 UTC

This package is auto-updated.

Last update: 2024-04-23 22:36:28 UTC


README

ID Generator for Doctrine implementing the Snowflake algorithm.

Install

$ composer require subiabre/doctrine-snowflakes

Usage

In your entity classes:

#[ORM\Id()]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(class: SnowflakeGenerator::class)]
#[ORM\Column(type: Types::BIGINT]
private string $id;

Take in consideration that PHP does not have a proper bigint data type. Due to this limitation, doctrine-snowflake IDs should be used as string.

public function getId(): string

Testing

This package includes unit tests with the PHPUnit library. Run the tests with:

./vendor/bin/phpunit

The default test cases test against concurrency and uniqueness in 10 batches of 300 IDs each batch, alternatively you can supply any batch size with:

BATCH_SIZE=1000 ./vendor/bin/phpunit