jeancodogno/doctrine-snowflake-id-bundle

Symfony bundle to automatically generate Snowflake IDs

v1.0.0 2025-04-25 01:47 UTC

This package is auto-updated.

Last update: 2025-06-16 02:19:49 UTC


README

Symfony bundle to automatically assign Snowflake-based IDs to your Doctrine entities and documents. Supports both primary keys and any other custom fields using attributes.

GitHub Workflow Status (main) Total Downloads Latest Version License

πŸš€ Features

βœ… Assigns unique Snowflake IDs to your entities

πŸ”„ Works for both primary keys and custom field using #[SnowflakeColumn] or #[SnowflakeField]

🧩 Seamlessly integrates with Doctrine ORM and Doctrine ODM

πŸ§ͺ Fully testable

πŸ“¦ Installation

Install via Composer:

composer require jeancodogno/doctrine-snowflake-id-bundle

The bundle uses autoconfiguration, no need to manually register it in bundles.php.

πŸ’‘ Usage

PHP does not have a native type that supports big integers, so the variable must be defined as a string.

πŸ” (Doctrine ORM) Using Snowflake ID Generator

Use the SnowflakeIdGenerator class with Doctrine ORM’s custom ID generation:

use JeanCodogno\DoctrineSnowflakeIdBundle\SnowflakeIdGenerator;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Product
{
    #[ORM\Id]
    #[ORM\Column(type: 'bigint')]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: SnowflakeIdGenerator::class)]
    private ?string $id = null;

    // ...
}

✳️ (Doctrine ORM) Assigning Snowflake ID to any column

Use the #[SnowflakeColumn] attribute to mark any non-ID field for automatic generation:

use JeanCodogno\DoctrineSnowflakeIdBundle\Attributes\SnowflakeColumn;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Product
{
    #[ORM\Column(type: 'bigint', unique: true)]
    #[SnowflakeColumn]
    private ?string $publicId = null;

    // ...
}

MongoDB logo (Doctrine ODM) Using Snowflake ID Generator

Use the MongoSnowflakeIdGenerator class with Doctrine ODM’s custom ID generation:

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use JeanCodogno\DoctrineSnowflakeIdBundle\IdGenerator\MongoSnowflakeIdGenerator;

#[ODM\Document(collection: 'products')]
class Product
{
    #[ODM\Id(strategy: 'CUSTOM', type: 'string', options: ['class' => MongoSnowflakeIdGenerator::class])]
    private ?string $id;

    // ...

MongoDB logo (Doctrine ODM) Assigning Snowflake ID to any field

use the #[SnowflakeColumn] attribute to marky any field for automatic generation:

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use JeanCodogno\DoctrineSnowflakeIdBundle\Attributes\SnowflakeField;

#[ODM\Document(collection: 'products')]
class Product
{
    #[SnowflakeField]
    private ?string $public_id = null;
    
    // ...

πŸ”§ Configuration

By default, DoctrineSnowflakeIdBundle works without any configuration, using default values for datacenterId, workerId, and startTimestamp.

If you want to customize these values, you can define the following parameters in your Symfony configuration:

#config/services.yaml

parameters:
    snowflake_id.datacenter_id: 2         # Default: 0
    snowflake_id.worker_id: 7             # Default: 0
    snowflake_id.start_timestamp: 1672531200000  # Optional – e.g., Jan 1, 2023 in milliseconds

πŸ§ͺ Testing

You can test ID assignment with tools like PHPUnit or Pest. Snowflake IDs are generated before persist, ensuring uniqueness without collisions.

πŸ“œ License

This bundle is open-source software licensed under the MIT license