google-cloud-firestore 0.25.1 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 827e034858a868b3f10ad1aa997efd49ae602d56a57d16cbc12fa57cd6d69f1c
4
- data.tar.gz: 3a7e878306128346310111ec5c99ca008b48cb030c44096f7a86c1d9b99da6fc
3
+ metadata.gz: ec9d1913fdc1c6817571acb97d791e1f452b7a774b8958d0affe0d80d61a3aa7
4
+ data.tar.gz: 139129578cb7616d7f3f1b4e8ffc0991913f57982c3c9e66e8b190b5166a0d02
5
5
  SHA512:
6
- metadata.gz: 2bee3a7931f0dedec0666925e3e8e1485cd9c2865cc6a2d5909dba2121a15590f4282baa61190829d888610e289c33ef3d60421a19378cdd1019773a9d9bce86
7
- data.tar.gz: afad9e59107345669f0918e6f28faf2fd5baefc01e445111e9e01abb6d19278e10789a7cff21930125f6229a5e03886c32bf63b8a8e17d70e1ac70b58917400c
6
+ metadata.gz: 28805388905373117c1ddb97e176d4e98bb45312ab6e5d2097ffe6dd96af9d6cef799496f3dedf5322f6b712c53091c51fd2e0365120d3c97ccfe2e01fdbe974
7
+ data.tar.gz: fe2f0cb02869d789fe5ee18336c3486f099662452ad4ad78676cbe3d7488feb46ef84b162c02508b07352dd785b2c6f0e5672af603dfcd060b62eb4f424b8f9f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Release History
2
2
 
3
+ ### 0.26.0 / 2019-06-13
4
+
5
+ BREAKING CHANGE: The default return value of Client#transaction has been
6
+ changed to the return value of the yielded block. Pass commit_response: true
7
+ for the previous default behavior of returning the CommitResponse.
8
+
9
+ * Add commit_response to Client#transaction
10
+ * Add Collection Group queries
11
+ * Add CollectionReference#list_documents
12
+ * Enable grpc.service_config_disable_resolution
13
+ * Use VERSION constant in GAPIC client
14
+
3
15
  ### 0.25.1 / 2019-04-29
4
16
 
5
17
  * Add AUTHENTICATION.md guide.
@@ -139,6 +139,46 @@ module Google
139
139
  end
140
140
  alias collection col
141
141
 
142
+ ##
143
+ # Creates and returns a new Query that includes all documents in the
144
+ # database that are contained in a collection or subcollection with the
145
+ # given collection_id.
146
+ #
147
+ # @param [String] collection_id Identifies the collections to query
148
+ # over. Every collection or subcollection with this ID as the last
149
+ # segment of its path will be included. Cannot contain a slash (`/`).
150
+ #
151
+ # @return [Query] The created Query.
152
+ #
153
+ # @example
154
+ # require "google/cloud/firestore"
155
+ #
156
+ # firestore = Google::Cloud::Firestore.new
157
+ #
158
+ # # Get the cities collection group query
159
+ # query = firestore.col_group "cities"
160
+ #
161
+ # query.get do |city|
162
+ # puts "#{city.document_id} has #{city[:population]} residents."
163
+ # end
164
+ #
165
+ def col_group collection_id
166
+ if collection_id.include? "/"
167
+ raise ArgumentError, "Invalid collection_id: '#{collection_id}', " \
168
+ "must not contain '/'."
169
+ end
170
+ query = Google::Firestore::V1::StructuredQuery.new(
171
+ from: [
172
+ Google::Firestore::V1::StructuredQuery::CollectionSelector.new(
173
+ collection_id: collection_id, all_descendants: true
174
+ )
175
+ ]
176
+ )
177
+
178
+ Query.start query, service.documents_path, self
179
+ end
180
+ alias collection_group col_group
181
+
142
182
  ##
143
183
  # Retrieves a document reference.
144
184
  #
@@ -555,12 +595,19 @@ module Google
555
595
  #
556
596
  # @param [Integer] max_retries The maximum number of retries for
557
597
  # transactions failed due to errors. Default is 5. Optional.
598
+ # @param [Boolean] commit_response When `true`, the return value from
599
+ # this method will be a `Google::Cloud::Firestore::CommitResponse`
600
+ # object with a `commit_time` attribute. Otherwise, the return
601
+ # value from this method will be the return value of the provided
602
+ # yield block. Default is `false`. Optional.
558
603
  #
559
604
  # @yield [transaction] The block for reading data and making changes.
560
605
  # @yieldparam [Transaction] transaction The transaction object for
561
606
  # making changes.
562
607
  #
563
- # @return [CommitResponse] The response from committing the changes.
608
+ # @return [Object, CommitResponse] The return value of the provided
609
+ # yield block, or if `commit_response` is provided and true, the
610
+ # `CommitResponse` object from the commit operation.
564
611
  #
565
612
  # @example
566
613
  # require "google/cloud/firestore"
@@ -578,14 +625,16 @@ module Google
578
625
  # tx.delete("cities/LA")
579
626
  # end
580
627
  #
581
- def transaction max_retries: nil
628
+ def transaction max_retries: nil, commit_response: nil
582
629
  max_retries = 5 unless max_retries.is_a? Integer
583
630
  backoff = { current: 0, delay: 1.0, max: max_retries, mod: 1.3 }
584
631
 
585
632
  transaction = Transaction.from_client self
586
633
  begin
587
- yield transaction
588
- transaction.commit
634
+ transaction_return = yield transaction
635
+ commit_return = transaction.commit
636
+ # Conditional return value, depending on truthy commit_response
637
+ commit_response ? commit_return : transaction_return
589
638
  rescue Google::Cloud::UnavailableError => err
590
639
  # Re-raise if retried more than the max
591
640
  raise err if backoff[:current] > backoff[:max]
@@ -618,6 +667,14 @@ module Google
618
667
 
619
668
  # @!endgroup
620
669
 
670
+ # @private
671
+ def list_documents parent, collection_id, token: nil, max: nil
672
+ ensure_service!
673
+ grpc = service.list_documents \
674
+ parent, collection_id, token: token, max: max
675
+ DocumentReference::List.from_grpc grpc, service, parent, collection_id
676
+ end
677
+
621
678
  protected
622
679
 
623
680
  ##
@@ -25,7 +25,7 @@ module Google
25
25
  ##
26
26
  # # CollectionReference
27
27
  #
28
- # A collection reference object ise used for adding documents, getting
28
+ # A collection reference object is used for adding documents, getting
29
29
  # document references, and querying for documents (See {Query}).
30
30
  #
31
31
  # @example
@@ -126,6 +126,39 @@ module Google
126
126
  end
127
127
  alias document doc
128
128
 
129
+ ##
130
+ # Retrieves a list of document references for the documents in this
131
+ # collection.
132
+ #
133
+ # The document references returned may include references to "missing
134
+ # documents", i.e. document locations that have no document present but
135
+ # which contain subcollections with documents. Attempting to read such a
136
+ # document reference (e.g. via {DocumentReference#get}) will return
137
+ # a {DocumentSnapshot} whose `exists?` method returns false.
138
+ #
139
+ # @param [String] token A previously-returned page token representing
140
+ # part of the larger set of results to view.
141
+ # @param [Integer] max Maximum number of results to return.
142
+ #
143
+ # @return [Array<DocumentReference>] An array of document references.
144
+ #
145
+ # @example
146
+ # require "google/cloud/firestore"
147
+ #
148
+ # firestore = Google::Cloud::Firestore.new
149
+ #
150
+ # col = firestore.col "cities"
151
+ #
152
+ # col.list_documents.each do |doc_ref|
153
+ # puts doc_ref.document_id
154
+ # end
155
+ #
156
+ def list_documents token: nil, max: nil
157
+ ensure_client!
158
+ client.list_documents \
159
+ parent_path, collection_id, token: token, max: max
160
+ end
161
+
129
162
  ##
130
163
  # The document reference or database the collection reference belongs
131
164
  # to. If the collection is a root collection, it will return the client
@@ -0,0 +1,191 @@
1
+ # Copyright 2019 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 "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Firestore
21
+ class DocumentReference
22
+ ##
23
+ # DocumentReference::List is a special case Array with additional
24
+ # values.
25
+ #
26
+ # @example
27
+ # require "google/cloud/firestore"
28
+ #
29
+ # firestore = Google::Cloud::Firestore.new
30
+ #
31
+ # col = firestore.col "cities"
32
+ #
33
+ # doc_refs = col.list_documents
34
+ #
35
+ # doc_refs.each do |doc_ref|
36
+ # puts doc_ref.document_id
37
+ # end
38
+ #
39
+ class List < DelegateClass(::Array)
40
+ ##
41
+ # If not empty, indicates that there are more records that match
42
+ # the request and this value should be passed to continue.
43
+ attr_accessor :token
44
+
45
+ ##
46
+ # @private Create a new DocumentReference::List with an array of
47
+ # DocumentReference instances.
48
+ def initialize arr = []
49
+ super arr
50
+ end
51
+
52
+ ##
53
+ # Whether there is a next page of document references.
54
+ #
55
+ # @return [Boolean]
56
+ #
57
+ # @example
58
+ # require "google/cloud/firestore"
59
+ #
60
+ # firestore = Google::Cloud::Firestore.new
61
+ # col = firestore.col "cities"
62
+ #
63
+ # doc_refs = col.list_documents
64
+ # if doc_refs.next?
65
+ # next_documents = doc_refs.next
66
+ # end
67
+ def next?
68
+ !token.nil?
69
+ end
70
+
71
+ ##
72
+ # Retrieve the next page of document references.
73
+ #
74
+ # @return [DocumentReference::List]
75
+ #
76
+ # @example
77
+ # require "google/cloud/firestore"
78
+ #
79
+ # firestore = Google::Cloud::Firestore.new
80
+ # col = firestore.col "cities"
81
+ #
82
+ # doc_refs = col.list_documents
83
+ # if doc_refs.next?
84
+ # next_documents = doc_refs.next
85
+ # end
86
+ def next
87
+ return nil unless next?
88
+ ensure_service!
89
+ options = { token: token, max: @max }
90
+ grpc = @service.list_documents @parent, @collection_id, options
91
+ self.class.from_grpc grpc, @service, @parent, @collection_id, @max
92
+ end
93
+
94
+ ##
95
+ # Retrieves remaining results by repeatedly invoking {#next} until
96
+ # {#next?} returns `false`. Calls the given block once for each
97
+ # result, which is passed as the argument to the block.
98
+ #
99
+ # An Enumerator is returned if no block is given.
100
+ #
101
+ # This method will make repeated API calls until all remaining results
102
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
103
+ # over the results returned by a single API call.) Use with caution.
104
+ #
105
+ # @param [Integer] request_limit The upper limit of API requests to
106
+ # make to load all document references. Default is no limit.
107
+ # @yield [document] The block for accessing each document.
108
+ # @yieldparam [DocumentReference] document The document reference
109
+ # object.
110
+ #
111
+ # @return [Enumerator]
112
+ #
113
+ # @example Iterating each document reference by passing a block:
114
+ # require "google/cloud/firestore"
115
+ #
116
+ # firestore = Google::Cloud::Firestore.new
117
+ # col = firestore.col "cities"
118
+ #
119
+ # doc_refs = col.list_documents
120
+ # doc_refs.all do |doc_ref|
121
+ # puts doc_ref.document_id
122
+ # end
123
+ #
124
+ # @example Using the enumerator by not passing a block:
125
+ # require "google/cloud/firestore"
126
+ #
127
+ # firestore = Google::Cloud::Firestore.new
128
+ # col = firestore.col "cities"
129
+ #
130
+ # doc_refs = col.list_documents
131
+ # all_document_ids = doc_refs.all.map do |doc_ref|
132
+ # doc_ref.document_id
133
+ # end
134
+ #
135
+ # @example Limit the number of API calls made:
136
+ # require "google/cloud/firestore"
137
+ #
138
+ # firestore = Google::Cloud::Firestore.new
139
+ # col = firestore.col "cities"
140
+ #
141
+ # doc_refs = col.list_documents
142
+ # doc_refs.all(request_limit: 10) do |doc_ref|
143
+ # puts doc_ref.document_id
144
+ # end
145
+ #
146
+ def all request_limit: nil
147
+ request_limit = request_limit.to_i if request_limit
148
+ unless block_given?
149
+ return enum_for :all, request_limit: request_limit
150
+ end
151
+ results = self
152
+ loop do
153
+ results.each { |r| yield r }
154
+ if request_limit
155
+ request_limit -= 1
156
+ break if request_limit < 0
157
+ end
158
+ break unless results.next?
159
+ results = results.next
160
+ end
161
+ end
162
+
163
+ ##
164
+ # @private New DocumentReference::List from a
165
+ # Google::Firestore::V1::ListDocumentsResponse object.
166
+ def self.from_grpc grpc, service, parent, collection_id, max = nil
167
+ documents = List.new(Array(grpc.documents).map do |document|
168
+ DocumentReference.from_path document.name, service
169
+ end)
170
+ documents.instance_variable_set :@parent, parent
171
+ documents.instance_variable_set :@collection_id, collection_id
172
+ token = grpc.next_page_token
173
+ token = nil if token == "".freeze
174
+ documents.instance_variable_set :@token, token
175
+ documents.instance_variable_set :@service, service
176
+ documents.instance_variable_set :@max, max
177
+ documents
178
+ end
179
+
180
+ protected
181
+
182
+ ##
183
+ # Raise an error unless an active service is available.
184
+ def ensure_service!
185
+ raise "Must have active connection" unless @service
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -17,6 +17,7 @@ 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"
20
+ require "google/cloud/firestore/document_reference/list"
20
21
 
21
22
  module Google
22
23
  module Cloud
@@ -63,7 +63,7 @@ module Google
63
63
  attr_accessor :parent_path
64
64
 
65
65
  ##
66
- # @private The Google::Firestore::V1::Query object.
66
+ # @private The Google::Firestore::V1::StructuredQuery object.
67
67
  attr_accessor :query
68
68
 
69
69
  ##
@@ -41,7 +41,11 @@ module Google
41
41
 
42
42
  def channel
43
43
  require "grpc"
44
- GRPC::Core::Channel.new host, nil, chan_creds
44
+ GRPC::Core::Channel.new host, chan_args, chan_creds
45
+ end
46
+
47
+ def chan_args
48
+ { "grpc.service_config_disable_resolution" => 1 }
45
49
  end
46
50
 
47
51
  def chan_creds
@@ -81,6 +85,26 @@ module Google
81
85
  end
82
86
  end
83
87
 
88
+ ##
89
+ # Returns a list of DocumentReferences that are directly nested under
90
+ # the given collection. Fetches all documents from the server, but
91
+ # provides an empty field mask to avoid unnecessary data transfer. Sets
92
+ # the showMissing flag to true to support full document traversal. If
93
+ # there are too many documents, recommendation will be not to call this
94
+ # method.
95
+ def list_documents parent, collection_id, token: nil, max: nil
96
+ mask = { field_paths: [] }
97
+ call_options = nil
98
+ call_options = Google::Gax::CallOptions.new page_token: token if token
99
+ execute do
100
+ paged_enum = firestore.list_documents \
101
+ parent, collection_id, mask: mask, show_missing: true,
102
+ page_size: max, options: call_options
103
+
104
+ paged_enum.page.response
105
+ end
106
+ end
107
+
84
108
  def list_collections parent, transaction: nil
85
109
  list_args = {}
86
110
  if transaction.is_a? String
@@ -604,6 +604,7 @@ module Google
604
604
 
605
605
  ##
606
606
  # @private commit the transaction
607
+ # @return [CommitResponse] The response from committing the changes.
607
608
  def commit
608
609
  ensure_not_closed!
609
610
 
@@ -27,6 +27,7 @@ require "google/gax"
27
27
 
28
28
  require "google/firestore/v1/firestore_pb"
29
29
  require "google/cloud/firestore/v1/credentials"
30
+ require "google/cloud/firestore/version"
30
31
 
31
32
  module Google
32
33
  module Cloud
@@ -223,7 +224,7 @@ module Google
223
224
  updater_proc = credentials.updater_proc
224
225
  end
225
226
 
226
- package_version = Gem.loaded_specs['google-cloud-firestore'].version.version
227
+ package_version = Google::Cloud::Firestore::VERSION
227
228
 
228
229
  google_api_client = "gl-ruby/#{RUBY_VERSION}"
229
230
  google_api_client << " #{lib_name}/#{lib_version}" if lib_name
@@ -27,6 +27,7 @@ require "google/gax"
27
27
 
28
28
  require "google/firestore/v1beta1/firestore_pb"
29
29
  require "google/cloud/firestore/v1beta1/credentials"
30
+ require "google/cloud/firestore/version"
30
31
 
31
32
  module Google
32
33
  module Cloud
@@ -223,7 +224,7 @@ module Google
223
224
  updater_proc = credentials.updater_proc
224
225
  end
225
226
 
226
- package_version = Gem.loaded_specs['google-cloud-firestore'].version.version
227
+ package_version = Google::Cloud::Firestore::VERSION
227
228
 
228
229
  google_api_client = "gl-ruby/#{RUBY_VERSION}"
229
230
  google_api_client << " #{lib_name}/#{lib_version}" if lib_name
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Firestore
19
- VERSION = "0.25.1".freeze
19
+ VERSION = "0.26.0".freeze
20
20
  end
21
21
  end
22
22
  end
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: 0.25.1
4
+ version: 0.26.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: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2019-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-core
@@ -234,6 +234,7 @@ files:
234
234
  - lib/google/cloud/firestore/document_change.rb
235
235
  - lib/google/cloud/firestore/document_listener.rb
236
236
  - lib/google/cloud/firestore/document_reference.rb
237
+ - lib/google/cloud/firestore/document_reference/list.rb
237
238
  - lib/google/cloud/firestore/document_snapshot.rb
238
239
  - lib/google/cloud/firestore/field_path.rb
239
240
  - lib/google/cloud/firestore/field_value.rb