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 Database#transaction completes. Transactions will be automatically retried when documents change before the transaction is committed. See Database#transaction.
Access collapse
-
#get(obj) {|documents| ... } ⇒ DocumentReference, Enumerator<DocumentReference>
(also: #run)
Retrieves document snapshots for the given value.
-
#get_all(*docs) {|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.
285 286 287 288 289 290 291 292 293 |
# File 'lib/google/cloud/firestore/transaction.rb', line 285 def create doc, data ensure_not_closed! doc_path = coalesce_doc_path_argument doc @writes << Convert.writes_for_create(doc_path, data) nil end |
#delete(doc, exists: nil, update_time: nil) ⇒ Object
Deletes a document from the database.
559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/google/cloud/firestore/transaction.rb', line 559 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| ... } ⇒ DocumentReference, Enumerator<DocumentReference> 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.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/google/cloud/firestore/transaction.rb', line 214 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, self) end end |
#get_all(*docs) {|documents| ... } ⇒ Enumerator<DocumentSnapshot> Also known as: get_docs, get_documents, find
Retrieves a list of document snapshots.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/google/cloud/firestore/transaction.rb', line 105 def get_all *docs ensure_not_closed! ensure_service! return enum_for(:get_all, docs) unless block_given? doc_paths = Array(docs).flatten.map do |doc_path| coalesce_doc_path_argument doc_path end results = service.get_documents \ doc_paths, transaction: transaction_or_create results.each do |result| extract_transaction_from_result! result next if result.result.nil? yield DocumentSnapshot.from_batch_result(result, self) 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.
386 387 388 389 390 391 392 393 394 |
# File 'lib/google/cloud/firestore/transaction.rb', line 386 def set doc, data, merge: nil ensure_not_closed! doc_path = coalesce_doc_path_argument doc @writes << Convert.writes_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.
492 493 494 495 496 497 498 499 500 501 |
# File 'lib/google/cloud/firestore/transaction.rb', line 492 def update doc, data, update_time: nil ensure_not_closed! doc_path = coalesce_doc_path_argument doc @writes << Convert.writes_for_update(doc_path, data, update_time: update_time) nil end |