Current section

16 Versions

Jump to

Compare versions

24 files changed
+429 additions
-366 deletions
  @@ -7,15 +7,15 @@ Xarango has a low level API that maps directly to the Arango REST API. On top of
7 7
8 8 ## Usage
9 9
10 - Configure xarango in `config/confix.exs`:
10 + Configure xarango in `config/config.exs`:
11 11
12 - config :xarango, db: [
12 + ```elixir
13 + config :xarango, :db,
13 14 server: "http://localhost:8529",
14 15 database: "test_db",
15 - version: 30000,
16 16 username: System.get_env("ARANGO_USER"),
17 17 password: System.get_env("ARANGO_PASSWORD")
18 - ]
18 + ```
19 19
20 20 Set your credentials:
21 21
  @@ -25,7 +25,7 @@ Set your credentials:
25 25 Run tests:
26 26
27 27 mix test # <= beware: running tests will destroy all data in the configured database.
28 -
28 +
29 29 ## Documents
30 30
31 31 ```elixir
  @@ -51,10 +51,10 @@ Article.destroy(ipsum)
51 51
52 52 ```elixir
53 53 defmodule Brand, do: use Xarango.Domain.Node
54 - defmodule Car, do: use Xarango.Domain.Node, graph: Vehicles
54 + defmodule Car, do: use Xarango.Domain.Node, graph: Vehicles, collection: :all_cars
55 55 defmodule Vehicles do
56 56 use Xarango.Domain.Graph
57 -
57 +
58 58 relationship Car, :made_by, Brand
59 59 end
60 60
  @@ -69,7 +69,7 @@ Vehicles.add_made_by(outback, subaru)
69 69 Vehicles.add(impreza, :made_by, subaru)
70 70
71 71 Vehicles.car_made_by(subaru) #=> [%Car{...}, %Car{...}] #outbound edges for car
72 - Vehicles.get(Car, :made_by, subaru) #=> [%Car{...}, %Car{...}]
72 + Vehicles.get(Car, :made_by, subaru) #=> [%Car{...}, %Car{...}]
73 73
74 74 Vehicles.made_by_brand(outback) #=> [%Brand{...}]
75 75 Vehicles.get(outback, :made_by, Brand) #=> [%Brand{...}
  @@ -109,7 +109,7 @@ See tests for low level usage examples.
109 109 ## Todo
110 110
111 111 - [x] Transactions
112 - - [ ] Graph operations
112 + - [x] Graph operations
113 113 - [ ] Sync/Async
114 114
115 115 ## Installation
  @@ -131,4 +131,3 @@ The package can be installed as:
131 131 [applications: [:xarango]]
132 132 end
133 133 ```
134 -
  @@ -1,7 +1,7 @@
1 1 {<<"app">>,<<"xarango">>}.
2 2 {<<"build_tools">>,[<<"mix">>]}.
3 3 {<<"description">>,<<"Client library for ArangoDB.">>}.
4 - {<<"elixir">>,<<"~> 1.3">>}.
4 + {<<"elixir">>,<<"~> 1.4">>}.
5 5 {<<"files">>,
6 6 [<<"lib/xarango.ex">>,<<"lib/xarango/client.ex">>,
7 7 <<"lib/xarango/collection.ex">>,<<"lib/xarango/database.ex">>,
  @@ -20,12 +20,12 @@
20 20 {<<"maintainers">>,[<<"Michel Benevento">>]}.
21 21 {<<"name">>,<<"xarango">>}.
22 22 {<<"requirements">>,
23 - [[{<<"app">>,<<"httpoison">>},
24 - {<<"name">>,<<"httpoison">>},
25 - {<<"optional">>,false},
26 - {<<"requirement">>,<<"> 0.0.0">>}],
27 - [{<<"app">>,<<"poison">>},
23 + [[{<<"app">>,<<"poison">>},
28 24 {<<"name">>,<<"poison">>},
29 25 {<<"optional">>,false},
26 + {<<"requirement">>,<<"> 0.0.0">>}],
27 + [{<<"app">>,<<"httpoison">>},
28 + {<<"name">>,<<"httpoison">>},
29 + {<<"optional">>,false},
30 30 {<<"requirement">>,<<"> 0.0.0">>}]]}.
31 - {<<"version">>,<<"0.4.1">>}.
31 + {<<"version">>,<<"0.5.0">>}.
  @@ -1,7 +1,7 @@
1 1 defmodule Xarango.Client do
2 -
2 +
3 3 alias Xarango.Util
4 -
4 +
5 5 [:get, :post, :put, :patch, :delete, :head, :options]
6 6 |> Enum.map(fn method ->
7 7 def unquote(method)(url, body\\"") do
  @@ -11,11 +11,11 @@ defmodule Xarango.Client do
11 11 end
12 12 end
13 13 end)
14 -
14 +
15 15 def _url(path, options\\[]) do
16 16 Xarango.Server.server.server <> path <> query_params(options)
17 17 end
18 -
18 +
19 19 def credentials do
20 20 case Xarango.Server.server do
21 21 %{username: nil} -> Util.do_error "missing database username, set ARANGO_USER environment variable"
  @@ -24,21 +24,21 @@ defmodule Xarango.Client do
24 24 _ -> Util.do_error "database credentials invalid, update `xarango.db` app config"
25 25 end
26 26 end
27 -
27 +
28 28 def headers do
29 - {username, password} = credentials
29 + {username, password} = credentials()
30 30 auth_header = "Basic " <> Base.encode64("#{username}:#{password}")
31 31 ["Accept": "*/*", "Authorization": auth_header]
32 32 #x-arango-async
33 33 end
34 -
34 +
35 35 defp query_params(options) do
36 36 case URI.encode_query(options) do
37 37 "" -> ""
38 38 params -> "?" <> params
39 39 end
40 40 end
41 -
41 +
42 42 defp do_request(method, url, body) when is_list(body) do
43 43 body = body
44 44 |> Enum.map(&Util.do_encode(&1))
  @@ -54,14 +54,14 @@ defmodule Xarango.Client do
54 54 end
55 55
56 56 defp do_request(method, url, body) when is_binary(body) do
57 - case HTTPoison.request(method, url, body, headers) do
57 + case HTTPoison.request(method, url, body, headers()) do
58 58 {:error, %HTTPoison.Error{reason: error}} when is_atom(error)-> raise Xarango.Error, message: Atom.to_string(error)
59 59 {:error, %HTTPoison.Error{reason: error}} when is_binary(error)-> raise Xarango.Error, message: error
60 60 {:error, error} when is_binary(error) -> raise Xarango.Error, message: error
61 61 {:ok, response} -> Util.do_decode(response)
62 62 end
63 63 end
64 -
64 +
65 65 def decode_data(data, into), do: Util.decode_data(data, into)
66 -
66 +
67 67 end
  @@ -1,11 +1,12 @@
1 1 defmodule Xarango.Collection do
2 - defstruct [:id, :journalSize, :replicationFactor, :keyOptions, :name, :waitForSync,
2 + defstruct [:id, :journalSize, :replicationFactor, :keyOptions, :name, :waitForSync,
3 3 :doCompact, :isVolatile, :shardKeys, :numberOfShards, :isSystem, :type, :indexBuckets, :status, :error, :count, :figures, :revision, :checksum]
4 -
4 +
5 5 alias Xarango.Collection
6 + alias Xarango.Index
6 7 import Xarango.Client
7 8 use Xarango.URI, prefix: "collection"
8 -
9 +
9 10 def collections(database\\nil) do
10 11 url("", database)
11 12 |> get
  @@ -18,64 +19,67 @@ defmodule Xarango.Collection do
18 19 |> get
19 20 |> to_collection
20 21 end
21 -
22 +
22 23 def properties(collection, database\\nil) do
23 24 url("#{collection.name}/properties", database)
24 25 |> get
25 26 |> to_collection
26 27 end
27 -
28 +
28 29 def count(collection, database\\nil) do
29 30 url("#{collection.name}/count", database)
30 31 |> get
31 32 |> to_collection
32 33 end
33 -
34 +
34 35 def figures(collection, database\\nil) do
35 36 url("#{collection.name}/figures", database)
36 37 |> get
37 38 |> to_collection
38 39 end
39 -
40 +
40 41 def revision(collection, database\\nil) do
41 42 url("#{collection.name}/revision", database)
42 43 |> get
43 44 |> to_collection
44 45 end
45 -
46 +
46 47 def checksum(collection, database\\nil) do
47 48 url("#{collection.name}/checksum", database)
48 49 |> get
49 50 |> to_collection
50 51 end
51 -
52 +
52 53 def create(collection, database\\nil) do
53 54 url("", database)
54 55 |> post(collection)
55 56 |> to_collection
56 57 end
57 -
58 +
58 59 def destroy(collection, database\\nil) do
59 60 url(collection.name, database)
60 61 |> delete
61 62 end
62 63
63 - def ensure(collection, database\\nil) do
64 + def ensure(collection, database\\nil, indexes\\[]) do
64 65 try do
65 66 collection(collection, database)
66 67 rescue
67 - Xarango.Error -> create(collection, database)
68 + Xarango.Error ->
69 + create(collection, database)
70 + Enum.each indexes, fn index ->
71 + Index.create(index, collection.name, database)
72 + end
68 73 end
69 74 end
70 -
75 +
71 76 def __destroy_all(database\\nil) do
72 77 collections(database)
73 78 |> Enum.reject(&Map.get(&1, :isSystem))
74 79 |> Enum.each(&destroy(&1, database))
75 80 end
76 -
81 +
77 82 defp to_collection(data) do
78 83 struct(Collection, data)
79 84 end
80 85 end
81 -
  @@ -1,41 +1,41 @@
1 1 defmodule Xarango.Database do
2 -
2 +
3 3 alias Xarango.Database
4 4 import Xarango.Client
5 5 use Xarango.URI, prefix: "database"
6 -
6 +
7 7 defstruct [:id, :name, :isSystem, :path, :users]
8 -
8 +
9 9 def databases() do
10 - url("", system)
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", system)
16 + url("user", system())
17 17 |> get
18 18 |> Map.get(:result)
19 19 |> Enum.map(&to_database(%{name: &1}))
20 20 end
21 -
21 +
22 22 def database(database) do
23 23 url("current", database)
24 24 |> get
25 25 |> to_database
26 26 end
27 -
27 +
28 28 def create(database) do
29 - url("", system)
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, system)
35 + url(database.name, system())
36 36 |> delete
37 37 end
38 -
38 +
39 39 def ensure(database) do
40 40 try do
41 41 database(database)
  @@ -43,30 +43,30 @@ defmodule Xarango.Database do
43 43 Xarango.Error -> create(database)
44 44 end
45 45 end
46 -
46 +
47 47 def default do
48 48 %Database{name: Application.get_env(:xarango, :db)[:database]}
49 49 end
50 -
50 +
51 51 def system do
52 52 %Database{name: "_system"}
53 53 end
54 54
55 -
55 +
56 56 defp to_database(data) do
57 57 struct(Database, Map.get(data, :result, data))
58 58 end
59 -
59 +
60 60
61 61 # defp url(path, options\\[]) do
62 62 # Xarango.Client.url("/_api/database/#{path}", options)
63 63 # end
64 - #
64 + #
65 65 # defp db_url(database, path, options\\[]) do
66 66 # Xarango.Client.url("/_db/#{database.name}/_api/database/#{path}", options)
67 67 # end
68 68
69 69
70 -
71 -
72 - end
\ No newline at end of file
70 +
71 +
72 + end
Loading more files…