Class: Google::Cloud::Firestore::Transaction
- Inherits:
-
Object
- Object
- Google::Cloud::Firestore::Transaction
- Defined in:
- lib/google/cloud/firestore/transaction.rb
Overview
Transaction
A transaction in Cloud Firestore is a set of reads and writes that execute atomically at a single logical point in time.
All changes are accumulated in memory until the block passed to Client#transaction completes. Transactions will be automatically retried when documents change before the transaction is committed. See Client#transaction.
Access collapse
-
#get(obj) {|documents| ... } ⇒ DocumentSnapshot, Enumerator<DocumentSnapshot>
(also: #run)
Retrieves document snapshots for the given value.
-
#get_aggregate(aggregate_query) {|documents| ... } ⇒ Object
Retrieves aggregate query snapshots for the given value.
-
#get_all(*docs, field_mask: nil) {|documents| ... } ⇒ Enumerator<DocumentSnapshot>
(also: #get_docs, #get_documents, #find)
Retrieves a list of document snapshots.
Modifications collapse
-
#create(doc, data) ⇒ Object
Creates a document with the provided data (fields and values).
-
#delete(doc, exists: nil, update_time: nil) ⇒ Object
Deletes a document from the database.
-
#set(doc, data, merge: nil) ⇒ Object
Writes the provided data (fields and values) to the provided document.
-
#update(doc, data, update_time: nil) ⇒ Object
Updates the document with the provided data (fields and values).
Instance Method Summary collapse
-
#firestore ⇒ Client
(also: #client)
The client the Cloud Firestore transaction belongs to.
-
#transaction_id ⇒ String
The transaction identifier.
Instance Method Details
#create(doc, data) ⇒ Object
Creates a document with the provided data (fields and values).
The operation will fail if the document already exists.
358 359 360 361 362 363 364 365 366 |
# File 'lib/google/cloud/firestore/transaction.rb', line 358 def create doc, data ensure_not_closed! doc_path = coalesce_doc_path_argument doc @writes << Convert.write_for_create(doc_path, data) nil end |
#delete(doc, exists: nil, update_time: nil) ⇒ Object
Deletes a document from the database.
630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/google/cloud/firestore/transaction.rb', line 630 def delete doc, exists: nil, update_time: nil ensure_not_closed! doc_path = coalesce_doc_path_argument doc @writes << Convert.write_for_delete( doc_path, exists: exists, update_time: update_time ) nil end |
#firestore ⇒ Client Also known as: client
The client the Cloud Firestore transaction belongs to.
74 75 76 |
# File 'lib/google/cloud/firestore/transaction.rb', line 74 def firestore @client end |
#get(obj) {|documents| ... } ⇒ DocumentSnapshot, Enumerator<DocumentSnapshot> Also known as: run
Retrieves document snapshots for the given value. Valid values can be a string representing either a document or a collection of documents, a document reference object, a collection reference object, or a query to be run.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/google/cloud/firestore/transaction.rb', line 248 def get obj ensure_not_closed! ensure_service! obj = coalesce_get_argument obj if obj.is_a? DocumentReference doc = get_all([obj]).first yield doc if block_given? return doc end return enum_for :get, obj unless block_given? results = service.run_query obj.parent_path, obj.query, transaction: transaction_or_create results.each do |result| extract_transaction_from_result! result next if result.document.nil? yield DocumentSnapshot.from_query_result result, client end end |
#get_aggregate(aggregate_query) {|documents| ... } ⇒ Object
Retrieves aggregate query snapshots for the given value. Valid values can be a string representing either a document or a collection of documents, a document reference object, a collection reference object, or a query to be run.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/google/cloud/firestore/transaction.rb', line 295 def get_aggregate aggregate_query ensure_not_closed! ensure_service! return enum_for :get_aggregate, aggregate_query unless block_given? results = service.run_aggregate_query aggregate_query.parent_path, aggregate_query.structured_aggregation_query, transaction: transaction_or_create results.each do |result| extract_transaction_from_result! result next if result.result.nil? yield AggregateQuerySnapshot.from_run_aggregate_query_response result end end |
#get_all(*docs, field_mask: nil) {|documents| ... } ⇒ Enumerator<DocumentSnapshot> Also known as: get_docs, get_documents, find
Retrieves a list of document snapshots.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/google/cloud/firestore/transaction.rb', line 129 def get_all *docs, field_mask: nil ensure_not_closed! ensure_service! unless block_given? return enum_for :get_all, *docs, field_mask: field_mask end doc_paths = Array(docs).flatten.map do |doc_path| coalesce_doc_path_argument doc_path end mask = Array(field_mask).map do |field_path| if field_path.is_a? FieldPath field_path.formatted_string else FieldPath.parse(field_path).formatted_string end end mask = nil if mask.empty? results = service.get_documents \ doc_paths, mask: mask, transaction: transaction_or_create results.each do |result| extract_transaction_from_result! result next if result.result.nil? yield DocumentSnapshot.from_batch_result result, client end end |
#set(doc, data, merge: nil) ⇒ Object
Writes the provided data (fields and values) to the provided document.
If the document does not exist, it will be created. By default, the
provided data overwrites existing data, but the provided data can be
merged into the existing document using the merge
argument.
If you're not sure whether the document exists, use the merge
argument to merge the new data with any existing document data to
avoid overwriting entire documents.
459 460 461 462 463 464 465 466 467 |
# File 'lib/google/cloud/firestore/transaction.rb', line 459 def set doc, data, merge: nil ensure_not_closed! doc_path = coalesce_doc_path_argument doc @writes << Convert.write_for_set(doc_path, data, merge: merge) nil end |
#transaction_id ⇒ String
The transaction identifier.
66 67 68 |
# File 'lib/google/cloud/firestore/transaction.rb', line 66 def transaction_id @transaction_id end |
#update(doc, data, update_time: nil) ⇒ Object
Updates the document with the provided data (fields and values). The provided data is merged into the existing document data.
The operation will fail if the document does not exist.
563 564 565 566 567 568 569 570 571 572 |
# File 'lib/google/cloud/firestore/transaction.rb', line 563 def update doc, data, update_time: nil ensure_not_closed! doc_path = coalesce_doc_path_argument doc @writes << Convert.write_for_update(doc_path, data, update_time: update_time) nil end |