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/doc/overview.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

Ruby Client for Google Cloud Datastore API (Alpha)

Google Cloud Datastore API: Accesses the schemaless NoSQL database to provide fully managed, robust, scalable storage for your application.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.
  2. Enable the Google Cloud Datastore API.
  3. Setup Authentication.

Installation

$ gem install google-cloud-datastore

Next Steps

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.4.0".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.
  • 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.



673
674
675
676
677
# File 'lib/google/cloud/datastore.rb', line 673

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, 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)

    Identifier for a Datastore project. If not present, the default project for the credentials is 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)

  • scope (String, Array<String>)

    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)

    Default timeout to use in requests. Optional.

  • client_config (Hash)

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

  • emulator_host (String)

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

  • project (String)

    Alias for the project_id argument. Deprecated.

  • keyfile (String)

    Alias for the credentials argument. Deprecated.

Returns:

Raises:

  • (ArgumentError)


618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/google/cloud/datastore.rb', line 618

def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
             client_config: nil, emulator_host: nil, project: nil,
             keyfile: nil
  project_id ||= (project || default_project_id)
  project_id = project_id.to_s # Always cast to a string
  raise ArgumentError, "project_id is missing" if project_id.empty?

  scope ||= configure.scope
  timeout ||= configure.timeout
  client_config ||= configure.client_config
  emulator_host ||= configure.emulator_host
  if emulator_host
    return Datastore::Dataset.new(
      Datastore::Service.new(
        project_id, :this_channel_is_insecure,
        host: emulator_host, 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

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