Firestore in Datastore mode API - Class Google::Cloud::Datastore::Key (v2.3.1)

Reference documentation and code samples for the Firestore in Datastore mode API 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?

Returns
  • (Boolean)

#dataset_id

def dataset_id() -> String
Alias Of: #project

The project of the Key.

Returns
  • (String)
Example
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
Alias Of: #project=

The project of the Key.

Returns
  • (String)
Example
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.

Returns
  • (Integer, nil)
Example
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?

Returns
  • (Boolean)

#initialize

def initialize(kind = nil, id_or_name = nil) -> Google::Cloud::Datastore::Dataset::Key

Create a new Key instance.

Parameters
  • 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.
Returns
  • (Google::Cloud::Datastore::Dataset::Key)
Example
require "google/cloud/datastore"

task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"

#kind

def kind() -> String

The kind of the Key.

Returns
  • (String)
Example
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.

Parameter
  • value (String)
Returns
  • (String)
Example
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.

Returns
  • (String, nil)
Example
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.

Returns
  • (String, nil)
Example
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.

Parameter
  • value (String, nil)
Returns
  • (String, nil)
Example
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.

Returns
Example
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.

Parameter
  • value (Key, nil)
Returns
Examples
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.

Returns
  • (Array<Array<(String, String)>>)
Example
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.

Returns
  • (String)
Example
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.

Parameter
  • value (String)
Returns
  • (String)
Example
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
Alias Of: #project

The project of the Key.

Returns
  • (String)
Example
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
Alias Of: #project=

The project of the Key.

Returns
  • (String)
Example
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.