Current section

16 Versions

Jump to

Compare versions

12 files changed
+175 additions
-95 deletions
  @@ -37,7 +37,7 @@ ipsum = Article.create(%{author: "Author", text: "Ipsum"})
37 37 IO.inspect lorem[:text] #=> "Lorem"
38 38
39 39 Article.one(%{author: "Author"}) #=> %Article{...}
40 - Article.list(%{author: "Author"}) #=> [%Article{...}, %Article{...}]
40 + Article.list(%{author: "Author"}) #=> %Xarango.QueryResult{result: [%Article{...}, %Article{...}]}
41 41
42 42 Article.update(ipsum, %{status: "review"})
43 43 Article.replace(lorem, %{author: "Author", text: "Foo"})
  @@ -112,7 +112,7 @@ See tests for low level usage examples.
112 112 - [x] Graph operations
113 113 - [x] Full text search
114 114 - [x] AQL support, query builder
115 - - [ ] Sync/Async
115 + - [ ] waitForSync option
116 116
117 117 ## Installation
118 118
  @@ -122,7 +122,7 @@ The package can be installed as:
122 122
123 123 ```elixir
124 124 def deps do
125 - [{:xarango, "~> 0.5.5"}]
125 + [{:xarango, "~> 0.5.6"}]
126 126 end
127 127 ```
  @@ -1,7 +1,7 @@
1 1 {<<"app">>,<<"xarango">>}.
2 2 {<<"build_tools">>,[<<"mix">>]}.
3 3 {<<"description">>,<<"Client library for ArangoDB.">>}.
4 - {<<"elixir">>,<<"~> 1.4">>}.
4 + {<<"elixir">>,<<"~> 1.3">>}.
5 5 {<<"files">>,
6 6 [<<"lib/xarango.ex">>,<<"lib/xarango/aql.ex">>,<<"lib/xarango/client.ex">>,
7 7 <<"lib/xarango/collection.ex">>,<<"lib/xarango/database.ex">>,
  @@ -9,12 +9,12 @@
9 9 <<"lib/xarango/domain/graph.ex">>,<<"lib/xarango/domain/node.ex">>,
10 10 <<"lib/xarango/edge.ex">>,<<"lib/xarango/error.ex">>,
11 11 <<"lib/xarango/graph.ex">>,<<"lib/xarango/index.ex">>,
12 - <<"lib/xarango/query.ex">>,<<"lib/xarango/server.ex">>,
13 - <<"lib/xarango/simple_query.ex">>,<<"lib/xarango/task.ex">>,
14 - <<"lib/xarango/transaction.ex">>,<<"lib/xarango/traversal.ex">>,
15 - <<"lib/xarango/uri.ex">>,<<"lib/xarango/user.ex">>,
16 - <<"lib/xarango/util.ex">>,<<"lib/xarango/vertex.ex">>,<<"mix.exs">>,
17 - <<"README.md">>,<<"LICENSE">>]}.
12 + <<"lib/xarango/query.ex">>,<<"lib/xarango/schema.ex">>,
13 + <<"lib/xarango/server.ex">>,<<"lib/xarango/simple_query.ex">>,
14 + <<"lib/xarango/task.ex">>,<<"lib/xarango/transaction.ex">>,
15 + <<"lib/xarango/traversal.ex">>,<<"lib/xarango/uri.ex">>,
16 + <<"lib/xarango/user.ex">>,<<"lib/xarango/util.ex">>,
17 + <<"lib/xarango/vertex.ex">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}.
18 18 {<<"licenses">>,[<<"Apache 2.0">>]}.
19 19 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/beno/xarango">>}]}.
20 20 {<<"maintainers">>,[<<"Michel Benevento">>]}.
  @@ -28,4 +28,4 @@
28 28 {<<"name">>,<<"httpoison">>},
29 29 {<<"optional">>,false},
30 30 {<<"requirement">>,<<"> 0.0.0">>}]]}.
31 - {<<"version">>,<<"0.5.5">>}.
31 + {<<"version">>,<<"0.5.6">>}.
  @@ -1,6 +1,6 @@
1 1 defmodule Xarango.AQL do
2 2
3 - defstruct [:collection, :filters, :sort, :limit]
3 + defstruct [:collection, :filters, :sort, :limit, :skip]
4 4
5 5 def from(collection) do
6 6 %Xarango.AQL{collection: collection, filters: []}
  @@ -15,8 +15,8 @@ defmodule Xarango.AQL do
15 15 %Xarango.AQL{aql | sort: add_sort(aql, sort, dir) }
16 16 end
17 17
18 - def limit(aql, limit) do
19 - %Xarango.AQL{aql | limit: add_limit(aql, limit) }
18 + def limit(aql, limit, skip\\nil) do
19 + %Xarango.AQL{aql | limit: add_limit(aql, limit, skip) }
20 20 end
21 21
22 22 def to_aql(aql) do
  @@ -54,9 +54,14 @@ defmodule Xarango.AQL do
54 54 ["SORT r.#{sort} #{dir}"]
55 55 end
56 56
57 - defp add_limit(_aql, limit) do
57 + defp add_limit(_aql, limit, nil) do
58 58 ["LIMIT #{limit}"]
59 59 end
60 + defp add_limit(_aql, limit, skip) do
61 + ["LIMIT #{skip}, #{limit}"]
62 + end
63 +
64 +
  @@ -29,7 +29,7 @@ defmodule Xarango.Document do
29 29 url(collection.name, database, options)
30 30 |> post(document)
31 31 |> case do
32 - %{new: new_doc} -> new_doc
32 + %{new: doc} -> doc
33 33 doc -> doc
34 34 end
35 35 |> to_document
  @@ -2,34 +2,24 @@ defmodule Xarango.Domain.Document do
2 2
3 3 alias Xarango.Document
4 4 alias Xarango.SimpleQuery
5 - alias Xarango.Index
6 5 alias Xarango.Query
7 -
8 - defmacro index(type, field) do
9 - quote do
10 - @indexes %Index{type: Atom.to_string(unquote(type)), fields: [Atom.to_string(unquote(field))]}
11 - defp indexes, do: @indexes
12 - defoverridable [indexes: 0]
13 - end
14 - end
15 -
6 +
16 7 defmacro __using__(options) do
17 8 db = options[:db] && Atom.to_string(options[:db]) || Xarango.Server.server.database
18 9 coll = options[:collection] && Atom.to_string(options[:collection])
19 10
20 11 quote do
21 - import Xarango.Domain.Document
12 + use Xarango.Index
13 + use Xarango.Schema
14 +
22 15 defstruct doc: %Xarango.Document{}
23 - Module.register_attribute __MODULE__, :indexes, accumulate: true
24 - defp indexes, do: @indexes
25 - defoverridable [indexes: 0]
26 16 def _database, do: %Xarango.Database{name: unquote(db)}
27 17 defp _collection, do: %Xarango.Collection{name: unquote(coll) || Xarango.Util.name_from(__MODULE__)}
28 18 def create(data, options\\[]) do
19 + options = Keyword.put(options, :returnNew, true)
29 20 Xarango.Database.ensure(_database())
30 21 Xarango.Collection.ensure(_collection(), _database(), indexes())
31 - Document.create(%Document{_data: data}, _collection(), _database())
32 - |> Document.document(_database())
22 + Document.create(%Document{_data: data}, _collection(), _database(), options)
33 23 |> to_document
34 24 end
35 25 def one(params) do
  @@ -37,9 +27,13 @@ defmodule Xarango.Domain.Document do
37 27 |> to_document
38 28 end
39 29 def list(params, options\\[]) do
40 - Query.build(_collection(), params, options)
41 - |> Query.query(_database())
42 - |> Map.get(:result)
30 + case options[:cursor] do
31 + cursor when is_binary(cursor) ->
32 + Query.next(%{hasMore: true, id: cursor}, _database())
33 + _ ->
34 + Query.build(_collection(), params, options)
35 + |> Query.query(_database())
36 + end
43 37 |> to_document
44 38 end
45 39 def replace(document, data) do
  @@ -58,7 +52,7 @@ defmodule Xarango.Domain.Document do
58 52 document.doc
59 53 |> Document.destroy(_database())
60 54 end
61 -
55 +
62 56 def search(field, value) do
63 57 %Xarango.Query{query: "FOR doc IN FULLTEXT(#{_collection().name}, \"#{field}\", \"prefix:#{value}\") RETURN doc", batchSize: 3}
64 58 |> Xarango.Query.query(_database())
  @@ -77,6 +71,9 @@ defmodule Xarango.Domain.Document do
77 71 defp to_document(docs) when is_list(docs) do
78 72 docs |> Enum.map(&to_document(&1))
79 73 end
74 + defp to_document(%Xarango.QueryResult{} = result) do
75 + %Xarango.QueryResult{result | result: to_document(result.result)}
76 + end
80 77 defp to_document(%Xarango.Document{} = doc) do
81 78 struct(__MODULE__, doc: doc)
82 79 end
  @@ -84,7 +81,7 @@ defmodule Xarango.Domain.Document do
84 81 struct(__MODULE__, doc: Document.to_document(doc))
85 82 end
86 83
87 -
84 +
88 85 end
89 86 end
Loading more files…