Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 30, 2025

This PR implements the UpdateItem operation for DynamoDB with comprehensive support for update options and TransactWriteItems integration, addressing the requirements outlined in the issue.

Overview

The implementation adds a complete UpdateItem operation that allows updating specific fields on DynamoDB records with support for:

  • Automatic update expression generation from struct/map fields
  • Optimistic locking with version control
  • Conditional updates with custom expressions
  • Custom update expressions (SET, ADD, REMOVE operations)
  • Full TransactWriteItems integration

Key Features Added

UpdateItem Operation

func (t *Client) UpdateItem(ctx context.Context, pk, sk Attribute, fields interface{}, opts ...UpdateOption) error
  • Automatically generates DynamoDB update expressions from Go structs or maps
  • Filters out partition and sort keys from update operations
  • Supports both struct fields and map-based updates
  • Proper error handling for edge cases

Update Options

Optimistic Locking

dynago.WithOptimisticLockForUpdate("Version", currentVersion)
  • Implements conditional checks to prevent concurrent update conflicts
  • Automatically increments version numbers on successful updates
  • Compatible with existing update expressions

Conditional Updates

dynago.WithConditionalUpdate("attribute_exists(#name)", values, names)
  • Support for DynamoDB conditional expressions
  • Proper attribute name and value placeholder handling
  • Prevents updates when conditions are not met

Custom Update Expressions

dynago.WithUpdateExpression("ADD balance :increment", values, names)
  • Support for complex DynamoDB operations (ADD, REMOVE, DELETE)
  • Full control over update expression syntax
  • Attribute placeholder management

TransactWriteItems Integration

table.WithUpdateItem("pk_value", "sk_value", updateFields)
  • Seamless integration with existing transaction framework
  • Works alongside WithPutItem and WithDeleteItem
  • Maintains transaction atomicity guarantees

Example Usage

// Simple field update
err := table.UpdateItem(ctx, pk, sk, map[string]interface{}{
    "Name": "Updated Name",
    "Age": 30,
})

// Update with optimistic locking
err := table.UpdateItem(ctx, pk, sk, updateFields, 
    dynago.WithOptimisticLockForUpdate("Version", currentVersion))

// Custom ADD operation
err := table.UpdateItem(ctx, pk, sk, nil,
    dynago.WithUpdateExpression("ADD balance :amount", 
        map[string]dynago.Attribute{":amount": dynago.NumberValue(100)},
        map[string]string{"#balance": "balance"}))

// Transaction with mixed operations
err := table.TransactItems(ctx,
    table.WithUpdateItem("user1", "profile", updateFields),
    table.WithPutItem("user2", "profile", newUser),
    table.WithDeleteItem("user3", "profile"))

Implementation Details

  • Field Name Handling: Uses DynamoDB struct field names (not JSON tags) for proper marshaling
  • Expression Combining: Smart logic to combine SET expressions with optimistic locking
  • Primary Key Filtering: Automatically excludes partition and sort keys from update expressions
  • Error Handling: Comprehensive validation with meaningful error messages
  • Interface Compliance: Added UpdateItem to the WriteAPI interface

Testing

Added comprehensive test coverage including:

  • Table-driven tests following existing project patterns
  • Single and multiple field updates
  • Optimistic locking scenarios with version verification
  • Conditional update expressions
  • Custom ADD/SET operations
  • Transaction integration tests
  • Error case validation

All tests pass with real DynamoDB integration using the existing Docker test infrastructure.

Breaking Changes

None. This is a purely additive change that maintains full backward compatibility with existing code.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cognito-identity.us-east-1.amazonaws.com
    • Triggering command: java -jar DynamoDBLocal.jar -inMemory (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Implement UpdateItem operation and support for TransactWriteItems</issue_title>
<issue_description>Implement UpdateItem operation to update specified fields on a DynamoDB record.
This operation should accept a variadic options argument for customizing behavior (example:s WithConditionalUpdate, WithOptimisticLock)

Key requirements

  1. Allow updating keys with provided values (automatic update expression compute)
  2. Support write conditions https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html#DDB-UpdateItem-request-ConditionalOperator
  3. Support for optimistic lock via version attribute
  4. Support field update expressions (example: update field based on existing value add balance 100)
    https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
type UpdateOption func(*dynamodb.UpdateItemInput) error

func UpdateItem(ctx context.Context, pk, sk Atrribute, fields interface{}, opt ...UpdateOption) 

Changes to TransactWriteItems

TransactWriteItems should support UpdateItem input.
Implement WithUpdateItem option for TransactWriteItems.
https://github.com/oolio-group/dynago/blob/main/transaction_items.go

func (t *Client) WithUpdateItem(pk string, sk string, fields interface{}) types.TransactUpdateItem {
}
```</issue_description>

<agent_instructions>Tests should be written following table driven tests pattern. Analyze existing tests for transact operations for example</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>
Fixes #21

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits September 30, 2025 03:55
…pport

Co-authored-by: shidil <4880359+shidil@users.noreply.github.com>
…me handling

Co-authored-by: shidil <4880359+shidil@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement UpdateItem operation and support for TransactWriteItems Implement UpdateItem operation and TransactWriteItems support Sep 30, 2025
Copilot AI requested a review from shidil September 30, 2025 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement UpdateItem operation and support for TransactWriteItems

2 participants