Class: Google::Cloud::Firestore::Client
- Inherits:
-
Object
- Object
- Google::Cloud::Firestore::Client
- Defined in:
- lib/google/cloud/firestore/client.rb
Overview
Client
The Cloud Firestore Client used is to access and manipulate the collections and documents in the Firestore database.
Access collapse
-
#col(collection_path) ⇒ CollectionReference
(also: #collection)
Retrieves a collection.
-
#cols {|collections| ... } ⇒ Enumerator<CollectionReference>
(also: #collections)
Retrieves a list of collections.
-
#doc(document_path) ⇒ DocumentReference
(also: #document)
Retrieves a document reference.
-
#document_id ⇒ FieldPath
Creates a field path object representing the sentinel ID of a document.
-
#field_delete ⇒ FieldValue
Creates a field value object representing the deletion of a field in document data.
-
#field_path(*fields) ⇒ FieldPath
Creates a field path object representing a nested field for document data.
-
#field_server_time ⇒ FieldValue
Creates a field value object representing set a field's value to the server timestamp when accessing the document data.
-
#get_all(*docs) {|documents| ... } ⇒ Enumerator<DocumentSnapshot>
(also: #get_docs, #get_documents, #find)
Retrieves a list of document snapshots.
Operations collapse
-
#batch {|batch| ... } ⇒ CommitResponse
Perform multiple changes at the same time.
-
#transaction(max_retries: nil) {|transaction| ... } ⇒ CommitResponse
Create a transaction to perform multiple reads and writes that are executed atomically at a single logical point in time in a database.
Instance Method Summary collapse
-
#database_id ⇒ String
The database identifier for the Cloud Firestore database.
-
#project_id ⇒ String
The project identifier for the Cloud Firestore database.
Instance Method Details
#batch {|batch| ... } ⇒ CommitResponse
Perform multiple changes at the same time.
All changes are accumulated in memory until the block completes. Unlike transactions, batches don't lock on document reads, should only fail if users provide preconditions, and are not automatically retried. See Batch.
342 343 344 345 346 |
# File 'lib/google/cloud/firestore/client.rb', line 342 def batch batch = Batch.from_client self yield batch batch.commit end |
#col(collection_path) ⇒ CollectionReference Also known as: collection
Retrieves a collection.
131 132 133 134 135 136 137 138 |
# File 'lib/google/cloud/firestore/client.rb', line 131 def col collection_path if collection_path.to_s.split("/").count.even? raise ArgumentError, "collection_path must refer to a collection." end CollectionReference.from_path \ "#{path}/documents/#{collection_path}", self end |
#cols {|collections| ... } ⇒ Enumerator<CollectionReference> Also known as: collections
Retrieves a list of collections.
102 103 104 105 106 107 108 109 |
# File 'lib/google/cloud/firestore/client.rb', line 102 def cols ensure_service! return enum_for(:cols) unless block_given? collection_ids = service.list_collections "#{path}/documents" collection_ids.each { |collection_id| yield col(collection_id) } end |
#database_id ⇒ String
The database identifier for the Cloud Firestore database.
70 71 72 |
# File 'lib/google/cloud/firestore/client.rb', line 70 def database_id "(default)" end |
#doc(document_path) ⇒ DocumentReference Also known as: document
Retrieves a document reference.
159 160 161 162 163 164 165 166 167 |
# File 'lib/google/cloud/firestore/client.rb', line 159 def doc document_path if document_path.to_s.split("/").count.odd? raise ArgumentError, "document_path must refer to a document." end doc_path = "#{path}/documents/#{document_path}" DocumentReference.from_path doc_path, self end |
#document_id ⇒ FieldPath
Creates a field path object representing the sentinel ID of a document. It can be used in queries to sort or filter by the document ID. See #document_id.
236 237 238 |
# File 'lib/google/cloud/firestore/client.rb', line 236 def document_id FieldPath.document_id end |
#field_delete ⇒ FieldValue
Creates a field value object representing the deletion of a field in document data.
281 282 283 |
# File 'lib/google/cloud/firestore/client.rb', line 281 def field_delete FieldValue.delete end |
#field_path(*fields) ⇒ FieldPath
Creates a field path object representing a nested field for document data.
260 261 262 |
# File 'lib/google/cloud/firestore/client.rb', line 260 def field_path *fields FieldPath.new(*fields) end |
#field_server_time ⇒ FieldValue
Creates a field value object representing set a field's value to the server timestamp when accessing the document data.
302 303 304 |
# File 'lib/google/cloud/firestore/client.rb', line 302 def field_server_time FieldValue.server_time end |
#get_all(*docs) {|documents| ... } ⇒ Enumerator<DocumentSnapshot> Also known as: get_docs, get_documents, find
Retrieves a list of document snapshots.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/google/cloud/firestore/client.rb', line 193 def get_all *docs 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 results.each do |result| next if result.result.nil? yield DocumentSnapshot.from_batch_result(result, self) end end |
#project_id ⇒ String
The project identifier for the Cloud Firestore database.
62 63 64 |
# File 'lib/google/cloud/firestore/client.rb', line 62 def project_id service.project end |
#transaction(max_retries: nil) {|transaction| ... } ⇒ CommitResponse
Create a transaction to perform multiple reads and writes that are executed atomically at a single logical point in time in a database.
All changes are accumulated in memory until the block completes. Transactions will be automatically retried when documents change before the transaction is committed. See Transaction.
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/google/cloud/firestore/client.rb', line 384 def transaction max_retries: nil max_retries = 5 unless max_retries.is_a? Integer retries = 0 backoff = 1.0 transaction = Transaction.from_client self begin yield transaction transaction.commit rescue Google::Cloud::UnavailableError => err # Re-raise if deadline has passed raise err if retries >= max_retries # Sleep with incremental backoff sleep(backoff *= 1.3) # Create new transaction and retry transaction = Transaction.from_client \ self, previous_transaction: transaction.transaction_id retries += 1 retry rescue Google::Cloud::InvalidArgumentError => err # Return if a previous call has succeeded return nil if retries > 0 # Re-raise error. raise err rescue StandardError => err # Rollback transaction when handling unexpected error transaction.rollback rescue nil # Re-raise error. raise err end end |