It is possible to use `FlakeId` with [`Ecto`](https://github.com/elixir-ecto/ecto/).
See [`FlakeId.Ecto.Type`](https://hexdocs.pm/flake_id/FlakeId.Ecto.Type.html) documentation for detailed instructions.
If you wish to migrate from integer serial ids to Flake, see [`FlakeId.Ecto.CompatType`](https://hexdocs.pm/flake_id/FlakeId.Ecto.Type.html) for instructions.
## Prior Art
* [flaky](https://github.com/nirvana/flaky), released under the terms of the Truly Free License,
* [Flake](https://github.com/boundary/flake), Copyright 2012, Boundary, Apache License, Version 2.0
- Generates a local Flake (in-process state). Worker ID will be derived from global worker-id and the process pid.
+ Generate a in-process flake, without hitting a node-wide worker. This is especially useful when you have
+ long-running processes that do a lot of insertions/ID generation. Note that the first call will be slowest as it
+ has to setup the Flake state.
- First call will be slower but subsequent calls will be faster than worker based `get/0` and is quite useful for long running processes.
+ Each process can have its Flake state stored in the process dictionary and the Worker ID is derived from the global
+ Worker ID and the current PID.
"""
@spec get_local :: {:ok, String.t()} | error()
def get_local do
case Local.get() do
{:ok, flake} -> {:ok, to_string(flake)}
error -> error
end
end
@doc """
- Generates a predictable and back-dated FlakeId.
-
- This can be useful when you want to insert historical data without messing with sorting: be **very careful** at choosing a `unique` value that is _really unique_ for the given `datetime`, otherwise, collisons will happen.
+ If you wish to insert historical data while preserving sorting, you can achieve this using a backdated flake. In that
+ case you have to provide yourself a **UNIQUE** Worker ID (for the given time). Be very careful to ensure the uniqueness
+ of the Worker ID otherwise you may generate the same flake twice.