Skip to content

Instantly share code, notes, and snippets.

@oharaandrew314
Last active December 8, 2022 01:16
Show Gist options
  • Save oharaandrew314/fdb41ee61a4b2976ef150a0ec91c5551 to your computer and use it in GitHub Desktop.
Save oharaandrew314/fdb41ee61a4b2976ef150a0ec91c5551 to your computer and use it in GitHub Desktop.
dependencies {
implementation("org.http4k:http4k-connect-amazon-dynamodb:3.25.4.0")
}
import org.http4k.connect.amazon.dynamodb.mapper.DynamoDbTableMapper
import org.http4k.connect.amazon.dynamodb.mapper.DynamoDbTableMapperSchema
import org.http4k.connect.amazon.dynamodb.model.Attribute
import org.http4k.connect.amazon.dynamodb.model.IndexName
import java.util.UUID
class Http4kDynamoCatsRepo(private val table: DynamoDbTableMapper<Cat, UUID, Unit>): CatsRepo {
object Schema {
private val idAttr = Attribute.uuid().required("id")
private val ownerIdAttr = Attribute.uuid().required("ownerId")
private val ownerIndexName = IndexName.of("owners")
val primaryIndex = DynamoDbTableMapperSchema.Primary<UUID, Unit>(idAttr, null)
val ownerIndex = DynamoDbTableMapperSchema.GlobalSecondary(ownerIndexName, ownerIdAttr, idAttr)
}
private val ownerIndex = table.index(Schema.ownerIndex)
override fun get(id: UUID) = table[id]
override fun listForOwner(ownerId: UUID) = ownerIndex.query(ownerId).toList()
override fun plusAssign(cat: Cat) = table.plusAssign(cat)
override fun minusAssign(id: UUID) = table.delete(id)
}
import org.http4k.connect.amazon.dynamodb.DynamoDb
import org.http4k.connect.amazon.dynamodb.Http
fun main() {
val tableName = TableName.of(System.getenv("TABLE"))
val repository = DynamoDb.Http()
.tableMapper<Cat, UUID, Unit>(tableName, Http4kDynamoCatsRepo.Schema.primaryIndex)
.let { Http4kDynamoCatsRepo(it) }
// do stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment