Class: Google::Cloud::Firestore::DocumentReference
- Inherits:
-
Object
- Object
- Google::Cloud::Firestore::DocumentReference
- Defined in:
- lib/google/cloud/firestore/document_reference.rb,
lib/google/cloud/firestore/document_reference/list.rb
Overview
DocumentReference
A document reference object refers to a document location in a Cloud Firestore database and can be used to write or read data. A document resource at the referenced location may or may not exist.
Defined Under Namespace
Classes: List
Access collapse
-
#col(collection_path) ⇒ CollectionReference
(also: #collection)
Retrieves a collection nested under the document snapshot.
-
#cols {|collections| ... } ⇒ Enumerator<CollectionReference>
(also: #collections, #list_collections)
Retrieves a list of collections nested under the document snapshot.
-
#get ⇒ DocumentSnapshot
Retrieve the document data.
-
#listen {|callback| ... } ⇒ DocumentListener
(also: #on_snapshot)
Listen to this document reference for changes.
-
#parent ⇒ CollectionReference
The collection the document reference belongs to.
Modifications collapse
-
#create(data) ⇒ CommitResponse::WriteResult
Create a document with the provided data (fields and values).
-
#delete(exists: nil, update_time: nil) ⇒ CommitResponse::WriteResult
Deletes a document from the database.
-
#set(data, merge: nil) ⇒ CommitResponse::WriteResult
Write the provided data (fields and values) to the document.
-
#update(data, update_time: nil) ⇒ CommitResponse::WriteResult
Update the document with the provided data (fields and values).
Instance Method Summary collapse
-
#document_id ⇒ String
The document identifier for the document resource.
-
#document_path ⇒ String
A string representing the path of the document, relative to the document root of the database.
Instance Method Details
#col(collection_path) ⇒ CollectionReference Also known as: collection
Retrieves a collection nested under the document snapshot.
122 123 124 125 126 127 128 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 122 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}/#{collection_path}", client end |
#cols {|collections| ... } ⇒ Enumerator<CollectionReference> Also known as: collections, list_collections
Retrieves a list of collections nested under the document snapshot.
92 93 94 95 96 97 98 99 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 92 def cols ensure_service! return enum_for :cols unless block_given? collection_ids = service.list_collections path collection_ids.each { |collection_id| yield col collection_id } end |
#create(data) ⇒ CommitResponse::WriteResult
Create a document with the provided data (fields and values).
The operation will fail if the document already exists.
240 241 242 243 244 245 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 240 def create data ensure_client! resp = client.batch { |b| b.create self, data } resp.write_results.first end |
#delete(exists: nil, update_time: nil) ⇒ CommitResponse::WriteResult
Deletes a document from the database.
452 453 454 455 456 457 458 459 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 452 def delete exists: nil, update_time: nil ensure_client! resp = client.batch do |b| b.delete self, exists: exists, update_time: update_time end resp.write_results.first end |
#document_id ⇒ String
The document identifier for the document resource.
49 50 51 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 49 def document_id path.split("/").last end |
#document_path ⇒ String
A string representing the path of the document, relative to the document root of the database.
58 59 60 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 58 def document_path path.split("/", 6).last end |
#get ⇒ DocumentSnapshot
Retrieve the document data.
147 148 149 150 151 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 147 def get ensure_client! client.get_all([self]).first end |
#listen {|callback| ... } ⇒ DocumentListener Also known as: on_snapshot
Listen to this document reference for changes.
178 179 180 181 182 183 184 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 178 def listen &callback raise ArgumentError, "callback required" if callback.nil? ensure_client! DocumentListener.new(self, &callback).start end |
#parent ⇒ CollectionReference
The collection the document reference belongs to.
202 203 204 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 202 def parent CollectionReference.from_path parent_path, client end |
#set(data, merge: nil) ⇒ CommitResponse::WriteResult
Write the provided data (fields and values) to the 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.
319 320 321 322 323 324 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 319 def set data, merge: nil ensure_client! resp = client.batch { |b| b.set self, data, merge: merge } resp.write_results.first end |
#update(data, update_time: nil) ⇒ CommitResponse::WriteResult
Update 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.
401 402 403 404 405 406 407 408 |
# File 'lib/google/cloud/firestore/document_reference.rb', line 401 def update data, update_time: nil ensure_client! resp = client.batch do |b| b.update self, data, update_time: update_time end resp.write_results.first end |