glhd/bits
Generate unique 64-bit IDs in PHP for distributed systems. Create Twitter Snowflake, Sonyflake, or custom bit-sequence identifiers. Configure worker/datacenter IDs and a custom epoch to avoid collisions across servers.
Strengths:
WHERE id > last_seen_id) without secondary created_at columns, reducing storage and index overhead.HasSnowflakes trait), Livewire, and Query Builder (Expression interface) reduces boilerplate.id as Snowflake, external_id as Uuid).Weaknesses:
HasSnowflakes trait. Supports casting existing integer columns to Snowflake objects.Expression, enabling native SQL queries (e.g., WHERE id > ?).setTestNow() avoids conflicts with Carbon’s time manipulation.BIGINT (64-bit integers). No schema migrations required for new projects.Critical Risks:
BITS_WORKER_ID/BITS_DATACENTER_ID across servers will cause duplicate IDs. Mitigate via:
CacheSequenceResolver (included) for distributed sequence management.Medium Risks:
Number.MAX_SAFE_INTEGER. Solution: Always cast to string for JS interop.ALTER TABLE users AUTO_INCREMENT = 1 → Snowflake).Low Risks:
WHERE id > last_seen)?incrementing models with HasSnowflakes for seamless ID generation.SnowflakeSynth for reactive components (e.g., real-time analytics).Expression interface for native SQL queries.Snowflake::make() for standalone PHP apps, but lose Eloquent/Livewire integrations.BIGINT UNSIGNED columns (no auto-increment).snowflake_id column (backward-compatible).| Phase | Action | Tools/Strategies |
|---|---|---|
| Assessment | Audit ID usage (auto-increment, UUIDs, etc.). | Static analysis, query profiling. |
| Pilot | Replace a non-critical model (e.g., Logs) with Snowflake IDs. |
HasSnowflakes trait, feature flags. |
| Core Migration | Migrate primary models (e.g., Users, Orders). |
Database migrations, CI/CD checks. |
| Query Optimization | Replace created_at queries with ID-based ranges (e.g., WHERE id > last_id). |
Query rewrites, benchmarking. |
| Full Rollout | Deprecate old ID schemes. | Deprecation warnings, monitoring. |
BIGINT support (MySQL, PostgreSQL, SQLite, etc.).CacheSequenceResolver).BITS_WORKER_ID and BITS_DATACENTER_ID in .env (e.g., BITS_WORKER_ID=1, BITS_DATACENTER_ID=1).BITS_EPOCH (e.g., BITS_EPOCH=2000-01-01).MakesSnowflakes interface (if using DI):
$this->app->bind(MakesSnowflakes::class, function () {
return new SnowflakeFactory();
});
HasSnowflakes trait or cast existing columns:
use Glhd\Bits\Database\HasSnowflakes;
use Glhd\Bits\Snowflake;
class User extends Model {
use HasSnowflakes;
// OR
protected $casts = ['id' => Snowflake::class];
}
AppServiceProvider:
Livewire::propertySynthesizer(SnowflakeSynth::class);
Snowflake::setTestNow() to mock timestamps:
Snowflake::setTestNow(now()->subDays(1));
How can I help you explore Laravel packages today?