Esempio n. 1
0
        private static async Task BatchWrite(string project)
        {
            FirestoreDb db = FirestoreDb.Create(project);
            // [START fs_batch_write]
            WriteBatch batch = db.CreateWriteBatch();

            // Set the data for NYC
            DocumentReference           nycRef  = db.Collection("cities").Document("NYC");
            Dictionary <string, object> nycData = new Dictionary <string, object>
            {
                { "name", "New York City" }
            };

            batch.Set(nycRef, nycData);

            // Update the population for SF
            DocumentReference sfRef = db.Collection("cities").Document("SF");
            Dictionary <FieldPath, object> updates = new Dictionary <FieldPath, object>
            {
                { new FieldPath("Population"), 1000000 }
            };

            batch.Update(sfRef, updates);

            // Delete LA
            DocumentReference laRef = db.Collection("cities").Document("LA");

            batch.Delete(laRef);

            // Commit the batch
            await batch.CommitAsync();

            // [END fs_batch_write]
            Console.WriteLine("Batch write successfully completed.");
        }
Esempio n. 2
0
        private async Task PopulateCollection(CollectionReference collection, IEnumerable <object> documents)
        {
            var batch = FirestoreDb.CreateWriteBatch();

            foreach (var doc in documents)
            {
                batch.Create(collection.GenerateDocument(), doc);
            }
            await batch.CommitAsync();
        }
Esempio n. 3
0
 private static void Check(bool expectedError, CommitRequest expectedRequest, Action<WriteBatch> action)
 {
     var mock = new MockCommitClient(expectedRequest);
     FirestoreDb db = FirestoreDb.Create(ProjectId, DatabaseId, mock);
     var batch = db.CreateWriteBatch();
     if (expectedError)
     {
         var exception = Assert.ThrowsAny<Exception>(() => action(batch));
         Assert.True(exception is ArgumentException || exception is InvalidOperationException, $"Exception type: {exception.GetType()}");
     }
     else
     {
         action(batch);
         batch.CommitAsync().Wait();
     }
     mock.Verify();
 }