Page MenuHomePhorge

No OneTemporary

Size
6 KB
Referenced Files
None
Subscribers
None
diff --git a/README.md b/README.md
index 9a097fc..f9243f3 100644
--- a/README.md
+++ b/README.md
@@ -1,35 +1,39 @@
# ❄ FlakeID
> Decentralized, k-ordered ID generation service
## Installation
Add `flake_id` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:flake_id, "~> 0.1.0"}
]
end
```
## Usage
```elixir
iex> FlakeID.get()
"9n3171dJZpdD77K3DU"
```
See [https://hexdocs.pm/flake_id](https://hexdocs.pm/flake_id) for the complete documentation.
+### With Ecto
+
+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.
+
## 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
## Copyright and License
Copyright © 2017-2019 [Pleroma Authors](https://pleroma.social/)
FlakeID source code is licensed under the GNU LGPLv3 License.
diff --git a/lib/flake_id/ecto/type.ex b/lib/flake_id/ecto/type.ex
new file mode 100644
index 0000000..c1ffa69
--- /dev/null
+++ b/lib/flake_id/ecto/type.ex
@@ -0,0 +1,46 @@
+# FlakeID: Decentralized, k-ordered ID generation service
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: LGPL-3.0-only
+
+if Code.ensure_compiled?(Ecto.Type) do
+ defmodule FlakeID.Ecto.Type do
+ @moduledoc """
+ Provides a type for Ecto usage.
+
+ The underlying data type should be an `:uuid`.
+
+ ## Migration Example
+
+ create table(:posts, primary_key: false) do
+ add :id, :uuid, primary_key: true
+ add :body, :text
+ end
+
+ ## Schema Example
+
+ @primary_key {:id, FlakeID.Ecto.Type, autogenerate: true}
+
+ schema "posts" do
+ add :body, :string
+ end
+
+ """
+ use Ecto.Type
+
+ def type, do: :uuid
+
+ defdelegate autogenerate, to: FlakeID, as: :get
+
+ def cast(value) do
+ {:ok, FlakeID.to_string(value)}
+ end
+
+ def load(value) do
+ {:ok, FlakeID.to_string(value)}
+ end
+
+ def dump(value) do
+ {:ok, FlakeID.from_string(value)}
+ end
+ end
+end
diff --git a/mix.exs b/mix.exs
index 8731d40..923b60c 100644
--- a/mix.exs
+++ b/mix.exs
@@ -1,49 +1,50 @@
defmodule FlakeID.MixProject do
use Mix.Project
def project do
[
app: :flake_id,
version: "0.1.0",
description: "Decentralized, k-ordered ID generation service",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps(),
package: package(),
# Docs
name: "FlakeID",
source_url: "https://git.pleroma.social/pleroma/flake_id",
homepage_url: "https://git.pleroma.social/pleroma/flake_id",
docs: [
main: "readme",
extras: ["README.md"],
source_url_pattern:
"https://git.pleroma.social/pleroma/flake_id/blob/master/%{path}#L%{line}"
]
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {FlakeID.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:credo, "~> 1.1.0", only: [:dev, :test], runtime: false},
+ {:ecto, ">= 2.0.0", optional: true},
{:ex_doc, "~> 0.21", only: :dev, runtime: false}
]
end
defp package do
[
licenses: ["LGPLv3"],
links: %{"GitLab" => "https://git.pleroma.social/pleroma/flake_id"}
]
end
end
diff --git a/mix.lock b/mix.lock
index a42ce06..b34bb20 100644
--- a/mix.lock
+++ b/mix.lock
@@ -1,10 +1,12 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"credo": {:hex, :credo, "1.1.4", "c2f3b73c895d81d859cec7fcee7ffdb972c595fd8e85ab6f8c2adbf01cf7c29c", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
+ "decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.4.0", "397e750b879df18198afc66505ca87ecf6a96645545585899f6185178433cc09", [:mix], [], "hexpm"},
+ "ecto": {:hex, :ecto, "3.2.1", "a0f9af0fb50b19d3bb6237e512ac0ba56ea222c2bbea92e7c6c94897932c76ba", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.1", "c90796ecee0289dbb5ad16d3ad06f957b0cd1199769641c961cfe0b97db190e0", [:mix], [], "hexpm"},
}
diff --git a/test/flake_id/ecto/type_test.exs b/test/flake_id/ecto/type_test.exs
new file mode 100644
index 0000000..4663852
--- /dev/null
+++ b/test/flake_id/ecto/type_test.exs
@@ -0,0 +1,30 @@
+defmodule FlakeID.Ecto.TypeTest do
+ use ExUnit.Case, async: true
+
+ alias FlakeID.Ecto.Type
+
+ @flake_string "9n2ciuz1wdesFnrGJU"
+ @flake_binary <<0, 0, 1, 109, 67, 124, 251, 125, 95, 28, 30, 59, 36, 42, 0, 0>>
+
+ test "cast/1" do
+ assert Type.cast(@flake_binary) == {:ok, @flake_string}
+ assert Type.cast(@flake_string) == {:ok, @flake_string}
+ end
+
+ test "load/1" do
+ assert Type.load(@flake_binary) == {:ok, @flake_string}
+ assert Type.load(@flake_string) == {:ok, @flake_string}
+ end
+
+ test "dump/1" do
+ assert Type.dump(@flake_string) == {:ok, @flake_binary}
+ assert Type.dump(@flake_binary) == {:ok, @flake_binary}
+ end
+
+ test "autogenerate/0" do
+ flake = Type.autogenerate()
+
+ assert String.valid?(flake)
+ assert FlakeID.flake_id?(flake)
+ end
+end

File Metadata

Mime Type
text/x-diff
Expires
Fri, Aug 28, 7:02 PM (15 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1735951
Default Alt Text
(6 KB)

Event Timeline