Diplomat is an Elixir library for interacting with Google's Cloud Datastore.
- Add datastore to your list of dependencies in
mix.exs
:
def deps do
[{:diplomat, "~> 0.2"}]
end
- Make sure you've configured Goth with your credentials:
config :goth,
json: {:system, "GCP_CREDENTIALS_JSON"}
Diplomat.Entity.new(
%{"name" => "My awesome book", "author" => "Phil Burrows"},
"Book",
"my-unique-book-id"
) |> Diplomat.Entity.insert
Diplomat.Query.new(
"select * from `Book` where name = @name",
%{name: "20,000 Leagues Under The Sea"}
) |> Diplomat.Query.execute
Configure Goth with additional accounts.
{:ok, alternative_account} = Jason.decode(File.read!("priv/goth/alternative-account.json"))
Goth.Config.add_config(alternative_account)
Require Diplomat and use the with_account option to set current (and only current) process to use alternative process within block.
The account name will be the client_email value from the additional Goth configuration you added by default.
require Diplomat
# copy data from prod to stage environment
# 1. Fetch data from production account
prod_entities = Diplomat.with_account(alternative_account["client_email"]) do
Diplomat.Query.new(
"select * from `Book` where name = @name",
%{name: "20,000 Leagues Under The Sea"}
) |> Diplomat.Query.execute
end
# 2. Write to stage/dev account (default environment)
target_project = Diplomat.Client.project()
stage_entities = Enum.map(prod_entities, fn(entity) ->
put_in(entity, [Access.key(:key), Access.key(:project_id)], target_project)
end)
Diplomat.Entity.upsert(stage_entities)