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:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.save task1, task2
  c.delete entity1, entity2
end

Instance Method Summary collapse

Instance Method Details

#delete(*entities_or_keys) ⇒ Object

Remove entities from the Datastore.

Examples:

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.



111
112
113
114
115
116
117
118
# File 'lib/google/cloud/datastore/commit.rb', line 111

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:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.insert task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to insert.



74
75
76
77
78
79
# File 'lib/google/cloud/datastore/commit.rb', line 74

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:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.save task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to save.



55
56
57
58
59
60
# File 'lib/google/cloud/datastore/commit.rb', line 55

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:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.update task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to update.



92
93
94
95
96
97
# File 'lib/google/cloud/datastore/commit.rb', line 92

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