Current section

16 Versions

Jump to

Compare versions

10 files changed
+158 additions
-30 deletions
  @@ -110,6 +110,8 @@ See tests for low level usage examples.
110 110
111 111 - [x] Transactions
112 112 - [x] Graph operations
113 + - [x] Full text search
114 + - [x] AQL support, query builder
113 115 - [ ] Sync/Async
114 116
115 117 ## Installation
  @@ -120,7 +122,7 @@ The package can be installed as:
120 122
121 123 ```elixir
122 124 def deps do
123 - [{:xarango, "~> 0.5.1"}]
125 + [{:xarango, "~> 0.5.5"}]
124 126 end
125 127 ```
  @@ -3,7 +3,7 @@
3 3 {<<"description">>,<<"Client library for ArangoDB.">>}.
4 4 {<<"elixir">>,<<"~> 1.4">>}.
5 5 {<<"files">>,
6 - [<<"lib/xarango.ex">>,<<"lib/xarango/client.ex">>,
6 + [<<"lib/xarango.ex">>,<<"lib/xarango/aql.ex">>,<<"lib/xarango/client.ex">>,
7 7 <<"lib/xarango/collection.ex">>,<<"lib/xarango/database.ex">>,
8 8 <<"lib/xarango/document.ex">>,<<"lib/xarango/domain/document.ex">>,
9 9 <<"lib/xarango/domain/graph.ex">>,<<"lib/xarango/domain/node.ex">>,
  @@ -28,4 +28,4 @@
28 28 {<<"name">>,<<"httpoison">>},
29 29 {<<"optional">>,false},
30 30 {<<"requirement">>,<<"> 0.0.0">>}]]}.
31 - {<<"version">>,<<"0.5.1">>}.
31 + {<<"version">>,<<"0.5.5">>}.
  @@ -0,0 +1,65 @@
1 + defmodule Xarango.AQL do
2 +
3 + defstruct [:collection, :filters, :sort, :limit]
4 +
5 + def from(collection) do
6 + %Xarango.AQL{collection: collection, filters: []}
7 + end
8 +
9 + def filter(aql, filters) do
10 + %Xarango.AQL{aql | filters: add_filters(aql, filters) }
11 + end
12 + defdelegate where(aql, filters), to: __MODULE__, as: :filter
13 +
14 + def sort(aql, sort, dir\\:desc) do
15 + %Xarango.AQL{aql | sort: add_sort(aql, sort, dir) }
16 + end
17 +
18 + def limit(aql, limit) do
19 + %Xarango.AQL{aql | limit: add_limit(aql, limit) }
20 + end
21 +
22 + def to_aql(aql) do
23 + ["FOR r IN #{aql.collection.name}"]
24 + |> add_to_query(aql.filters)
25 + |> add_to_query(aql.sort)
26 + |> add_to_query(aql.limit)
27 + |> Kernel.++(["RETURN r"])
28 + |> Enum.join(" ")
29 + end
30 +
31 + defp add_to_query(query, nil), do: query
32 + defp add_to_query(query, items) do
33 + query ++ items
34 + end
35 +
36 + defp add_filters(aql, filters) do
37 + filters
38 + |> Enum.reduce(aql.filters, fn {key, value}, f -> f ++ [to_filter(key, value)] end)
39 + end
40 + defp to_filter(key, value) when is_binary(value) do
41 + "FILTER r.#{key} == \"#{value}\""
42 + end
43 + defp to_filter(key, value) do
44 + "FILTER r.#{key} == #{value}"
45 + end
46 +
47 + defp add_sort(_aql, sort, dir) when is_list(sort) do
48 + dir = Atom.to_string(dir) |> String.upcase
49 + sort = Enum.map(sort, fn s -> "r.#{s}" end) |> Enum.join(", ")
50 + ["SORT #{sort} #{dir}"]
51 + end
52 + defp add_sort(_aql, sort, dir) do
53 + dir = Atom.to_string(dir) |> String.upcase
54 + ["SORT r.#{sort} #{dir}"]
55 + end
56 +
57 + defp add_limit(_aql, limit) do
58 + ["LIMIT #{limit}"]
59 + end
60 +
61 +
62 +
63 +
64 +
65 + end
\ No newline at end of file
  @@ -9,7 +9,7 @@ defmodule Xarango.Document do
9 9 def document(document, database\\nil) do
10 10 url(document._id, database)
11 11 |> get
12 - |> to_document
12 + |> to_document
13 13 end
14 14
15 15 def documents(collection, database\\nil) do
  @@ -95,11 +95,13 @@ defmodule Xarango.Document do
95 95 |> to_document
96 96 end
97 97
98 + def to_document(docs) when is_list(docs) do
99 + docs |> Enum.map(&to_document(&1))
100 + end
98 101 def to_document(doc) do
99 102 struct(Document, decode_data(doc, Document))
100 103 end
101 104
102 -
103 105 defp docs_per_collection(documents) do
104 106 documents
105 107 |> Enum.reduce(%{}, fn doc, acc ->
  @@ -3,6 +3,7 @@ defmodule Xarango.Domain.Document do
3 3 alias Xarango.Document
4 4 alias Xarango.SimpleQuery
5 5 alias Xarango.Index
6 + alias Xarango.Query
6 7
7 8 defmacro index(type, field) do
8 9 quote do
  @@ -35,8 +36,10 @@ defmodule Xarango.Domain.Document do
35 36 SimpleQuery.first_example(%SimpleQuery{example: params, collection: _collection().name}, _database())
36 37 |> to_document
37 38 end
38 - def list(params) do
39 - SimpleQuery.by_example(%SimpleQuery{example: params, collection: _collection().name}, _database())
39 + def list(params, options\\[]) do
40 + Query.build(_collection(), params, options)
41 + |> Query.query(_database())
42 + |> Map.get(:result)
40 43 |> to_document
41 44 end
42 45 def replace(document, data) do
  @@ -74,9 +77,13 @@ defmodule Xarango.Domain.Document do
74 77 defp to_document(docs) when is_list(docs) do
75 78 docs |> Enum.map(&to_document(&1))
76 79 end
77 - defp to_document(doc) do
80 + defp to_document(%Xarango.Document{} = doc) do
78 81 struct(__MODULE__, doc: doc)
79 82 end
83 + defp to_document(doc) do
84 + struct(__MODULE__, doc: Document.to_document(doc))
85 + end
86 +
80 87
81 88 end
82 89 end
Loading more files…