Skip to content

Commit d342b93

Browse files
authored
Merge pull request #4 from TomGrozev/master
Add the :database option for transactions and cursors
2 parents 8ce80b9 + c4b9694 commit d342b93

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/arangox.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ defmodule Arangox do
327327
* `:read` - An array of collection names or a single collection name as a binary.
328328
* `:write` - An array of collection names or a single collection name as a binary.
329329
* `:exclusive` - An array of collection names or a single collection name as a binary.
330+
* `:database` - Sets what database to run the transaction on
330331
* `:properties` - A list or map of additional body attributes to append to the request
331332
body when beginning a transaction.
332333
@@ -387,6 +388,7 @@ defmodule Arangox do
387388
Accepts any of the options accepted by `DBConnection.stream/4`, as well as any of the
388389
following:
389390
391+
* `:database` - Sets what database to run the cursor query on
390392
* `:properties` - A list or map of additional body attributes to append to the
391393
request body when creating the cursor.
392394

lib/arangox/connection.ex

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,11 @@ defmodule Arangox.Connection do
467467
end
468468

469469
@impl true
470-
def handle_execute(_q, %Request{} = request, _opts, %__MODULE__{} = state) do
470+
def handle_execute(_q, %Request{} = request, opts, %__MODULE__{} = state) do
471471
request =
472472
request
473473
|> merge_headers(state.headers)
474-
|> maybe_prepend_database(state)
474+
|> maybe_prepend_database(state, opts)
475475
|> maybe_encode_body(state)
476476

477477
case Client.request(request, state) do
@@ -533,17 +533,27 @@ defmodule Arangox.Connection do
533533

534534
defp sanitize_headers(%{headers: _headers} = struct) when is_map(struct), do: struct
535535

536+
defp maybe_prepend_database(%Request{path: path} = request, state, opts) do
537+
case Keyword.get(opts, :database) do
538+
nil ->
539+
do_db_prepend(request, state)
540+
541+
db ->
542+
%{request | path: "/_db/" <> db <> path}
543+
end
544+
end
545+
536546
# Only prepends when not velocy or nil or path already contains /_db/
537-
defp maybe_prepend_database(%Request{} = request, %{client: VelocyClient}),
547+
defp do_db_prepend(%Request{} = request, %{client: VelocyClient}),
538548
do: request
539549

540-
defp maybe_prepend_database(%Request{} = request, %{database: nil}),
550+
defp do_db_prepend(%Request{} = request, %{database: nil}),
541551
do: request
542552

543-
defp maybe_prepend_database(%Request{path: "/_db/" <> _} = request, %{database: _db}),
553+
defp do_db_prepend(%Request{path: "/_db/" <> _} = request, %{database: _db}),
544554
do: request
545555

546-
defp maybe_prepend_database(%Request{path: path} = request, %{database: db}),
556+
defp do_db_prepend(%Request{path: path} = request, %{database: db}),
547557
do: %{request | path: "/_db/" <> db <> path}
548558

549559
# Only encodes when not velocy or empty string

0 commit comments

Comments
 (0)