Reference documentation and code samples for the google-cloud-datastore class Google::Cloud::Datastore::Key.
Key
Every Datastore record has an identifying key, which includes the record's entity kind and a unique identifier. The identifier may be either a key name string, assigned explicitly by the application, or an integer numeric ID, assigned automatically by Datastore.
Inherits
- Object
Example
require "google/cloud/datastore" task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"
Methods
#complete?
def complete?() -> Boolean
Determine if the key is complete. A complete key has either an id or a name.
Inverse of #incomplete?
- (Boolean)
#dataset_id
def dataset_id() -> String
The project of the Key.
- (String)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.project #=> "my-todo-project"
#dataset_id=
def dataset_id=(value) -> String
The project of the Key.
- (String)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.project #=> "my-todo-project"
#id
def id() -> Integer, nil
The id of the Key.
- (Integer, nil)
require "google/cloud/datastore" task_key = Google::Cloud::Datastore::Key.new "Task", 123456 task_key.id #=> 123456
#incomplete?
def incomplete?() -> Boolean
Determine if the key is incomplete. An incomplete key has neither an id nor a name.
Inverse of #complete?
- (Boolean)
#initialize
def initialize(kind = nil, id_or_name = nil) -> Google::Cloud::Datastore::Dataset::Key
Create a new Key instance.
- kind (String) — The kind of the Key. This is optional.
- id_or_name (Integer, String) — The id or name of the Key. This is optional.
- (Google::Cloud::Datastore::Dataset::Key)
require "google/cloud/datastore" task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"
#kind
def kind() -> String
The kind of the Key.
- (String)
require "google/cloud/datastore" key = Google::Cloud::Datastore::Key.new "TaskList" key.kind #=> "TaskList" key.kind = "Task"
#kind=
def kind=(value) -> String
The kind of the Key.
- value (String)
- (String)
require "google/cloud/datastore" key = Google::Cloud::Datastore::Key.new "TaskList" key.kind #=> "TaskList" key.kind = "Task"
#name
def name() -> String, nil
The name of the Key.
- (String, nil)
require "google/cloud/datastore" task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask" task_key.name #=> "sampleTask"
#namespace
def namespace() -> String, nil
The namespace of the Key.
- (String, nil)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.namespace #=> "example-ns"
#namespace=
def namespace=(value) -> String, nil
The namespace of the Key.
- value (String, nil)
- (String, nil)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.namespace #=> "example-ns"
#parent
def parent() -> Key, nil
The parent of the Key.
- (Key, nil)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new task_list = datastore.find "TaskList", "default" query = datastore.query("Task"). ancestor(task_list) lists = datastore.run query lists.first.key.parent # Key("TaskList", "default")
#parent=
def parent=(new_parent) -> Key, nil
Set the parent of the Key.
- value (Key, nil)
- (Key, nil)
require "google/cloud/datastore" parent_key = Google::Cloud::Datastore::Key.new "TaskList", "default" task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask" task_key.parent = parent_key
With multiple levels:
require "google/cloud/datastore" user_key = Google::Cloud::Datastore::Key.new "User", "alice" list_key = Google::Cloud::Datastore::Key.new "TaskList", "default" task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask" list_key.parent = user_key task_key.parent = list_key
#path
def path() -> Array<Array<(String, String)>>
Represent the Key's path (including parent) as an array of arrays. Each inner array contains two values, the kind and the id or name. If neither an id or name exist then nil will be returned.
- (Array<Array<(String, String)>>)
require "google/cloud/datastore" parent_key = Google::Cloud::Datastore::Key.new "TaskList", "default" task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask" task_key.parent = parent_key task_key.path #=> [["TaskList", "default"], ["Task", "sampleTask"]]
#project
def project() -> String
The project of the Key.
- (String)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.project #=> "my-todo-project"
#project=
def project=(value) -> String
The project of the Key.
- value (String)
- (String)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.project #=> "my-todo-project"
#project_id
def project_id() -> String
The project of the Key.
- (String)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.project #=> "my-todo-project"
#project_id=
def project_id=(value) -> String
The project of the Key.
- (String)
require "google/cloud/datastore" datastore = Google::Cloud::Datastore.new( project: "my-todo-project", keyfile: "/path/to/keyfile.json" ) task = datastore.find "Task", "sampleTask" task.key.project #=> "my-todo-project"
#serialized_size
def serialized_size()
The number of bytes the Key will take to serialize during API calls.