google-cloud-firestore 1.4.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +1 -1
  3. data/CHANGELOG.md +23 -0
  4. data/lib/google-cloud-firestore.rb +14 -21
  5. data/lib/google/cloud/firestore.rb +12 -23
  6. data/lib/google/cloud/firestore/client.rb +12 -14
  7. data/lib/google/cloud/firestore/collection_reference.rb +1 -0
  8. data/lib/google/cloud/firestore/collection_reference_list.rb +137 -0
  9. data/lib/google/cloud/firestore/convert.rb +39 -39
  10. data/lib/google/cloud/firestore/credentials.rb +2 -2
  11. data/lib/google/cloud/firestore/document_reference.rb +8 -8
  12. data/lib/google/cloud/firestore/document_reference/list.rb +2 -2
  13. data/lib/google/cloud/firestore/document_snapshot.rb +4 -4
  14. data/lib/google/cloud/firestore/query.rb +4 -4
  15. data/lib/google/cloud/firestore/service.rb +63 -104
  16. data/lib/google/cloud/firestore/transaction.rb +2 -2
  17. data/lib/google/cloud/firestore/version.rb +1 -1
  18. data/lib/google/cloud/firestore/watch/listener.rb +9 -9
  19. data/lib/google/cloud/firestore/watch/order.rb +1 -1
  20. metadata +9 -107
  21. data/lib/google/cloud/firestore/admin.rb +0 -144
  22. data/lib/google/cloud/firestore/admin/v1.rb +0 -151
  23. data/lib/google/cloud/firestore/admin/v1/credentials.rb +0 -44
  24. data/lib/google/cloud/firestore/admin/v1/doc/google/firestore/admin/v1/field.rb +0 -86
  25. data/lib/google/cloud/firestore/admin/v1/doc/google/firestore/admin/v1/firestore_admin.rb +0 -161
  26. data/lib/google/cloud/firestore/admin/v1/doc/google/firestore/admin/v1/index.rb +0 -142
  27. data/lib/google/cloud/firestore/admin/v1/doc/google/longrunning/operations.rb +0 -51
  28. data/lib/google/cloud/firestore/admin/v1/doc/google/protobuf/any.rb +0 -131
  29. data/lib/google/cloud/firestore/admin/v1/doc/google/protobuf/empty.rb +0 -29
  30. data/lib/google/cloud/firestore/admin/v1/doc/google/protobuf/field_mask.rb +0 -222
  31. data/lib/google/cloud/firestore/admin/v1/doc/google/rpc/status.rb +0 -39
  32. data/lib/google/cloud/firestore/admin/v1/firestore_admin_client.rb +0 -902
  33. data/lib/google/cloud/firestore/admin/v1/firestore_admin_client_config.json +0 -76
  34. data/lib/google/cloud/firestore/v1.rb +0 -152
  35. data/lib/google/cloud/firestore/v1/credentials.rb +0 -42
  36. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/common.rb +0 -64
  37. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/document.rb +0 -136
  38. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/firestore.rb +0 -566
  39. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/query.rb +0 -227
  40. data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/write.rb +0 -241
  41. data/lib/google/cloud/firestore/v1/doc/google/protobuf/any.rb +0 -131
  42. data/lib/google/cloud/firestore/v1/doc/google/protobuf/empty.rb +0 -29
  43. data/lib/google/cloud/firestore/v1/doc/google/protobuf/timestamp.rb +0 -113
  44. data/lib/google/cloud/firestore/v1/doc/google/protobuf/wrappers.rb +0 -26
  45. data/lib/google/cloud/firestore/v1/doc/google/rpc/status.rb +0 -39
  46. data/lib/google/cloud/firestore/v1/firestore_client.rb +0 -1011
  47. data/lib/google/cloud/firestore/v1/firestore_client_config.json +0 -105
  48. data/lib/google/cloud/firestore/v1beta1.rb +0 -162
  49. data/lib/google/cloud/firestore/v1beta1/credentials.rb +0 -42
  50. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/common.rb +0 -64
  51. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/document.rb +0 -136
  52. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/firestore.rb +0 -564
  53. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/query.rb +0 -227
  54. data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/write.rb +0 -237
  55. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/any.rb +0 -131
  56. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/empty.rb +0 -29
  57. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/timestamp.rb +0 -113
  58. data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/wrappers.rb +0 -26
  59. data/lib/google/cloud/firestore/v1beta1/doc/google/rpc/status.rb +0 -39
  60. data/lib/google/cloud/firestore/v1beta1/firestore_client.rb +0 -1021
  61. data/lib/google/cloud/firestore/v1beta1/firestore_client_config.json +0 -100
  62. data/lib/google/firestore/admin/v1/field_pb.rb +0 -32
  63. data/lib/google/firestore/admin/v1/firestore_admin_pb.rb +0 -84
  64. data/lib/google/firestore/admin/v1/firestore_admin_services_pb.rb +0 -94
  65. data/lib/google/firestore/admin/v1/index_pb.rb +0 -58
  66. data/lib/google/firestore/admin/v1/location_pb.rb +0 -22
  67. data/lib/google/firestore/admin/v1/operation_pb.rb +0 -90
  68. data/lib/google/firestore/v1/common_pb.rb +0 -45
  69. data/lib/google/firestore/v1/document_pb.rb +0 -50
  70. data/lib/google/firestore/v1/firestore_pb.rb +0 -222
  71. data/lib/google/firestore/v1/firestore_services_pb.rb +0 -80
  72. data/lib/google/firestore/v1/query_pb.rb +0 -107
  73. data/lib/google/firestore/v1/write_pb.rb +0 -80
  74. data/lib/google/firestore/v1beta1/common_pb.rb +0 -45
  75. data/lib/google/firestore/v1beta1/document_pb.rb +0 -50
  76. data/lib/google/firestore/v1beta1/firestore_pb.rb +0 -222
  77. data/lib/google/firestore/v1beta1/firestore_services_pb.rb +0 -89
  78. data/lib/google/firestore/v1beta1/query_pb.rb +0 -107
  79. data/lib/google/firestore/v1beta1/write_pb.rb +0 -79
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/firestore/v1/credentials"
16
+ require "google/cloud/firestore/v1/firestore/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::V1::Credentials
40
+ class Credentials < Google::Cloud::Firestore::V1::Firestore::Credentials
41
41
  end
42
42
  end
43
43
  end
@@ -70,12 +70,13 @@ module Google
70
70
  # @!group Access
71
71
 
72
72
  ##
73
- # Retrieves a list of collections nested under the document snapshot.
73
+ # Retrieves an enumerator for the collections nested under the document snapshot.
74
74
  #
75
75
  # @yield [collections] The block for accessing the collections.
76
- # @yieldparam [CollectionReference] collection A collection.
76
+ # @yieldparam [CollectionReference] collection A collection reference object.
77
77
  #
78
- # @return [Enumerator<CollectionReference>] collection list.
78
+ # @return [Enumerator<CollectionReference>] An enumerator of collection references. If a block is provided, this
79
+ # is the same enumerator that is accessed through the block.
79
80
  #
80
81
  # @example
81
82
  # require "google/cloud/firestore"
@@ -91,11 +92,10 @@ module Google
91
92
  #
92
93
  def cols
93
94
  ensure_service!
94
-
95
- return enum_for :cols unless block_given?
96
-
97
- collection_ids = service.list_collections path
98
- collection_ids.each { |collection_id| yield col collection_id }
95
+ grpc = service.list_collections path
96
+ cols_enum = CollectionReferenceList.from_grpc(grpc, client, path).all
97
+ cols_enum.each { |c| yield c } if block_given?
98
+ cols_enum
99
99
  end
100
100
  alias collections cols
101
101
  alias list_collections cols
@@ -162,7 +162,7 @@ module Google
162
162
 
163
163
  ##
164
164
  # @private New DocumentReference::List from a
165
- # Google::Firestore::V1::ListDocumentsResponse object.
165
+ # Google::Cloud::Firestore::V1::ListDocumentsResponse object.
166
166
  def self.from_grpc grpc, client, parent, collection_id, max = nil
167
167
  documents = List.new(Array(grpc.documents).map do |document|
168
168
  DocumentReference.from_path document.name, client
@@ -170,7 +170,7 @@ module Google
170
170
  documents.instance_variable_set :@parent, parent
171
171
  documents.instance_variable_set :@collection_id, collection_id
172
172
  token = grpc.next_page_token
173
- token = nil if token == "".freeze
173
+ token = nil if token == ""
174
174
  documents.instance_variable_set :@token, token
175
175
  documents.instance_variable_set :@client, client
176
176
  documents.instance_variable_set :@max, max
@@ -62,7 +62,7 @@ module Google
62
62
  #
63
63
  class DocumentSnapshot
64
64
  ##
65
- # @private The Google::Firestore::V1::Document object.
65
+ # @private The Google::Cloud::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::V1::RunQueryResponse object.
348
+ # Google::Cloud::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::V1::DocumentChange object.
362
+ # Google::Cloud::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::V1::BatchGetDocumentsResponse object.
375
+ # Google::Cloud::Firestore::V1::BatchGetDocumentsResponse object.
376
376
  def self.from_batch_result result, client
377
377
  ref = nil
378
378
  grpc = nil
@@ -63,7 +63,7 @@ module Google
63
63
  attr_accessor :parent_path
64
64
 
65
65
  ##
66
- # @private The Google::Firestore::V1::StructuredQuery object.
66
+ # @private The Google::Cloud::Firestore::V1::StructuredQuery object.
67
67
  attr_accessor :query
68
68
 
69
69
  ##
@@ -883,7 +883,7 @@ module Google
883
883
 
884
884
  ##
885
885
  # @private
886
- StructuredQuery = Google::Firestore::V1::StructuredQuery
886
+ StructuredQuery = Google::Cloud::Firestore::V1::StructuredQuery
887
887
 
888
888
  ##
889
889
  # @private
@@ -987,7 +987,7 @@ module Google
987
987
  end
988
988
 
989
989
  def order_direction direction
990
- return :DESCENDING if direction.to_s.downcase.start_with? "d".freeze
990
+ return :DESCENDING if direction.to_s.downcase.start_with? "d"
991
991
  :ASCENDING
992
992
  end
993
993
 
@@ -1014,7 +1014,7 @@ module Google
1014
1014
  Convert.raw_to_value value
1015
1015
  end
1016
1016
 
1017
- Google::Firestore::V1::Cursor.new values: values
1017
+ Google::Cloud::Firestore::V1::Cursor.new values: values
1018
1018
  end
1019
1019
 
1020
1020
  def snapshot_to_cursor snapshot, query
@@ -26,65 +26,42 @@ 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, :host
29
+ attr_accessor :project, :credentials, :timeout, :host
30
30
 
31
31
  ##
32
32
  # Creates a new Service instance.
33
- def initialize project, credentials, host: nil, timeout: nil,
34
- client_config: nil
33
+ def initialize project, credentials, host: nil, timeout: nil
35
34
  @project = project
36
35
  @credentials = credentials
37
- @host = host || V1::FirestoreClient::SERVICE_ADDRESS
36
+ @host = host
38
37
  @timeout = timeout
39
- @client_config = client_config || {}
40
- end
41
-
42
- def channel
43
- require "grpc"
44
- GRPC::Core::Channel.new host, chan_args, chan_creds
45
- end
46
-
47
- def chan_args
48
- { "grpc.service_config_disable_resolution" => 1 }
49
- end
50
-
51
- def chan_creds
52
- return credentials if insecure?
53
- require "grpc"
54
- GRPC::Core::ChannelCredentials.new.compose \
55
- GRPC::Core::CallCredentials.new credentials.client.updater_proc
56
38
  end
57
39
 
58
40
  def firestore
59
41
  @firestore ||= \
60
- V1::FirestoreClient.new(
61
- credentials: channel,
62
- timeout: timeout,
63
- client_config: client_config,
64
- lib_name: "gccl",
65
- lib_version: Google::Cloud::Firestore::VERSION
66
- )
67
- end
68
-
69
- def insecure?
70
- credentials == :this_channel_is_insecure
42
+ V1::Firestore::Client.new do |config|
43
+ config.credentials = credentials if credentials
44
+ config.timeout = timeout if timeout
45
+ config.endpoint = host if host
46
+ config.lib_name = "gccl"
47
+ config.lib_version = Google::Cloud::Firestore::VERSION
48
+ config.metadata = { "google-cloud-resource-prefix": "projects/#{@project}" }
49
+ end
71
50
  end
72
51
 
73
52
  def get_documents document_paths, mask: nil, transaction: nil
74
- batch_get_args = {
53
+ batch_get_req = {
54
+ database: database_path,
75
55
  documents: document_paths,
76
56
  mask: document_mask(mask)
77
57
  }
78
58
  if transaction.is_a? String
79
- batch_get_args[:transaction] = transaction
59
+ batch_get_req[:transaction] = transaction
80
60
  elsif transaction
81
- batch_get_args[:new_transaction] = transaction
61
+ batch_get_req[:new_transaction] = transaction
82
62
  end
83
- batch_get_args[:options] = call_options parent: database_path
84
63
 
85
- execute do
86
- firestore.batch_get_documents database_path, **batch_get_args
87
- end
64
+ firestore.batch_get_documents batch_get_req, call_options(parent: database_path)
88
65
  end
89
66
 
90
67
  ##
@@ -96,81 +73,72 @@ module Google
96
73
  # method.
97
74
  def list_documents parent, collection_id, token: nil, max: nil
98
75
  mask = { field_paths: [] }
99
- call_options = nil
100
- call_options = Google::Gax::CallOptions.new page_token: token if token
101
- execute do
102
- paged_enum = firestore.list_documents \
103
- parent, collection_id, mask: mask, show_missing: true,
104
- page_size: max, options: call_options
105
-
106
- paged_enum.page.response
107
- end
108
- end
109
-
110
- def list_collections parent, transaction: nil
111
- list_args = {}
112
- if transaction.is_a? String
113
- list_args[:transaction] = transaction
114
- elsif transaction
115
- list_args[:new_transaction] = transaction
116
- end
117
- list_args[:options] = call_options parent: database_path
118
-
119
- execute do
120
- firestore.list_collection_ids parent, **list_args
121
- end
76
+ paged_enum = firestore.list_documents parent: parent,
77
+ collection_id: collection_id,
78
+ page_size: max,
79
+ page_token: token,
80
+ mask: mask,
81
+ show_missing: true
82
+ paged_enum.response
83
+ end
84
+
85
+ def list_collections parent, token: nil, max: nil
86
+ firestore.list_collection_ids(
87
+ {
88
+ parent: parent,
89
+ page_size: max,
90
+ page_token: token
91
+ },
92
+ call_options(parent: database_path)
93
+ )
122
94
  end
123
95
 
124
96
  def run_query path, query_grpc, transaction: nil
125
- run_query_args = { structured_query: query_grpc }
97
+ run_query_req = {
98
+ parent: path,
99
+ structured_query: query_grpc
100
+ }
126
101
  if transaction.is_a? String
127
- run_query_args[:transaction] = transaction
102
+ run_query_req[:transaction] = transaction
128
103
  elsif transaction
129
- run_query_args[:new_transaction] = transaction
104
+ run_query_req[:new_transaction] = transaction
130
105
  end
131
- run_query_args[:options] = call_options parent: database_path
132
106
 
133
- execute do
134
- firestore.run_query path, **run_query_args
135
- end
107
+ firestore.run_query run_query_req, call_options(parent: database_path)
136
108
  end
137
109
 
138
110
  def listen enum
139
- options = call_options parent: database_path
140
-
141
- execute do
142
- firestore.listen enum, options: options
143
- end
111
+ firestore.listen enum, call_options(parent: database_path)
144
112
  end
145
113
 
146
114
  def begin_transaction transaction_opt
147
- options = call_options parent: database_path
148
-
149
- execute do
150
- firestore.begin_transaction database_path,
151
- options_: transaction_opt,
152
- options: options
153
- end
115
+ firestore.begin_transaction(
116
+ {
117
+ database: database_path,
118
+ options: transaction_opt
119
+ },
120
+ call_options(parent: database_path)
121
+ )
154
122
  end
155
123
 
156
124
  def commit writes, transaction: nil
157
- commit_args = {
158
- writes: writes
125
+ commit_req = {
126
+ database: database_path,
127
+ writes: writes
159
128
  }
160
- commit_args[:transaction] = transaction if transaction
161
- commit_args[:options] = call_options parent: database_path
129
+ commit_req[:transaction] = transaction if transaction
162
130
 
163
- execute do
164
- firestore.commit database_path, **commit_args
165
- end
131
+ firestore.commit commit_req, call_options(parent: database_path)
166
132
  end
167
133
 
168
134
  def rollback transaction
169
- options = call_options parent: database_path
170
-
171
- execute do
172
- firestore.rollback database_path, transaction, options: options
173
- end
135
+ firestore.rollback(
136
+ {
137
+ database: database_path,
138
+ transaction: transaction
139
+ },
140
+ call_options(parent: database_path)
141
+ )
174
142
  end
175
143
 
176
144
  def database_path project_id: project, database_id: "(default)"
@@ -195,7 +163,7 @@ module Google
195
163
  end
196
164
 
197
165
  def call_options parent: nil, token: nil
198
- Google::Gax::CallOptions.new(**{
166
+ Gapic::CallOptions.new(**{
199
167
  metadata: default_headers(parent),
200
168
  page_token: token
201
169
  }.delete_if { |_, v| v.nil? })
@@ -207,16 +175,7 @@ module Google
207
175
  mask = Array(mask).map(&:to_s).reject(&:nil?).reject(&:empty?)
208
176
  return nil if mask.empty?
209
177
 
210
- Google::Firestore::V1::DocumentMask.new field_paths: mask
211
- end
212
-
213
- def execute
214
- yield
215
- rescue Google::Gax::GaxError => e
216
- # GaxError wraps BadStatus, but exposes it as #cause
217
- raise Google::Cloud::Error.from_error(e.cause)
218
- rescue GRPC::BadStatus => e
219
- raise Google::Cloud::Error.from_error(e)
178
+ Google::Cloud::Firestore::V1::DocumentMask.new field_paths: mask
220
179
  end
221
180
  end
222
181
  end
@@ -700,14 +700,14 @@ module Google
700
700
  # @private
701
701
  def transaction_opt
702
702
  read_write = \
703
- Google::Firestore::V1::TransactionOptions::ReadWrite.new
703
+ Google::Cloud::Firestore::V1::TransactionOptions::ReadWrite.new
704
704
 
705
705
  if @previous_transaction
706
706
  read_write.retry_transaction = @previous_transaction
707
707
  @previous_transaction = nil
708
708
  end
709
709
 
710
- Google::Firestore::V1::TransactionOptions.new(
710
+ Google::Cloud::Firestore::V1::TransactionOptions.new(
711
711
  read_write: read_write
712
712
  )
713
713
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Firestore
19
- VERSION = "1.4.4".freeze
19
+ VERSION = "2.0.0".freeze
20
20
  end
21
21
  end
22
22
  end
@@ -34,12 +34,12 @@ module Google
34
34
  raise ArgumentError if doc_ref.nil?
35
35
  raise ArgumentError if callback.nil?
36
36
 
37
- documents = Google::Firestore::V1::Target::DocumentsTarget.new(
37
+ documents = Google::Cloud::Firestore::V1::Target::DocumentsTarget.new(
38
38
  documents: [doc_ref.path]
39
39
  )
40
- init_listen_req = Google::Firestore::V1::ListenRequest.new(
40
+ init_listen_req = Google::Cloud::Firestore::V1::ListenRequest.new(
41
41
  database: doc_ref.client.path,
42
- add_target: Google::Firestore::V1::Target.new(
42
+ add_target: Google::Cloud::Firestore::V1::Target.new(
43
43
  documents: documents
44
44
  )
45
45
  )
@@ -51,10 +51,10 @@ module Google
51
51
  raise ArgumentError if query.nil?
52
52
  raise ArgumentError if callback.nil?
53
53
 
54
- init_listen_req = Google::Firestore::V1::ListenRequest.new(
54
+ init_listen_req = Google::Cloud::Firestore::V1::ListenRequest.new(
55
55
  database: query.client.path,
56
- add_target: Google::Firestore::V1::Target.new(
57
- query: Google::Firestore::V1::Target::QueryTarget.new(
56
+ add_target: Google::Cloud::Firestore::V1::Target.new(
57
+ query: Google::Cloud::Firestore::V1::Target::QueryTarget.new(
58
58
  parent: query.parent_path,
59
59
  structured_query: query.query
60
60
  )
@@ -223,7 +223,7 @@ module Google
223
223
  raise RestartStream # Raise to restart the stream
224
224
  end
225
225
  when :document_change
226
- # A {Google::Firestore::V1::Document Document} has changed.
226
+ # A {Google::Cloud::Firestore::V1::Document Document} has changed.
227
227
 
228
228
  if response.document_change.removed_target_ids.any?
229
229
  @inventory.delete response.document_change.document.name
@@ -231,12 +231,12 @@ module Google
231
231
  @inventory.add response.document_change.document
232
232
  end
233
233
  when :document_delete
234
- # A {Google::Firestore::V1::Document Document} has been
234
+ # A {Google::Cloud::Firestore::V1::Document Document} has been
235
235
  # deleted.
236
236
 
237
237
  @inventory.delete response.document_delete.document
238
238
  when :document_remove
239
- # A {Google::Firestore::V1::Document Document} has been
239
+ # A {Google::Cloud::Firestore::V1::Document Document} has been
240
240
  # removed from a target (because it is no longer relevant to
241
241
  # that target).
242
242