google-cloud-firestore 2.10.1 → 2.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/lib/google/cloud/firestore/client.rb +51 -0
- data/lib/google/cloud/firestore/filter.rb +326 -0
- data/lib/google/cloud/firestore/query.rb +68 -86
- data/lib/google/cloud/firestore/service.rb +6 -3
- data/lib/google/cloud/firestore/version.rb +1 -1
- data/lib/google/cloud/firestore.rb +6 -2
- data/lib/google-cloud-firestore.rb +28 -4
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8d082b21e20a784bbc4ded66f836f6d04bd36b9925791edabe11e8d134427b4
|
4
|
+
data.tar.gz: f7d0f0cc23591434e68a92fee5132860fa6128cdaa3fbe4d3143911eb0c5f147
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19c611f16d93bedede808df8f049f9e6ec500a49e1b883b4d7e658d3c7f940c7afdf05eb4407615b80e91ee849eea6091662a886bf442b0a12f7887837578c8b
|
7
|
+
data.tar.gz: 9948560560760181151c1ec60f0c005a84e6ec1ae29c05f40efcca77d6be159f60592eafed854c69e7ab75a075c19b3786316782dccd5b98efaa814354f7d7e1
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# Release History
|
2
2
|
|
3
|
+
### 2.12.0 (2023-04-20)
|
4
|
+
|
5
|
+
#### Features
|
6
|
+
|
7
|
+
* Add support for OR query ([#20920](https://github.com/googleapis/google-cloud-ruby/issues/20920))
|
8
|
+
|
9
|
+
### 2.11.0 (2023-02-23)
|
10
|
+
|
11
|
+
#### Features
|
12
|
+
|
13
|
+
* Support REST transport ([#20446](https://github.com/googleapis/google-cloud-ruby/issues/20446))
|
14
|
+
|
3
15
|
### 2.10.1 (2023-02-16)
|
4
16
|
|
5
17
|
#### Bug Fixes
|
@@ -23,6 +23,7 @@ require "google/cloud/firestore/document_snapshot"
|
|
23
23
|
require "google/cloud/firestore/collection_group"
|
24
24
|
require "google/cloud/firestore/batch"
|
25
25
|
require "google/cloud/firestore/transaction"
|
26
|
+
require "google/cloud/firestore/filter"
|
26
27
|
|
27
28
|
module Google
|
28
29
|
module Cloud
|
@@ -185,6 +186,56 @@ module Google
|
|
185
186
|
end
|
186
187
|
alias collection_group col_group
|
187
188
|
|
189
|
+
##
|
190
|
+
# Creates a filter object.
|
191
|
+
#
|
192
|
+
# @param field [FieldPath, String, Symbol] A field path to filter
|
193
|
+
# results with.
|
194
|
+
# If a {FieldPath} object is not provided then the field will be
|
195
|
+
# treated as a dotted string, meaning the string represents individual
|
196
|
+
# fields joined by ".". Fields containing `~`, `*`, `/`, `[`, `]`, and
|
197
|
+
# `.` cannot be in a dotted string, and should provided using a
|
198
|
+
# {FieldPath} object instead.
|
199
|
+
#
|
200
|
+
# @param operator [String, Symbol] The operation to compare the field
|
201
|
+
# to. Acceptable values include:
|
202
|
+
# * less than: `<`, `lt`
|
203
|
+
# * less than or equal: `<=`, `lte`
|
204
|
+
# * greater than: `>`, `gt`
|
205
|
+
# * greater than or equal: `>=`, `gte`
|
206
|
+
# * equal: `=`, `==`, `eq`, `eql`, `is`
|
207
|
+
# * not equal: `!=`
|
208
|
+
# * in: `in`
|
209
|
+
# * not in: `not-in`, `not_in`
|
210
|
+
# * array contains: `array-contains`, `array_contains`
|
211
|
+
#
|
212
|
+
# @param value [Object] The value to compare the property to. Defaults to nil.
|
213
|
+
# Possible values are:
|
214
|
+
# * Integer
|
215
|
+
# * Float/BigDecimal
|
216
|
+
# * String
|
217
|
+
# * Boolean
|
218
|
+
# * Array
|
219
|
+
# * Date/Time
|
220
|
+
# * StringIO
|
221
|
+
# * Google::Cloud::Datastore::Key
|
222
|
+
# * Google::Cloud::Datastore::Entity
|
223
|
+
# * nil
|
224
|
+
#
|
225
|
+
# @return [Google::Cloud::Firestore::Filter] New filter object.
|
226
|
+
#
|
227
|
+
# @example
|
228
|
+
# require "google/cloud/firestore"
|
229
|
+
#
|
230
|
+
# firestore = Google::Cloud::Firestore.new
|
231
|
+
#
|
232
|
+
# # Create a filter
|
233
|
+
# filter = firestore.filter(:population, :>=, 1000000)
|
234
|
+
#
|
235
|
+
def filter field, operator, value
|
236
|
+
Filter.new field, operator, value
|
237
|
+
end
|
238
|
+
|
188
239
|
##
|
189
240
|
# Retrieves a document reference.
|
190
241
|
#
|
@@ -0,0 +1,326 @@
|
|
1
|
+
# Copyright 2023 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
require "google/cloud/firestore/v1"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Firestore
|
21
|
+
##
|
22
|
+
# Represents the filter for structured query.
|
23
|
+
#
|
24
|
+
class Filter
|
25
|
+
##
|
26
|
+
# @private Object of type
|
27
|
+
# Google::Cloud::Firestore::V1::StructuredQuery::Filter
|
28
|
+
attr_accessor :filter
|
29
|
+
|
30
|
+
##
|
31
|
+
# Create a Filter object.
|
32
|
+
#
|
33
|
+
# @param field [FieldPath, String, Symbol] A field path to filter
|
34
|
+
# results with.
|
35
|
+
# If a {FieldPath} object is not provided then the field will be
|
36
|
+
# treated as a dotted string, meaning the string represents individual
|
37
|
+
# fields joined by ".". Fields containing `~`, `*`, `/`, `[`, `]`, and
|
38
|
+
# `.` cannot be in a dotted string, and should provided using a
|
39
|
+
# {FieldPath} object instead.
|
40
|
+
#
|
41
|
+
# @param operator [String, Symbol] The operation to compare the field
|
42
|
+
# to. Acceptable values include:
|
43
|
+
# * less than: `<`, `lt`
|
44
|
+
# * less than or equal: `<=`, `lte`
|
45
|
+
# * greater than: `>`, `gt`
|
46
|
+
# * greater than or equal: `>=`, `gte`
|
47
|
+
# * equal: `=`, `==`, `eq`, `eql`, `is`
|
48
|
+
# * not equal: `!=`
|
49
|
+
# * in: `in`
|
50
|
+
# * not in: `not-in`, `not_in`
|
51
|
+
# * array contains: `array-contains`, `array_contains`
|
52
|
+
#
|
53
|
+
# @param value [Object] The value to compare the property to. Defaults to nil.
|
54
|
+
# Possible values are:
|
55
|
+
# * Integer
|
56
|
+
# * Float/BigDecimal
|
57
|
+
# * String
|
58
|
+
# * Boolean
|
59
|
+
# * Array
|
60
|
+
# * Date/Time
|
61
|
+
# * StringIO
|
62
|
+
# * Google::Cloud::Datastore::Key
|
63
|
+
# * Google::Cloud::Datastore::Entity
|
64
|
+
# * nil
|
65
|
+
#
|
66
|
+
# @return [Google::Cloud::Firestore::Filter] New filter for the given condition
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# require "google/cloud/firestore"
|
70
|
+
#
|
71
|
+
# firestore = Google::Cloud::Firestore.new
|
72
|
+
#
|
73
|
+
# # Create a Filter
|
74
|
+
# Google::Cloud::Firestore::Filter.new(:population, :>=, 1000000)
|
75
|
+
#
|
76
|
+
def initialize field, operator, value
|
77
|
+
@filter = create_filter field, operator, value
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# Joins filter using AND operator.
|
82
|
+
#
|
83
|
+
# @overload where(filter)
|
84
|
+
# Pass Firestore::Filter to `where` via field_or_filter argument.
|
85
|
+
#
|
86
|
+
# @param filter [::Google::Cloud::Firestore::Filter]
|
87
|
+
#
|
88
|
+
# @overload where(field, operator, value)
|
89
|
+
# Pass arguments to `where` via positional arguments.
|
90
|
+
#
|
91
|
+
# @param field [FieldPath, String, Symbol] A field path to filter
|
92
|
+
# results with.
|
93
|
+
# If a {FieldPath} object is not provided then the field will be
|
94
|
+
# treated as a dotted string, meaning the string represents individual
|
95
|
+
# fields joined by ".". Fields containing `~`, `*`, `/`, `[`, `]`, and
|
96
|
+
# `.` cannot be in a dotted string, and should provided using a
|
97
|
+
# {FieldPath} object instead.
|
98
|
+
#
|
99
|
+
# @param operator [String, Symbol] The operation to compare the field
|
100
|
+
# to. Acceptable values include:
|
101
|
+
# * less than: `<`, `lt`
|
102
|
+
# * less than or equal: `<=`, `lte`
|
103
|
+
# * greater than: `>`, `gt`
|
104
|
+
# * greater than or equal: `>=`, `gte`
|
105
|
+
# * equal: `=`, `==`, `eq`, `eql`, `is`
|
106
|
+
# * not equal: `!=`
|
107
|
+
# * in: `in`
|
108
|
+
# * not in: `not-in`, `not_in`
|
109
|
+
# * array contains: `array-contains`, `array_contains`
|
110
|
+
#
|
111
|
+
# @param value [Object] The value to compare the property to. Defaults to nil.
|
112
|
+
# Possible values are:
|
113
|
+
# * Integer
|
114
|
+
# * Float/BigDecimal
|
115
|
+
# * String
|
116
|
+
# * Boolean
|
117
|
+
# * Array
|
118
|
+
# * Date/Time
|
119
|
+
# * StringIO
|
120
|
+
# * Google::Cloud::Datastore::Key
|
121
|
+
# * Google::Cloud::Datastore::Entity
|
122
|
+
# * nil
|
123
|
+
#
|
124
|
+
# @return [Filter] New Filter object.
|
125
|
+
#
|
126
|
+
# @example Pass a Filter type object in argument
|
127
|
+
# require "google/cloud/firestore"
|
128
|
+
#
|
129
|
+
# filter_1 = Google::Cloud::Firestore.Firestore.new(:population, :>=, 1000000)
|
130
|
+
# filter_2 = Google::Cloud::Firestore.Firestore.new("done", "=", "false")
|
131
|
+
#
|
132
|
+
# filter = filter_1.and(filter_2)
|
133
|
+
#
|
134
|
+
# @example Pass filter conditions in the argument
|
135
|
+
# require "google/cloud/firestore"
|
136
|
+
#
|
137
|
+
# filter_1 = Google::Cloud::Firestore.Firestore.new(:population, :>=, 1000000)
|
138
|
+
#
|
139
|
+
# filter = filter_1.and("done", "=", "false")
|
140
|
+
#
|
141
|
+
def and filter_or_field = nil, operator = nil, value = nil
|
142
|
+
combine_filters composite_filter_and, filter_or_field, operator, value
|
143
|
+
end
|
144
|
+
|
145
|
+
##
|
146
|
+
# Joins filter using OR operator.
|
147
|
+
#
|
148
|
+
# @overload where(filter)
|
149
|
+
# Pass Firestore::Filter to `where` via field_or_filter argument.
|
150
|
+
#
|
151
|
+
# @param filter [::Google::Cloud::Firestore::Filter]
|
152
|
+
#
|
153
|
+
# @overload where(field, operator, value)
|
154
|
+
# Pass arguments to `where` via positional arguments.
|
155
|
+
#
|
156
|
+
# @param field [FieldPath, String, Symbol] A field path to filter
|
157
|
+
# results with.
|
158
|
+
# If a {FieldPath} object is not provided then the field will be
|
159
|
+
# treated as a dotted string, meaning the string represents individual
|
160
|
+
# fields joined by ".". Fields containing `~`, `*`, `/`, `[`, `]`, and
|
161
|
+
# `.` cannot be in a dotted string, and should provided using a
|
162
|
+
# {FieldPath} object instead.
|
163
|
+
#
|
164
|
+
# @param operator [String, Symbol] The operation to compare the field
|
165
|
+
# to. Acceptable values include:
|
166
|
+
# * less than: `<`, `lt`
|
167
|
+
# * less than or equal: `<=`, `lte`
|
168
|
+
# * greater than: `>`, `gt`
|
169
|
+
# * greater than or equal: `>=`, `gte`
|
170
|
+
# * equal: `=`, `==`, `eq`, `eql`, `is`
|
171
|
+
# * not equal: `!=`
|
172
|
+
# * in: `in`
|
173
|
+
# * not in: `not-in`, `not_in`
|
174
|
+
# * array contains: `array-contains`, `array_contains`
|
175
|
+
#
|
176
|
+
# @param value [Object] The value to compare the property to. Defaults to nil.
|
177
|
+
# Possible values are:
|
178
|
+
# * Integer
|
179
|
+
# * Float/BigDecimal
|
180
|
+
# * String
|
181
|
+
# * Boolean
|
182
|
+
# * Array
|
183
|
+
# * Date/Time
|
184
|
+
# * StringIO
|
185
|
+
# * Google::Cloud::Datastore::Key
|
186
|
+
# * Google::Cloud::Datastore::Entity
|
187
|
+
# * nil
|
188
|
+
#
|
189
|
+
# @return [Filter] New Filter object.
|
190
|
+
#
|
191
|
+
# @example Pass a Filter type object in argument
|
192
|
+
# require "google/cloud/firestore"
|
193
|
+
#
|
194
|
+
# filter_1 = Google::Cloud::Firestore.Firestore.new(:population, :>=, 1000000)
|
195
|
+
# filter_2 = Google::Cloud::Firestore.Firestore.new("done", "=", "false")
|
196
|
+
#
|
197
|
+
# filter = filter_1.or(filter_2)
|
198
|
+
#
|
199
|
+
# @example Pass filter conditions in the argument
|
200
|
+
# require "google/cloud/firestore"
|
201
|
+
#
|
202
|
+
# filter_1 = Google::Cloud::Firestore.Firestore.new(:population, :>=, 1000000)
|
203
|
+
#
|
204
|
+
# filter = filter_1.or("done", "=", "false")
|
205
|
+
#
|
206
|
+
def or filter_or_field = nil, operator = nil, value = nil
|
207
|
+
combine_filters composite_filter_or, filter_or_field, operator, value
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
|
212
|
+
##
|
213
|
+
# @private
|
214
|
+
StructuredQuery = Google::Cloud::Firestore::V1::StructuredQuery
|
215
|
+
|
216
|
+
##
|
217
|
+
# @private
|
218
|
+
FILTER_OPS = {
|
219
|
+
"<" => :LESS_THAN,
|
220
|
+
"lt" => :LESS_THAN,
|
221
|
+
"<=" => :LESS_THAN_OR_EQUAL,
|
222
|
+
"lte" => :LESS_THAN_OR_EQUAL,
|
223
|
+
">" => :GREATER_THAN,
|
224
|
+
"gt" => :GREATER_THAN,
|
225
|
+
">=" => :GREATER_THAN_OR_EQUAL,
|
226
|
+
"gte" => :GREATER_THAN_OR_EQUAL,
|
227
|
+
"=" => :EQUAL,
|
228
|
+
"==" => :EQUAL,
|
229
|
+
"eq" => :EQUAL,
|
230
|
+
"eql" => :EQUAL,
|
231
|
+
"is" => :EQUAL,
|
232
|
+
"!=" => :NOT_EQUAL,
|
233
|
+
"array_contains" => :ARRAY_CONTAINS,
|
234
|
+
"array-contains" => :ARRAY_CONTAINS,
|
235
|
+
"include" => :ARRAY_CONTAINS,
|
236
|
+
"include?" => :ARRAY_CONTAINS,
|
237
|
+
"has" => :ARRAY_CONTAINS,
|
238
|
+
"in" => :IN,
|
239
|
+
"not_in" => :NOT_IN,
|
240
|
+
"not-in" => :NOT_IN,
|
241
|
+
"array_contains_any" => :ARRAY_CONTAINS_ANY,
|
242
|
+
"array-contains-any" => :ARRAY_CONTAINS_ANY
|
243
|
+
}.freeze
|
244
|
+
|
245
|
+
##
|
246
|
+
# @private
|
247
|
+
INEQUALITY_FILTERS = [
|
248
|
+
:LESS_THAN,
|
249
|
+
:LESS_THAN_OR_EQUAL,
|
250
|
+
:GREATER_THAN,
|
251
|
+
:GREATER_THAN_OR_EQUAL
|
252
|
+
].freeze
|
253
|
+
|
254
|
+
def composite_filter_and
|
255
|
+
StructuredQuery::Filter.new(
|
256
|
+
composite_filter: StructuredQuery::CompositeFilter.new(op: :AND)
|
257
|
+
)
|
258
|
+
end
|
259
|
+
|
260
|
+
def composite_filter_or
|
261
|
+
StructuredQuery::Filter.new(
|
262
|
+
composite_filter: StructuredQuery::CompositeFilter.new(op: :OR)
|
263
|
+
)
|
264
|
+
end
|
265
|
+
|
266
|
+
def combine_filters new_filter, filter_or_field, operator, value
|
267
|
+
new_filter.composite_filter.filters << @filter
|
268
|
+
new_filter.composite_filter.filters << if filter_or_field.is_a? Google::Cloud::Firestore::Filter
|
269
|
+
filter_or_field.filter
|
270
|
+
else
|
271
|
+
create_filter filter_or_field, operator, value
|
272
|
+
end
|
273
|
+
dup.tap do |f|
|
274
|
+
f.filter = new_filter
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
def value_nil? value
|
279
|
+
[nil, :null, :nil].include? value
|
280
|
+
end
|
281
|
+
|
282
|
+
def value_nan? value
|
283
|
+
# Comparing NaN values raises, so check for #nan? first.
|
284
|
+
return true if value.respond_to?(:nan?) && value.nan?
|
285
|
+
[:nan].include? value
|
286
|
+
end
|
287
|
+
|
288
|
+
def value_unary? value
|
289
|
+
value_nil?(value) || value_nan?(value)
|
290
|
+
end
|
291
|
+
|
292
|
+
def create_filter field, op_key, value
|
293
|
+
return if field.nil? && op_key.nil? && value.nil?
|
294
|
+
field = FieldPath.parse field unless field.is_a? FieldPath
|
295
|
+
field = StructuredQuery::FieldReference.new field_path: field.formatted_string.to_s
|
296
|
+
operator = FILTER_OPS[op_key.to_s.downcase]
|
297
|
+
raise ArgumentError, "unknown operator #{op_key}" if operator.nil?
|
298
|
+
|
299
|
+
if value_unary? value
|
300
|
+
operator = case operator
|
301
|
+
when :EQUAL
|
302
|
+
value_nan?(value) ? :IS_NAN : :IS_NULL
|
303
|
+
when :NOT_EQUAL
|
304
|
+
value_nan?(value) ? :IS_NOT_NAN : :IS_NOT_NULL
|
305
|
+
else
|
306
|
+
raise ArgumentError, "can only perform '==' and '!=' comparisons on #{value} values"
|
307
|
+
end
|
308
|
+
|
309
|
+
return StructuredQuery::Filter.new(
|
310
|
+
unary_filter: StructuredQuery::UnaryFilter.new(
|
311
|
+
field: field, op: operator
|
312
|
+
)
|
313
|
+
)
|
314
|
+
end
|
315
|
+
|
316
|
+
value = Convert.raw_to_value value
|
317
|
+
StructuredQuery::Filter.new(
|
318
|
+
field_filter: StructuredQuery::FieldFilter.new(
|
319
|
+
field: field, op: operator, value: value
|
320
|
+
)
|
321
|
+
)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
@@ -18,6 +18,7 @@ require "google/cloud/firestore/document_snapshot"
|
|
18
18
|
require "google/cloud/firestore/query_listener"
|
19
19
|
require "google/cloud/firestore/convert"
|
20
20
|
require "google/cloud/firestore/aggregate_query"
|
21
|
+
require "google/cloud/firestore/filter"
|
21
22
|
require "json"
|
22
23
|
|
23
24
|
module Google
|
@@ -210,29 +211,48 @@ module Google
|
|
210
211
|
end
|
211
212
|
|
212
213
|
##
|
213
|
-
#
|
214
|
-
#
|
215
|
-
# @
|
216
|
-
#
|
217
|
-
#
|
218
|
-
#
|
219
|
-
#
|
220
|
-
#
|
221
|
-
#
|
222
|
-
#
|
223
|
-
#
|
224
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
#
|
228
|
-
#
|
229
|
-
#
|
230
|
-
#
|
231
|
-
#
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
#
|
214
|
+
# Adds filter to the where clause
|
215
|
+
#
|
216
|
+
# @overload where(filter)
|
217
|
+
# Pass Firestore::Filter to `where` via field_or_filter argument.
|
218
|
+
#
|
219
|
+
# @param filter [::Google::Cloud::Firestore::Filter]
|
220
|
+
#
|
221
|
+
# @overload where(field, operator, value)
|
222
|
+
# Pass arguments to `where` via positional arguments.
|
223
|
+
#
|
224
|
+
# @param field [FieldPath, String, Symbol] A field path to filter
|
225
|
+
# results with.
|
226
|
+
# If a {FieldPath} object is not provided then the field will be
|
227
|
+
# treated as a dotted string, meaning the string represents individual
|
228
|
+
# fields joined by ".". Fields containing `~`, `*`, `/`, `[`, `]`, and
|
229
|
+
# `.` cannot be in a dotted string, and should provided using a
|
230
|
+
# {FieldPath} object instead.
|
231
|
+
#
|
232
|
+
# @param operator [String, Symbol] The operation to compare the field
|
233
|
+
# to. Acceptable values include:
|
234
|
+
# * less than: `<`, `lt`
|
235
|
+
# * less than or equal: `<=`, `lte`
|
236
|
+
# * greater than: `>`, `gt`
|
237
|
+
# * greater than or equal: `>=`, `gte`
|
238
|
+
# * equal: `=`, `==`, `eq`, `eql`, `is`
|
239
|
+
# * not equal: `!=`
|
240
|
+
# * in: `in`
|
241
|
+
# * not in: `not-in`, `not_in`
|
242
|
+
# * array contains: `array-contains`, `array_contains`
|
243
|
+
#
|
244
|
+
# @param value [Object] The value to compare the property to. Defaults to nil.
|
245
|
+
# Possible values are:
|
246
|
+
# * Integer
|
247
|
+
# * Float/BigDecimal
|
248
|
+
# * String
|
249
|
+
# * Boolean
|
250
|
+
# * Array
|
251
|
+
# * Date/Time
|
252
|
+
# * StringIO
|
253
|
+
# * Google::Cloud::Datastore::Key
|
254
|
+
# * Google::Cloud::Datastore::Entity
|
255
|
+
# * nil
|
236
256
|
#
|
237
257
|
# @return [Query] New query with `where` called on it.
|
238
258
|
#
|
@@ -251,7 +271,25 @@ module Google
|
|
251
271
|
# puts "#{city.document_id} has #{city[:population]} residents."
|
252
272
|
# end
|
253
273
|
#
|
254
|
-
|
274
|
+
# @example
|
275
|
+
# require "google/cloud/firestore"
|
276
|
+
#
|
277
|
+
# firestore = Google::Cloud::Firestore.new
|
278
|
+
#
|
279
|
+
# # Get a collection reference
|
280
|
+
# cities_col = firestore.col "cities"
|
281
|
+
#
|
282
|
+
# # Create a filter
|
283
|
+
# filter = Filter.create(:population, :>=, 1000000)
|
284
|
+
#
|
285
|
+
# # Add filter to where clause
|
286
|
+
# query = query.where filter
|
287
|
+
#
|
288
|
+
# query.get do |city|
|
289
|
+
# puts "#{city.document_id} has #{city[:population]} residents."
|
290
|
+
# end
|
291
|
+
#
|
292
|
+
def where filter_or_field = nil, operator = nil, value = nil
|
255
293
|
if query_has_cursors?
|
256
294
|
raise "cannot call where after calling " \
|
257
295
|
"start_at, start_after, end_before, or end_at"
|
@@ -260,10 +298,12 @@ module Google
|
|
260
298
|
new_query = @query.dup
|
261
299
|
new_query ||= StructuredQuery.new
|
262
300
|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
301
|
+
if filter_or_field.is_a? Google::Cloud::Firestore::Filter
|
302
|
+
new_query.where = filter_or_field.filter
|
303
|
+
else
|
304
|
+
new_filter = Google::Cloud::Firestore::Filter.new filter_or_field, operator, value
|
305
|
+
add_filters_to_query new_query, new_filter.filter
|
306
|
+
end
|
267
307
|
|
268
308
|
Query.start new_query, parent_path, client, limit_type: limit_type
|
269
309
|
end
|
@@ -1087,34 +1127,6 @@ module Google
|
|
1087
1127
|
# @private
|
1088
1128
|
StructuredQuery = Google::Cloud::Firestore::V1::StructuredQuery
|
1089
1129
|
|
1090
|
-
##
|
1091
|
-
# @private
|
1092
|
-
FILTER_OPS = {
|
1093
|
-
"<" => :LESS_THAN,
|
1094
|
-
"lt" => :LESS_THAN,
|
1095
|
-
"<=" => :LESS_THAN_OR_EQUAL,
|
1096
|
-
"lte" => :LESS_THAN_OR_EQUAL,
|
1097
|
-
">" => :GREATER_THAN,
|
1098
|
-
"gt" => :GREATER_THAN,
|
1099
|
-
">=" => :GREATER_THAN_OR_EQUAL,
|
1100
|
-
"gte" => :GREATER_THAN_OR_EQUAL,
|
1101
|
-
"=" => :EQUAL,
|
1102
|
-
"==" => :EQUAL,
|
1103
|
-
"eq" => :EQUAL,
|
1104
|
-
"eql" => :EQUAL,
|
1105
|
-
"is" => :EQUAL,
|
1106
|
-
"!=" => :NOT_EQUAL,
|
1107
|
-
"array_contains" => :ARRAY_CONTAINS,
|
1108
|
-
"array-contains" => :ARRAY_CONTAINS,
|
1109
|
-
"include" => :ARRAY_CONTAINS,
|
1110
|
-
"include?" => :ARRAY_CONTAINS,
|
1111
|
-
"has" => :ARRAY_CONTAINS,
|
1112
|
-
"in" => :IN,
|
1113
|
-
"not_in" => :NOT_IN,
|
1114
|
-
"not-in" => :NOT_IN,
|
1115
|
-
"array_contains_any" => :ARRAY_CONTAINS_ANY,
|
1116
|
-
"array-contains-any" => :ARRAY_CONTAINS_ANY
|
1117
|
-
}.freeze
|
1118
1130
|
##
|
1119
1131
|
# @private
|
1120
1132
|
INEQUALITY_FILTERS = [
|
@@ -1138,36 +1150,6 @@ module Google
|
|
1138
1150
|
value_nil?(value) || value_nan?(value)
|
1139
1151
|
end
|
1140
1152
|
|
1141
|
-
def filter name, op_key, value
|
1142
|
-
field = StructuredQuery::FieldReference.new field_path: name.to_s
|
1143
|
-
operator = FILTER_OPS[op_key.to_s.downcase]
|
1144
|
-
raise ArgumentError, "unknown operator #{op_key}" if operator.nil?
|
1145
|
-
|
1146
|
-
if value_unary? value
|
1147
|
-
operator = case operator
|
1148
|
-
when :EQUAL
|
1149
|
-
value_nan?(value) ? :IS_NAN : :IS_NULL
|
1150
|
-
when :NOT_EQUAL
|
1151
|
-
value_nan?(value) ? :IS_NOT_NAN : :IS_NOT_NULL
|
1152
|
-
else
|
1153
|
-
raise ArgumentError, "can only perform '==' and '!=' comparisons on #{value} values"
|
1154
|
-
end
|
1155
|
-
|
1156
|
-
return StructuredQuery::Filter.new(
|
1157
|
-
unary_filter: StructuredQuery::UnaryFilter.new(
|
1158
|
-
field: field, op: operator
|
1159
|
-
)
|
1160
|
-
)
|
1161
|
-
end
|
1162
|
-
|
1163
|
-
value = Convert.raw_to_value value
|
1164
|
-
StructuredQuery::Filter.new(
|
1165
|
-
field_filter: StructuredQuery::FieldFilter.new(
|
1166
|
-
field: field, op: operator, value: value
|
1167
|
-
)
|
1168
|
-
)
|
1169
|
-
end
|
1170
|
-
|
1171
1153
|
def composite_filter
|
1172
1154
|
StructuredQuery::Filter.new(
|
1173
1155
|
composite_filter: StructuredQuery::CompositeFilter.new(op: :AND)
|
@@ -34,17 +34,19 @@ module Google
|
|
34
34
|
|
35
35
|
##
|
36
36
|
# Creates a new Service instance.
|
37
|
-
def initialize project, credentials, host: nil, timeout: nil, database: nil
|
37
|
+
def initialize project, credentials, host: nil, timeout: nil, database: nil, transport: :grpc
|
38
38
|
@project = project
|
39
39
|
@credentials = credentials
|
40
40
|
@host = host
|
41
41
|
@timeout = timeout
|
42
42
|
@database = database
|
43
|
+
@transport = transport
|
43
44
|
end
|
44
45
|
|
45
46
|
def firestore
|
46
|
-
@firestore ||=
|
47
|
-
V1::Firestore::Client
|
47
|
+
@firestore ||= begin
|
48
|
+
client_class = @transport == :rest ? V1::Firestore::Rest::Client : V1::Firestore::Client
|
49
|
+
client_class.new do |config|
|
48
50
|
config.credentials = credentials if credentials
|
49
51
|
config.timeout = timeout if timeout
|
50
52
|
config.endpoint = host if host
|
@@ -52,6 +54,7 @@ module Google
|
|
52
54
|
config.lib_version = Google::Cloud::Firestore::VERSION
|
53
55
|
config.metadata = { "google-cloud-resource-prefix": "projects/#{@project}/databases/#{@database}" }
|
54
56
|
end
|
57
|
+
end
|
55
58
|
end
|
56
59
|
|
57
60
|
def get_documents document_paths, mask: nil, transaction: nil, read_time: nil
|
@@ -61,6 +61,8 @@ module Google
|
|
61
61
|
# If the param is nil, uses the value of the `emulator_host` config.
|
62
62
|
# @param [String] database_id Identifier for a Firestore database. If not
|
63
63
|
# present, the default database of the project is used.
|
64
|
+
# @param [:grpc,:rest] transport Which transport to use to communicate
|
65
|
+
# with the server. Defaults to `:grpc`.
|
64
66
|
# @param [String] project Alias for the `project_id` argument. Deprecated.
|
65
67
|
# @param [String] keyfile Alias for the `credentials` argument.
|
66
68
|
# Deprecated.
|
@@ -79,6 +81,7 @@ module Google
|
|
79
81
|
endpoint: nil,
|
80
82
|
emulator_host: nil,
|
81
83
|
database_id: nil,
|
84
|
+
transport: nil,
|
82
85
|
project: nil,
|
83
86
|
keyfile: nil
|
84
87
|
project_id ||= (project || default_project_id)
|
@@ -87,13 +90,14 @@ module Google
|
|
87
90
|
endpoint ||= configure.endpoint
|
88
91
|
emulator_host ||= configure.emulator_host
|
89
92
|
database_id ||= configure.database_id
|
93
|
+
transport ||= configure.transport
|
90
94
|
|
91
95
|
if emulator_host
|
92
96
|
project_id = project_id.to_s
|
93
97
|
raise ArgumentError, "project_id is missing" if project_id.empty?
|
94
98
|
|
95
99
|
service = Firestore::Service.new project_id, :this_channel_is_insecure, host: emulator_host,
|
96
|
-
timeout: timeout, database: database_id
|
100
|
+
timeout: timeout, database: database_id, transport: transport
|
97
101
|
return Firestore::Client.new service
|
98
102
|
end
|
99
103
|
|
@@ -109,7 +113,7 @@ module Google
|
|
109
113
|
raise ArgumentError, "project_id is missing" if project_id.empty?
|
110
114
|
|
111
115
|
service = Firestore::Service.new project_id, credentials, host: endpoint,
|
112
|
-
timeout: timeout, database: database_id
|
116
|
+
timeout: timeout, database: database_id, transport: transport
|
113
117
|
Firestore::Client.new service
|
114
118
|
end
|
115
119
|
|
@@ -44,6 +44,8 @@ module Google
|
|
44
44
|
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
45
45
|
# @param [String] database_id Identifier for a Firestore database. If not
|
46
46
|
# present, the default database of the project is used.
|
47
|
+
# @param [:grpc,:rest] transport Which transport to use to communicate
|
48
|
+
# with the server. Defaults to `:grpc`.
|
47
49
|
#
|
48
50
|
# @return [Google::Cloud::Firestore::Client]
|
49
51
|
#
|
@@ -67,8 +69,16 @@ module Google
|
|
67
69
|
# database_id = "my-todo-database"
|
68
70
|
# firestore = gcloud.firestore database_id: database_id
|
69
71
|
#
|
70
|
-
def firestore scope: nil,
|
71
|
-
|
72
|
+
def firestore scope: nil,
|
73
|
+
timeout: nil,
|
74
|
+
database_id: nil,
|
75
|
+
transport: nil
|
76
|
+
transport ||= Google::Cloud.configure.firestore.transport
|
77
|
+
Google::Cloud.firestore @project, @keyfile,
|
78
|
+
scope: scope,
|
79
|
+
timeout: (timeout || @timeout),
|
80
|
+
database_id: database_id,
|
81
|
+
transport: transport
|
72
82
|
end
|
73
83
|
|
74
84
|
##
|
@@ -94,6 +104,8 @@ module Google
|
|
94
104
|
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
95
105
|
# @param [String] database_id Identifier for a Firestore database. If not
|
96
106
|
# present, the default database of the project is used.
|
107
|
+
# @param [:grpc,:rest] transport Which transport to use to communicate
|
108
|
+
# with the server. Defaults to `:grpc`.
|
97
109
|
#
|
98
110
|
# @return [Google::Cloud::Firestore::Client]
|
99
111
|
#
|
@@ -102,17 +114,26 @@ module Google
|
|
102
114
|
#
|
103
115
|
# firestore = Google::Cloud.firestore
|
104
116
|
#
|
105
|
-
def self.firestore project_id = nil,
|
117
|
+
def self.firestore project_id = nil,
|
118
|
+
credentials = nil,
|
119
|
+
scope: nil,
|
120
|
+
timeout: nil,
|
121
|
+
database_id: nil,
|
122
|
+
transport: nil
|
106
123
|
require "google/cloud/firestore"
|
124
|
+
transport ||= Google::Cloud.configure.firestore.transport
|
107
125
|
Google::Cloud::Firestore.new project_id: project_id,
|
108
126
|
credentials: credentials,
|
109
127
|
scope: scope,
|
110
128
|
timeout: timeout,
|
111
|
-
database_id: database_id
|
129
|
+
database_id: database_id,
|
130
|
+
transport: transport
|
112
131
|
end
|
113
132
|
end
|
114
133
|
end
|
115
134
|
|
135
|
+
# rubocop:disable Metrics/BlockLength
|
136
|
+
|
116
137
|
# Set the default firestore configuration
|
117
138
|
Google::Cloud.configure.add_config! :firestore do |config|
|
118
139
|
default_project = Google::Cloud::Config.deferred do
|
@@ -141,4 +162,7 @@ Google::Cloud.configure.add_config! :firestore do |config|
|
|
141
162
|
config.add_field! :emulator_host, default_emulator, match: String, allow_nil: true
|
142
163
|
config.add_field! :endpoint, "firestore.googleapis.com", match: String
|
143
164
|
config.add_field! :database_id, "(default)", match: String
|
165
|
+
config.add_field! :transport, :grpc, match: Symbol
|
144
166
|
end
|
167
|
+
|
168
|
+
# rubocop:enable Metrics/BlockLength
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-cloud-firestore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Google Inc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-cloud-core
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.10'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: concurrent-ruby
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -242,6 +242,7 @@ files:
|
|
242
242
|
- lib/google/cloud/firestore/document_snapshot.rb
|
243
243
|
- lib/google/cloud/firestore/field_path.rb
|
244
244
|
- lib/google/cloud/firestore/field_value.rb
|
245
|
+
- lib/google/cloud/firestore/filter.rb
|
245
246
|
- lib/google/cloud/firestore/generate.rb
|
246
247
|
- lib/google/cloud/firestore/query.rb
|
247
248
|
- lib/google/cloud/firestore/query_listener.rb
|