google-cloud-datastore 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/google-cloud-datastore.rb +141 -0
- data/lib/google/cloud/datastore.rb +532 -0
- data/lib/google/cloud/datastore/commit.rb +150 -0
- data/lib/google/cloud/datastore/credentials.rb +38 -0
- data/lib/google/cloud/datastore/cursor.rb +79 -0
- data/lib/google/cloud/datastore/dataset.rb +667 -0
- data/lib/google/cloud/datastore/dataset/lookup_results.rb +222 -0
- data/lib/google/cloud/datastore/dataset/query_results.rb +389 -0
- data/lib/google/cloud/datastore/entity.rb +454 -0
- data/lib/google/cloud/datastore/errors.rb +43 -0
- data/lib/google/cloud/datastore/gql_query.rb +216 -0
- data/lib/google/cloud/datastore/grpc_utils.rb +140 -0
- data/lib/google/cloud/datastore/key.rb +289 -0
- data/lib/google/cloud/datastore/properties.rb +133 -0
- data/lib/google/cloud/datastore/query.rb +351 -0
- data/lib/google/cloud/datastore/service.rb +171 -0
- data/lib/google/cloud/datastore/transaction.rb +365 -0
- data/lib/google/cloud/datastore/version.rb +22 -0
- data/lib/google/datastore/v1/datastore_pb.rb +120 -0
- data/lib/google/datastore/v1/datastore_services_pb.rb +61 -0
- data/lib/google/datastore/v1/entity_pb.rb +63 -0
- data/lib/google/datastore/v1/query_pb.rb +131 -0
- metadata +236 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 38ee5cd7d5c58d59744397ca7dfd52f2dcad5505
|
4
|
+
data.tar.gz: 5bc9a57ae0e231478b3b4c41e54c8b798b9930cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bc065e2f16a173c663a780900c732b007659ef26528d16ad938f816fddc580076f12255f8680ef1e5dadbc8adf445c6ffccde044264a9ff768254ac6a60ee909
|
7
|
+
data.tar.gz: 48530d73bb1727e971972f12713a0180318ec57f9ad63d30ae56be7a06bcc570ba7310311f8c7bf47df02b3117770e60705879c55cff56b1cd2716eb2d4a231c
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
# http://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
|
+
# This file is here to be autorequired by bundler, so that the .bigquery and
|
17
|
+
# #bigquery methods can be available, but the library and all dependencies won't
|
18
|
+
# be loaded until required and used.
|
19
|
+
|
20
|
+
|
21
|
+
gem "google-cloud-core"
|
22
|
+
require "google/cloud"
|
23
|
+
|
24
|
+
module Google
|
25
|
+
module Cloud
|
26
|
+
##
|
27
|
+
# Creates a new object for connecting to the Datastore service.
|
28
|
+
# Each call creates a new connection.
|
29
|
+
#
|
30
|
+
# For more information on connecting to Google Cloud see the [Authentication
|
31
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
32
|
+
#
|
33
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
34
|
+
# set of resources and operations that the connection can access. See
|
35
|
+
# [Using OAuth 2.0 to Access Google
|
36
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
37
|
+
#
|
38
|
+
# The default scope is:
|
39
|
+
#
|
40
|
+
# * `https://www.googleapis.com/auth/datastore`
|
41
|
+
# @param [Integer] retries Number of times to retry requests on server
|
42
|
+
# error. The default value is `3`. Optional.
|
43
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
44
|
+
#
|
45
|
+
# @return [Google::Cloud::Datastore::Dataset]
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
# require "google/cloud"
|
49
|
+
#
|
50
|
+
# gcloud = Google::Cloud.new
|
51
|
+
# datastore = gcloud.datastore
|
52
|
+
#
|
53
|
+
# task = datastore.entity "Task" do |t|
|
54
|
+
# t["type"] = "Personal"
|
55
|
+
# t["done"] = false
|
56
|
+
# t["priority"] = 4
|
57
|
+
# t["description"] = "Learn Cloud Datastore"
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# datastore.save task
|
61
|
+
#
|
62
|
+
# @example You shouldn't need to override the default scope, but you can:
|
63
|
+
# require "google/cloud"
|
64
|
+
#
|
65
|
+
# gcloud = Google::Cloud.new
|
66
|
+
# platform_scope = "https://www.googleapis.com/auth/cloud-platform"
|
67
|
+
# datastore = gcloud.datastore scope: platform_scope
|
68
|
+
#
|
69
|
+
def datastore scope: nil, retries: nil, timeout: nil
|
70
|
+
Google::Cloud.datastore @project, @keyfile,
|
71
|
+
scope: scope, retries: (retries || @retries),
|
72
|
+
timeout: (timeout || @timeout)
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Creates a new object for connecting to the Datastore service.
|
77
|
+
# Each call creates a new connection.
|
78
|
+
#
|
79
|
+
# For more information on connecting to Google Cloud see the [Authentication
|
80
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
81
|
+
#
|
82
|
+
# @param [String] project Dataset identifier for the Datastore you are
|
83
|
+
# connecting to.
|
84
|
+
# @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
|
85
|
+
# file path the file must be readable.
|
86
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
87
|
+
# set of resources and operations that the connection can access. See
|
88
|
+
# [Using OAuth 2.0 to Access Google
|
89
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
90
|
+
#
|
91
|
+
# The default scope is:
|
92
|
+
#
|
93
|
+
# * `https://www.googleapis.com/auth/datastore`
|
94
|
+
# @param [Integer] retries Number of times to retry requests on server
|
95
|
+
# error. The default value is `3`. Optional.
|
96
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
97
|
+
#
|
98
|
+
# @return [Google::Cloud::Datastore::Dataset]
|
99
|
+
#
|
100
|
+
# @example
|
101
|
+
# require "google/cloud/datastore"
|
102
|
+
#
|
103
|
+
# datastore = Google::Cloud.datastore "my-todo-project",
|
104
|
+
# "/path/to/keyfile.json"
|
105
|
+
#
|
106
|
+
# task = datastore.entity "Task", "sampleTask" do |t|
|
107
|
+
# t["type"] = "Personal"
|
108
|
+
# t["done"] = false
|
109
|
+
# t["priority"] = 4
|
110
|
+
# t["description"] = "Learn Cloud Datastore"
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# datastore.save task
|
114
|
+
#
|
115
|
+
def self.datastore project = nil, keyfile = nil, scope: nil, retries: nil,
|
116
|
+
timeout: nil
|
117
|
+
require "google/cloud/datastore"
|
118
|
+
project ||= Google::Cloud::Datastore::Dataset.default_project
|
119
|
+
project = project.to_s # Always cast to a string
|
120
|
+
fail ArgumentError, "project is missing" if project.empty?
|
121
|
+
|
122
|
+
if ENV["DATASTORE_EMULATOR_HOST"]
|
123
|
+
return Google::Cloud::Datastore::Dataset.new(
|
124
|
+
Google::Cloud::Datastore::Service.new(
|
125
|
+
project, :this_channel_is_insecure,
|
126
|
+
host: ENV["DATASTORE_EMULATOR_HOST"], retries: retries))
|
127
|
+
end
|
128
|
+
|
129
|
+
if keyfile.nil?
|
130
|
+
credentials = Google::Cloud::Datastore::Credentials.default scope: scope
|
131
|
+
else
|
132
|
+
credentials = Google::Cloud::Datastore::Credentials.new(
|
133
|
+
keyfile, scope: scope)
|
134
|
+
end
|
135
|
+
|
136
|
+
Google::Cloud::Datastore::Dataset.new(
|
137
|
+
Google::Cloud::Datastore::Service.new(
|
138
|
+
project, credentials, retries: retries, timeout: timeout))
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,532 @@
|
|
1
|
+
# Copyright 2014 Google Inc. All rights reserved.
|
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
|
+
# http://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-datastore"
|
17
|
+
require "google/cloud/datastore/errors"
|
18
|
+
require "google/cloud/datastore/dataset"
|
19
|
+
require "google/cloud/datastore/transaction"
|
20
|
+
require "google/cloud/datastore/credentials"
|
21
|
+
|
22
|
+
module Google
|
23
|
+
module Cloud
|
24
|
+
##
|
25
|
+
# # Google Cloud Datastore
|
26
|
+
#
|
27
|
+
# Google Cloud Datastore is a fully managed, schemaless database for storing
|
28
|
+
# non-relational data. You should feel at home if you are familiar with
|
29
|
+
# relational databases, but there are some key differences to be aware of to
|
30
|
+
# make the most of using Datastore.
|
31
|
+
#
|
32
|
+
# The goal of google-cloud is to provide a API that is comfortable to
|
33
|
+
# Rubyists. Authentication is handled by {Google::Cloud#datastore}. You can
|
34
|
+
# provide the project and credential information to connect to the Datastore
|
35
|
+
# service, or if you are running on Google Compute Engine this configuration
|
36
|
+
# is taken care of for you.
|
37
|
+
#
|
38
|
+
# ```ruby
|
39
|
+
# require "google/cloud"
|
40
|
+
#
|
41
|
+
# gcloud = Google::Cloud.new "my-todo-project",
|
42
|
+
# "/path/to/keyfile.json"
|
43
|
+
# datastore = gcloud.datastore
|
44
|
+
#
|
45
|
+
# task = datastore.find "Task", "sampleTask"
|
46
|
+
# task["priority"] = 5
|
47
|
+
# datastore.save task
|
48
|
+
# ```
|
49
|
+
#
|
50
|
+
# You can learn more about various options for connection on the
|
51
|
+
# [Authentication
|
52
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
53
|
+
#
|
54
|
+
# To learn more about Datastore, read the
|
55
|
+
# [Google Cloud Datastore Concepts Overview
|
56
|
+
# ](https://cloud.google.com/datastore/docs/concepts/overview).
|
57
|
+
#
|
58
|
+
# ## Retrieving records
|
59
|
+
#
|
60
|
+
# Records, called "entities" in Datastore, are retrieved by using a key.
|
61
|
+
# The key is more than a numeric identifier, it is a complex data structure
|
62
|
+
# that can be used to model relationships. The simplest key has a string
|
63
|
+
# <tt>kind</tt> value, and either a numeric <tt>id</tt> value, or a string
|
64
|
+
# <tt>name</tt> value. A single record can be retrieved by calling
|
65
|
+
# {Google::Cloud::Datastore::Dataset#find} and passing the parts of the key:
|
66
|
+
#
|
67
|
+
# ```ruby
|
68
|
+
# require "google/cloud"
|
69
|
+
#
|
70
|
+
# gcloud = Google::Cloud.new
|
71
|
+
# datastore = gcloud.datastore
|
72
|
+
#
|
73
|
+
# task = datastore.find "Task", "sampleTask"
|
74
|
+
# ```
|
75
|
+
#
|
76
|
+
# Optionally, {Google::Cloud::Datastore::Dataset#find} can be given a key
|
77
|
+
# object:
|
78
|
+
#
|
79
|
+
# ```ruby
|
80
|
+
# require "google/cloud"
|
81
|
+
#
|
82
|
+
# gcloud = Google::Cloud.new
|
83
|
+
# datastore = gcloud.datastore
|
84
|
+
#
|
85
|
+
# task_key = datastore.key "Task", 123456
|
86
|
+
# task = datastore.find task_key
|
87
|
+
# ```
|
88
|
+
#
|
89
|
+
# See {Google::Cloud::Datastore::Dataset#find}
|
90
|
+
#
|
91
|
+
# ## Querying records
|
92
|
+
#
|
93
|
+
# Multiple records can be found that match criteria.
|
94
|
+
# (See {Google::Cloud::Datastore::Query#where})
|
95
|
+
#
|
96
|
+
# ```ruby
|
97
|
+
# require "google/cloud"
|
98
|
+
#
|
99
|
+
# gcloud = Google::Cloud.new
|
100
|
+
# datastore = gcloud.datastore
|
101
|
+
#
|
102
|
+
# query = datastore.query("Task").
|
103
|
+
# where("done", "=", false)
|
104
|
+
#
|
105
|
+
# tasks = datastore.run query
|
106
|
+
# ```
|
107
|
+
#
|
108
|
+
# Records can also be ordered. (See {Google::Cloud::Datastore::Query#order})
|
109
|
+
#
|
110
|
+
# ```ruby
|
111
|
+
# require "google/cloud"
|
112
|
+
#
|
113
|
+
# gcloud = Google::Cloud.new
|
114
|
+
# datastore = gcloud.datastore
|
115
|
+
#
|
116
|
+
# query = datastore.query("Task").
|
117
|
+
# order("created")
|
118
|
+
#
|
119
|
+
# tasks = datastore.run query
|
120
|
+
# ```
|
121
|
+
#
|
122
|
+
# The number of records returned can be specified.
|
123
|
+
# (See {Google::Cloud::Datastore::Query#limit})
|
124
|
+
#
|
125
|
+
# ```ruby
|
126
|
+
# require "google/cloud"
|
127
|
+
#
|
128
|
+
# gcloud = Google::Cloud.new
|
129
|
+
# datastore = gcloud.datastore
|
130
|
+
#
|
131
|
+
# query = datastore.query("Task").
|
132
|
+
# limit(5)
|
133
|
+
#
|
134
|
+
# tasks = datastore.run query
|
135
|
+
# ```
|
136
|
+
#
|
137
|
+
# Records' key structures can also be queried.
|
138
|
+
# (See {Google::Cloud::Datastore::Query#ancestor})
|
139
|
+
#
|
140
|
+
# ```ruby
|
141
|
+
# require "google/cloud"
|
142
|
+
#
|
143
|
+
# gcloud = Google::Cloud.new
|
144
|
+
# datastore = gcloud.datastore
|
145
|
+
#
|
146
|
+
# task_list_key = datastore.key "TaskList", "default"
|
147
|
+
#
|
148
|
+
# query = datastore.query("Task").
|
149
|
+
# ancestor(task_list_key)
|
150
|
+
#
|
151
|
+
# tasks = datastore.run query
|
152
|
+
# ```
|
153
|
+
#
|
154
|
+
# See {Google::Cloud::Datastore::Query} and
|
155
|
+
# {Google::Cloud::Datastore::Dataset#run}
|
156
|
+
#
|
157
|
+
# ### Paginating records
|
158
|
+
#
|
159
|
+
# All records may not return at once, but multiple calls can be made to
|
160
|
+
# Datastore to return them all.
|
161
|
+
#
|
162
|
+
# ```ruby
|
163
|
+
# require "google/cloud"
|
164
|
+
#
|
165
|
+
# gcloud = Google::Cloud.new
|
166
|
+
# datastore = gcloud.datastore
|
167
|
+
#
|
168
|
+
# query = datastore.query("Task")
|
169
|
+
# tasks = datastore.run query
|
170
|
+
# tasks.all do |task|
|
171
|
+
# puts t["description"]
|
172
|
+
# end
|
173
|
+
# ```
|
174
|
+
#
|
175
|
+
# See {Google::Cloud::Datastore::Dataset::LookupResults} and
|
176
|
+
# {Google::Cloud::Datastore::Dataset::QueryResults}
|
177
|
+
#
|
178
|
+
# ## Creating records
|
179
|
+
#
|
180
|
+
# New entities can be created and persisted buy calling
|
181
|
+
# {Google::Cloud::Datastore::Dataset#save}. The entity must have a key to be
|
182
|
+
# saved. If the key is incomplete then it will be completed when saved.
|
183
|
+
#
|
184
|
+
# ```ruby
|
185
|
+
# require "google/cloud"
|
186
|
+
#
|
187
|
+
# gcloud = Google::Cloud.new
|
188
|
+
# datastore = gcloud.datastore
|
189
|
+
#
|
190
|
+
# task = datastore.entity "Task" do |t|
|
191
|
+
# t["type"] = "Personal"
|
192
|
+
# t["done"] = false
|
193
|
+
# t["priority"] = 4
|
194
|
+
# t["description"] = "Learn Cloud Datastore"
|
195
|
+
# end
|
196
|
+
# task.key.id #=> nil
|
197
|
+
# datastore.save task
|
198
|
+
# task.key.id #=> 123456
|
199
|
+
# ```
|
200
|
+
#
|
201
|
+
# Multiple new entities may be created in a batch.
|
202
|
+
#
|
203
|
+
# ```ruby
|
204
|
+
# require "google/cloud"
|
205
|
+
#
|
206
|
+
# gcloud = Google::Cloud.new
|
207
|
+
# datastore = gcloud.datastore
|
208
|
+
#
|
209
|
+
# task1 = datastore.entity "Task" do |t|
|
210
|
+
# t["type"] = "Personal"
|
211
|
+
# t["done"] = false
|
212
|
+
# t["priority"] = 4
|
213
|
+
# t["description"] = "Learn Cloud Datastore"
|
214
|
+
# end
|
215
|
+
#
|
216
|
+
# task2 = datastore.entity "Task" do |t|
|
217
|
+
# t["type"] = "Personal"
|
218
|
+
# t["done"] = false
|
219
|
+
# t["priority"] = 5
|
220
|
+
# t["description"] = "Integrate Cloud Datastore"
|
221
|
+
# end
|
222
|
+
#
|
223
|
+
# tasks = datastore.save(task1, task2)
|
224
|
+
# task_key1 = tasks[0].key
|
225
|
+
# task_key2 = tasks[1].key
|
226
|
+
# ```
|
227
|
+
#
|
228
|
+
# Entities in Datastore form a hierarchically structured space similar to
|
229
|
+
# the directory structure of a file system. When you create an entity, you
|
230
|
+
# can optionally designate another entity as its parent; the new entity is a
|
231
|
+
# child of the parent entity.
|
232
|
+
#
|
233
|
+
# ```ruby
|
234
|
+
# task_key = datastore.key "Task", "sampleTask"
|
235
|
+
# task_key.parent = datastore.key "TaskList", "default"
|
236
|
+
#
|
237
|
+
# task = datastore.entity task_key do |t|
|
238
|
+
# t["type"] = "Personal"
|
239
|
+
# t["done"] = false
|
240
|
+
# t["priority"] = 5
|
241
|
+
# t["description"] = "Integrate Cloud Datastore"
|
242
|
+
# end
|
243
|
+
# ```
|
244
|
+
#
|
245
|
+
# ## Setting properties
|
246
|
+
#
|
247
|
+
# Entities hold properties. A property has a name that is a string or
|
248
|
+
# symbol, and a value that is an object. Most value objects are supported,
|
249
|
+
# including String, Integer, Date, Time, and even other entity or key
|
250
|
+
# objects. Changes to the entity's properties are persisted by calling
|
251
|
+
# {Google::Cloud::Datastore::Dataset#save}.
|
252
|
+
#
|
253
|
+
# ```ruby
|
254
|
+
# require "google/cloud"
|
255
|
+
#
|
256
|
+
# gcloud = Google::Cloud.new
|
257
|
+
# datastore = gcloud.datastore
|
258
|
+
#
|
259
|
+
# task = datastore.find "Task", "sampleTask"
|
260
|
+
# # Read the priority property
|
261
|
+
# task["priority"] #=> 4
|
262
|
+
# # Write the priority property
|
263
|
+
# task["priority"] = 5
|
264
|
+
# # Persist the changes
|
265
|
+
# datastore.save task
|
266
|
+
# ```
|
267
|
+
#
|
268
|
+
# Array properties can be used to store more than one value.
|
269
|
+
#
|
270
|
+
# ```ruby
|
271
|
+
# require "google/cloud"
|
272
|
+
#
|
273
|
+
# gcloud = Google::Cloud.new
|
274
|
+
# datastore = gcloud.datastore
|
275
|
+
#
|
276
|
+
# task = datastore.entity "Task", "sampleTask" do |t|
|
277
|
+
# t["tags"] = ["fun", "programming"]
|
278
|
+
# t["collaborators"] = ["alice", "bob"]
|
279
|
+
# end
|
280
|
+
# ```
|
281
|
+
#
|
282
|
+
# ## Deleting records
|
283
|
+
#
|
284
|
+
# Entities can be removed from Datastore by calling
|
285
|
+
# {Google::Cloud::Datastore::Dataset#delete} and passing the entity object
|
286
|
+
# or the entity's key object.
|
287
|
+
#
|
288
|
+
# ```ruby
|
289
|
+
# require "google/cloud"
|
290
|
+
#
|
291
|
+
# gcloud = Google::Cloud.new
|
292
|
+
# datastore = gcloud.datastore
|
293
|
+
#
|
294
|
+
# task = datastore.find "Task", "sampleTask"
|
295
|
+
# datastore.delete task
|
296
|
+
# ```
|
297
|
+
#
|
298
|
+
# Multiple entities may be deleted in a batch.
|
299
|
+
#
|
300
|
+
# ```ruby
|
301
|
+
# require "google/cloud"
|
302
|
+
#
|
303
|
+
# gcloud = Google::Cloud.new
|
304
|
+
# datastore = gcloud.datastore
|
305
|
+
#
|
306
|
+
# task_key1 = datastore.key "Task", "sampleTask1"
|
307
|
+
# task_key2 = datastore.key "Task", "sampleTask2"
|
308
|
+
# datastore.delete task_key1, task_key2
|
309
|
+
# ```
|
310
|
+
#
|
311
|
+
# ## Transactions
|
312
|
+
#
|
313
|
+
# Complex logic can be wrapped in a Transaction. All queries and updates
|
314
|
+
# within the {Google::Cloud::Datastore::Dataset#transaction} block are run
|
315
|
+
# within the transaction scope, and will be automatically committed when the
|
316
|
+
# block completes.
|
317
|
+
#
|
318
|
+
# ```ruby
|
319
|
+
# require "google/cloud"
|
320
|
+
#
|
321
|
+
# gcloud = Google::Cloud.new
|
322
|
+
# datastore = gcloud.datastore
|
323
|
+
#
|
324
|
+
# task_key = datastore.key "Task", "sampleTask"
|
325
|
+
#
|
326
|
+
# datastore.transaction do |tx|
|
327
|
+
# if tx.find(task_key).nil?
|
328
|
+
# task = datastore.entity task_key do |t|
|
329
|
+
# t["type"] = "Personal"
|
330
|
+
# t["done"] = false
|
331
|
+
# t["priority"] = 4
|
332
|
+
# t["description"] = "Learn Cloud Datastore"
|
333
|
+
# end
|
334
|
+
# tx.save task
|
335
|
+
# end
|
336
|
+
# end
|
337
|
+
# ```
|
338
|
+
#
|
339
|
+
# Alternatively, if no block is given the transaction object is returned
|
340
|
+
# allowing you to commit or rollback manually.
|
341
|
+
#
|
342
|
+
# ```ruby
|
343
|
+
# require "google/cloud"
|
344
|
+
#
|
345
|
+
# gcloud = Google::Cloud.new
|
346
|
+
# datastore = gcloud.datastore
|
347
|
+
#
|
348
|
+
# task_key = datastore.key "Task", "sampleTask"
|
349
|
+
#
|
350
|
+
# tx = datastore.transaction
|
351
|
+
# begin
|
352
|
+
# if tx.find(task_key).nil?
|
353
|
+
# task = datastore.entity task_key do |t|
|
354
|
+
# t["type"] = "Personal"
|
355
|
+
# t["done"] = false
|
356
|
+
# t["priority"] = 4
|
357
|
+
# t["description"] = "Learn Cloud Datastore"
|
358
|
+
# end
|
359
|
+
# tx.save task
|
360
|
+
# end
|
361
|
+
# tx.commit
|
362
|
+
# rescue
|
363
|
+
# tx.rollback
|
364
|
+
# end
|
365
|
+
# ```
|
366
|
+
#
|
367
|
+
# See {Google::Cloud::Datastore::Transaction} and
|
368
|
+
# {Google::Cloud::Datastore::Dataset#transaction}
|
369
|
+
#
|
370
|
+
# ## Querying metadata
|
371
|
+
#
|
372
|
+
# Datastore provides programmatic access to some of its metadata to support
|
373
|
+
# meta-programming, implementing backend administrative functions, simplify
|
374
|
+
# consistent caching, and similar purposes. The metadata available includes
|
375
|
+
# information about the entity groups, namespaces, entity kinds, and
|
376
|
+
# properties your application uses, as well as the property representations
|
377
|
+
# for each property.
|
378
|
+
#
|
379
|
+
# The special entity kind `__namespace__` can be used to find all the
|
380
|
+
# namespaces used in your application entities.
|
381
|
+
#
|
382
|
+
# ```ruby
|
383
|
+
# query = datastore.query("__namespace__").
|
384
|
+
# select("__key__").
|
385
|
+
# where("__key__", ">=", datastore.key("__namespace__", "g")).
|
386
|
+
# where("__key__", "<", datastore.key("__namespace__", "h"))
|
387
|
+
#
|
388
|
+
# namespaces = datastore.run(query).map do |entity|
|
389
|
+
# entity.key.name
|
390
|
+
# end
|
391
|
+
# ```
|
392
|
+
#
|
393
|
+
# The special entity kind `__kind__` can be used to return all the
|
394
|
+
# kinds used in your application.
|
395
|
+
#
|
396
|
+
# ```ruby
|
397
|
+
# query = datastore.query("__kind__").
|
398
|
+
# select("__key__")
|
399
|
+
#
|
400
|
+
# kinds = datastore.run(query).map do |entity|
|
401
|
+
# entity.key.name
|
402
|
+
# end
|
403
|
+
# ```
|
404
|
+
#
|
405
|
+
# Property queries return entities of kind `__property__` denoting the
|
406
|
+
# indexed properties associated with an entity kind. (Unindexed properties
|
407
|
+
# are not included.)
|
408
|
+
#
|
409
|
+
# ```ruby
|
410
|
+
# query = datastore.query("__property__").
|
411
|
+
# select("__key__")
|
412
|
+
#
|
413
|
+
# entities = datastore.run(query)
|
414
|
+
# properties_by_kind = entities.each_with_object({}) do |entity, memo|
|
415
|
+
# kind = entity.key.parent.name
|
416
|
+
# prop = entity.key.name
|
417
|
+
# memo[kind] ||= []
|
418
|
+
# memo[kind] << prop
|
419
|
+
# end
|
420
|
+
# ```
|
421
|
+
#
|
422
|
+
# Property queries support ancestor filtering on a `__kind__` or
|
423
|
+
# `__property__` key, to limit the query results to a single kind or
|
424
|
+
# property. The `property_representation` property in the entity
|
425
|
+
# representing property `p` of kind `k` is an array containing all
|
426
|
+
# representations of `p`'s value in any entity of kind `k`.
|
427
|
+
#
|
428
|
+
# ```ruby
|
429
|
+
# ancestor_key = datastore.key "__kind__", "Task"
|
430
|
+
# query = datastore.query("__property__").
|
431
|
+
# ancestor(ancestor_key)
|
432
|
+
#
|
433
|
+
# entities = datastore.run(query)
|
434
|
+
# representations = entities.each_with_object({}) do |entity, memo|
|
435
|
+
# property_name = entity.key.name
|
436
|
+
# property_types = entity["property_representation"]
|
437
|
+
# memo[property_name] = property_types
|
438
|
+
# end
|
439
|
+
# ```
|
440
|
+
#
|
441
|
+
# Property queries can also be filtered with a range over the
|
442
|
+
# pseudo-property `__key__`, where the keys denote either `__kind__` or
|
443
|
+
# `__property__` entities.
|
444
|
+
#
|
445
|
+
# ```ruby
|
446
|
+
# start_key = datastore.key "__property__", "priority"
|
447
|
+
# start_key.parent = datastore.key "__kind__", "Task"
|
448
|
+
# query = datastore.query("__property__").
|
449
|
+
# select("__key__").
|
450
|
+
# where("__key__", ">=", start_key)
|
451
|
+
#
|
452
|
+
# entities = datastore.run(query)
|
453
|
+
# properties_by_kind = entities.each_with_object({}) do |entity, memo|
|
454
|
+
# kind = entity.key.parent.name
|
455
|
+
# prop = entity.key.name
|
456
|
+
# memo[kind] ||= []
|
457
|
+
# memo[kind] << prop
|
458
|
+
# end
|
459
|
+
# ```
|
460
|
+
#
|
461
|
+
# ## Configuring retries and timeout
|
462
|
+
#
|
463
|
+
# You can configure how many times API requests may be automatically
|
464
|
+
# retried. When an API request fails, the response will be inspected to see
|
465
|
+
# if the request meets criteria indicating that it may succeed on retry,
|
466
|
+
# such as `500` and `503` status codes or a specific internal error code
|
467
|
+
# such as `rateLimitExceeded`. If it meets the criteria, the request will be
|
468
|
+
# retried after a delay. If another error occurs, the delay will be
|
469
|
+
# increased before a subsequent attempt, until the `retries` limit is
|
470
|
+
# reached.
|
471
|
+
#
|
472
|
+
# You can also set the request `timeout` value in seconds.
|
473
|
+
#
|
474
|
+
# ```ruby
|
475
|
+
# require "google/cloud"
|
476
|
+
#
|
477
|
+
# gcloud = Google::Cloud.new
|
478
|
+
# datastore = gcloud.datastore retries: 10, timeout: 120
|
479
|
+
# ```
|
480
|
+
#
|
481
|
+
# See the [Datastore error
|
482
|
+
# codes](https://cloud.google.com/datastore/docs/concepts/errors#error_codes)
|
483
|
+
# for a list of error conditions.
|
484
|
+
#
|
485
|
+
# ## The Cloud Datastore Emulator
|
486
|
+
#
|
487
|
+
# As of this release, the Cloud Datastore emulator that is part of the
|
488
|
+
# gcloud SDK is no longer compatible with google-cloud. This is because the
|
489
|
+
# gcloud SDK's Cloud Datastore emulator does not yet support gRPC as a
|
490
|
+
# transport layer.
|
491
|
+
#
|
492
|
+
# A gRPC-compatible emulator is available until the gcloud SDK Cloud
|
493
|
+
# Datastore emulator supports gRPC. To use it you must [download the gRPC
|
494
|
+
# emulator](https://storage.googleapis.com/gcd/tools/cloud-datastore-emulator-1.1.1.zip)
|
495
|
+
# and use the `cloud_datastore_emulator` script.
|
496
|
+
#
|
497
|
+
# When you run the Cloud Datastore emulator you will see a message similar
|
498
|
+
# to the following printed:
|
499
|
+
#
|
500
|
+
# ```
|
501
|
+
# If you are using a library that supports the DATASTORE_EMULATOR_HOST
|
502
|
+
# environment variable, run:
|
503
|
+
#
|
504
|
+
# export DATASTORE_EMULATOR_HOST=localhost:8978
|
505
|
+
# ```
|
506
|
+
#
|
507
|
+
# Now you can connect to the emulator using the `DATASTORE_EMULATOR_HOST`
|
508
|
+
# environment variable:
|
509
|
+
#
|
510
|
+
# ```ruby
|
511
|
+
# require "google/cloud"
|
512
|
+
#
|
513
|
+
# # Make Datastore use the emulator
|
514
|
+
# ENV["DATASTORE_EMULATOR_HOST"] = "localhost:8978"
|
515
|
+
#
|
516
|
+
# gcloud = Google::Cloud.new "emulator-project-id"
|
517
|
+
# datastore = gcloud.datastore
|
518
|
+
#
|
519
|
+
# task = datastore.entity "Task", "emulatorTask" do |t|
|
520
|
+
# t["type"] = "Testing"
|
521
|
+
# t["done"] = false
|
522
|
+
# t["priority"] = 5
|
523
|
+
# t["description"] = "Use Datastore Emulator"
|
524
|
+
# end
|
525
|
+
#
|
526
|
+
# datastore.save task
|
527
|
+
# ```
|
528
|
+
#
|
529
|
+
module Datastore
|
530
|
+
end
|
531
|
+
end
|
532
|
+
end
|