I am using derjust/spring-data-dynamodb library to interact with DynamoDB. I have defined a class Product as follows:
@DynamoDBTable(tableName = "product")
public class Product {
@Id
@NotNull
@DynamoDBHashKey
private String productId;
@DynamoDBIndexHashKey(globalSecondaryIndexName = "product-category-gsi")
private String categoryId;
private double amount;
// more fields, getters and setters
}
I want to use the same code across multiple environments - dev, staging, prod
. So, the table names would be dev-product, staging-product and prod-product
.
The environment is available as an application property. I have configured the table name using the steps mentioned here: https://github.com/derjust/spring-data-dynamodb/wiki/Alter-table-name-during-runtime
@Configuration
@EnableDynamoDBRepositories(basePackages = "com.example.entity",dynamoDBMapperConfigRef = "dynamoDBMapperConfig")
public class DynamoDBConfiguration {
@Value("${aws.region}")
private String awsRegion;
@Value("${env.name}")
private String envName;
@Bean
public AmazonDynamoDB amazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard()
.withRegion(awsRegion)
.build();
}
@Bean
public DynamoDBMapperConfig.TableNameOverride tableNameOverrider() {
return DynamoDBMapperConfig.TableNameOverride.withTableNameReplacement(envName + "-product");
}
@Bean
public DynamoDBMapperConfig dynamoDBMapperConfig() {
DynamoDBMapperConfig.Builder builder = new DynamoDBMapperConfig.Builder();
builder.setTableNameOverride(tableNameOverrider());
return new DynamoDBMapperConfig(DynamoDBMapperConfig.DEFAULT, builder.build());
}
}
But how can I override the global secondary index name ? Currently, I have hardcoded it to "product-category-gsi
".
I want to be able to set it dynamically like I am setting the table name to dev-product-category-gsi, staging-product-category-gsi and prod-product-category-gsi
.