@@ -136,8 +136,35 @@ async def replace_item(container, doc_id):
136
136
print ('Replaced Item\' s Id is {0}, new subtotal={1}' .format (response ['id' ], response ['subtotal' ]))
137
137
138
138
139
+ async def replace_item_using_etags (container , doc_id ):
140
+ print ('\n 1.7 Replace an Item using Etags and IfMatch\n ' )
141
+ # The use of etags and if-match/if-none-match options allows users to run conditional replace operations
142
+ # based on the etag value passed. When using if-match, the request will only succeed if the item's latest etag
143
+ # matches the passed in value. For more on optimistic concurrency control, see the link below:
144
+ # https://learn.microsoft.com/azure/cosmos-db/nosql/database-transactions-optimistic-concurrency
145
+
146
+ read_item = await container .read_item (item = doc_id , partition_key = doc_id )
147
+ item_etag = read_item ["_etag" ]
148
+ read_item ['subtotal' ] = read_item ['subtotal' ] + 1
149
+ response = await container .replace_item (
150
+ read_item ,
151
+ read_item ,
152
+ if_match = item_etag )
153
+
154
+ print ('Replaced Item\' s Id is {0}, new subtotal={1}' .format (response ['id' ], response ['subtotal' ]))
155
+
156
+ read_item = await container .read_item (item = doc_id , partition_key = doc_id )
157
+ read_item ['subtotal' ] = read_item ['subtotal' ] + 1
158
+ response = await container .replace_item (
159
+ read_item ,
160
+ read_item ,
161
+ if_none_match = "some-etag" )
162
+
163
+ print ('Replaced Item\' s Id is {0}, new subtotal={1}' .format (response ['id' ], response ['subtotal' ]))
164
+
165
+
139
166
async def upsert_item (container , doc_id ):
140
- print ('\n 1.7 Upserting an item\n ' )
167
+ print ('\n 1.8 Upserting an item\n ' )
141
168
142
169
read_item = await container .read_item (item = doc_id , partition_key = doc_id )
143
170
read_item ['subtotal' ] = read_item ['subtotal' ] + 1
@@ -147,7 +174,7 @@ async def upsert_item(container, doc_id):
147
174
148
175
149
176
async def conditional_patch_item (container , doc_id ):
150
- print ('\n 1.8 Patching Item by Id based on filter\n ' )
177
+ print ('\n 1.9 Patching Item by Id based on filter\n ' )
151
178
operations = [
152
179
{"op" : "add" , "path" : "/favorite_color" , "value" : "red" },
153
180
{"op" : "remove" , "path" : "/ttl" },
@@ -168,7 +195,7 @@ async def conditional_patch_item(container, doc_id):
168
195
169
196
170
197
async def patch_item (container , doc_id ):
171
- print ('\n 1.9 Patching Item by Id\n ' )
198
+ print ('\n 1.10 Patching Item by Id\n ' )
172
199
173
200
operations = [
174
201
{"op" : "add" , "path" : "/favorite_color" , "value" : "red" },
@@ -191,7 +218,7 @@ async def patch_item(container, doc_id):
191
218
192
219
193
220
async def execute_item_batch (database ):
194
- print ('\n 1.10 Executing Batch Item operations\n ' )
221
+ print ('\n 1.11 Executing Batch Item operations\n ' )
195
222
container = await database .create_container_if_not_exists (id = "batch_container" ,
196
223
partition_key = PartitionKey (path = '/account_number' ))
197
224
# We create three items to use for the sample.
@@ -246,15 +273,15 @@ async def execute_item_batch(database):
246
273
247
274
248
275
async def delete_item (container , doc_id ):
249
- print ('\n 1.11 Deleting Item by Id\n ' )
276
+ print ('\n 1.12 Deleting Item by Id\n ' )
250
277
251
278
await container .delete_item (item = doc_id , partition_key = doc_id )
252
279
253
280
print ('Deleted item\' s Id is {0}' .format (doc_id ))
254
281
255
282
256
283
async def delete_all_items_by_partition_key (db , partitionkey ):
257
- print ('\n 1.12 Deleting all Items by Partition Key\n ' )
284
+ print ('\n 1.13 Deleting all Items by Partition Key\n ' )
258
285
259
286
# A container with a partition key that is different from id is needed
260
287
container = await db .create_container_if_not_exists (id = "Partition Key Delete Container" ,
@@ -297,7 +324,7 @@ async def delete_all_items_by_partition_key(db, partitionkey):
297
324
298
325
async def create_mh_items (container ):
299
326
print ('Creating Items' )
300
- print ('\n 1.13 Create Item with Multi Hash Partition Key\n ' )
327
+ print ('\n 2.1 Create Item with Multi Hash Partition Key\n ' )
301
328
302
329
# Create a SalesOrder object. This object has nested properties and various types including numbers, DateTimes and strings.
303
330
# This can be saved as JSON as is without converting into rows/columns.
@@ -311,7 +338,7 @@ async def create_mh_items(container):
311
338
312
339
313
340
async def read_mh_item (container , doc_id , pk ):
314
- print ('\n 1.14 Reading Item by Multi Hash Partition Key\n ' )
341
+ print ('\n 2.2 Reading Item by Multi Hash Partition Key\n ' )
315
342
316
343
# Note that Reads require a partition key to be specified.
317
344
response = await container .read_item (item = doc_id , partition_key = pk )
@@ -322,7 +349,7 @@ async def read_mh_item(container, doc_id, pk):
322
349
323
350
324
351
async def query_mh_items (container , pk ):
325
- print ('\n 1.15 Querying for an Item by Multi Hash Partition Key\n ' )
352
+ print ('\n 2.3 Querying for an Item by Multi Hash Partition Key\n ' )
326
353
327
354
query_items_response = container .query_items (
328
355
query = "SELECT * FROM r WHERE r.account_number=@account_number and r.purchase_order_number=@purchase_order_number" ,
@@ -339,7 +366,7 @@ async def query_mh_items(container, pk):
339
366
340
367
341
368
async def replace_mh_item (container , doc_id , pk ):
342
- print ('\n 1.16 Replace an Item with Multi Hash Partition Key\n ' )
369
+ print ('\n 2.4 Replace an Item with Multi Hash Partition Key\n ' )
343
370
344
371
read_item = await container .read_item (item = doc_id , partition_key = pk )
345
372
read_item ['subtotal' ] = read_item ['subtotal' ] + 1
@@ -350,7 +377,7 @@ async def replace_mh_item(container, doc_id, pk):
350
377
351
378
352
379
async def upsert_mh_item (container , doc_id , pk ):
353
- print ('\n 1.17 Upserting an item with Multi Hash Partition Key\n ' )
380
+ print ('\n 2.5 Upserting an item with Multi Hash Partition Key\n ' )
354
381
355
382
read_item = await container .read_item (item = doc_id , partition_key = pk )
356
383
read_item ['subtotal' ] = read_item ['subtotal' ] + 1
@@ -361,7 +388,7 @@ async def upsert_mh_item(container, doc_id, pk):
361
388
362
389
363
390
async def patch_mh_item (container , doc_id , pk ):
364
- print ('\n 1.18 Patching Item by Multi Hash Partition Key\n ' )
391
+ print ('\n 2.6 Patching Item by Multi Hash Partition Key\n ' )
365
392
366
393
operations = [
367
394
{"op" : "add" , "path" : "/favorite_color" , "value" : "red" },
@@ -382,14 +409,14 @@ async def patch_mh_item(container, doc_id, pk):
382
409
383
410
384
411
async def delete_mh_item (container , doc_id , pk ):
385
- print ('\n 1.19 Deleting Item by Multi Hash Partition Key\n ' )
412
+ print ('\n 2.7 Deleting Item by Multi Hash Partition Key\n ' )
386
413
387
414
response = await container .delete_item (item = doc_id , partition_key = pk )
388
415
print ('Deleted item\' s Account Number is {0} Purchase Order Number is {1}' .format (pk [0 ], pk [1 ]))
389
416
390
417
391
418
async def delete_all_items_by_partition_key_mh (db , partitionkey ):
392
- print ('\n 1.20 Deleting all Items by Partition Key Multi Hash\n ' )
419
+ print ('\n 2.8 Deleting all Items by Partition Key Multi Hash\n ' )
393
420
394
421
# A container with a partition key that is different from id is needed
395
422
container = await db .create_container_if_not_exists (id = "Partition Key Delete Container Multi Hash" ,
@@ -432,7 +459,7 @@ async def delete_all_items_by_partition_key_mh(db, partitionkey):
432
459
433
460
434
461
async def query_items_with_continuation_token_size_limit (container , doc_id ):
435
- print ('\n 1.21 Query Items With Continuation Token Size Limit.\n ' )
462
+ print ('\n 2.9 Query Items With Continuation Token Size Limit.\n ' )
436
463
437
464
size_limit_in_kb = 8
438
465
sales_order = get_sales_order (doc_id )
@@ -516,6 +543,7 @@ async def run_sample():
516
543
await query_items (container , 'SalesOrder1' )
517
544
await query_items_with_continuation_token (container )
518
545
await replace_item (container , 'SalesOrder1' )
546
+ await replace_item_using_etags (container , 'SalesOrder1' )
519
547
await upsert_item (container , 'SalesOrder1' )
520
548
await conditional_patch_item (container , 'SalesOrder1' )
521
549
await patch_item (container , 'SalesOrder1' )
0 commit comments