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
orname
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
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
orString
) id_or_name
-
The Key's
id
orname
value if akind
was provided in the first parameter. (Integer
orString
ornil
)
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
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
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
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