Current section
16 Versions
Jump to
Current section
16 Versions
Compare versions
13
files changed
+388
additions
-135
deletions
| @@ -1,6 +1,9 @@ | |
| 1 1 | # Xarango |
| 2 2 | |
| 3 | - Elixir client library for ArangoDB. |
| 3 | + Elixir client library for [ArangoDB](https://www.arangodb.com). |
| 4 | + |
| 5 | + Xarango has a low level API that maps directly to the Arango REST API. |
| 6 | + |
| 4 7 | |
| 5 8 | ## Usage |
| 6 9 | |
| @@ -23,7 +26,7 @@ Run tests: | |
| 23 26 | |
| 24 27 | mix test # <= beware: running tests will destroy all data in the configured database. |
| 25 28 | |
| 26 | - ## Example Document |
| 29 | + ## Documents |
| 27 30 | |
| 28 31 | ```elixir |
| 29 32 | defmodule Article, do: use Xarango.Domain.Document |
| @@ -37,14 +40,14 @@ Article.one(%{author: "Author"}) #=> %Article{...} | |
| 37 40 | Article.list(%{author: "Author"}) #=> [%Article{...}, %Article{...}] |
| 38 41 | |
| 39 42 | Article.update(ipsum, %{status: "review"}) |
| 40 | - Article.replace(lorem, %{author: "Author", text: "FooBar"}) |
| 43 | + Article.replace(lorem, %{author: "Author", text: "Foo"}) |
| 41 44 | |
| 42 45 | Article.destroy(ipsum) |
| 43 46 | |
| 44 47 | ``` |
| 45 48 | |
| 46 49 | |
| 47 | - ## Example Graph |
| 50 | + ## Graphs |
| 48 51 | |
| 49 52 | ```elixir |
| 50 53 | defmodule Brand, do: use Xarango.Domain.Node |
| @@ -75,8 +78,39 @@ Vehicles.car_made_by(subaru) #=> [%Car{...}] | |
| 75 78 | |
| 76 79 | ``` |
| 77 80 | |
| 81 | + ## Transactions |
| 82 | + |
| 83 | + ``` |
| 84 | + defmodule Brand, do: use Xarango.Domain.Node |
| 85 | + defmodule Car, do: use Xarango.Domain.Node, graph: Vehicles |
| 86 | + defmodule Vehicles do |
| 87 | + use Xarango.Domain.Graph |
| 88 | + |
| 89 | + relationship Car, :made_by, Brand |
| 90 | + end |
| 91 | + |
| 92 | + alias Xarango.Transaction |
| 93 | + |
| 94 | + Transaction.begin(Vehicles) |
| 95 | + |> Transaction.create(Car, %{name: "Foo"}, var: :car1) |
| 96 | + |> Transaction.create(Car, %{name: "Bar"}, var: :car2) |
| 97 | + |> Transaction.create(Brand, %{name: "Baz"}, var: :brand) |
| 98 | + |> Transaction.add(:car1, :made_by, :brand) |
| 99 | + |> Transaction.add(:car2, :made_by, :brand) |
| 100 | + |> Transaction.get(Car, :made_by, :brand) |
| 101 | + |> Transaction.execute #=> [%Car{vertex: ...}, %Car{vertex: ...}] |
| 102 | + ``` |
| 103 | + |
| 104 | + ## Low level API |
| 105 | + |
| 78 106 | See tests for low level usage examples. |
| 79 107 | |
| 108 | + ## Todo |
| 109 | + |
| 110 | + - [x] Transactions |
| 111 | + - [ ] Graph operations |
| 112 | + - [ ] Sync/Async |
| 113 | + |
| 80 114 | ## Installation |
| 81 115 | |
| 82 116 | The package can be installed as: |
| @@ -85,7 +119,7 @@ The package can be installed as: | |
| 85 119 | |
| 86 120 | ```elixir |
| 87 121 | def deps do |
| 88 | - [{:xarango, "~> 0.3.1"}] |
| 122 | + [{:xarango, "~> 0.4.0"}] |
| 89 123 | end |
| 90 124 | ``` |
| @@ -27,4 +27,4 @@ | |
| 27 27 | {<<"name">>,<<"poison">>}, |
| 28 28 | {<<"optional">>,false}, |
| 29 29 | {<<"requirement">>,<<"> 0.0.0">>}]]}. |
| 30 | - {<<"version">>,<<"0.3.1">>}. |
| 30 | + {<<"version">>,<<"0.4.0">>}. |
| @@ -70,11 +70,11 @@ defmodule Xarango.Client do | |
| 70 70 | end |
| 71 71 | end |
| 72 72 | |
| 73 | - defp do_encode(body) when is_map(body) do |
| 73 | + def do_encode(body) when is_map(body) do |
| 74 74 | encode_map(body) |
| 75 75 | end |
| 76 76 | |
| 77 | - defp do_encode(body) do |
| 77 | + def do_encode(body) do |
| 78 78 | body |
| 79 79 | end |
| 80 80 | |
| @@ -94,7 +94,7 @@ defmodule Xarango.Client do | |
| 94 94 | %{:__struct__ => _} = body -> Map.from_struct(body) |
| 95 95 | body -> body |
| 96 96 | end |
| 97 | - |> Enum.reject(fn {_,v} -> v == nil end) |
| 97 | + |> Enum.reject(&match?({_,nil}, &1)) |
| 98 98 | |> Enum.map(fn {key, value} -> {key, compact(value)} end) |
| 99 99 | |> Enum.into(%{}) |
| 100 100 | end |
| @@ -7,13 +7,13 @@ defmodule Xarango.Database do | |
| 7 7 | defstruct [:id, :name, :isSystem, :path, :users] |
| 8 8 | |
| 9 9 | def databases() do |
| 10 | - url("") |
| 10 | + url("", system) |
| 11 11 | |> get |
| 12 12 | |> Enum.map(fn name -> struct(Database, [name: name]) end) |
| 13 13 | end |
| 14 14 | |
| 15 15 | def user_databases() do |
| 16 | - url("user") |
| 16 | + url("user", system) |
| 17 17 | |> get |
| 18 18 | |> Map.get(:result) |
| 19 19 | |> Enum.map(&to_database(%{name: &1})) |
| @@ -26,13 +26,13 @@ defmodule Xarango.Database do | |
| 26 26 | end |
| 27 27 | |
| 28 28 | def create(database) do |
| 29 | - url("") |
| 29 | + url("", system) |
| 30 30 | |> post(database) |
| 31 31 | database |
| 32 32 | end |
| 33 33 | |
| 34 34 | def destroy(database) do |
| 35 | - url(database.name) |
| 35 | + url(database.name, system) |
| 36 36 | |> delete |
| 37 37 | end |
| 38 38 | |
| @@ -44,6 +44,15 @@ defmodule Xarango.Database do | |
| 44 44 | end |
| 45 45 | end |
| 46 46 | |
| 47 | + def default do |
| 48 | + %Database{name: Application.get_env(:xarango, :db)[:database]} |
| 49 | + end |
| 50 | + |
| 51 | + def system do |
| 52 | + %Database{name: "_system"} |
| 53 | + end |
| 54 | + |
| 55 | + |
| 47 56 | defp to_database(data) do |
| 48 57 | struct(Database, Map.get(data, :result, data)) |
| 49 58 | end |
| @@ -10,33 +10,34 @@ defmodule Xarango.Domain.Document do | |
| 10 10 | quote do |
| 11 11 | import Xarango.Domain.Document |
| 12 12 | defstruct doc: %Xarango.Document{} |
| 13 | - defp _database, do: %Xarango.Database{name: unquote(db)} |
| 13 | + def _database, do: %Xarango.Database{name: unquote(db)} |
| 14 14 | defp _collection, do: %Xarango.Collection{name: unquote(coll) || Xarango.Util.name_from(__MODULE__)} |
| 15 15 | def create(data, options\\[]) do |
| 16 16 | Xarango.Database.ensure(_database) |
| 17 17 | Xarango.Collection.ensure(_collection, _database) |
| 18 | - doc = Document.create(%Document{_data: data}, _collection, _database) |> Document.document(_database) |
| 19 | - struct(__MODULE__, doc: doc) |
| 18 | + Document.create(%Document{_data: data}, _collection, _database) |
| 19 | + |> Document.document(_database) |
| 20 | + |> to_document |
| 20 21 | end |
| 21 22 | def one(params) do |
| 22 | - document = SimpleQuery.first_example(%SimpleQuery{example: params, collection: _collection.name}, _database) |
| 23 | - struct(__MODULE__, doc: document) |
| 23 | + SimpleQuery.first_example(%SimpleQuery{example: params, collection: _collection.name}, _database) |
| 24 | + |> to_document |
| 24 25 | end |
| 25 26 | def list(params) do |
| 26 27 | SimpleQuery.by_example(%SimpleQuery{example: params, collection: _collection.name}, _database) |
| 27 28 | |> Enum.map(&struct(__MODULE__, doc: &1)) |
| 28 29 | end |
| 29 30 | def replace(document, data) do |
| 30 | - doc = %{ document.doc | _data: data } |
| 31 | - |> Document.replace(_database) |
| 32 | - |> Document.document(_database) |
| 33 | - struct(__MODULE__, doc: doc) |
| 31 | + %{ document.doc | _data: data } |
| 32 | + |> Document.replace(_database) |
| 33 | + |> Document.document(_database) |
| 34 | + |> to_document |
| 34 35 | end |
| 35 36 | def update(document, data) do |
| 36 | - doc = %{ document.doc | _data: data } |
| 37 | - |> Document.update(_database) |
| 38 | - |> Document.document(_database) |
| 39 | - struct(__MODULE__, doc: doc) |
| 37 | + %{ document.doc | _data: data } |
| 38 | + |> Document.update(_database) |
| 39 | + |> Document.document(_database) |
| 40 | + |> to_document |
| 40 41 | end |
| 41 42 | def destroy(document) do |
| 42 43 | document.doc |
| @@ -47,6 +48,10 @@ defmodule Xarango.Domain.Document do | |
| 47 48 | |> Map.get(field) |
| 48 49 | {:ok, value} |
| 49 50 | end |
| 51 | + |
| 52 | + defp to_document(data) do |
| 53 | + struct(__MODULE__, doc: data) |
| 54 | + end |
| 50 55 | |
| 51 56 | end |
| 52 57 | end |
Loading more files…