Class: Google::Cloud::Datastore::Commit

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/commit.rb

Overview

Commit

Object yielded from commit methods to allow multiple changes to be made in a single commit.

See Dataset#commit and Transaction#commit.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

datastore.commit do |c|
  c.save task3, task4
  c.delete task1, task2
end

Instance Method Summary collapse

Instance Method Details

#delete(*entities_or_keys) ⇒ Object

Remove entities from the Datastore.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

datastore.commit do |c|
  c.delete task1, task2
end

Parameters:

  • entities_or_keys (Entity, Key)

    One or more Entity or Key objects to remove.



126
127
128
129
130
131
132
133
# File 'lib/google/cloud/datastore/commit.rb', line 126

def delete *entities_or_keys
  keys = Array(entities_or_keys).flatten.map do |e_or_k|
    e_or_k.respond_to?(:key) ? e_or_k.key : e_or_k
  end
  @shared_deletes += keys unless keys.empty?
  # Do not delete yet
  true
end

#insert(*entities) ⇒ Object

Inserts entities to the Datastore.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

datastore.commit do |c|
  c.insert task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to insert.



83
84
85
86
87
88
# File 'lib/google/cloud/datastore/commit.rb', line 83

def insert *entities
  entities = Array(entities).flatten
  @shared_inserts += entities unless entities.empty?
  # Do not insert yet
  entities
end

#save(*entities) ⇒ Object Also known as: upsert

Saves entities to the Datastore.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

datastore.commit do |c|
  c.save task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to save.



61
62
63
64
65
66
# File 'lib/google/cloud/datastore/commit.rb', line 61

def save *entities
  entities = Array(entities).flatten
  @shared_upserts += entities unless entities.empty?
  # Do not save yet
  entities
end

#update(*entities) ⇒ Object

Updates entities to the Datastore.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

datastore.commit do |c|
  c.update task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to update.



104
105
106
107
108
109
# File 'lib/google/cloud/datastore/commit.rb', line 104

def update *entities
  entities = Array(entities).flatten
  @shared_updates += entities unless entities.empty?
  # Do not update yet
  entities
end