Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction support for Query #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/diplomat/entity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ defmodule Diplomat.Entity do
end

# at some point we should validate the entity keys
@spec upsert([t] | t) :: {:ok, CommitResponse.t()} | Client.error()
@spec upsert([t] | t) :: CommitResponse.t() | Client.error()
def upsert(%Entity{} = entity), do: upsert([entity])

def upsert(entities) when is_list(entities) do
Expand Down
2 changes: 1 addition & 1 deletion lib/diplomat/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Diplomat.Query do

RunQueryRequest.new(
query_type: {:gql_query, q |> Query.proto()},
partition_id: PartitionId.new(namespace_id: namespace, proejct_id: project)
partition_id: %PartitionId{namespace_id: namespace, project_id: project}
)
|> Diplomat.Client.run_query()
end
Expand Down
18 changes: 16 additions & 2 deletions lib/diplomat/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ defmodule Diplomat.Transaction do
RollbackRequest,
RollbackResponse,
LookupRequest,
ReadOptions
ReadOptions,
RunQueryRequest,
PartitionId
}

alias Diplomat.{Transaction, Entity, Key, Client}
alias Diplomat.{Transaction, Entity, Key, Client, Query}

@type t :: %__MODULE__{
id: integer,
Expand Down Expand Up @@ -108,6 +110,18 @@ defmodule Diplomat.Transaction do
find(transaction, [key])
end

@spec execute(Transaction.t(), Query.t(), String.t() | nil) :: list(Entity.t()) | Client.error()
def execute(%Transaction{id: id}, %Query{} = q, namespace \\ nil) do
{:ok, project} = Goth.Config.get(:project_id)

RunQueryRequest.new(
query_type: {:gql_query, q |> Query.proto()},
partition_id: %PartitionId{namespace_id: namespace, project_id: project},
read_options: %ReadOptions{consistency_type: {:transaction, id}}
)
|> Diplomat.Client.run_query()
end

# we could clean this up with some macros
@spec insert(t, Entity.t() | [Entity.t()]) :: t
def insert(%Transaction{} = t, %Entity{} = e), do: insert(t, [e])
Expand Down