Skip to content

Dynamic specification of index name on @DynamoDbSecondaryPartitionKey annotation #6253

@scottcarter87

Description

@scottcarter87

Describe the feature

Provide the ability to dynamically specify the name of the index when using the @DynamoDbSecondaryPartitionKey which today only allows for static definition of the index name.

Use Case

I need to be able to dynamically provide the name of the index in a Spring Boot application since our index names have an environment name component to them. Currently the @DynamoDbSecondaryPartitionKey annotation requires a static name of the index so this is not possible.

Proposed Solution

Establish a new DynamoDbSecondaryIndexNameProvider interface and update the annotation to accept a static name or any implementing class of DynamoDbSecondaryIndexNameProvider so the name can be determined at runtime.

Domain Object

@DyanmoDbBean
public class DomainObject {
    
    @DynamoDbPartitionKey
    @DynamoDbAttribute("key")
    private String key;

    @DynamoDbAttribute("secondary_index_key")
    @DynamoDbSecondaryPartitionKey(indexNames = "secondary_index_gsi")
    private String secondaryIndexKey;
}

Interface for index name provider
Default implementation acts as a passthrough which provides current behavior of annotation

public interface IndexNameResolver {
    default String getIndexName(String indexStaticName) {
        return indexStaticName;
    }
}

User defined implementation

@Service
public class DynamicIndexNameResolverImpl implements IndexNameResolver {

    @Autowired
    private Environment environment;

    @Override
    public String getIndexName(final String indexStaticName) {
        /* 
         * user defined logic for deriving the actual index name from the static value provided in the annotation
         * essentially treating the value as a lookup key instead of the actual name.
         */
    }
}

User defined Spring bean configuration

@Bean
public DynamoDbEnhancedClient dynamoDbEnhancedClient(final DynamoDbClient dynamoDbClient, final IndexNameResolver indexNameResolver)  {
    return DynamoDbEnhancedClient.builder()
            .dynamoDbClient(dynamoDbClient)
            .indexNameResolver(indexNameResolver) // Enhanced client would be modified to accept the custom resolver implementation
            .build();
    }

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS Java SDK version used

2.31.77

JDK version used

21

Operating System and version

macOS Sequoia 15.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestA feature should be added or improved.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions