-
Notifications
You must be signed in to change notification settings - Fork 5
/
snippets.cpp
1304 lines (1139 loc) · 50.8 KB
/
snippets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <cstdint>
#include <iostream>
#include <string>
#include "snippets.h"
#include "firebase/app.h"
#include "firebase/auth.h"
#include "firebase/auth/user.h"
#include "firebase/firestore.h"
#include "firebase/util.h"
/*
* A collection of code snippets for the Firestore C++ SDK. These snippets
* were modeled after the existing Firestore guide, which can be found
* here: https://firebase.google.com/docs/firestore.
*
* Note that not all of the Firestore API has been implemented yet, so some
* snippets are incomplete/missing.
*/
namespace snippets {
// https://firebase.google.com/docs/firestore/data-model#references
void DataModelReferenceDeclarations(firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentReference;
using firebase::firestore::CollectionReference;
// [START doc_reference]
DocumentReference alovelace_document_reference =
db->Collection("users").Document("alovelace");
// [END doc_reference]
// [START collection_reference]
CollectionReference users_collection_reference = db->Collection("users");
// [END collection_reference]
// https://firebase.google.com/docs/firestore/data-model#hierarchical-data
// [START subcollection_reference]
DocumentReference message_reference = db->Collection("rooms")
.Document("roomA")
.Collection("messages")
.Document("message1");
// [END subcollection_reference]
// [START path_reference]
DocumentReference alovelace_document = db->Document("users/alovelace");
// [END path_reference]
}
// https://firebase.google.com/docs/firestore/quickstart#add_data
void QuickstartAddData(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
// Firestore stores data in Documents, which are stored in Collections.
// Firestore creates collections and documents implicitly the first time
// you add data to the document. You do not need to explicitly create
// collections or documents.
// [START add_ada_lovelace]
// Add a new document with a generated ID
Future<DocumentReference> user_ref =
db->Collection("users").Add({{"first", FieldValue::String("Ada")},
{"last", FieldValue::String("Lovelace")},
{"born", FieldValue::Integer(1815)}});
user_ref.OnCompletion([](const Future<DocumentReference>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "DocumentSnapshot added with ID: " << future.result()->id()
<< std::endl;
} else {
std::cout << "Error adding document: " << future.error_message() << std::endl;
}
});
// [END add_ada_lovelace]
// Now add another document to the users collection. Notice that this document
// includes a key-value pair (middle name) that does not appear in the first
// document. Documents in a collection can contain different sets of
// information.
// [START add_alan_turing]
db->Collection("users")
.Add({{"first", FieldValue::String("Alan")},
{"middle", FieldValue::String("Mathison")},
{"last", FieldValue::String("Turing")},
{"born", FieldValue::Integer(1912)}})
.OnCompletion([](const Future<DocumentReference>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "DocumentSnapshot added with ID: "
<< future.result()->id() << std::endl;
} else {
std::cout << "Error adding document: " << future.error_message()
<< std::endl;
}
});
// [END add_alan_turing]
}
// https://firebase.google.com/docs/firestore/quickstart#read_data
void QuickstartReadData(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::QuerySnapshot;
// To quickly verify that you've added data to Firestore, use the data
// viewer in the Firebase console.
//
// You can also use the "Get" method to retrieve the entire collection.
// [START get_collection]
Future<QuerySnapshot> users = db->Collection("users").Get();
users.OnCompletion([](const Future<QuerySnapshot>& future) {
if (future.error() == Error::kErrorOk) {
for (const DocumentSnapshot& document : future.result()->documents()) {
std::cout << document << std::endl;
}
} else {
std::cout << "Error getting documents: " << future.error_message()
<< std::endl;
}
});
// [END get_collection]
}
// https://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document
void AddDataSetDocument(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::SetOptions;
// To create or overwrite a single document, use the Set() method:
// [START set_document]
// Add a new document in collection 'cities'
db->Collection("cities")
.Document("LA")
.Set({{"name", FieldValue::String("Los Angeles")},
{"state", FieldValue::String("CA")},
{"country", FieldValue::String("USA")}})
.OnCompletion([](const Future<void>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "DocumentSnapshot successfully written!" << std::endl;
} else {
std::cout << "Error writing document: " << future.error_message()
<< std::endl;
}
});
// [END set_document]
// If the document does not exist, it will be created. If the document does
// exist, its contents will be overwritten with the newly provided data,
// unless you specify that the data should be merged into the existing
// document, as follows:
// [START create_if_missing]
db->Collection("cities").Document("BJ").Set(
{{"capital", FieldValue::Boolean(true)}}, SetOptions::Merge());
// [END create_if_missing]
}
// https://firebase.google.com/docs/firestore/manage-data/add-data#data_types
void AddDataDataTypes(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::Timestamp;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::MapFieldValue;
// Firestore lets you write a variety of data types inside a document,
// including strings, booleans, numbers, dates, null, and nested arrays and
// objects. Firestore always stores numbers as doubles, regardless of
// what type of number you use in your code.
// [START data_types]
MapFieldValue doc_data{
{"stringExample", FieldValue::String("Hello world!")},
{"booleanExample", FieldValue::Boolean(true)},
{"numberExample", FieldValue::Double(3.14159265)},
{"dateExample", FieldValue::Timestamp(Timestamp::Now())},
{"arrayExample", FieldValue::Array({FieldValue::Integer(1),
FieldValue::Integer(2),
FieldValue::Integer(3)})},
{"nullExample", FieldValue::Null()},
{"objectExample",
FieldValue::Map(
{{"a", FieldValue::Integer(5)},
{"b", FieldValue::Map(
{{"nested", FieldValue::String("foo")}})}})},
};
db->Collection("data").Document("one").Set(doc_data).OnCompletion(
[](const Future<void>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "DocumentSnapshot successfully written!" << std::endl;
} else {
std::cout << "Error writing document: " << future.error_message()
<< std::endl;
}
});
// [END data_types]
}
// https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document
void AddDataAddDocument(firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentReference;
// When you use Set() to create a document, you must specify an ID for the
// document to create. For example:
// [START set_data]
db->Collection("cities").Document("SF").Set({/*some data*/});
// [END set_data]
// But sometimes there isn't a meaningful ID for the document, and it's more
// convenient to let Firestore auto-generate an ID for you. You can do
// this by calling Add():
// [START add_document]
db->Collection("cities").Add({/*some data*/});
// [END add_document]
// In some cases, it can be useful to create a document reference with an
// auto-generated ID, then use the reference later. For this use case, you can
// call Document():
// [START new_document]
DocumentReference new_city_ref = db->Collection("cities").Document();
// [END new_document]
// Behind the scenes, Add(...) and Document().Set(...) are completely
// equivalent, so you can use whichever is more convenient.
}
// https://firebase.google.com/docs/firestore/manage-data/add-data#update-data
void AddDataUpdateDocument(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::FieldValue;
// To update some fields of a document without overwriting the entire
// document, use the Update() method:
// [START update_document]
DocumentReference washington_ref = db->Collection("cities").Document("DC");
// Set the "capital" field of the city "DC".
washington_ref.Update({{"capital", FieldValue::Boolean(true)}});
// [END update_document]
// You can set a field in your document to a server timestamp which tracks
// when the server receives the update.
// [START server_timestamp]
DocumentReference doc_ref = db->Collection("objects").Document("some-id");
doc_ref.Update({{"timestamp", FieldValue::ServerTimestamp()}})
.OnCompletion([](const Future<void>& future) {
// ...
});
// [END server_timestamp]
}
// https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
void AddDataUpdateNestedObjects(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::FieldValue;
// If your document contains nested objects, you can use "dot notation" to
// reference nested fields within the document when you call Update():
// [START update_document_nested]
// Assume the document contains:
// {
// name: "Frank",
// favorites: { food: "Pizza", color: "Blue", subject: "recess" }
// age: 12
// }
//
// To update age and favorite color:
db->Collection("users").Document("frank").Update({
{"age", FieldValue::Integer(13)},
{"favorites.color", FieldValue::String("red")},
});
// [END update_document_nested]
// Dot notation allows you to update a single nested field without overwriting
// other nested fields. If you update a nested field without dot notation, you
// will overwrite the entire map field.
}
// https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes
void AddDataBatchedWrites(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::WriteBatch;
// If you do not need to read any documents in your operation set, you can
// execute multiple write operations as a single batch that contains any
// combination of set(), update(), or delete() operations. A batch of writes
// completes atomically and can write to multiple documents. The following
// example shows how to build and commit a write batch:
// [START write_batch]
// Get a new write batch
WriteBatch batch = db->batch();
// Set the value of 'NYC'
DocumentReference nyc_ref = db->Collection("cities").Document("NYC");
batch.Set(nyc_ref, {});
// Update the population of 'SF'
DocumentReference sf_ref = db->Collection("cities").Document("SF");
batch.Update(sf_ref, {{"population", FieldValue::Integer(1000000)}});
// Delete the city 'LA'
DocumentReference la_ref = db->Collection("cities").Document("LA");
batch.Delete(la_ref);
// Commit the batch
batch.Commit().OnCompletion([](const Future<void>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "Write batch success!" << std::endl;
} else {
std::cout << "Write batch failure: " << future.error_message() << std::endl;
}
});
// [END write_batch]
}
// https://firebase.google.com/docs/firestore/manage-data/transactions#transactions
void AddDataTransactions(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::Transaction;
// The following example shows how to create and run a transaction:
// [START simple_transaction]
DocumentReference sf_doc_ref = db->Collection("cities").Document("SF");
db->RunTransaction([sf_doc_ref](Transaction& transaction,
std::string& out_error_message) -> Error {
Error error = Error::kErrorOk;
DocumentSnapshot snapshot =
transaction.Get(sf_doc_ref, &error, &out_error_message);
// Note: this could be done without a transaction by updating the
// population using FieldValue::Increment().
std::int64_t new_population =
snapshot.Get("population").integer_value() + 1;
transaction.Update(
sf_doc_ref,
{{"population", FieldValue::Integer(new_population)}});
return Error::kErrorOk;
}).OnCompletion([](const Future<void>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "Transaction success!" << std::endl;
} else {
std::cout << "Transaction failure: " << future.error_message() << std::endl;
}
});
// [END simple_transaction]
}
// https://firebase.google.com/docs/firestore/manage-data/delete-data#delete_documents
void AddDataDeleteDocuments(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::Error;
// To delete a document, use the Delete() method:
// [START delete_document]
db->Collection("cities").Document("DC").Delete().OnCompletion(
[](const Future<void>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "DocumentSnapshot successfully deleted!" << std::endl;
} else {
std::cout << "Error deleting document: " << future.error_message()
<< std::endl;
}
});
// [END delete_document]
// WARNING: Deleting a document does not delete its subcollections!
}
// https://firebase.google.com/docs/firestore/manage-data/delete-data#fields
void AddDataDeleteFields(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::FieldValue;
// To delete specific fields from a document, use the FieldValue.delete()
// method when you update a document:
// [START delete_field]
DocumentReference doc_ref = db->Collection("cities").Document("BJ");
doc_ref.Update({{"capital", FieldValue::Delete()}})
.OnCompletion([](const Future<void>& future) { /*...*/ });
// [END delete_field]
// https://firebase.google.com/docs/firestore/manage-data/delete-data#collections
// To delete an entire collection or subcollection in Firestore,
// retrieve all the documents within the collection or subcollection and
// delete them.
// WARNING: deleting collections from a client SDK is not recommended.
}
// https://firebase.google.com/docs/firestore/query-data/get-data#example_data
void ReadDataExampleData(firebase::firestore::Firestore* db) {
using firebase::firestore::CollectionReference;
using firebase::firestore::FieldValue;
// To get started, write some data about cities so we can look at different
// ways to read it back:
// [START example_data]
CollectionReference cities = db->Collection("cities");
cities.Document("SF").Set({
{"name", FieldValue::String("San Francisco")},
{"state", FieldValue::String("CA")},
{"country", FieldValue::String("USA")},
{"capital", FieldValue::Boolean(false)},
{"population", FieldValue::Integer(860000)},
{"regions", FieldValue::Array({FieldValue::String("west_coast"),
FieldValue::String("norcal")})},
});
cities.Document("LA").Set({
{"name", FieldValue::String("Los Angeles")},
{"state", FieldValue::String("CA")},
{"country", FieldValue::String("USA")},
{"capital", FieldValue::Boolean(false)},
{"population", FieldValue::Integer(3900000)},
{"regions", FieldValue::Array({FieldValue::String("west_coast"),
FieldValue::String("socal")})},
});
cities.Document("DC").Set({
{"name", FieldValue::String("Washington D.C.")},
{"state", FieldValue::Null()},
{"country", FieldValue::String("USA")},
{"capital", FieldValue::Boolean(true)},
{"population", FieldValue::Integer(680000)},
{"regions",
FieldValue::Array({FieldValue::String("east_coast")})},
});
cities.Document("TOK").Set({
{"name", FieldValue::String("Tokyo")},
{"state", FieldValue::Null()},
{"country", FieldValue::String("Japan")},
{"capital", FieldValue::Boolean(true)},
{"population", FieldValue::Integer(9000000)},
{"regions", FieldValue::Array({FieldValue::String("kanto"),
FieldValue::String("honshu")})},
});
cities.Document("BJ").Set({
{"name", FieldValue::String("Beijing")},
{"state", FieldValue::Null()},
{"country", FieldValue::String("China")},
{"capital", FieldValue::Boolean(true)},
{"population", FieldValue::Integer(21500000)},
{"regions", FieldValue::Array({FieldValue::String("jingjinji"),
FieldValue::String("hebei")})},
});
// [END example_data]
}
// https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document
void ReadDataGetDocument(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
// The following example shows how to retrieve the contents of a single
// document using Get():
// [START get_document]
DocumentReference doc_ref = db->Collection("cities").Document("SF");
doc_ref.Get().OnCompletion([](const Future<DocumentSnapshot>& future) {
if (future.error() == Error::kErrorOk) {
const DocumentSnapshot& document = *future.result();
if (document.exists()) {
std::cout << "DocumentSnapshot id: " << document.id() << std::endl;
} else {
std::cout << "no such document" << std::endl;
}
} else {
std::cout << "Get failed with: " << future.error_message() << std::endl;
}
});
// [END get_document]
}
void ReadDataSourceOptions(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::Source;
// You can set the source option to control how a get call uses the offline
// cache.
//
// By default, a get call will attempt to fetch the latest document snapshot
// from your database. On platforms with offline support, the client library
// will use the offline cache if the network is unavailable or if the request
// times out.
//
// You can specify the source option in a Get() call to change the default
// behavior. You can fetch from only the database and ignore the offline
// cache, or you can fetch from only the offline cache. For example:
// [START get_document_options]
DocumentReference doc_ref = db->Collection("cities").Document("SF");
Source source = Source::kCache;
doc_ref.Get(source).OnCompletion([](const Future<DocumentSnapshot>& future) {
if (future.error() == Error::kErrorOk) {
const DocumentSnapshot& document = *future.result();
if (document.exists()) {
std::cout << "Cached document id: " << document.id() << std::endl;
} else {
}
} else {
std::cout << "Cached get failed: " << future.error_message() << std::endl;
}
});
// [END get_document_options]
}
// https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection
void ReadDataGetMultipleDocumentsFromCollection(
firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::QuerySnapshot;
// You can also retrieve multiple documents with one request by querying
// documents in a collection. For example, you can use Where() to query for
// all of the documents that meet a certain condition, then use Get() to
// retrieve the results:
// [START get_multiple]
db->Collection("cities")
.WhereEqualTo("capital", FieldValue::Boolean(true))
.Get()
.OnCompletion([](const Future<QuerySnapshot>& future) {
if (future.error() == Error::kErrorOk) {
for (const DocumentSnapshot& document :
future.result()->documents()) {
std::cout << document << std::endl;
}
} else {
std::cout << "Error getting documents: " << future.error_message()
<< std::endl;
}
});
// [END get_multiple]
}
// https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection
void ReadDataGetAllDocumentsInCollection(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::QuerySnapshot;
// In addition, you can retrieve all documents in a collection by omitting the
// Where() filter entirely:
// [START get_multiple_all]
db->Collection("cities").Get().OnCompletion(
[](const Future<QuerySnapshot>& future) {
if (future.error() == Error::kErrorOk) {
for (const DocumentSnapshot& document :
future.result()->documents()) {
std::cout << document << std::endl;
}
} else {
std::cout << "Error getting documents: " << future.error_message()
<< std::endl;
}
});
// [END get_multiple_all]
}
// https://firebase.google.com/docs/firestore/query-data/listen
void ReadDataListen(firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
// You can listen to a document with the AddSnapshotListener() method. An
// initial call using the callback you provide creates a document snapshot
// immediately with the current contents of the single document. Then, each
// time the contents change, another call updates the document snapshot.
// [START listen_document]
DocumentReference doc_ref = db->Collection("cities").Document("SF");
doc_ref.AddSnapshotListener(
[](const DocumentSnapshot& snapshot, Error error, const std::string& errorMsg) {
if (error == Error::kErrorOk) {
if (snapshot.exists()) {
std::cout << "Current data: " << snapshot << std::endl;
} else {
std::cout << "Current data: null" << std::endl;
}
} else {
std::cout << "Listen failed: " << error << std::endl;
}
});
// [END listen_document]
}
// https://firebase.google.com/docs/firestore/query-data/listen#events-local-changes
void ReadDataEventsForLocalChanges(firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
// Local writes in your app will invoke snapshot listeners immediately. This
// is because of an important feature called "latency compensation." When you
// perform a write, your listeners will be notified with the new data before
// the data is sent to the backend.
//
// Retrieved documents have metadata().has_pending_writes() property that
// indicates whether the document has local changes that haven't been written
// to the backend yet. You can use this property to determine the source of
// events received by your snapshot listener:
// [START listen_document_local]
DocumentReference doc_ref = db->Collection("cities").Document("SF");
doc_ref.AddSnapshotListener([](const DocumentSnapshot& snapshot,
Error error, const std::string& errorMsg) {
if (error == Error::kErrorOk) {
const char* source =
snapshot.metadata().has_pending_writes() ? "Local" : "Server";
if (snapshot.exists()) {
std::cout << source << " data: " << snapshot.Get("name").string_value()
<< std::endl;
} else {
std::cout << source << " data: null" << std::endl;
}
} else {
std::cout << "Listen failed: " << error << std::endl;
}
});
// [END listen_document_local]
}
// https://firebase.google.com/docs/firestore/query-data/listen#events-metadata-changes
void ReadDataEventsForMetadataChanges(firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::MetadataChanges;
// When listening for changes to a document, collection, or query, you can
// pass options to control the granularity of events that your listener will
// receive.
//
// By default, listeners are not notified of changes that only affect
// metadata. Consider what happens when your app writes a new document:
//
// A change event is immediately fired with the new data. The document has not
// yet been written to the backend so the "pending writes" flag is true.
// The document is written to the backend.
// The backend notifies the client of the successful write. There is no change
// to the document data, but there is a metadata change because the "pending
// writes" flag is now false.
// If you want to receive snapshot events when the document or query metadata
// changes, pass a listen options object when attaching your listener:
// [START listen_with_metadata]
DocumentReference doc_ref = db->Collection("cities").Document("SF");
doc_ref.AddSnapshotListener(
MetadataChanges::kInclude,
[](const DocumentSnapshot& snapshot, Error error, const std::string& errorMsg) { /* ... */ });
// [END listen_with_metadata]
}
// https://firebase.google.com/docs/firestore/query-data/listen#listen_to_multiple_documents_in_a_collection
void ReadDataListenToMultipleDocumentsInCollection(
firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::QuerySnapshot;
// As with documents, you can use AddSnapshotListener() instead of Get() to
// listen to the results of a query. This creates a query snapshot. For
// example, to listen to the documents with state CA:
// [START listen_multiple]
db->Collection("cities")
.WhereEqualTo("state", FieldValue::String("CA"))
.AddSnapshotListener([](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) {
if (error == Error::kErrorOk) {
std::vector<std::string> cities;
std::cout << "Current cities in CA: " << error << std::endl;
for (const DocumentSnapshot& doc : snapshot.documents()) {
cities.push_back(doc.Get("name").string_value());
std::cout << "" << cities.back() << std::endl;
}
} else {
std::cout << "Listen failed: " << error << std::endl;
}
});
// [END listen_multiple]
// The snapshot handler will receive a new query snapshot every time the query
// results change (that is, when a document is added, removed, or modified).
}
// https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots
void ReadDataViewChangesBetweenSnapshots(firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentChange;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::QuerySnapshot;
// It is often useful to see the actual changes to query results between query
// snapshots, instead of simply using the entire query snapshot. For example,
// you may want to maintain a cache as individual documents are added,
// removed, and modified.
// [START listen_diffs]
db->Collection("cities")
.WhereEqualTo("state", FieldValue::String("CA"))
.AddSnapshotListener([](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) {
if (error == Error::kErrorOk) {
for (const DocumentChange& dc : snapshot.DocumentChanges()) {
switch (dc.type()) {
case DocumentChange::Type::kAdded:
std::cout << "New city: "
<< dc.document().Get("name").string_value() << std::endl;
break;
case DocumentChange::Type::kModified:
std::cout << "Modified city: "
<< dc.document().Get("name").string_value() << std::endl;
break;
case DocumentChange::Type::kRemoved:
std::cout << "Removed city: "
<< dc.document().Get("name").string_value() << std::endl;
break;
}
}
} else {
std::cout << "Listen failed: " << error << std::endl;
}
});
// [END listen_diffs]
}
// https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener
void ReadDataDetachListener(firebase::firestore::Firestore* db) {
using firebase::firestore::Error;
using firebase::firestore::ListenerRegistration;
using firebase::firestore::Query;
using firebase::firestore::QuerySnapshot;
// When you are no longer interested in listening to your data, you must
// detach your listener so that your event callbacks stop getting called. This
// allows the client to stop using bandwidth to receive updates. For example:
// [START detach_listener]
// Add a listener
Query query = db->Collection("cities");
ListenerRegistration registration = query.AddSnapshotListener(
[](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) { /* ... */ });
// Stop listening to changes
registration.Remove();
// [END detach_listener]
// A listen may occasionally fail — for example, due to security permissions,
// or if you tried to listen on an invalid query. After an error, the listener
// will not receive any more events, and there is no need to detach your
// listener.
}
// https://firebase.google.com/docs/firestore/query-data/queries#simple_queries
void ReadDataSimpleQueries(firebase::firestore::Firestore* db) {
using firebase::firestore::CollectionReference;
using firebase::firestore::FieldValue;
using firebase::firestore::Query;
// Firestore provides powerful query functionality for specifying which
// documents you want to retrieve from a collection.
// The following query returns all cities with state CA:
// [START simple_queries]
CollectionReference cities_ref = db->Collection("cities");
// Create a query against the collection.
Query query_ca =
cities_ref.WhereEqualTo("state", FieldValue::String("CA"));
// [END simple_queries]
// The following query returns all the capital cities:
// [START query_capitals]
Query capital_cities = db->Collection("cities").WhereEqualTo(
"capital", FieldValue::Boolean(true));
// [END query_capitals]
}
// https://firebase.google.com/docs/firestore/query-data/queries#execute_a_query
void ReadDataExecuteQuery(firebase::firestore::Firestore* db) {
using firebase::Future;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::QuerySnapshot;
// After creating a query object, use the Get() function to retrieve the
// results:
// This snippet is identical to get_multiple above.
db->Collection("cities")
.WhereEqualTo("capital", FieldValue::Boolean(true))
.Get()
.OnCompletion([](const Future<QuerySnapshot>& future) {
if (future.error() == Error::kErrorOk) {
for (const DocumentSnapshot& document :
future.result()->documents()) {
std::cout << document << std::endl;
}
} else {
std::cout << "Error getting documents: " << future.error_message()
<< std::endl;
}
});
}
// https://firebase.google.com/docs/firestore/query-data/queries#query_operators
void ReadDataQueryOperators(firebase::firestore::Firestore* db) {
using firebase::firestore::CollectionReference;
using firebase::firestore::FieldValue;
CollectionReference cities_ref = db->Collection("cities");
// Some example filters:
// [START example_filters]
cities_ref.WhereEqualTo("state", FieldValue::String("CA"));
cities_ref.WhereLessThan("population", FieldValue::Integer(100000));
cities_ref.WhereGreaterThanOrEqualTo("name",
FieldValue::String("San Francisco"));
// [END example_filters]
// [START query_filter_not_eq]
cities_ref.WhereNotEqualTo("capital", FieldValue::Boolean(false));
// [END query_filter_not_eq]
}
// https://firebase.google.com/docs/firestore/query-data/queries#array_membership
void ReadDataArrayMembershipOperators(firebase::firestore::Firestore* db) {
using firebase::firestore::CollectionReference;
using firebase::firestore::FieldValue;
// Some example filters:
// [START cpp_array_contains_filter]
CollectionReference cities_ref = db->Collection("cities");
cities_ref.WhereArrayContains("region", FieldValue::String("west_coast"));
// [END cpp_array_contains_filter]
}
// https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any
void ReadDataArrayInNotInOperators(firebase::firestore::Firestore* db) {
using firebase::firestore::CollectionReference;
using firebase::firestore::FieldValue;
// Some example filters:
// [START cpp_in_filter]
CollectionReference cities_ref = db->Collection("cities");
cities_ref.WhereIn("country", std::vector<FieldValue> {
FieldValue::String("USA"),
FieldValue::String("Japan")
});
// [END cpp_in_filter]
// [START cpp_not_in_filter]
cities_ref.WhereNotIn("country", std::vector<FieldValue> {
FieldValue::String("USA"),
FieldValue::String("Japan")
});
// [END cpp_not_in_filter]
}
// https://firebase.google.com/docs/firestore/query-data/queries#array-contains-any
void ReadDataArrayContainsAnyOperators(firebase::firestore::Firestore* db) {
using firebase::firestore::CollectionReference;
using firebase::firestore::FieldValue;
// Some example filters:
// [START cpp_array_contains_any_filter]
CollectionReference cities_ref = db->Collection("cities");
cities_ref.WhereArrayContainsAny("region", std::vector<FieldValue> {
FieldValue::String("west_coast"),
FieldValue::String("east_coast")
});
// [END cpp_array_contains_any_filter]
// [START cpp_in_filter_with_array]
cities_ref.WhereIn("region", std::vector<FieldValue> {
FieldValue::String("west_coast"),
FieldValue::String("east_coast")
});
// [END cpp_in_filter_with_array]
}
void QueryCollectionGroupFilterEq(firebase::firestore::Firestore* db) // 2 TODO
{
using firebase::firestore::CollectionReference;
using firebase::firestore::FieldValue;
using firebase::firestore::Error;
using firebase::firestore::QuerySnapshot;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Query;
// [START query_collection_group_filter_eq]
db->CollectionGroup("landmarks")
.WhereEqualTo("type", FieldValue::String("museum")).Get()
.OnCompletion([](const firebase::Future<QuerySnapshot>& future) {
if (future.error() == Error::kErrorOk) {
for (const DocumentSnapshot& document : future.result()->documents()) {
std::cout << document << std::endl;
}
} else {
std::cout << "Error getting documents: " << future.error_message()
<< std::endl;
}
});
// [END query_collection_group_filter_eq]
}
void QueryCollectionGroupDataset(firebase::firestore::Firestore* db)
{
using firebase::Future;
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
using firebase::firestore::FieldValue;
using firebase::firestore::QuerySnapshot;
using firebase::firestore::WriteBatch;
// [START query_collection_group_dataset]
// Get a new write batch
WriteBatch batch = db->batch();
DocumentReference sf_ref = db->Collection("cities").Document("SF");
batch.Set(sf_ref,{{"name", FieldValue::String("Golden Gate Bridge")}, {"type", FieldValue::String("bridge")}});
batch.Set(sf_ref,{{"name", FieldValue::String("Legion of Honor")}, {"type", FieldValue::String("museum")}});
DocumentReference la_ref = db->Collection("cities").Document("LA");
batch.Set(la_ref,{{"name", FieldValue::String("Griffith Park")}, {"type", FieldValue::String("park")}});
batch.Set(la_ref,{{"name", FieldValue::String("The Getty")}, {"type", FieldValue::String("museum")}});
DocumentReference dc_ref = db->Collection("cities").Document("DC");
batch.Set(dc_ref,{{"name", FieldValue::String("Lincoln Memorial")}, {"type", FieldValue::String("memorial")}});
batch.Set(dc_ref,{{"name", FieldValue::String("National Air and Space Museum")}, {"type", FieldValue::String("museum")}});
DocumentReference tok_ref = db->Collection("cities").Document("TOK");
batch.Set(tok_ref,{{"name", FieldValue::String("Ueno Park")}, {"type", FieldValue::String("park")}});
batch.Set(tok_ref,{{"name", FieldValue::String("National Museum of Nature and Science")}, {"type", FieldValue::String("museum")}});
DocumentReference bj_ref = db->Collection("cities").Document("BJ");
batch.Set(bj_ref,{{"name", FieldValue::String("Jingshan Park")}, {"type", FieldValue::String("park")}});
batch.Set(bj_ref,{{"name", FieldValue::String("Beijing Ancient Observatory")}, {"type", FieldValue::String("museum")}});
// Commit the batch
batch.Commit().OnCompletion([](const Future<void>& future) {
if (future.error() == Error::kErrorOk) {
std::cout << "Write batch success!" << std::endl;
} else {
std::cout << "Write batch failure: " << future.error_message() << std::endl;
}
});
// [END query_collection_group_dataset]
}