Example #1
0
        /// <summary>
        /// Adds an update operation that updates just the specified fields paths in the document, with the corresponding values.
        /// </summary>
        /// <param name="documentReference">A document reference indicating the path of the document to update. Must not be null.</param>
        /// <param name="updates">The updates to perform on the document, keyed by the field path to update. Fields not present in this dictionary are not updated. Must not be null.</param>
        /// <param name="precondition">Optional precondition for updating the document. May be null, which is equivalent to <see cref="Precondition.MustExist"/>.</param>
        /// <returns>This batch, for the purposes of method chaining.</returns>
        public WriteBatch Update(DocumentReference documentReference, IDictionary <FieldPath, object> updates, Precondition precondition = null)
        {
            GaxPreconditions.CheckNotNull(documentReference, nameof(documentReference));
            GaxPreconditions.CheckNotNull(updates, nameof(updates));

            var serializedUpdates = updates.ToDictionary(pair => pair.Key, pair => ValueSerializer.Serialize(pair.Value));
            var deconstructed     = ExpandObject(serializedUpdates);

            // TODO: Validate that the precondition is reasonable (i.e. not "MustNotExist")?
            var write = new Write
            {
                CurrentDocument = (precondition ?? Precondition.MustExist).Proto,
                Update          = new Document
                {
                    Fields = { deconstructed },
                    Name   = documentReference.Path,
                },
                UpdateMask = new DocumentMask {
                    FieldPaths = { serializedUpdates.Keys.Select(fp => fp.EncodedPath) }
                }
            };

            AddUpdate(write);
            return(this);
        }
Example #2
0
 /// <summary>
 /// Adds an operation to update a document's data in this transaction.
 /// </summary>
 /// <param name="documentReference">The document to update. Must not be null.</param>
 /// <param name="updates">The updates to perform on the document, keyed by the field path to update. Fields not present in this dictionary are not updated. Must not be null.</param>
 /// <param name="precondition">Optional precondition for updating the document. May be null, which is equivalent to <see cref="Precondition.MustExist"/>.</param>
 public void Update(DocumentReference documentReference, Dictionary <FieldPath, object> updates, Precondition precondition = null)
 {
     // Preconditions are validated by WriteBatch.
     _writes.Update(documentReference, updates, precondition);
 }
Example #3
0
 /// <summary>
 /// Adds an operation to delete a document's data in this transaction.
 /// </summary>
 /// <param name="documentReference">The document to delete. Must not be null.</param>
 /// <param name="precondition">Optional precondition for deletion. May be null, in which case the deletion is unconditional.</param>
 public void Delete(DocumentReference documentReference, Precondition precondition = null)
 {
     // Preconditions are validated by WriteBatch.
     _writes.Delete(documentReference, precondition);
 }
        /// <summary>
        /// Asynchronously performs a set of updates on the document referred to by this path, with an optional precondition.
        /// </summary>
        /// <param name="updates">The updates to perform on the document, keyed by the field path to update. Fields not present in this dictionary are not updated. Must not be null.</param>
        /// <param name="precondition">Optional precondition for updating the document. May be null, which is equivalent to <see cref="Precondition.MustExist"/>.</param>
        /// <param name="cancellationToken">A cancellation token to monitor for the asynchronous operation.</param>
        /// <returns>The write result of the server operation.</returns>
        public async Task <WriteResult> UpdateAsync(IDictionary <FieldPath, object> updates, Precondition precondition = null, CancellationToken cancellationToken = default)
        {
            var batch = Database.CreateWriteBatch();

            batch.Update(this, updates, precondition);
            var results = await batch.CommitAsync(cancellationToken).ConfigureAwait(false);

            return(results[0]);
        }