Skip to content

Elixir library for interacting with Google's Cloud Datastore

Notifications You must be signed in to change notification settings

noizu-labs/diplomat

 
 

Repository files navigation

Build Status

Diplomat

Diplomat is an Elixir library for interacting with Google's Cloud Datastore.

Installation

  1. Add datastore to your list of dependencies in mix.exs:
def deps do
  [{:diplomat, "~> 0.2"}]
end
  1. Make sure you've configured Goth with your credentials:
config :goth,
  json: {:system, "GCP_CREDENTIALS_JSON"}

Usage

Insert an Entity:

Diplomat.Entity.new(
  %{"name" => "My awesome book", "author" => "Phil Burrows"},
  "Book",
  "my-unique-book-id"
) |> Diplomat.Entity.insert

Find an Entity via a GQL Query:

Diplomat.Query.new(
  "select * from `Book` where name = @name",
  %{name: "20,000 Leagues Under The Sea"}
) |> Diplomat.Query.execute

Use multiple accounts with Diplomat

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)

About

Elixir library for interacting with Google's Cloud Datastore

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Elixir 100.0%