-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsecondaryIndex.kt
71 lines (61 loc) · 2.57 KB
/
secondaryIndex.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package dev.andrewohara.dynamokt.samples
import dev.andrewohara.dynamokt.*
import io.kotest.matchers.collections.shouldContainExactly
import org.http4k.aws.AwsSdkClient
import org.http4k.connect.amazon.dynamodb.FakeDynamoDb
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient
import software.amazon.awssdk.enhanced.dynamodb.Key
import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional
import software.amazon.awssdk.enhanced.dynamodb.model.QueryEnhancedRequest
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.dynamodb.DynamoDbClient
import java.time.Instant
internal data class PersonById(
@DynamoKtPartitionKey
val id: Int,
@DynamoKtSecondaryPartitionKey(indexNames = ["names"])
val name: String,
@DynamoKtSecondarySortKey(indexNames = ["names"])
val dob: Instant
)
class SecondaryIndexSample {
private val table = DynamoDbEnhancedClient.builder()
.dynamoDbClient(
DynamoDbClient.builder()
.httpClient(AwsSdkClient(FakeDynamoDb()))
.credentialsProvider { AwsBasicCredentials.create("id", "secret") }
.region(Region.CA_CENTRAL_1)
.build()
)
.build()
.table("people", DataClassTableSchema(PersonById::class))
@BeforeEach
fun seed() {
table.createTable()
table.putItem(PersonById(1, "John", Instant.ofEpochSecond(9001)))
table.putItem(PersonById(2, "Jane", Instant.ofEpochSecond(1337)))
table.putItem(PersonById(3, "John", Instant.ofEpochSecond(4242)))
}
@Test
fun `search index by name`() {
val request = QueryEnhancedRequest.builder()
.scanIndexForward(true)
.queryConditional(QueryConditional.keyEqualTo(Key.builder().partitionValue("John").build()))
.build()
table.index("names").query(request).flatMap { it.items() }.shouldContainExactly(
PersonById(3, "John", Instant.ofEpochSecond(4242)),
PersonById(1, "John", Instant.ofEpochSecond(9001))
)
}
@Test
fun `search index by name and dob`() {
val condition = QueryConditional
.keyEqualTo(Key.builder().partitionValue("John").sortValue(Instant.ofEpochSecond(4242).toString()).build())
table.index("names").query(condition).flatMap { it.items() }.shouldContainExactly(
PersonById(3, "John", Instant.ofEpochSecond(4242))
)
}
}