Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit 83fdbfd

Browse files
committed
added migration Blueprint methods and updated readme
1 parent 05de471 commit 83fdbfd

3 files changed

Lines changed: 46 additions & 17 deletions

File tree

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,39 +70,42 @@ Since this is a little cumbersome, the package also registers a global `snowflak
7070
snowflake(); // (string) "5585066784854016"
7171
```
7272

73-
> **IMPORTANT**: The initial release converted the Snowflake to an integer. This has been rolled back to prevent integer overflows in some languages.
73+
## Databases
7474

75-
### Eloquent models
75+
If you want to use Snowflakes in your database e.g. for primary and foreign keys, then you'll need to perform a couple of steps.
7676

77-
If you want to use a Snowflake as the primary key for an Eloquent model, then you'll need to perform a couple of steps.
78-
79-
First, modify the model's migration so that it no longer uses auto-incrementing integers e.g.
77+
First, modify your migrations so that they use the Snowflake migration methods e.g.
8078

8179
```php
8280
// Before
8381
$table->id();
82+
$table->foreignId('user_id');
83+
$table->foreignIdFor(User::class);
8484

8585
// After
86-
$table->unsignedBigInteger('id')->primary();
86+
$table->snowflake()->primary();
87+
$table->foreignSnowflake('user_id');
88+
$table->foreignSnowflakeFor(User::class);
8789
```
8890

8991
Here's an example:
9092

9193
```php
92-
class CreateUsersTable extends Migration
94+
class CreatePostsTable extends Migration
9395
{
9496
public function up()
9597
{
96-
Schema::create('users', function(Blueprint $table) {
97-
$table->unsignedBigInteger('id')->primary();
98-
$table->string('name', 100);
98+
Schema::create('posts', function(Blueprint $table) {
99+
$table->snowflake()->primary();
100+
$table->foreignSnowflake('user_id')->constrained()->cascadeOnDelete();
101+
$table->string('title', 100);
99102
$table->timestamps();
100103
});
101104
}
102105
}
103106
```
104107

105-
Finally, add the package's `Snowflakes` trait to the model:
108+
Next, if you're using Eloquent, add the package's `Snowflakes` trait to your Eloquent models:
106109

107110
```php
108111
<?php
@@ -111,15 +114,13 @@ namespace App\Models;
111114

112115
use Snowflake\Snowflakes;
113116

114-
class User extends Model
117+
class Post extends Model
115118
{
116119
use Snowflakes;
117120
}
118121
```
119122

120-
#### Optional casting
121-
122-
The package also includes a custom `SnowflakeCast` that will automatically handle conversion from `string` to `integer` and vice-versa when storing or fetching a Snowflake from the database. If you wish, you may use this cast for any model attribute that will contain a Snowflake e.g.
123+
Finally, configure the model's `$casts` array to use the package's `SnowflakeCast` for all Snowflake attributes. This cast automatically handles conversion from `string` to `integer` and vice-versa when storing or fetching a Snowflake from the database. It also ensures that languages which do not support 64-bit integers (such as JavaScript), will not truncate the Snowflake.
123124

124125
```php
125126
<?php

resources/version.svg

Lines changed: 2 additions & 2 deletions
Loading

src/ServiceProvider.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Snowflake;
44

55
use Godruoyi\Snowflake\Snowflake;
6+
use Illuminate\Database\Schema\Blueprint;
67
use Godruoyi\Snowflake\RandomSequenceResolver;
78
use Illuminate\Support\ServiceProvider as Provider;
9+
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
810

911
class ServiceProvider extends Provider
1012
{
@@ -14,6 +16,8 @@ class ServiceProvider extends Provider
1416
*/
1517
public function boot() : void
1618
{
19+
$this->macros();
20+
1721
$this->publishes([__DIR__ . '/../config/snowflake.php' => config_path('snowflake.php')]);
1822
}
1923

@@ -28,6 +32,30 @@ public function register() : void
2832
$this->app->singleton('snowflake', fn() => $this->singleton());
2933
}
3034

35+
/**
36+
* Register any custom macros.
37+
*
38+
*/
39+
protected function macros() : void
40+
{
41+
Blueprint::macro('snowflake', function($column = 'id') {
42+
return $this->unsignedBigInteger($column);
43+
});
44+
45+
Blueprint::macro('foreignSnowflake', function($column) {
46+
return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
47+
'type' => 'bigInteger',
48+
'name' => $column,
49+
'autoIncrement' => false,
50+
'unsigned' => true,
51+
]));
52+
});
53+
54+
Blueprint::macro('foreignSnowflakeFor', function($model, $column = null) {
55+
return $this->foreignSnowflake($column ?: (new $model())->getForeignKey());
56+
});
57+
}
58+
3159
/**
3260
* Register the Snowflake singleton service.
3361
*

0 commit comments

Comments
 (0)