Ecto.ParameterizedType, time: ... for new/2

This commit is contained in:
Sloane Perrault 2023-07-07 08:58:01 -04:00
parent 818b216424
commit 1884261b6c
No known key found for this signature in database
4 changed files with 21 additions and 3 deletions

View file

@ -4,6 +4,11 @@
- **BREAKING:** `type/1` has been renamed to `prefix/1`
- `Ecto.ParameterizedType` implementation
- `new/2` now accepts an optional keyword list to specify the UUID `time:` in unix milliseconds
```elixir
iex> TypeID.new("test", time: 0)
#TypeID<"test_0000000000fq893mf5039xea5j">
```
## 0.2.2

View file

@ -18,6 +18,9 @@ defmodule TypeID do
@doc """
Generates a new `t:t/0` with the given prefix.
**Optional**: Specify the time of the UUID v7 by passing
`time: unix_millisecond_time` as the second argument.
### Example
iex> TypeID.new("acct")
@ -25,9 +28,10 @@ defmodule TypeID do
"""
@spec new(prefix :: String.t()) :: t()
def new(prefix) do
@spec new(prefix :: String.t(), Keyword.t()) :: t()
def new(prefix, opts \\ []) do
suffix =
UUID.uuid7()
UUID.uuid7(opts)
|> Base32.encode()
%__MODULE__{prefix: prefix, suffix: suffix}

View file

@ -1,6 +1,6 @@
defmodule TypeIDTest do
use ExUnit.Case
doctest TypeID, except: [new: 1]
doctest TypeID, except: [new: 2]
describe "new/1" do
test "returns a new TypeID struct" do
@ -10,6 +10,15 @@ defmodule TypeIDTest do
end
end
describe "new/2" do
test "allows setting the time" do
time = ~U[1950-12-17 00:00:00Z] |> DateTime.to_unix(:millisecond)
tid = TypeID.new("test", time: time)
assert "test" == TypeID.prefix(tid)
assert "7zegbdn300" <> _ = TypeID.suffix(tid)
end
end
describe "prefix/1" do
test "returns the prefix of the given TypeID" do
tid = TypeID.from_string!("test_01h44had5rfswbvpc383ktj0aa")