statelydb 0.9.0 → 0.11.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: 0ab280ee4fd3fa97d0664bdb6b5337fa2492fe952b3490f8cc6af1d93bd30174
4
- data.tar.gz: db12681d9ad3e39dd642cddda23dde195f1e584da9119659ba995daec303909b
3
+ metadata.gz: 2a342b194da9de36c892bb4d9f7c15b5904b75a07c3a5d6b2a06c94c9df7615d
4
+ data.tar.gz: b5d1c84628d6fff0c186efc30912b52c81e637427456647e7856099bf0218996
5
5
  SHA512:
6
- metadata.gz: 5437b8f31d4300a9409b4bea9e4db3c2f046a3f8f1b3b9e55646c10fd1e24e37e1147ee2afe6a6562e8f4d1f6abc130b9525896c1c1b9816108adc27b5c62342
7
- data.tar.gz: dff2b5feb310a189392d270835e63110ab273ca54e71d12ae2fcd58222d412e364f80c8f7e849e4249241c4242ff8ada2595fa5ad1790580214a4efccbfba3f4
6
+ metadata.gz: 92cc17e071ff1fd85f69c6a7716e123e81d5238727c8d09d9bf1f450e6082db65c4b724268cd4478489b532ab366a95208f8d81efc820fd697461daf5dcb740c
7
+ data.tar.gz: 7b90b2148bfcf696c91ed701233731dc0c7e6c027967bf266aa2cecbc023acdb5bcc3269998f997c47112de10f43555182560656e4bd73fcd29bb92092b55ca2
@@ -5,7 +5,7 @@
5
5
  require 'google/protobuf'
6
6
 
7
7
 
8
- descriptor_data = "\n\x13\x64\x62/list_token.proto\x12\nstately.db\"h\n\tListToken\x12\x1d\n\ntoken_data\x18\x01 \x01(\x0cR\ttokenData\x12!\n\x0c\x63\x61n_continue\x18\x02 \x01(\x08R\x0b\x63\x61nContinue\x12\x19\n\x08\x63\x61n_sync\x18\x03 \x01(\x08R\x07\x63\x61nSyncBi\n\x0e\x63om.stately.dbB\x0eListTokenProtoP\x01\xa2\x02\x03SDX\xaa\x02\nStately.Db\xca\x02\nStately\\Db\xe2\x02\x16Stately\\Db\\GPBMetadata\xea\x02\x0bStately::Dbb\x06proto3"
8
+ descriptor_data = "\n\x13\x64\x62/list_token.proto\x12\nstately.db\"\x94\x01\n\tListToken\x12\x1d\n\ntoken_data\x18\x01 \x01(\x0cR\ttokenData\x12!\n\x0c\x63\x61n_continue\x18\x02 \x01(\x08R\x0b\x63\x61nContinue\x12\x19\n\x08\x63\x61n_sync\x18\x03 \x01(\x08R\x07\x63\x61nSync\x12*\n\x11schema_version_id\x18\x04 \x01(\rR\x0fschemaVersionIdBi\n\x0e\x63om.stately.dbB\x0eListTokenProtoP\x01\xa2\x02\x03SDX\xaa\x02\nStately.Db\xca\x02\nStately\\Db\xe2\x02\x16Stately\\Db\\GPBMetadata\xea\x02\x0bStately::Dbb\x06proto3"
9
9
 
10
10
  pool = Google::Protobuf::DescriptorPool.generated_pool
11
11
  pool.add_serialized_file(descriptor_data)
data/lib/api/db/put_pb.rb CHANGED
@@ -7,7 +7,7 @@ require 'google/protobuf'
7
7
  require 'db/item_pb'
8
8
 
9
9
 
10
- descriptor_data = "\n\x0c\x64\x62/put.proto\x12\nstately.db\x1a\rdb/item.proto\"|\n\nPutRequest\x12\x19\n\x08store_id\x18\x01 \x01(\x04R\x07storeId\x12\'\n\x04puts\x18\x02 \x03(\x0b\x32\x13.stately.db.PutItemR\x04puts\x12*\n\x11schema_version_id\x18\x03 \x01(\rR\x0fschemaVersionId\"/\n\x07PutItem\x12$\n\x04item\x18\x01 \x01(\x0b\x32\x10.stately.db.ItemR\x04item\"5\n\x0bPutResponse\x12&\n\x05items\x18\x01 \x03(\x0b\x32\x10.stately.db.ItemR\x05itemsBc\n\x0e\x63om.stately.dbB\x08PutProtoP\x01\xa2\x02\x03SDX\xaa\x02\nStately.Db\xca\x02\nStately\\Db\xe2\x02\x16Stately\\Db\\GPBMetadata\xea\x02\x0bStately::Dbb\x06proto3"
10
+ descriptor_data = "\n\x0c\x64\x62/put.proto\x12\nstately.db\x1a\rdb/item.proto\"|\n\nPutRequest\x12\x19\n\x08store_id\x18\x01 \x01(\x04R\x07storeId\x12\'\n\x04puts\x18\x02 \x03(\x0b\x32\x13.stately.db.PutItemR\x04puts\x12*\n\x11schema_version_id\x18\x03 \x01(\rR\x0fschemaVersionId\"U\n\x07PutItem\x12$\n\x04item\x18\x01 \x01(\x0b\x32\x10.stately.db.ItemR\x04item\x12$\n\x0emust_not_exist\x18\x03 \x01(\x08R\x0cmustNotExist\"5\n\x0bPutResponse\x12&\n\x05items\x18\x01 \x03(\x0b\x32\x10.stately.db.ItemR\x05itemsBc\n\x0e\x63om.stately.dbB\x08PutProtoP\x01\xa2\x02\x03SDX\xaa\x02\nStately.Db\xca\x02\nStately\\Db\xe2\x02\x16Stately\\Db\\GPBMetadata\xea\x02\x0bStately::Dbb\x06proto3"
11
11
 
12
12
  pool = Google::Protobuf::DescriptorPool.generated_pool
13
13
  pool.add_serialized_file(descriptor_data)
@@ -35,7 +35,7 @@ module Stately
35
35
  rpc :Get, ::Stately::Db::GetRequest, ::Stately::Db::GetResponse
36
36
  # Delete removes one or more Items from the Store by their key paths. This
37
37
  # will fail if the caller does not have permission to delete Items.
38
- # Tombstones will be saved for deleted items for time, so
38
+ # Tombstones will be saved for deleted items for some time, so
39
39
  # that SyncList can return information about deleted items. Deletes are
40
40
  # always applied atomically; all will fail or all will succeed.
41
41
  rpc :Delete, ::Stately::Db::DeleteRequest, ::Stately::Db::DeleteResponse
data/lib/error.rb CHANGED
@@ -11,10 +11,13 @@ module StatelyDB
11
11
  # The Error class contains common StatelyDB error types.
12
12
  class Error < StandardError
13
13
  # The gRPC/Connect Code for this error.
14
+ # @return [Integer]
14
15
  attr_reader :code
15
16
  # The more fine-grained Stately error code, which is a human-readable string.
17
+ # @return [String]
16
18
  attr_reader :stately_code
17
19
  # The upstream cause of the error, if available.
20
+ # @return [Exception]
18
21
  attr_reader :cause
19
22
 
20
23
  # @param [String] message
@@ -53,6 +56,8 @@ module StatelyDB
53
56
  new(error.message, code: GRPC::Core::StatusCodes::UNKNOWN, stately_code: "Unknown", cause: error)
54
57
  end
55
58
 
59
+ # Turn this error's gRPC status code into a human-readable string. e.g. 3 -> "InvalidArgument"
60
+ # @return [String]
56
61
  def code_string
57
62
  self.class.grpc_code_to_string(@code)
58
63
  end
data/lib/statelydb.rb CHANGED
@@ -38,6 +38,16 @@ module StatelyDB
38
38
  token_provider: Common::Auth::Auth0TokenProvider.new,
39
39
  endpoint: nil,
40
40
  region: nil)
41
+ if store_id.nil?
42
+ raise StatelyDB::Error.new("store_id is required",
43
+ code: GRPC::Core::StatusCodes::INVALID_ARGUMENT,
44
+ stately_code: "InvalidArgument")
45
+ end
46
+ if schema.nil?
47
+ raise StatelyDB::Error.new("schema is required",
48
+ code: GRPC::Core::StatusCodes::INVALID_ARGUMENT,
49
+ stately_code: "InvalidArgument")
50
+ end
41
51
 
42
52
  endpoint = self.class.make_endpoint(endpoint:, region:)
43
53
  channel = Common::Net.new_channel(endpoint:)
@@ -169,12 +179,19 @@ module StatelyDB
169
179
  # Put an Item into a StatelyDB Store at the given key_path.
170
180
  #
171
181
  # @param item [StatelyDB::Item] a StatelyDB Item
182
+ # @param must_not_exist [Boolean] A condition that indicates this item must
183
+ # not already exist at any of its key paths. If there is already an item
184
+ # at one of those paths, the Put operation will fail with a
185
+ # "ConditionalCheckFailed" error. Note that if the item has an
186
+ # `initialValue` field in its key, that initial value will automatically
187
+ # be chosen not to conflict with existing items, so this condition only
188
+ # applies to key paths that do not contain the `initialValue` field.
172
189
  # @return [StatelyDB::Item] the item that was stored
173
190
  #
174
- # @example
175
- # client.data.put(my_item)
176
- def put(item)
177
- resp = put_batch(item)
191
+ # @example client.data.put(my_item)
192
+ # @example client.data.put(my_item, must_not_exist: true)
193
+ def put(item, must_not_exist: false)
194
+ resp = put_batch({ item:, must_not_exist: })
178
195
 
179
196
  # Always return a single Item.
180
197
  resp.first
@@ -182,21 +199,32 @@ module StatelyDB
182
199
 
183
200
  # Put a batch of up to 50 Items into a StatelyDB Store.
184
201
  #
185
- # @param items [StatelyDB::Item, Array<StatelyDB::Item>] the items to store. Max 50 items.
202
+ # @param items [StatelyDB::Item, Array<StatelyDB::Item>] the items to store.
203
+ # Max 50 items.
186
204
  # @return [Array<StatelyDB::Item>] the items that were stored
187
205
  #
188
206
  # @example
189
207
  # client.data.put_batch(item1, item2)
208
+ # @example
209
+ # client.data.put_batch({ item: item1, must_not_exist: true }, item2)
190
210
  def put_batch(*items)
191
- items = Array(items).flatten
192
- req = Stately::Db::PutRequest.new(
193
- store_id: @store_id,
194
- schema_version_id: @schema::SCHEMA_VERSION_ID,
195
- puts: items.map do |item|
211
+ puts = Array(items).flatten.map do |input|
212
+ if input.is_a?(Hash)
213
+ item = input[:item]
196
214
  Stately::Db::PutItem.new(
197
- item: item.send("marshal_stately")
215
+ item: item.send("marshal_stately"),
216
+ must_not_exist: input[:must_not_exist]
217
+ )
218
+ else
219
+ Stately::Db::PutItem.new(
220
+ item: input.send("marshal_stately")
198
221
  )
199
222
  end
223
+ end
224
+ req = Stately::Db::PutRequest.new(
225
+ store_id: @store_id,
226
+ schema_version_id: @schema::SCHEMA_VERSION_ID,
227
+ puts:
200
228
  )
201
229
  resp = @stub.put(req)
202
230
 
@@ -208,8 +236,8 @@ module StatelyDB
208
236
  # Delete up to 50 Items from a StatelyDB Store at the given key_paths.
209
237
  #
210
238
  # @param key_paths [String, Array<String>] the paths to the items. Max 50 key paths.
211
- # @raise [StatelyDB::Error::InvalidParameters] if the parameters are invalid
212
- # @raise [StatelyDB::Error::NotFound] if the item is not found
239
+ # @raise [StatelyDB::Error] if the parameters are invalid
240
+ # @raise [StatelyDB::Error] if the item is not found
213
241
  # @return [void] nil
214
242
  #
215
243
  # @example
@@ -230,8 +258,8 @@ module StatelyDB
230
258
  # If the block completes successfully, the transaction is committed.
231
259
  #
232
260
  # @return [StatelyDB::Transaction::Transaction::Result] the result of the transaction
233
- # @raise [StatelyDB::Error::InvalidParameters] if the parameters are invalid
234
- # @raise [StatelyDB::Error::NotFound] if the item is not found
261
+ # @raise [StatelyDB::Error] if the parameters are invalid
262
+ # @raise [StatelyDB::Error] if the item is not found
235
263
  # @raise [Exception] if any other exception is raised
236
264
  #
237
265
  # @example
@@ -298,7 +326,8 @@ module StatelyDB
298
326
  raw_token = r.finished.token
299
327
  token = StatelyDB::Token.new(token_data: raw_token.token_data,
300
328
  can_continue: raw_token.can_continue,
301
- can_sync: raw_token.can_sync)
329
+ can_sync: raw_token.can_sync,
330
+ schema_version_id: raw_token.schema_version_id)
302
331
  end
303
332
  end
304
333
  [items, token]
@@ -334,7 +363,8 @@ module StatelyDB
334
363
  raw_token = r.finished.token
335
364
  token = StatelyDB::Token.new(token_data: raw_token.token_data,
336
365
  can_continue: raw_token.can_continue,
337
- can_sync: raw_token.can_sync)
366
+ can_sync: raw_token.can_sync,
367
+ schema_version_id: raw_token.schema_version_id)
338
368
  end
339
369
  end
340
370
  SyncResult.new(changed_items:, deleted_item_paths:, updated_outside_list_window_paths:, is_reset:, token:)
data/lib/token.rb CHANGED
@@ -13,10 +13,12 @@ module StatelyDB
13
13
  # @param [String] token_data
14
14
  # @param [Boolean] can_continue
15
15
  # @param [Boolean] can_sync
16
- def initialize(token_data:, can_continue:, can_sync:)
16
+ # @param [Integer] schema_version_id
17
+ def initialize(token_data:, can_continue:, can_sync:, schema_version_id:)
17
18
  @token_data = token_data
18
19
  @can_continue = can_continue
19
20
  @can_sync = can_sync
21
+ @schema_version_id = schema_version_id
20
22
  end
21
23
 
22
24
  # Returns true if the list operation can be continued, otherwise false.
@@ -30,5 +32,10 @@ module StatelyDB
30
32
  def can_sync?
31
33
  !!@can_sync
32
34
  end
35
+
36
+ # Returns the schema version ID associated with the token.
37
+ def schema_version_id
38
+ !!@schema_version_id
39
+ end
33
40
  end
34
41
  end
@@ -110,7 +110,8 @@ module StatelyDB
110
110
  raw_token = resp.finished.token
111
111
  token = StatelyDB::Token.new(token_data: raw_token.token_data,
112
112
  can_continue: raw_token.can_continue,
113
- can_sync: raw_token.can_sync)
113
+ can_sync: raw_token.can_sync,
114
+ schema_version_id: raw_token.schema_version_id)
114
115
  break
115
116
  end
116
117
  yield resp
@@ -225,6 +226,13 @@ module StatelyDB
225
226
  # the item will be returned while inside the transaction block.
226
227
  #
227
228
  # @param item [StatelyDB::Item] the item to store
229
+ # @param must_not_exist [Boolean] A condition that indicates this item must
230
+ # not already exist at any of its key paths. If there is already an item
231
+ # at one of those paths, the Put operation will fail with a
232
+ # "ConditionalCheckFailed" error. Note that if the item has an
233
+ # `initialValue` field in its key, that initial value will automatically
234
+ # be chosen not to conflict with existing items, so this condition only
235
+ # applies to key paths that do not contain the `initialValue` field.
228
236
  # @return [String, Integer] the id of the item
229
237
  #
230
238
  # @example
@@ -234,16 +242,18 @@ module StatelyDB
234
242
  # results.puts.each do |result|
235
243
  # puts result.key_path
236
244
  # end
237
- def put(item)
238
- resp = put_batch(item)
245
+ def put(item, must_not_exist: false)
246
+ resp = put_batch({ item:, must_not_exist: })
239
247
  resp.first
240
248
  end
241
249
 
242
- # Put a batch of up to 50 Items into a StatelyDB Store. Results are not returned until the transaction is
243
- # committed and will be available in the Result object returned by commit. A list of identifiers
244
- # for the items will be returned while inside the transaction block.
250
+ # Put a batch of up to 50 Items into a StatelyDB Store. Results are not
251
+ # returned until the transaction is committed and will be available in the
252
+ # Result object returned by commit. A list of identifiers for the items
253
+ # will be returned while inside the transaction block.
245
254
  #
246
- # @param items [StatelyDB::Item, Array<StatelyDB::Item>] the items to store. Max 50 items.
255
+ # @param items [StatelyDB::Item, Array<StatelyDB::Item>] the items to store. Max
256
+ # 50 items.
247
257
  # @return [Array<StatelyDB::UUID, String, Integer, nil>] the ids of the items
248
258
  #
249
259
  # @example
@@ -254,14 +264,22 @@ module StatelyDB
254
264
  # puts result.key_path
255
265
  # end
256
266
  def put_batch(*items)
257
- items = Array(items).flatten
267
+ puts = Array(items).flatten.map do |input|
268
+ if input.is_a?(Hash)
269
+ item = input[:item]
270
+ Stately::Db::PutItem.new(
271
+ item: item.send("marshal_stately"),
272
+ must_not_exist: input[:must_not_exist]
273
+ )
274
+ else
275
+ Stately::Db::PutItem.new(
276
+ item: input.send("marshal_stately")
277
+ )
278
+ end
279
+ end
258
280
  req = Stately::Db::TransactionRequest.new(
259
281
  put_items: Stately::Db::TransactionPut.new(
260
- puts: items.map do |item|
261
- Stately::Db::PutItem.new(
262
- item: item.send("marshal_stately")
263
- )
264
- end
282
+ puts:
265
283
  )
266
284
  )
267
285
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statelydb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stately Cloud, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-19 00:00:00.000000000 Z
11
+ date: 2024-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async