Demonstrate UpdateItem with update expressions.
(client)
| 334 | # --------------------------------------------------------------------------- |
| 335 | |
| 336 | def update_data(client) -> None: |
| 337 | """Demonstrate UpdateItem with update expressions.""" |
| 338 | section("Step 5: Update Data (UpdateItem)") |
| 339 | |
| 340 | # Update a user's age and add a new attribute |
| 341 | client.update_item( |
| 342 | TableName=USERS_TABLE, |
| 343 | Key={"userId": {"S": "user-001"}}, |
| 344 | UpdateExpression="SET age = :newage, verified = :v", |
| 345 | ExpressionAttributeValues={ |
| 346 | ":newage": {"N": "31"}, |
| 347 | ":v": {"BOOL": True}, |
| 348 | }, |
| 349 | ) |
| 350 | print(" ✓ Updated user-001: age=31, verified=true") |
| 351 | |
| 352 | # Update an order status with a condition |
| 353 | client.update_item( |
| 354 | TableName=ORDERS_TABLE, |
| 355 | Key={ |
| 356 | "customerId": {"S": "user-001"}, |
| 357 | "orderId": {"S": "ord-103"}, |
| 358 | }, |
| 359 | UpdateExpression="SET #s = :newstatus", |
| 360 | ConditionExpression="#s = :oldstatus", |
| 361 | ExpressionAttributeNames={"#s": "status"}, |
| 362 | ExpressionAttributeValues={ |
| 363 | ":newstatus": {"S": "shipped"}, |
| 364 | ":oldstatus": {"S": "pending"}, |
| 365 | }, |
| 366 | ) |
| 367 | print(" ✓ Updated ord-103: pending → shipped (conditional)") |
| 368 | |
| 369 | # Verify the updates |
| 370 | resp = client.get_item( |
| 371 | TableName=USERS_TABLE, |
| 372 | Key={"userId": {"S": "user-001"}}, |
| 373 | ) |
| 374 | item = resp["Item"] |
| 375 | print(f" Verified user-001: age={item['age']['N']}, verified={item['verified']['BOOL']}") |
| 376 | # --------------------------------------------------------------------------- |
| 377 | # Step 6: Batch operations |
| 378 | # --------------------------------------------------------------------------- |
no test coverage detected