Skip to content

Instantly share code, notes, and snippets.

@billydh
Created November 10, 2020 05:44
Show Gist options
  • Save billydh/389dd47ea795a645954c2f08b6bd6a99 to your computer and use it in GitHub Desktop.
Save billydh/389dd47ea795a645954c2f08b6bd6a99 to your computer and use it in GitHub Desktop.
CustomerRepo with DynamoDB Enhanced Client
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Repository
import reactor.core.publisher.Mono
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient
import software.amazon.awssdk.enhanced.dynamodb.Key
import software.amazon.awssdk.enhanced.dynamodb.TableSchema
@Repository
class CustomerRepo(private val client: DynamoDbEnhancedAsyncClient,
@Value("\${application.dynamo.customer-table-name}") private val customerTableName: String) {
private val logger: Logger = LoggerFactory.getLogger(javaClass)
private val table = client.table(customerTableName, tableSchema)
fun saveCustomer(customer: CustomerPersist): Mono<Unit> {
return Mono.fromFuture(table.putItem(customer))
.map { Unit }
.doOnError { logger.error("Exception while saving Customer information - $it") }
}
fun retrieveCustomer(customerId: String): Mono<CustomerPersist> {
val key = Key.builder().partitionValue(customerId).build()
return Mono.fromFuture(table.getItem(key))
.doOnError { logger.error("Exception while retrieving Customer information - $it") }
}
companion object {
private val tableSchema = TableSchema.fromBean(CustomerPersist::class.java)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment