Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions generator/.DevConfigs/9490947f-209f-47e9-8c70-3698872df304.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"services": [
{
"serviceName": "DynamoDBv2",
"type": "minor",
"changeLogMessages": [
"Add support for DynamoDbUpdateBehavior for operations."
]
}
]
}
58 changes: 58 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,62 @@ public DynamoDBAutoGeneratedTimestampAttribute(string attributeName, [Dynamicall
{
}
}

/// <summary>
/// Specifies the update behavior for a property when performing DynamoDB update operations.
/// This attribute can be used to control whether a property is always updated, only updated if not null.
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

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

The documentation is incomplete and incorrect. It states 'only updated if not null' but the actual behavior is 'only set if the attribute does not exist in DynamoDB'. Update to: 'This attribute can be used to control whether a property is always updated or only set when the item is created (if the attribute does not exist).'

Suggested change
/// This attribute can be used to control whether a property is always updated, only updated if not null.
/// This attribute can be used to control whether a property is always updated or only set when the item is created (if the attribute does not exist).

Copilot uses AI. Check for mistakes.

/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDbUpdateBehaviorAttribute : DynamoDBPropertyAttribute
{
/// <summary>
/// Gets the update behavior for the property.
/// </summary>
public UpdateBehavior Behavior { get; }

/// <summary>
/// Default constructor. Sets behavior to Always.
/// </summary>
public DynamoDbUpdateBehaviorAttribute()
: base()
{
Behavior = UpdateBehavior.Always;
}

/// <summary>
/// Constructor that specifies the update behavior.
/// </summary>
/// <param name="behavior">The update behavior to apply.</param>
public DynamoDbUpdateBehaviorAttribute(UpdateBehavior behavior)
: base()
{
Behavior = behavior;
}

/// <summary>
/// Constructor that specifies an alternate attribute name and update behavior.
/// </summary>
/// <param name="attributeName">Name of attribute to be associated with property or field.</param>
/// <param name="behavior">The update behavior to apply.</param>
public DynamoDbUpdateBehaviorAttribute(string attributeName, UpdateBehavior behavior)
: base(attributeName)
{
Behavior = behavior;
}
}

/// <summary>
/// Specifies when a property value should be set.
/// </summary>
public enum UpdateBehavior
{
/// <summary>
/// Set the value on both create and update.
/// </summary>
Always,
/// <summary>
/// Set the value only when the item is created.
/// </summary>
IfNotExists
}
}
114 changes: 59 additions & 55 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -375,31 +375,35 @@ public IMultiTableTransactWrite CreateMultiTableTransactWrite(params ITransactWr

Document updateDocument;
Expression versionExpression = null;

var returnValues=counterConditionExpression == null ? ReturnValues.None : ReturnValues.AllNewAttributes;

if ((flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value) || !storage.Config.HasVersion)
var updateIfNotExistsAttributeName = GetUpdateIfNotExistsAttributeNames(storage);

var returnValues = counterConditionExpression == null && !updateIfNotExistsAttributeName.Any()
? ReturnValues.None
: ReturnValues.AllNewAttributes;
Comment on lines +381 to +383
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

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

The return value logic now includes updateIfNotExistsAttributeName.Any() which forces enumeration. Since this list is used multiple times (lines 381, 406, and later in UpdateHelperAsync), consider caching the result of .Any() in a boolean variable to avoid multiple enumerations.

Copilot uses AI. Check for mistakes.


var updateItemOperationConfig = new UpdateItemOperationConfig
{
updateDocument = table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), new UpdateItemOperationConfig()
{
ReturnValues = returnValues
}, counterConditionExpression);
}
else
ReturnValues = returnValues
};

if (!(flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value) && storage.Config.HasVersion)
{
var conversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
var conversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
versionExpression = CreateConditionExpressionForVersion(storage, conversionConfig);
SetNewVersion(storage);

var updateItemOperationConfig = new UpdateItemOperationConfig
{
ReturnValues = returnValues,
ConditionalExpression = versionExpression,
};
updateDocument = table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), updateItemOperationConfig, counterConditionExpression);
updateItemOperationConfig.ConditionalExpression = versionExpression;
}

if (counterConditionExpression == null && versionExpression == null) return;
updateDocument = table.UpdateHelper(
storage.Document,
table.MakeKey(storage.Document),
updateItemOperationConfig,
counterConditionExpression,
updateIfNotExistsAttributeName
);

if (counterConditionExpression == null && versionExpression == null && !updateIfNotExistsAttributeName.Any()) return;

if (returnValues == ReturnValues.AllNewAttributes)
{
Expand Down Expand Up @@ -428,36 +432,36 @@ private async Task SaveHelperAsync([DynamicallyAccessedMembers(InternalConstants
Document updateDocument;
Expression versionExpression = null;

var returnValues = counterConditionExpression == null ? ReturnValues.None : ReturnValues.AllNewAttributes;
var updateIfNotExistsAttributeName = GetUpdateIfNotExistsAttributeNames(storage);

var returnValues = counterConditionExpression == null && !updateIfNotExistsAttributeName.Any()
? ReturnValues.None
: ReturnValues.AllNewAttributes;

if (
(flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value)
|| !storage.Config.HasVersion)
var updateItemOperationConfig = new UpdateItemOperationConfig
{
updateDocument = await table.UpdateHelperAsync(storage.Document, table.MakeKey(storage.Document), new UpdateItemOperationConfig
{
ReturnValues = returnValues
}, counterConditionExpression, cancellationToken).ConfigureAwait(false);
}
else
ReturnValues = returnValues
};

if (!(flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value) && storage.Config.HasVersion)
{
var conversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
var conversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
versionExpression = CreateConditionExpressionForVersion(storage, conversionConfig);
SetNewVersion(storage);

updateDocument = await table.UpdateHelperAsync(
storage.Document,
table.MakeKey(storage.Document),
new UpdateItemOperationConfig
{
ReturnValues = returnValues,
ConditionalExpression = versionExpression
}, counterConditionExpression,
cancellationToken)
.ConfigureAwait(false);
updateItemOperationConfig.ConditionalExpression = versionExpression;
}

if (counterConditionExpression == null && versionExpression == null && !storage.Config.HasAutogeneratedProperties) return;
updateDocument = await table.UpdateHelperAsync(
storage.Document,
table.MakeKey(storage.Document),
updateItemOperationConfig,
counterConditionExpression,
cancellationToken,
updateIfNotExistsAttributeName
).ConfigureAwait(false);


if (counterConditionExpression == null && versionExpression == null && !updateIfNotExistsAttributeName.Any()) return;

if (returnValues == ReturnValues.AllNewAttributes)
{
Expand Down Expand Up @@ -698,4 +702,4 @@ private async Task SaveHelperAsync([DynamicallyAccessedMembers(InternalConstants

#endregion
}
}
}
44 changes: 29 additions & 15 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ internal static void SetNewVersion(ItemStorage storage)
}
storage.Document[versionAttributeName] = version;
}

private static void IncrementVersion(Type memberType, ref Primitive version)
{
if (memberType.IsAssignableFrom(typeof(Byte))) version = version.AsByte() + 1;
Expand Down Expand Up @@ -140,16 +141,15 @@ private static PropertyStorage[] GetCounterProperties(ItemStorage storage)
{
var counterProperties = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.IsCounter).ToArray();
var flatten= storage.Config.BaseTypeStorageConfig.Properties.
var flatten = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.FlattenProperties.Any()).ToArray();
while (flatten.Any())
{
var flattenCounters = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.IsCounter)).ToArray();
counterProperties = counterProperties.Concat(flattenCounters).ToArray();
flatten = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.FlattenProperties.Any())).ToArray();
}



return counterProperties;
}

Expand All @@ -175,12 +175,27 @@ private static DocumentModel.Expression CreateUpdateExpressionForCounterProperti
propertyStorage.CounterStartValue - propertyStorage.CounterDelta;
}
updateExpression.ExpressionStatement = $"SET {asserts.Substring(0, asserts.Length - 2)}";

return updateExpression;
}

#endregion

internal static List<string> GetUpdateIfNotExistsAttributeNames(ItemStorage storage)
{
var ifNotExistsProperties = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.UpdateBehaviorMode == UpdateBehavior.IfNotExists).ToArray();
var flatten = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.FlattenProperties.Any()).ToArray();
while (flatten.Any())
{
var flattenIfNotExists = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.UpdateBehaviorMode == UpdateBehavior.IfNotExists)).ToArray();
ifNotExistsProperties = ifNotExistsProperties.Concat(flattenIfNotExists).ToArray();
flatten = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.FlattenProperties.Any())).ToArray();
}
return ifNotExistsProperties.Select(p => p.AttributeName).ToList();
}


#region Table methods

// Retrieves the target table for the specified type
Expand Down Expand Up @@ -456,7 +471,7 @@ private void PopulateInstance(ItemStorage storage, object instance, DynamoDBFlat
{
foreach (PropertyStorage propertyStorage in storageConfig.AllPropertyStorage)
{
if(propertyStorage.IsFlattened) continue;
if (propertyStorage.IsFlattened) continue;
string attributeName = propertyStorage.AttributeName;
if (propertyStorage.ShouldFlattenChildProperties)
{
Expand Down Expand Up @@ -573,7 +588,6 @@ private void PopulateItemStorage(object toStore, ItemStorage storage, DynamoDBFl

if (ShouldSave(dbe, ignoreNullValues))
{

if (propertyStorage.ShouldFlattenChildProperties)
{
if (dbe == null) continue;
Expand Down Expand Up @@ -683,14 +697,14 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB

bool isAotRuntime = InternalSDKUtils.IsRunningNativeAot();
string errorMessage;

if (isAotRuntime)
{
errorMessage = $"Unable to convert DynamoDB entry [{entry}] of type {entry.GetType().FullName} to property {propertyStorage.PropertyName} of type {targetType.FullName}. " +
"Since the application is running in Native AOT mode the type could possibly be trimmed. " +
"This can happen if the type being created is a nested type of a type being used for saving and loading DynamoDB items. " +
$"This can be worked around by adding the \"[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof({targetType.FullName}))]\" attribute to the constructor of the parent type." +
" If the parent type can not be modified the attribute can also be used on the method invoking the DynamoDB sdk or some other method that you are sure is not being trimmed.";
"Since the application is running in Native AOT mode the type could possibly be trimmed. " +
"This can happen if the type being created is a nested type of a type being used for saving and loading DynamoDB items. " +
$"This can be worked around by adding the \"[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof({targetType.FullName}))]\" attribute to the constructor of the parent type." +
" If the parent type can not be modified the attribute can also be used on the method invoking the DynamoDB sdk or some other method that you are sure is not being trimmed.";
}
else
{
Expand Down Expand Up @@ -1514,7 +1528,7 @@ public ContextSearch(Search search, DynamoDBFlatConfig flatConfig)

DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, Config);
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);

ContextSearch query;
if (operationConfig is { Expression: { Filter: not null } })
{
Expand Down Expand Up @@ -1561,7 +1575,7 @@ internal ContextSearch
ContextSearch query;
if (operationConfig is { Expression: { Filter: not null } })
{
if(conditions!=null && conditions.Any())
if (conditions != null && conditions.Any())
{
throw new InvalidOperationException("Query conditions are not supported with filter expression. Use either Query conditions or filter expression, but not both.");
}
Expand Down Expand Up @@ -1848,7 +1862,7 @@ private ExpressionNode HandleAttributeTypeMethodCall(MethodCallExpression expr,
{
var memberObj = ContextExpressionsUtils.GetMember(expr.Arguments[0]);
var typeExpr = ContextExpressionsUtils.GetConstant(expr.Arguments[1]);
if (memberObj!=null && typeExpr!=null)
if (memberObj != null && typeExpr != null)
{
SetExpressionNodeAttributes(storageConfig, memberObj, typeExpr, node, flatConfig);
}
Expand Down Expand Up @@ -1990,7 +2004,7 @@ private ExpressionNode HandleStartsWithMethodCall(MethodCallExpression expr, Ite
};
if (expr.Object is MemberExpression memberObj && expr.Arguments[0] is ConstantExpression argConst)
{
var constantValue=ContextExpressionsUtils.GetConstant(argConst);
var constantValue = ContextExpressionsUtils.GetConstant(argConst);
SetExpressionNodeAttributes(storageConfig, memberObj, constantValue, node, flatConfig);
}
else
Expand Down
Loading