-
Notifications
You must be signed in to change notification settings - Fork 540
/
add_data.rb
288 lines (246 loc) · 9.24 KB
/
add_data.rb
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
# Copyright 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.
require "google/cloud/firestore"
require "date"
def set_document project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_data_set_from_map]
city_ref = firestore.doc "#{collection_path}/LA"
data = {
name: "Los Angeles",
state: "CA",
country: "USA"
}
city_ref.set data
# [END firestore_data_set_from_map]
puts "Set data for the LA document in the cities collection."
end
def update_create_if_missing project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_data_set_doc_upsert]
city_ref = firestore.doc "#{collection_path}/LA"
city_ref.set({ capital: false }, merge: true)
# [END firestore_data_set_doc_upsert]
puts "Merged data into the LA document in the cities collection."
end
def set_document_data_types project_id:, collection_path: "data"
# project_id = "Your Google Cloud Project ID"
# collection_path = "data"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_data_set_from_map_nested]
doc_ref = firestore.doc "#{collection_path}/one"
data = {
stringExample: "Hello, World!",
booleanExample: true,
numberExample: 3.14159265,
dateExample: DateTime.now,
arrayExample: [5, true, "hello"],
nullExample: nil,
objectExample: {
a: 5,
b: true
}
}
doc_ref.set data
# [END firestore_data_set_from_map_nested]
puts "Set multiple data-type data for the one document in the data collection."
end
def set_requires_id project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
data = {
name: "Phuket",
country: "Thailand"
}
# [START firestore_data_set_id_specified]
city_ref = firestore.doc "#{collection_path}/new-city-id"
city_ref.set data
# [END firestore_data_set_id_specified]
puts "Added document with ID: new-city-id."
end
def add_doc_data_with_auto_id project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_data_set_id_random_collection]
data = {
name: "Tokyo",
country: "Japan"
}
cities_ref = firestore.col collection_path
added_doc_ref = cities_ref.add data
puts "Added document with ID: #{added_doc_ref.document_id}."
# [END firestore_data_set_id_random_collection]
end
def add_doc_data_after_auto_id project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
data = {
name: "Moscow",
country: "Russia"
}
# [START firestore_data_set_id_random_document_ref]
cities_ref = firestore.col collection_path
added_doc_ref = cities_ref.doc
puts "Added document with ID: #{added_doc_ref.document_id}."
added_doc_ref.set data
# [END firestore_data_set_id_random_document_ref]
puts "Added data to the #{added_doc_ref.document_id} document in the cities collection."
end
def update_doc project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
doc_ref = firestore.doc "#{collection_path}/DC"
data = {
name: "Washington D.C.",
country: "USA"
}
doc_ref.set data
# [START firestore_data_set_field]
city_ref = firestore.doc "#{collection_path}/DC"
city_ref.update({ capital: true })
# [END firestore_data_set_field]
puts "Updated the capital field of the DC document in the cities collection."
end
def update_doc_array project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
doc_ref = firestore.doc "#{collection_path}/DC"
data = {
name: "Washington D.C.",
country: "USA",
regions: ["east_coast"]
}
doc_ref.set data
# [START firestore_data_set_array_operations]
city_ref = firestore.doc "#{collection_path}/DC"
# Atomically add a new region to the 'regions' array field.
city_ref.update({ regions: firestore.field_array_union("greater_virginia") })
# Atomically remove a region from the 'regions' array field.
city_ref.update({ regions: firestore.field_array_delete("east_coast") })
# [END firestore_data_set_array_operations]
puts "Updated the regions field of the DC document in the cities collection."
end
def update_nested_fields project_id:, collection_path: "users"
# project_id = "Your Google Cloud Project ID"
# collection_path = "users"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_data_set_nested_fields]
# Create an initial document to update
frank_ref = firestore.doc "#{collection_path}/frank"
frank_ref.set(
{
name: "Frank",
favorites: {
food: "Pizza",
color: "Blue",
subject: "Recess"
},
age: 12
}
)
# Update age and favorite color
frank_ref.update({ age: 13, "favorites.color": "Red" })
# [END firestore_data_set_nested_fields]
puts "Updated the age and favorite color fields of the frank document in the users collection."
end
def update_server_timestamp project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
firestore.doc("#{collection_path}/new-city-id").set(
{
name: "new city",
state: nil,
country: "country",
capital: false,
population: 85
}
)
# [START firestore_data_set_server_timestamp]
city_ref = firestore.doc "#{collection_path}/new-city-id"
city_ref.update({ timestamp: firestore.field_server_time })
# [END firestore_data_set_server_timestamp]
puts "Updated the timestamp field of the new-city-id document in the cities collection."
end
def update_document_increment project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
firestore.doc("#{collection_path}/DC").set(
{
name: "Washington D.C.",
state: nil,
country: "USA",
capital: true,
population: 680_000
}
)
# [START firestore_data_set_numeric_increment]
city_ref = firestore.doc "#{collection_path}/DC"
city_ref.update({ population: firestore.field_increment(50) })
# [END firestore_data_set_numeric_increment]
puts "Updated the population of the DC document in the cities collection."
end
if $PROGRAM_NAME == __FILE__
project = ENV["FIRESTORE_PROJECT"]
case ARGV.shift
when "set_document"
set_document project_id: project
when "update_create_if_missing"
update_create_if_missing project_id: project
when "set_document_data_types"
set_document_data_types project_id: project
when "set_requires_id"
set_requires_id project_id: project
when "add_doc_data_with_auto_id"
add_doc_data_with_auto_id project_id: project
when "add_doc_data_after_auto_id"
add_doc_data_after_auto_id project_id: project
when "update_doc"
update_doc project_id: project
when "update_doc_array"
update_doc_array project_id: project
when "update_nested_fields"
update_nested_fields project_id: project
when "update_server_timestamp"
update_server_timestamp project_id: project
when "update_document_increment"
update_document_increment project_id: project
else
puts <<~USAGE
Usage: bundle exec ruby add_data.rb [command]
Commands:
set_document Set document data.
update_create_if_missing Update a document - create it if it's missing.
set_document_data_types Set document data with multiple data types.
set_requires_id Set document data with a given document id.
add_doc_data_with_auto_id Add document data with autogenerated id.
add_doc_data_after_auto_id Generate id, then add document data.
update_doc Update a document.
update_doc_array Update an array field in a document.
update_nested_fields Update fields in nested data.
update_server_timestamp Update field with server timestamp.
update_document_increment Update a document number field using Increment.
USAGE
end
end