Current section

16 Versions

Jump to

Compare versions

8 files changed
+142 additions
-168 deletions
  @@ -23,11 +23,30 @@ Run tests:
23 23
24 24 mix test # <= beware: running tests will destroy all data in the configured database.
25 25
26 - # Example Document
26 + ## Example Document
27 27
28 28 ```elixir
29 + defmodule Article, do: use Xarango.Domain.Document
30 +
31 + lorem = Article.create(%{author: "Author", text: "Lorem"})
32 + ipsum = Article.create(%{author: "Author", text: "Lorem"})
33 +
34 + IO.inspect lorem[:text] #=> "Lorem"
35 +
36 + Article.list(%{author: "Author"}) #=> [%Article{...}, %Article{...}]
37 +
38 + Article.update(ipsum, %{status: "review"})
39 +
40 + Article.destroy(ipsum)
41 +
42 + ```
43 +
44 +
45 + ## Example Graph
46 +
47 + ```elixir
48 + defmodule Brand, do: use Xarango.Domain.Vertex
29 49 defmodule Car, do: use Xarango.Domain.Vertex, graph: :vehicles
30 - defmodule Brand, do: use Xarango.Domain.Vertex, graph: :vehicles
31 50 defmodule Vehicles do
32 51 use Xarango.Domain.Graph
33 52
  @@ -35,21 +54,27 @@ defmodule Vehicles do
35 54 end
36 55
37 56 Vehicles.create
57 + subaru = Brand.create(%{name: "Subaru"}, :vehicles)
38 58 outback = Car.create(%{type: "Outback"})
39 59 impreza = Car.create(%{type: "Impreza"})
40 - subaru = Brand.create(%{name: "Subaru"})
60 +
61 + IO.inspect subaru[:name] #=> "Subaru"
62 + IO.inspect outback[:type] #=> "Outback"
63 +
41 64 Vehicles.add_has_brand(outback, subaru)
42 65 Vehicles.add_has_brand(impreza, subaru)
43 66
44 - Vehicles.has_brand!(outback) #=> [%Brand{...}]
45 - Vehicles.has_brand?(subaru) #=> [%Car{...}, %Car{...}]
67 + Vehicles.car_has_brand(subaru) #=> [%Car{...}, %Car{...}] #outbound edges for car
68 + Vehicles.has_brand_brand(outback) #=> [%Brand{...}] #inbound edges for brand
46 69
70 + Vehicles.remove_has_brand(impreza, subaru)
47 71
72 + Vehicles.car_has_brand(subaru) #=> [%Car{...}]
48 73
49 74
50 75 ```
51 76
52 - See tests for detailed usage examples.
77 + See tests for low level usage examples.
53 78
54 79 ## Installation
55 80
  @@ -59,7 +84,7 @@ The package can be installed as:
59 84
60 85 ```elixir
61 86 def deps do
62 - [{:xarango, "~> 0.2.0"}]
87 + [{:xarango, "~> 0.2.1"}]
63 88 end
64 89 ```
  @@ -6,15 +6,14 @@
6 6 [<<"lib/xarango.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 - <<"lib/xarango/domain/edge.ex">>,<<"lib/xarango/domain/graph.ex">>,
10 - <<"lib/xarango/domain/vertex.ex">>,<<"lib/xarango/edge.ex">>,
11 - <<"lib/xarango/error.ex">>,<<"lib/xarango/graph.ex">>,
12 - <<"lib/xarango/index.ex">>,<<"lib/xarango/query.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/vertex.ex">>,<<"mix.exs">>,
17 - <<"README.md">>,<<"LICENSE">>]}.
9 + <<"lib/xarango/domain/graph.ex">>,<<"lib/xarango/domain/vertex.ex">>,
10 + <<"lib/xarango/edge.ex">>,<<"lib/xarango/error.ex">>,
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/vertex.ex">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}.
18 17 {<<"licenses">>,[<<"Apache 2.0">>]}.
19 18 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/beno/xarango">>}]}.
20 19 {<<"maintainers">>,[<<"Michel Benevento">>]}.
  @@ -28,4 +27,4 @@
28 27 {<<"name">>,<<"poison">>},
29 28 {<<"optional">>,false},
30 29 {<<"requirement">>,<<"> 0.0.0">>}]]}.
31 - {<<"version">>,<<"0.2.0">>}.
30 + {<<"version">>,<<"0.2.1">>}.
  @@ -1,2 +1,13 @@
1 1 defmodule Xarango do
2 2 end
3 +
4 + defmodule Xarango.Util do
5 +
6 + def name_from(module) do
7 + module
8 + |> Module.split
9 + |> Enum.join("")
10 + |> Macro.underscore
11 + end
12 +
13 + end
\ No newline at end of file
  @@ -3,59 +3,53 @@ defmodule Xarango.Domain.Document do
3 3 alias Xarango.Document
4 4 alias Xarango.SimpleQuery
5 5
6 - defmacro __using__(_options) do
6 + defmacro __using__(options) do
7 + db = options[:db] && Atom.to_string(options[:db]) || Xarango.Server.server.database
8 + coll = options[:collection] && Atom.to_string(options[:collection])
9 +
7 10 quote do
8 11 import Xarango.Domain.Document
9 12 defstruct doc: %Xarango.Document{}
10 - end
11 - end
12 -
13 - defmacro collection(coll, db\\nil) do
14 - coll = Atom.to_string(coll)
15 - db = db && Atom.to_string(db) || Xarango.Server.server.database
16 - quote do
17 - defp database, do: %Xarango.Database{name: unquote(db)}
18 - defp collection, do: %Xarango.Collection{name: unquote(coll)}
13 + defp _database, do: %Xarango.Database{name: unquote(db)}
14 + defp _collection, do: %Xarango.Collection{name: unquote(coll) || Xarango.Util.name_from(__MODULE__)}
19 15 def create(data, options\\[]) do
20 - database = Xarango.Database.ensure(database)
21 - Xarango.Collection.ensure(collection, database)
22 - doc = Document.create(%Document{_data: data}, collection, database) |> Document.document(database)
16 + Xarango.Database.ensure(_database)
17 + Xarango.Collection.ensure(_collection, _database)
18 + doc = Document.create(%Document{_data: data}, _collection, _database) |> Document.document(_database)
23 19 struct(__MODULE__, doc: doc)
24 20 end
25 21 def one(params) do
26 - document = SimpleQuery.first_example(%SimpleQuery{example: params, collection: collection.name}, database)
22 + document = SimpleQuery.first_example(%SimpleQuery{example: params, collection: _collection.name}, _database)
27 23 struct(__MODULE__, doc: document)
28 24 end
29 25 def list(params) do
30 - SimpleQuery.by_example(%SimpleQuery{example: params, collection: collection.name}, database)
26 + SimpleQuery.by_example(%SimpleQuery{example: params, collection: _collection.name}, _database)
31 27 |> Enum.map(&struct(__MODULE__, doc: &1))
32 28 end
33 - def replace(params, data) do
34 - doc = %{ one(params).doc | _data: data }
35 - |> Document.replace(database)
36 - |> Document.document(database)
29 + def replace(document, data) do
30 + doc = %{ document.doc | _data: data }
31 + |> Document.replace(_database)
32 + |> Document.document(_database)
37 33 struct(__MODULE__, doc: doc)
38 34 end
39 - def update(params, data) do
40 - doc = %{ one(params).doc | _data: data }
41 - |> Document.update(database)
42 - |> Document.document(database)
35 + def update(document, data) do
36 + doc = %{ document.doc | _data: data }
37 + |> Document.update(_database)
38 + |> Document.document(_database)
43 39 struct(__MODULE__, doc: doc)
44 40 end
45 - def destroy(params) do
46 - one(params).doc
47 - |> Document.destroy(database)
41 + def destroy(document) do
42 + document.doc
43 + |> Document.destroy(_database)
48 44 end
49 45 def fetch(document, field) do
50 46 value = document.doc._data
51 47 |> Map.get(field)
52 48 {:ok, value}
53 49 end
50 +
54 51 end
55 52 end
56 53
57 - defmacro field(name) do
58 - Module.put_attribute(__MODULE__, :fields, name)
59 - end
60 54
61 55 end
\ No newline at end of file
  @@ -1,62 +0,0 @@
1 - # defmodule Xarango.Domain.Edge do
2 - #
3 - # alias Xarango.Edge
4 - # alias Xarango.SimpleQuery
5 - #
6 - # defmacro __using__(_options) do
7 - # quote do
8 - # import Xarango.Domain.Edge
9 - # defstruct doc: %Xarango.Edge{}
10 - # end
11 - # end
12 - #
13 - # defmacro collection(coll, gr, db\\nil) do
14 - # db = db && Atom.to_string(db) || Xarango.Server.server.database
15 - # quote do
16 - # defp database, do: %Xarango.Database{name: unquote(db)}
17 - # defp graph, do: %Xarango.Graph{name: unquote(gr)}
18 - # defp collection, do: %Xarango.VertexCollection{collection: unquote(coll)}
19 - # def create(data, options\\[]) do
20 - # database = Xarango.Database.ensure(database)
21 - # vc = Xarango.EdgeCollection.ensure(collection, graph, database)
22 - # vertex = Vertex.create(%Vertex{_data: data}, vc, graph, database) |> Vertex.vertex(vc, graph, database)
23 - # struct(__MODULE__, vertex: vertex)
24 - # end
25 - # def one(params) do
26 - # document = SimpleQuery.first_example(%SimpleQuery{example: params, collection: collection.collection}, database)
27 - # struct(__MODULE__, vertex: to_vertex(document))
28 - # end
29 - # def list(params) do
30 - # SimpleQuery.by_example(%SimpleQuery{example: params, collection: collection.collection}, database)
31 - # |> Enum.map(&struct(__MODULE__, vertex: to_vertex(&1)))
32 - # end
33 - # def replace(params, data) do
34 - # vertex = %{ one(params).vertex | _data: data }
35 - # |> Vertex.replace(collection, graph, database)
36 - # |> Vertex.vertex(collection, graph, database)
37 - # struct(__MODULE__, vertex: vertex)
38 - # end
39 - # def update(params, data) do
40 - # vertex = %{ one(params).vertex | _data: data }
41 - # |> Vertex.update(collection, graph, database)
42 - # |> Vertex.vertex(collection, graph, database)
43 - # struct(__MODULE__, vertex: vertex)
44 - # end
45 - # def destroy(params) do
46 - # one(params).vertex
47 - # |> Vertex.destroy(collection, graph, database)
48 - # end
49 - # def fetch(vertex, field) do
50 - # value = vertex.vertex._data
51 - # |> Map.get(field)
52 - # {:ok, value}
53 - # end
54 - # defp to_vertex(document) do
55 - # doc = Map.from_struct(document)
56 - # struct(Xarango.Vertex, doc)
57 - # end
58 - # end
59 - # end
60 - #
61 - #
62 - # end
\ No newline at end of file
Loading more files…