/
Program.cs
338 lines (296 loc) · 12.5 KB
/
Program.cs
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
// Copyright(c) 2017 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.
using CommandLine;
using Google.Cloud.Firestore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GoogleCloudSamples
{
public class AddData
{
public static string Usage = @"Usage:
C:\> dotnet run command YOUR_PROJECT_ID
Where command is one of
add-doc-as-map
update-create-if-missing
add-doc-data-types
add-simple-doc-as-entity
set-requires-id
add-doc-data-with-auto-id
add-doc-data-after-auto-id
update-doc
update-nested-fields
update-server-timestamp
update-document-array
";
private static async Task AddDocAsMap(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_add_doc_as_map]
DocumentReference docRef = db.Collection("cities").Document("LA");
Dictionary<string, object> city = new Dictionary<string, object>
{
{ "name", "Los Angeles" },
{ "state", "CA" },
{ "country", "USA" }
};
await docRef.SetAsync(city);
// [END fs_add_doc_as_map]
Console.WriteLine("Added data to the LA document in the cities collection.");
}
private static async Task UpdateCreateIfMissing(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_update_create_if_missing]
DocumentReference docRef = db.Collection("cities").Document("LA");
Dictionary<string, object> update = new Dictionary<string, object>
{
{ "capital", false }
};
await docRef.SetAsync(update, SetOptions.MergeAll);
// [END fs_update_create_if_missing]
Console.WriteLine("Merged data into the LA document in the cities collection.");
}
private static async Task AddDocDataTypes(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_add_doc_data_types]
DocumentReference docRef = db.Collection("data").Document("one");
Dictionary<string, object> docData = new Dictionary<string, object>
{
{ "stringExample", "Hello World" },
{ "booleanExample", false },
{ "numberExample", 3.14159265 },
{ "nullExample", null },
};
ArrayList arrayExample = new ArrayList();
arrayExample.Add(5);
arrayExample.Add(true);
arrayExample.Add("Hello");
docData.Add("arrayExample", arrayExample);
Dictionary<string, object> objectExample = new Dictionary<string, object>
{
{ "a", 5 },
{ "b", true },
};
docData.Add("objectExample", objectExample);
await docRef.SetAsync(docData);
// [END fs_add_doc_data_types]
Console.WriteLine("Set multiple data-type data for the one document in the data collection.");
}
// [START fs_class_definition]
[FirestoreData]
public class City
{
[FirestoreProperty]
public string Name { get; set; }
[FirestoreProperty]
public string State { get; set; }
[FirestoreProperty]
public string Country { get; set; }
[FirestoreProperty]
public bool Capital { get; set; }
[FirestoreProperty]
public long Population { get; set; }
}
// [END fs_class_definition]
private static async Task AddSimpleDocAsEntity(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_add_simple_doc_as_entity]
DocumentReference docRef = db.Collection("cities").Document("LA");
City city = new City
{
Name = "Los Angeles",
State = "CA",
Country = "USA",
Capital = false,
Population = 3900000L
};
await docRef.SetAsync(city);
// [END fs_add_simple_doc_as_entity]
Console.WriteLine("Added custom City object to the cities collection.");
}
private static async Task SetRequiresId(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
Dictionary<string, object> city = new Dictionary<string, object>
{
{ "Name", "Phuket" },
{ "Country", "Thailand" }
};
// [START fs_set_requires_id]
await db.Collection("cities").Document("new-city-id").SetAsync(city);
// [END fs_set_requires_id]
Console.WriteLine("Added document with ID: new-city-id.");
}
private static async Task AddDocDataWithAutoId(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_add_doc_data_with_auto_id]
Dictionary<string, object> city = new Dictionary<string, object>
{
{ "Name", "Tokyo" },
{ "Country", "Japan" }
};
DocumentReference addedDocRef = await db.Collection("cities").AddAsync(city);
Console.WriteLine("Added document with ID: {0}.", addedDocRef.Id);
// [END fs_add_doc_data_with_auto_id]
}
private static async Task AddDocDataAfterAutoId(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
Dictionary<string, object> city = new Dictionary<string, object>
{
{ "Name", "Moscow" },
{ "Country", "Russia" }
};
// [START fs_add_doc_data_after_auto_id]
DocumentReference addedDocRef = db.Collection("cities").Document();
Console.WriteLine("Added document with ID: {0}.", addedDocRef.Id);
await addedDocRef.SetAsync(city);
// [END fs_add_doc_data_after_auto_id]
Console.WriteLine("Added data to the {0} document in the cities collection.", addedDocRef.Id);
}
private static async Task UpdateDoc(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_update_doc]
DocumentReference cityRef = db.Collection("cities").Document("new-city-id");
Dictionary<string, object> updates = new Dictionary<string, object>
{
{ "Capital", false }
};
await cityRef.UpdateAsync(updates);
// You can also update a single field with: await cityRef.UpdateAsync("Capital", false);
// [END fs_update_doc]
Console.WriteLine("Updated the Capital field of the new-city-id document in the cities collection.");
}
private static async Task UpdateNestedFields(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_update_nested_fields]
DocumentReference frankDocRef = db.Collection("users").Document("frank");
Dictionary<string, object> initialData = new Dictionary<string, object>
{
{ "Name", "Frank" },
{ "Age", 12 }
};
Dictionary<string, object> favorites = new Dictionary<string, object>
{
{ "Food", "Pizza" },
{ "Color", "Blue" },
{ "Subject", "Recess" },
};
initialData.Add("Favorites", favorites);
await frankDocRef.SetAsync(initialData);
// Update age and favorite color
Dictionary<string, object> updates = new Dictionary<string, object>
{
{ "Age", 13 },
{ "Favorites.Color", "Red" },
};
// Asynchronously update the document
await frankDocRef.UpdateAsync(updates);
// [END fs_update_nested_fields]
Console.WriteLine("Updated the age and favorite color fields of the Frank document in the users collection.");
}
private static async Task UpdateServerTimestamp(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_update_server_timestamp]
DocumentReference cityRef = db.Collection("cities").Document("new-city-id");
await cityRef.UpdateAsync("Timestamp", Timestamp.GetCurrentTimestamp());
// [END fs_update_server_timestamp]
Console.WriteLine("Updated the Timestamp field of the new-city-id document in the cities collection.");
}
private static async Task UpdateDocumentArray(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_update_document_array]
DocumentReference washingtonRef = db.Collection("cities").Document("DC");
// Atomically add a new region to the "regions" array field.
await washingtonRef.UpdateAsync("Regions", FieldValue.ArrayUnion("greater_virginia"));
// Atomically remove a region from the "regions" array field.
await washingtonRef.UpdateAsync("Regions", FieldValue.ArrayRemove("east_coast"));
// [END fs_update_document_array]
Console.WriteLine("Updated the Regions array of the DC document in the cities collection.");
}
private static async Task UpdateDocumentIncrement(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_update_document_increment]
DocumentReference washingtonRef = db.Collection("cities").Document("DC");
// Atomically increment the population of the city by 50.
await washingtonRef.UpdateAsync("Regions", FieldValue.Increment(50));
// [END fs_update_document_increment]
Console.WriteLine("Updated the population of the DC document in the cities collection.");
}
public static void Main(string[] args)
{
if (args.Length < 2)
{
Console.Write(Usage);
return;
}
string command = args[0].ToLower();
string project = string.Join(" ",
new ArraySegment<string>(args, 1, args.Length - 1));
switch (command)
{
case "add-doc-as-map":
AddDocAsMap(project).Wait();
break;
case "update-create-if-missing":
UpdateCreateIfMissing(project).Wait();
break;
case "add-doc-data-types":
AddDocDataTypes(project).Wait();
break;
case "add-simple-doc-as-entity":
AddSimpleDocAsEntity(project).Wait();
break;
case "set-requires-id":
SetRequiresId(project).Wait();
break;
case "add-doc-data-with-auto-id":
AddDocDataWithAutoId(project).Wait();
break;
case "add-doc-data-after-auto-id":
AddDocDataAfterAutoId(project).Wait();
break;
case "update-doc":
UpdateDoc(project).Wait();
break;
case "update-nested-fields":
UpdateNestedFields(project).Wait();
break;
case "update-server-timestamp":
UpdateServerTimestamp(project).Wait();
break;
case "update-document-array":
UpdateDocumentArray(project).Wait();
break;
case "update-document-increment":
UpdateDocumentIncrement(project).Wait();
break;
default:
Console.Write(Usage);
return;
}
}
}
}