google-cloud-firestore 0.24.2 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +26 -0
  4. data/EMULATOR.md +49 -0
  5. data/lib/google-cloud-firestore.rb +6 -1
  6. data/lib/google/cloud/firestore.rb +36 -8
  7. data/lib/google/cloud/firestore/batch.rb +1 -1
  8. data/lib/google/cloud/firestore/client.rb +152 -4
  9. data/lib/google/cloud/firestore/collection_reference.rb +1 -1
  10. data/lib/google/cloud/firestore/commit_response.rb +1 -1
  11. data/lib/google/cloud/firestore/convert.rb +53 -38
  12. data/lib/google/cloud/firestore/credentials.rb +2 -2
  13. data/lib/google/cloud/firestore/document_reference.rb +2 -1
  14. data/lib/google/cloud/firestore/document_snapshot.rb +5 -5
  15. data/lib/google/cloud/firestore/field_path.rb +2 -2
  16. data/lib/google/cloud/firestore/field_value.rb +139 -9
  17. data/lib/google/cloud/firestore/query.rb +21 -15
  18. data/lib/google/cloud/firestore/query_snapshot.rb +1 -1
  19. data/lib/google/cloud/firestore/service.rb +31 -13
  20. data/lib/google/cloud/firestore/transaction.rb +39 -5
  21. data/lib/google/cloud/firestore/v1.rb +152 -0
  22. data/lib/google/cloud/firestore/v1/credentials.rb +42 -0
  23. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/common.rb +64 -0
  24. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/document.rb +136 -0
  25. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/firestore.rb +574 -0
  26. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/query.rb +219 -0
  27. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/write.rb +237 -0
  28. data/lib/google/cloud/firestore/v1/doc/google/protobuf/any.rb +130 -0
  29. data/lib/google/cloud/firestore/v1/doc/google/protobuf/empty.rb +29 -0
  30. data/lib/google/cloud/firestore/v1/doc/google/protobuf/timestamp.rb +109 -0
  31. data/lib/google/cloud/firestore/v1/doc/google/protobuf/wrappers.rb +26 -0
  32. data/lib/google/cloud/firestore/v1/doc/google/rpc/status.rb +84 -0
  33. data/lib/google/cloud/firestore/v1/firestore_client.rb +1044 -0
  34. data/lib/google/cloud/firestore/v1/firestore_client_config.json +100 -0
  35. data/lib/google/cloud/firestore/v1beta1.rb +1 -1
  36. data/lib/google/cloud/firestore/v1beta1/credentials.rb +1 -1
  37. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/common.rb +1 -1
  38. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/document.rb +1 -1
  39. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/firestore.rb +1 -1
  40. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/query.rb +1 -1
  41. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/write.rb +41 -1
  42. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/any.rb +1 -1
  43. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/empty.rb +1 -1
  44. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/timestamp.rb +1 -1
  45. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/wrappers.rb +1 -65
  46. data/lib/google/cloud/firestore/v1beta1/doc/google/rpc/status.rb +1 -1
  47. data/lib/google/cloud/firestore/v1beta1/firestore_client.rb +1 -1
  48. data/lib/google/cloud/firestore/v1beta1/firestore_client_config.json +1 -1
  49. data/lib/google/cloud/firestore/version.rb +1 -1
  50. data/lib/google/cloud/firestore/watch/enumerator_queue.rb +0 -2
  51. data/lib/google/cloud/firestore/watch/inventory.rb +2 -2
  52. data/lib/google/cloud/firestore/watch/listener.rb +16 -17
  53. data/lib/google/cloud/firestore/watch/order.rb +1 -1
  54. data/lib/google/firestore/v1/common_pb.rb +45 -0
  55. data/lib/google/firestore/v1/document_pb.rb +50 -0
  56. data/lib/google/firestore/v1/firestore_pb.rb +220 -0
  57. data/lib/google/firestore/v1/firestore_services_pb.rb +87 -0
  58. data/lib/google/firestore/v1/query_pb.rb +105 -0
  59. data/lib/google/firestore/v1/write_pb.rb +79 -0
  60. data/lib/google/firestore/v1beta1/firestore_services_pb.rb +2 -1
  61. data/lib/google/firestore/v1beta1/write_pb.rb +3 -0
  62. metadata +26 -5
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/firestore/v1beta1/credentials"
16
+ require "google/cloud/firestore/v1/credentials"
17
17
 
18
18
  module Google
19
19
  module Cloud
@@ -37,7 +37,7 @@ module Google
37
37
  #
38
38
  # firestore.project_id #=> "my-project"
39
39
  #
40
- class Credentials < Google::Cloud::Firestore::V1beta1::Credentials
40
+ class Credentials < Google::Cloud::Firestore::V1::Credentials
41
41
  end
42
42
  end
43
43
  end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/firestore/v1beta1"
16
+ require "google/cloud/firestore/v1"
17
17
  require "google/cloud/firestore/document_snapshot"
18
18
  require "google/cloud/firestore/collection_reference"
19
19
  require "google/cloud/firestore/document_listener"
@@ -97,6 +97,7 @@ module Google
97
97
  collection_ids.each { |collection_id| yield col(collection_id) }
98
98
  end
99
99
  alias collections cols
100
+ alias list_collections cols
100
101
 
101
102
  ##
102
103
  # Retrieves a collection nested under the document snapshot.
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/firestore/v1beta1"
16
+ require "google/cloud/firestore/v1"
17
17
  require "google/cloud/firestore/document_reference"
18
18
  require "google/cloud/firestore/collection_reference"
19
19
  require "google/cloud/firestore/convert"
@@ -62,7 +62,7 @@ module Google
62
62
  #
63
63
  class DocumentSnapshot
64
64
  ##
65
- # @private The Google::Firestore::V1beta1::Document object.
65
+ # @private The Google::Firestore::V1::Document object.
66
66
  attr_accessor :grpc
67
67
 
68
68
  ##
@@ -345,7 +345,7 @@ module Google
345
345
 
346
346
  ##
347
347
  # @private New DocumentSnapshot from a
348
- # Google::Firestore::V1beta1::RunQueryResponse object.
348
+ # Google::Firestore::V1::RunQueryResponse object.
349
349
  def self.from_query_result result, client
350
350
  ref = DocumentReference.from_path result.document.name, client
351
351
  read_at = Convert.timestamp_to_time result.read_time
@@ -359,7 +359,7 @@ module Google
359
359
 
360
360
  ##
361
361
  # @private New DocumentSnapshot from a
362
- # Google::Firestore::V1beta1::DocumentChange object.
362
+ # Google::Firestore::V1::DocumentChange object.
363
363
  def self.from_document document, client, read_at: nil
364
364
  ref = DocumentReference.from_path document.name, client
365
365
 
@@ -372,7 +372,7 @@ module Google
372
372
 
373
373
  ##
374
374
  # @private New DocumentSnapshot from a
375
- # Google::Firestore::V1beta1::BatchGetDocumentsResponse object.
375
+ # Google::Firestore::V1::BatchGetDocumentsResponse object.
376
376
  def self.from_batch_result result, client
377
377
  ref = nil
378
378
  grpc = nil
@@ -208,8 +208,8 @@ module Google
208
208
 
209
209
  protected
210
210
 
211
- START_FIELD_PATH_CHARS = /\A[a-zA-Z_]/
212
- INVALID_FIELD_PATH_CHARS = %r{[\~\*\/\[\]]}
211
+ START_FIELD_PATH_CHARS = /\A[a-zA-Z_]/.freeze
212
+ INVALID_FIELD_PATH_CHARS = %r{[\~\*\/\[\]]}.freeze
213
213
 
214
214
  def escape_field_for_path field
215
215
  field = String field
@@ -35,9 +35,9 @@ module Google
35
35
  ##
36
36
  # @private Creates a field value object representing changes made to
37
37
  # fields in document data.
38
- def initialize type, values = nil
38
+ def initialize type, value = nil
39
39
  @type = type
40
- @values = values
40
+ @value = value
41
41
  end
42
42
 
43
43
  ##
@@ -65,10 +65,10 @@ module Google
65
65
 
66
66
  ##
67
67
  # @private
68
- # The values to change to an individual field in document data,
69
- # depending on the type of change.
68
+ # The value to change to an individual field in document data, depending
69
+ # on the type of change.
70
70
  #
71
- # @return [Array<Object>] The values.
71
+ # @return [Object] The value.
72
72
  #
73
73
  # @example
74
74
  # require "google/cloud/firestore"
@@ -82,13 +82,13 @@ module Google
82
82
  # 1, 2, 3
83
83
  # )
84
84
  # array_union.type #=> :array_union
85
- # array_union.values #=> [1, 2, 3]
85
+ # array_union.value #=> [1, 2, 3]
86
86
  #
87
87
  # nyc_ref.update({ name: "New York City",
88
88
  # lucky_numbers: array_union })
89
89
  #
90
- def values
91
- @values
90
+ def value
91
+ @value
92
92
  end
93
93
 
94
94
  ##
@@ -138,7 +138,7 @@ module Google
138
138
  end
139
139
 
140
140
  ##
141
- # Creates a sentinel value to indicate the union of the given values
141
+ # Creates a sentinel value to indicate the union of the given value
142
142
  # with an array.
143
143
  #
144
144
  # @param [Object] values The values to add to the array. Required.
@@ -202,6 +202,136 @@ module Google
202
202
 
203
203
  new :array_delete, values
204
204
  end
205
+
206
+ ##
207
+ # Creates a sentinel value to indicate the addition the given value to
208
+ # the field's current value.
209
+ #
210
+ # If the field's current value is not an integer or a double value
211
+ # (Numeric), or if the field does not yet exist, the transformation will
212
+ # set the field to the given value. If either of the given value or the
213
+ # current field value are doubles, both values will be interpreted as
214
+ # doubles. Double arithmetic and representation of double values follow
215
+ # IEEE 754 semantics. If there is positive/negative integer overflow,
216
+ # the field is resolved to the largest magnitude positive/negative
217
+ # integer.
218
+ #
219
+ # @param [Numeric] value The value to add to the given value. Required.
220
+ #
221
+ # @return [FieldValue] The increment field value object.
222
+ #
223
+ # @raise [ArgumentError] if the value is not an Integer or Numeric.
224
+ #
225
+ # @example
226
+ # require "google/cloud/firestore"
227
+ #
228
+ # firestore = Google::Cloud::Firestore.new
229
+ #
230
+ # # Get a document reference
231
+ # nyc_ref = firestore.doc "cities/NYC"
232
+ #
233
+ # # Set the population to increment by 1.
234
+ # increment_value = Google::Cloud::Firestore::FieldValue.increment 1
235
+ #
236
+ # nyc_ref.update({ name: "New York City",
237
+ # population: increment_value })
238
+ #
239
+ def self.increment value
240
+ # verify the values are the correct types
241
+ unless value.is_a? Numeric
242
+ raise ArgumentError, "value must be a Numeric"
243
+ end
244
+
245
+ new :increment, value
246
+ end
247
+
248
+ ##
249
+ # Creates a sentinel value to indicate the setting the field to the
250
+ # maximum of its current value and the given value.
251
+ #
252
+ # If the field is not an integer or double (Numeric), or if the field
253
+ # does not yet exist, the transformation will set the field to the given
254
+ # value. If a maximum operation is applied where the field and the input
255
+ # value are of mixed types (that is - one is an integer and one is a
256
+ # double) the field takes on the type of the larger operand. If the
257
+ # operands are equivalent (e.g. 3 and 3.0), the field does not change.
258
+ # 0, 0.0, and -0.0 are all zero. The maximum of a zero stored value and
259
+ # zero input value is always the stored value. The maximum of any
260
+ # numeric value x and NaN is NaN.
261
+ #
262
+ # @param [Numeric] value The value to compare against the given value to
263
+ # calculate the maximum value to set. Required.
264
+ #
265
+ # @return [FieldValue] The maximum field value object.
266
+ #
267
+ # @raise [ArgumentError] if the value is not an Integer or Numeric.
268
+ #
269
+ # @example
270
+ # require "google/cloud/firestore"
271
+ #
272
+ # firestore = Google::Cloud::Firestore.new
273
+ #
274
+ # # Get a document reference
275
+ # nyc_ref = firestore.doc "cities/NYC"
276
+ #
277
+ # # Set the population to be at maximum 4,000,000.
278
+ # maximum_value = Google::Cloud::Firestore::FieldValue.maximum 4000000
279
+ #
280
+ # nyc_ref.update({ name: "New York City",
281
+ # population: maximum_value })
282
+ #
283
+ def self.maximum value
284
+ # verify the values are the correct types
285
+ unless value.is_a? Numeric
286
+ raise ArgumentError, "value must be a Numeric"
287
+ end
288
+
289
+ new :maximum, value
290
+ end
291
+
292
+ ##
293
+ # Creates a sentinel value to indicate the setting the field to the
294
+ # minimum of its current value and the given value.
295
+ #
296
+ # If the field is not an integer or double (Numeric), or if the field
297
+ # does not yet exist, the transformation will set the field to the input
298
+ # value. If a minimum operation is applied where the field and the input
299
+ # value are of mixed types (that is - one is an integer and one is a
300
+ # double) the field takes on the type of the smaller operand. If the
301
+ # operands are equivalent (e.g. 3 and 3.0), the field does not change.
302
+ # 0, 0.0, and -0.0 are all zero. The minimum of a zero stored value and
303
+ # zero input value is always the stored value. The minimum of any
304
+ # numeric value x and NaN is NaN.
305
+ #
306
+ # @param [Numeric] value The value to compare against the given value to
307
+ # calculate the minimum value to set. Required.
308
+ #
309
+ # @return [FieldValue] The minimum field value object.
310
+ #
311
+ # @raise [ArgumentError] if the value is not an Integer or Numeric.
312
+ #
313
+ # @example
314
+ # require "google/cloud/firestore"
315
+ #
316
+ # firestore = Google::Cloud::Firestore.new
317
+ #
318
+ # # Get a document reference
319
+ # nyc_ref = firestore.doc "cities/NYC"
320
+ #
321
+ # # Set the population to be at minimum 1,000,000.
322
+ # minimum_value = Google::Cloud::Firestore::FieldValue.minimum 1000000
323
+ #
324
+ # nyc_ref.update({ name: "New York City",
325
+ # population: minimum_value })
326
+ #
327
+ def self.minimum value
328
+ # verify the values are the correct types
329
+ unless value.is_a? Numeric
330
+ raise ArgumentError, "value must be a Numeric"
331
+ end
332
+
333
+ new :minimum, value
334
+ end
205
335
  end
206
336
  end
207
337
  end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/firestore/v1beta1"
16
+ require "google/cloud/firestore/v1"
17
17
  require "google/cloud/firestore/document_snapshot"
18
18
  require "google/cloud/firestore/query_listener"
19
19
  require "google/cloud/firestore/convert"
@@ -63,7 +63,7 @@ module Google
63
63
  attr_accessor :parent_path
64
64
 
65
65
  ##
66
- # @private The Google::Firestore::V1beta1::Query object.
66
+ # @private The Google::Firestore::V1::Query object.
67
67
  attr_accessor :query
68
68
 
69
69
  ##
@@ -309,7 +309,7 @@ module Google
309
309
  field = FieldPath.parse field unless field.is_a? FieldPath
310
310
 
311
311
  new_query.order_by << StructuredQuery::Order.new(
312
- field: StructuredQuery::FieldReference.new(
312
+ field: StructuredQuery::FieldReference.new(
313
313
  field_path: field.formatted_string
314
314
  ),
315
315
  direction: order_direction(direction)
@@ -883,7 +883,7 @@ module Google
883
883
 
884
884
  ##
885
885
  # @private
886
- StructuredQuery = Google::Firestore::V1beta1::StructuredQuery
886
+ StructuredQuery = Google::Firestore::V1::StructuredQuery
887
887
 
888
888
  ##
889
889
  # @private
@@ -949,19 +949,25 @@ module Google
949
949
 
950
950
  operator = value_nan?(value) ? :IS_NAN : :IS_NULL
951
951
 
952
- return StructuredQuery::Filter.new(unary_filter:
953
- StructuredQuery::UnaryFilter.new(field: field, op: operator))
952
+ return StructuredQuery::Filter.new(
953
+ unary_filter: StructuredQuery::UnaryFilter.new(
954
+ field: field, op: operator
955
+ )
956
+ )
954
957
  end
955
958
 
956
959
  value = Convert.raw_to_value value
957
- StructuredQuery::Filter.new(field_filter:
958
- StructuredQuery::FieldFilter.new(field: field, op: operator,
959
- value: value))
960
+ StructuredQuery::Filter.new(
961
+ field_filter: StructuredQuery::FieldFilter.new(
962
+ field: field, op: operator, value: value
963
+ )
964
+ )
960
965
  end
961
966
 
962
967
  def composite_filter
963
- StructuredQuery::Filter.new(composite_filter:
964
- StructuredQuery::CompositeFilter.new(op: :AND))
968
+ StructuredQuery::Filter.new(
969
+ composite_filter: StructuredQuery::CompositeFilter.new(op: :AND)
970
+ )
965
971
  end
966
972
 
967
973
  def add_filters_to_query query, filter
@@ -1005,7 +1011,7 @@ module Google
1005
1011
  Convert.raw_to_value value
1006
1012
  end
1007
1013
 
1008
- Google::Firestore::V1beta1::Cursor.new values: values
1014
+ Google::Firestore::V1::Cursor.new values: values
1009
1015
  end
1010
1016
 
1011
1017
  def snapshot_to_cursor snapshot, query
@@ -1035,11 +1041,11 @@ module Google
1035
1041
  inequality_paths = inequality_filter_field_paths query
1036
1042
  orig_order = order_by_field_paths query
1037
1043
 
1038
- inequality_paths.reverse.each do |field_path|
1044
+ inequality_paths.reverse_each do |field_path|
1039
1045
  next if orig_order.include? field_path
1040
1046
 
1041
1047
  query.order_by.unshift StructuredQuery::Order.new(
1042
- field: StructuredQuery::FieldReference.new(
1048
+ field: StructuredQuery::FieldReference.new(
1043
1049
  field_path: field_path
1044
1050
  ),
1045
1051
  direction: :ASCENDING
@@ -1051,7 +1057,7 @@ module Google
1051
1057
  return if order_by_field_paths(query).include? doc_id_path
1052
1058
 
1053
1059
  query.order_by.push StructuredQuery::Order.new(
1054
- field: StructuredQuery::FieldReference.new(
1060
+ field: StructuredQuery::FieldReference.new(
1055
1061
  field_path: doc_id_path
1056
1062
  ),
1057
1063
  direction: last_order_direction(query)
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/firestore/v1beta1"
16
+ require "google/cloud/firestore/v1"
17
17
  require "google/cloud/firestore/document_reference"
18
18
  require "google/cloud/firestore/collection_reference"
19
19
  require "google/cloud/firestore/convert"
@@ -17,7 +17,7 @@ require "google/cloud/env"
17
17
  require "google/cloud/errors"
18
18
  require "google/cloud/firestore/credentials"
19
19
  require "google/cloud/firestore/version"
20
- require "google/cloud/firestore/v1beta1"
20
+ require "google/cloud/firestore/v1"
21
21
 
22
22
  module Google
23
23
  module Cloud
@@ -26,28 +26,46 @@ module Google
26
26
  # @private Represents the gRPC Firestore service, including all the API
27
27
  # methods.
28
28
  class Service
29
- attr_accessor :project, :credentials, :timeout, :client_config
29
+ attr_accessor :project, :credentials, :timeout, :client_config, :host
30
30
 
31
31
  ##
32
32
  # Creates a new Service instance.
33
- def initialize project, credentials, timeout: nil, client_config: nil
33
+ def initialize project, credentials, host: nil, timeout: nil,
34
+ client_config: nil
34
35
  @project = project
35
36
  @credentials = credentials
37
+ @host = host || V1::FirestoreClient::SERVICE_ADDRESS
36
38
  @timeout = timeout
37
39
  @client_config = client_config || {}
38
40
  end
39
41
 
42
+ def channel
43
+ require "grpc"
44
+ GRPC::Core::Channel.new host, nil, chan_creds
45
+ end
46
+
47
+ def chan_creds
48
+ return credentials if insecure?
49
+ require "grpc"
50
+ GRPC::Core::ChannelCredentials.new.compose \
51
+ GRPC::Core::CallCredentials.new credentials.client.updater_proc
52
+ end
53
+
40
54
  def firestore
41
55
  @firestore ||= \
42
- V1beta1::FirestoreClient.new(
43
- credentials: credentials,
44
- timeout: timeout,
56
+ V1::FirestoreClient.new(
57
+ credentials: channel,
58
+ timeout: timeout,
45
59
  client_config: client_config,
46
- lib_name: "gccl",
47
- lib_version: Google::Cloud::Firestore::VERSION
60
+ lib_name: "gccl",
61
+ lib_version: Google::Cloud::Firestore::VERSION
48
62
  )
49
63
  end
50
64
 
65
+ def insecure?
66
+ credentials == :this_channel_is_insecure
67
+ end
68
+
51
69
  def get_documents document_paths, mask: nil, transaction: nil
52
70
  batch_get_args = { mask: document_mask(mask) }
53
71
  if transaction.is_a? String
@@ -105,7 +123,7 @@ module Google
105
123
  execute do
106
124
  firestore.begin_transaction database_path,
107
125
  options_: transaction_opt,
108
- options: options
126
+ options: options
109
127
  end
110
128
  end
111
129
 
@@ -128,11 +146,11 @@ module Google
128
146
  end
129
147
 
130
148
  def database_path project_id: project, database_id: "(default)"
131
- V1beta1::FirestoreClient.database_root_path project_id, database_id
149
+ V1::FirestoreClient.database_root_path project_id, database_id
132
150
  end
133
151
 
134
152
  def documents_path project_id: project, database_id: "(default)"
135
- V1beta1::FirestoreClient.document_root_path project_id, database_id
153
+ V1::FirestoreClient.document_root_path project_id, database_id
136
154
  end
137
155
 
138
156
  def inspect
@@ -148,7 +166,7 @@ module Google
148
166
 
149
167
  def call_options parent: nil, token: nil
150
168
  Google::Gax::CallOptions.new({
151
- kwargs: default_headers(parent),
169
+ metadata: default_headers(parent),
152
170
  page_token: token
153
171
  }.delete_if { |_, v| v.nil? })
154
172
  end
@@ -159,7 +177,7 @@ module Google
159
177
  mask = Array(mask).map(&:to_s).reject(&:nil?).reject(&:empty?)
160
178
  return nil if mask.empty?
161
179
 
162
- Google::Firestore::V1beta1::DocumentMask.new field_paths: mask
180
+ Google::Firestore::V1::DocumentMask.new field_paths: mask
163
181
  end
164
182
 
165
183
  def execute