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

How can I have dynamodb table name dynamically based on my env? #297

Open
manoj-ahi opened this issue Aug 24, 2022 · 1 comment
Open

How can I have dynamodb table name dynamically based on my env? #297

manoj-ahi opened this issue Aug 24, 2022 · 1 comment

Comments

@manoj-ahi
Copy link

Expected Behavior

I have my dev and stage env in one AWS region and I am using the suffix _dev and _stage to tables.
Is there any way to add this suffix in runtime to @DynamoDBTable()?

Actual Behavior

I tried to override the value using TableNameResolver but the override method is not getting called. also, I tried with Spring expression to evaluate the expression but that is also not working.

Would appreciate any help. Thanks

@ShubamVirdi
Copy link

Hi You can achieve this by making a table name resolver


@Component
public class TableNameResolver extends DynamoDBMapperConfig.DefaultTableNameResolver {

    private String env;

    public TableNameResolver() {
    }

    public TableNameResolver(String envProfile) {
        this.env = envProfile;
    }

    @Override
    public String getTableName(Class<?> clazz, DynamoDBMapperConfig config) {
        String stageName = env;
        String rawTableName = super.getTableName(clazz, config);
        return String.join("_", rawTableName, stageName);
    }
}

in the class where you are creating dynamo db bean just pass env value (the env value can be saved at application yml for respective environments)

    @Value("${env}")
    private String env;

and create a dbmapper like this


    @Bean
    @Primary
    public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB) {

        return new DynamoDBMapper(amazonDynamoDB, new DynamoDBMapperConfig.Builder().withTableNameResolver(new TableNameResolver(env)).build());
    }

and now for table entities just provide the base name for the table and it will automatically append the env at the end of the table name.

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

No branches or pull requests

2 participants