Last active
December 8, 2022 01:16
-
-
Save oharaandrew314/fdb41ee61a4b2976ef150a0ec91c5551 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation("org.http4k:http4k-connect-amazon-dynamodb:3.25.4.0") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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