Gcloud::Datastore::Dataset

Dataset

Dataset is the data saved in a project's Datastore. Dataset is analogous to a database in relational database world.

Gcloud::Datastore::Dataset is the main object for interacting with Google Datastore. Gcloud::Datastore::Entity objects are created, read, updated, and deleted by Gcloud::Datastore::Dataset.

require "gcloud"

gcloud = Gcloud.new
dataset = gcloud.datastore

query = Gcloud::Datastore::Query.new.kind("Task").
  where("completed", "=", true)

tasks = dataset.run query

See Gcloud#datastore

Methods

Public Instance Methods

allocate_ids(incomplete_key, count = 1)

Generate IDs for a Key before creating an entity.

Parameters

incomplete_key

A Key without id or name set. (Key)

count

The number of new key IDs to create. (Integer)

Returns

Array of Gcloud::Datastore::Key

Example

empty_key = Gcloud::Datastore::Key.new "Task"
task_keys = dataset.allocate_ids empty_key, 5

delete(*entities_or_keys)

Remove entities from the Datastore.

Parameters

entities_or_keys

One or more Entity or Key objects to remove. (Entity or Key)

Returns

true if successful

Example

gcloud = Gcloud.new
dataset = gcloud.datastore
dataset.delete entity1, entity2

find(key_or_kind, id_or_name = nil)

Retrieve an entity by providing key information.

Parameters

key_or_kind

A Key object or kind string value. (Key or String)

id_or_name

The Key's id or name value if a kind was provided in the first parameter. (Integer or String or nil)

Returns

Gcloud::Datastore::Entity or nil

Example

Finding an entity with a key:

key = Gcloud::Datastore::Key.new "Task", 123456
task = dataset.find key

Finding an entity with a kind and id/name:

task = dataset.find "Task", 123456
Also aliased as: get

find_all(*keys)

Retrieve the entities for the provided keys.

Parameters

keys

One or more Key objects to find records for. (Key)

Returns

Gcloud::Datastore::Dataset::LookupResults

Example

gcloud = Gcloud.new
dataset = gcloud.datastore
key1 = Gcloud::Datastore::Key.new "Task", 123456
key2 = Gcloud::Datastore::Key.new "Task", 987654
tasks = dataset.find_all key1, key2
Also aliased as: lookup

get(key_or_kind, id_or_name = nil)

Alias for: find

lookup(*keys)

Alias for: find_all

project()

The Datastore project connected to.

Example

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"

dataset = gcloud.datastore
dataset.project #=> "my-todo-project"

run(query)

Retrieve entities specified by a Query.

Parameters

query

The Query object with the search criteria. (Query)

Returns

Gcloud::Datastore::Dataset::QueryResults

Example

query = Gcloud::Datastore::Query.new.kind("Task").
  where("completed", "=", true)
tasks = dataset.run query
Also aliased as: run_query

run_query(query)

Alias for: run

save(*entities)

Persist one or more entities to the Datastore.

Parameters

entities

One or more entity objects to be saved without id or name set. (Entity)

Returns

Array of Gcloud::Datastore::Entity

Example

dataset.save task1, task2

transaction()

Creates a Datastore Transaction.

Example

Runs the given block in a database transaction:

require "gcloud"

gcloud = Gcloud.new
dataset = gcloud.datastore

key = Gcloud::Datastore::Key.new "User", "heidi"

user = Gcloud::Datastore::Entity.new
user.key = key
user["name"] = "Heidi Henderson"
user["email"] = "heidi@example.net"

dataset.transaction do |tx|
  if tx.find(user.key).nil?
    tx.save user
  end
end

Alternatively, if no block is given a Transaction object is returned:

require "gcloud"

gcloud = Gcloud.new
dataset = gcloud.datastore

key = Gcloud::Datastore::Key.new "User", "heidi"

user = Gcloud::Datastore::Entity.new
user.key = key
user["name"] = "Heidi Henderson"
user["email"] = "heidi@example.net"

tx = dataset.transaction
begin
  if tx.find(user.key).nil?
    tx.save user
  end
  tx.commit
rescue
  tx.rollback
end