-
Notifications
You must be signed in to change notification settings - Fork 545
/
query_watch.rb
208 lines (177 loc) · 5.4 KB
/
query_watch.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
# 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"
def listen_document project_id:, collection_path: "cities", document_path: "SF"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
# document_path = "SF"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_listen_document]
doc_ref = firestore.col(collection_path).doc document_path
snapshots = []
# Watch the document.
listener = doc_ref.listen do |snapshot|
puts "Received document snapshot: #{snapshot.document_id}"
snapshots << snapshot
end
# [END firestore_listen_document]
# Create the document.
doc_ref.set(
{
name: "San Francisco",
state: "CA",
country: "USA",
capital: false,
population: 860_000
}
)
# Wait for the callback.
wait_until { !snapshots.empty? }
# [START firestore_listen_detach]
listener.stop
# [END firestore_listen_detach]
end
def listen_changes 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_listen_query_changes]
query = firestore.col(collection_path).where :state, :==, "CA"
added = []
modified = []
removed = []
# Watch the collection query.
listener = query.listen do |snapshot|
puts "Callback received query snapshot."
puts "Current cities in California:"
snapshot.changes.each do |change|
if change.added?
puts "New city: #{change.doc.document_id}"
added << snapshot
elsif change.modified?
puts "Modified city: #{change.doc.document_id}"
modified << snapshot
elsif change.removed?
puts "Removed city: #{change.doc.document_id}"
removed << snapshot
end
end
end
# [END firestore_listen_query_changes]
mtv_doc = firestore.col(collection_path).doc("MTV")
# Create the document.
mtv_doc.set(
{
name: "Mountain View",
state: "CA",
country: "USA",
capital: false,
population: 80_000
}
)
wait_until { !added.empty? }
# Update the document.
mtv_doc.update(
{
name: "Mountain View",
state: "CA",
country: "USA",
capital: false,
population: 90_000
}
)
wait_until { !modified.empty? }
# Delete the document.
mtv_doc.delete exists: true
wait_until { !removed.empty? }
listener.stop
end
def listen_errors 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_listen_handle_error]
listener = firestore.col(collection_path).listen do |snapshot|
snapshot.changes.each do |change|
puts "New city: #{change.doc.document_id}" if change.added?
end
end
# Register to be notified when unhandled errors occur.
listener.on_error do |error|
puts "Listen failed: #{error.message}"
end
# [END firestore_listen_handle_error]
listener.stop
end
def listen_multiple 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_listen_query_snapshots]
query = firestore.col(collection_path).where :state, :==, "CA"
docs = []
# Watch the collection query.
listener = query.listen do |snapshot|
puts "Callback received query snapshot."
puts "Current cities in California:"
snapshot.docs.each do |doc|
puts doc.document_id
docs << doc
end
end
# [END firestore_listen_query_snapshots]
# Create the document.
firestore.col(collection_path).doc("SF").set(
{
name: "San Francisco",
state: "CA",
country: "USA",
capital: false,
population: 860_000
}
)
# Wait for the callback.
wait_until { !docs.empty? }
listener.stop
end
def wait_until &block
wait_count = 0
until block.call
raise "wait_until criteria was not met." if wait_count > 200
wait_count += 1
sleep 0.1
end
end
if $PROGRAM_NAME == __FILE__
project = ENV["FIRESTORE_PROJECT"]
case ARGV.shift
when "listen_document"
listen_document project_id: project
when "listen_changes"
listen_changes project_id: project
when "listen_multiple"
listen_multiple project_id: project
when "listen_errors"
listen_errors project_id: project
else
puts <<~USAGE
Usage: bundle exec ruby query_data.rb [command]
Commands:
listen_document Listen for changes to a document.
listen_changes Listen for changes to a query.
listen_multiple Listen for changes to a query, returning the names of all cities for a state.
listen_errors Handle listening errors.
USAGE
end
end