Module: Google::Cloud::Datastore

Defined in:
lib/google/cloud/datastore.rb,
lib/google/cloud/datastore/key.rb,
lib/google/cloud/datastore/query.rb,
lib/google/cloud/datastore/commit.rb,
lib/google/cloud/datastore/cursor.rb,
lib/google/cloud/datastore/entity.rb,
lib/google/cloud/datastore/errors.rb,
lib/google/cloud/datastore/convert.rb,
lib/google/cloud/datastore/dataset.rb,
lib/google/cloud/datastore/service.rb,
lib/google/cloud/datastore/version.rb,
lib/google/cloud/datastore/gql_query.rb,
lib/google/cloud/datastore/properties.rb,
lib/google/cloud/datastore/credentials.rb,
lib/google/cloud/datastore/transaction.rb,
lib/google/cloud/datastore/v1/credentials.rb,
lib/google/cloud/datastore/v1/datastore_client.rb,
lib/google/cloud/datastore/dataset/query_results.rb,
lib/google/cloud/datastore/read_only_transaction.rb,
lib/google/cloud/datastore/dataset/lookup_results.rb

Overview

Google Cloud Datastore

Google Cloud Datastore is a fully managed, schemaless database for storing non-relational data. You should feel at home if you are familiar with relational databases, but there are some key differences to be aware of to make the most of using Datastore.

See Datastore Overview.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new(
  project_id: "my-todo-project",
  credentials: "/path/to/keyfile.json"
)

task = datastore.find "Task", "sampleTask"
task["priority"] = 5
datastore.save task

Defined Under Namespace

Modules: V1 Classes: Commit, Credentials, Cursor, Dataset, Entity, GqlQuery, Key, KeyError, Properties, PropertyError, Query, ReadOnlyTransaction, Transaction, TransactionError

Constant Summary collapse

VERSION =
"1.8.2".freeze

Class Method Summary collapse

Class Method Details

.configure {|Google::Cloud.configure.datastore| ... } ⇒ Google::Cloud::Config

Configure the Google Cloud Datastore library.

The following Datastore configuration parameters are supported:

  • project_id - (String) Identifier for a Datastore project. (The parameter project is considered deprecated, but may also be used.)
  • credentials - (String, Hash, Google::Auth::Credentials) The path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object. (See Credentials) (The parameter keyfile is considered deprecated, but may also be used.)
  • scope - (String, Array) The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access.
  • timeout - (Integer) Default timeout to use in requests.
  • client_config - (Hash) A hash of values to override the default behavior of the API client.
  • endpoint - (String) Override of the endpoint host name, or nil to use the default endpoint.
  • emulator_host - (String) Host name of the emulator. Defaults to ENV["DATASTORE_EMULATOR_HOST"]

Yields:

Returns:

  • (Google::Cloud::Config)

    The configuration object the Google::Cloud::Datastore library uses.



171
172
173
174
175
# File 'lib/google/cloud/datastore.rb', line 171

def self.configure
  yield Google::Cloud.configure.datastore if block_given?

  Google::Cloud.configure.datastore
end

.new(project_id: nil, credentials: nil, scope: nil, timeout: nil, client_config: nil, endpoint: nil, emulator_host: nil, project: nil, keyfile: nil) ⇒ Google::Cloud::Datastore::Dataset

Creates a new object for connecting to the Datastore service. Each call creates a new connection.

For more information on connecting to Google Cloud see the Authentication Guide.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new(
  project_id: "my-todo-project",
  credentials: "/path/to/keyfile.json"
)

task = datastore.entity "Task", "sampleTask" do |t|
  t["type"] = "Personal"
  t["done"] = false
  t["priority"] = 4
  t["description"] = "Learn Cloud Datastore"
end

datastore.save task

Parameters:

  • project_id (String) (defaults to: nil)

    Identifier for a Datastore project. If not present, the default project for the credentials is used.

  • credentials (String, Hash, Google::Auth::Credentials) (defaults to: nil)

    The path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object. (See Credentials)

  • scope (String, Array<String>) (defaults to: nil)

    The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access. See Using OAuth 2.0 to Access Google APIs.

    The default scope is:

    • https://www.googleapis.com/auth/datastore
  • timeout (Integer) (defaults to: nil)

    Default timeout to use in requests. Optional.

  • client_config (Hash) (defaults to: nil)

    A hash of values to override the default behavior of the API client. See Google::Gax::CallSettings. Optional.

  • endpoint (String) (defaults to: nil)

    Override of the endpoint host name. Optional. If the param is nil, uses the default endpoint.

  • emulator_host (String) (defaults to: nil)

    Datastore emulator host. Optional. If the param is nil, uses the value of the emulator_host config.

  • project (String) (defaults to: nil)

    Alias for the project_id argument. Deprecated.

  • keyfile (String) (defaults to: nil)

    Alias for the credentials argument. Deprecated.

Returns:

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/google/cloud/datastore.rb', line 102

def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
             client_config: nil, endpoint: nil, emulator_host: nil,
             project: nil, keyfile: nil
  project_id    ||= (project || default_project_id)
  scope         ||= configure.scope
  timeout       ||= configure.timeout
  client_config ||= configure.client_config
  endpoint      ||= configure.endpoint
  emulator_host ||= configure.emulator_host

  if emulator_host
    project_id = project_id.to_s # Always cast to a string
    raise ArgumentError, "project_id is missing" if project_id.empty?

    return Datastore::Dataset.new(
      Datastore::Service.new(
        project_id, :this_channel_is_insecure,
        host: emulator_host, timeout: timeout,
        client_config: client_config
      )
    )
  end

  credentials ||= (keyfile || default_credentials(scope: scope))
  unless credentials.is_a? Google::Auth::Credentials
    credentials = Datastore::Credentials.new credentials, scope: scope
  end

  if credentials.respond_to? :project_id
    project_id ||= credentials.project_id
  end
  project_id = project_id.to_s # Always cast to a string
  raise ArgumentError, "project_id is missing" if project_id.empty?

  Datastore::Dataset.new(
    Datastore::Service.new(
      project_id, credentials,
      host: endpoint, timeout: timeout, client_config: client_config
    )
  )
end