Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDB: add support for transactional writes that include update operations with custom update and condition expressions #3294

Open
wants to merge 5 commits into
base: main-staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 66 additions & 2 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactWrite.cs
Expand Up @@ -143,6 +143,39 @@ public void AddSaveItem(T item)
objectItems.Add(objectItem);
}

/// <summary>
/// Add a single item to be saved in the current transaction operation.
/// Item is identified by its hash primary key and will be updated using the update expression provided.
/// </summary>
/// <param name="hashKey">Hash key of the item to delete.</param>
/// <param name="updateExpression">Update expression to use.</param>
/// <param name="conditionExpression">Condition to check before the operation.</param>
public void AddSaveKey(object hashKey, Expression updateExpression, Expression conditionExpression = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about naming here, Key feels implied since all items will have one. I wonder if something like AddSaveExpression or AddSaveViaExpression is more meaningful relative to the existing AddSaveItem.

(not asking for a change yet, curious what you or other reviewers think)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I think we have two alternate paths to take here instead of AddSaveKey. The first is to include the term "expression" in the name, like AddSave(Item)(Via|By|With)(Update)Expression. The second is to call this a new operation entirely, e.g. AddUpdateItem. I'm happy with any of these, whatever you decide.

{
AddSaveKey(hashKey, rangeKey: null, updateExpression, conditionExpression);
}

/// <summary>
/// Add a single item to be saved in the current transaction operation.
/// Item is identified by its hash-and-range primary key and will be updated using the update expression provided.
/// </summary>
/// <param name="hashKey">Hash key of the item to delete.</param>
/// <param name="rangeKey">Range key of the item to delete.</param>
/// <param name="updateExpression">Update expression to use.</param>
/// <param name="conditionExpression">Condition to check before the operation.</param>
public void AddSaveKey(object hashKey, object rangeKey, Expression updateExpression, Expression conditionExpression = null)
{
var operationConfig = conditionExpression != null
? new TransactWriteItemOperationConfig
{
ConditionalExpression = conditionExpression,
ReturnValuesOnConditionCheckFailure = DocumentModel.ReturnValuesOnConditionCheckFailure.None
}
: null;

DocumentTransaction.AddKeyToUpdateHelper(Context.MakeKey(hashKey, rangeKey, StorageConfig, Config), updateExpression, operationConfig);
}

#endregion


Expand Down Expand Up @@ -188,7 +221,18 @@ public void AddDeleteItem(T item)
/// <param name="hashKey">Hash key of the item to delete.</param>
public void AddDeleteKey(object hashKey)
{
AddDeleteKey(hashKey, rangeKey: null);
AddDeleteKey(hashKey, conditionExpression: null);
}

/// <summary>
/// Add a single item to be deleted in the current transaction operation.
/// Item is identified by its hash primary key.
/// </summary>
/// <param name="hashKey">Hash key of the item to delete.</param>
/// <param name="conditionExpression">Condition to check before the operation.</param>
public void AddDeleteKey(object hashKey, Expression conditionExpression)
{
AddDeleteKey(hashKey, rangeKey: null, conditionExpression);
}

/// <summary>
Expand All @@ -199,7 +243,27 @@ public void AddDeleteKey(object hashKey)
/// <param name="rangeKey">Range key of the item to delete.</param>
public void AddDeleteKey(object hashKey, object rangeKey)
{
DocumentTransaction.AddKeyToDeleteHelper(Context.MakeKey(hashKey, rangeKey, StorageConfig, Config));
AddDeleteKey(hashKey, rangeKey, conditionExpression: null);
}

/// <summary>
/// Add a single item to be deleted in the current transaction operation.
/// Item is identified by its hash-and-range primary key.
/// </summary>
/// <param name="hashKey">Hash key of the item to delete.</param>
/// <param name="rangeKey">Range key of the item to delete.</param>
/// <param name="conditionExpression">Condition to check before the operation.</param>
public void AddDeleteKey(object hashKey, object rangeKey, Expression conditionExpression)
{
var operationConfig = conditionExpression != null
? new TransactWriteItemOperationConfig
{
ConditionalExpression = conditionExpression,
ReturnValuesOnConditionCheckFailure = DocumentModel.ReturnValuesOnConditionCheckFailure.None
}
: null;

DocumentTransaction.AddKeyToDeleteHelper(Context.MakeKey(hashKey, rangeKey, StorageConfig, Config), operationConfig);
}

#endregion
Expand Down