Skip to main content
Version: 2.x

Primary Keys

In the native AWS DynamoDB API primary keys are represented in two different ways depending on context:

AWSExampleContext
Primary Keys{"id": "1", "year": 2023}GetItem, PutItem, DeleteItem AWS API
Key Condition Expressions#id=:val1 and #year > :val2Query AWS API

Here we see that for CRUD operations the primary key is represented as a JSON object where each value is checked for equality, while for Query operations the primary key is represented as a key condition expression .

The High Level API unifies the two different ways to represent a primary key into a single type safe API.

Unified Type Safe High Level API for Primary Key Expressionsโ€‹

Assuming the below model

final case class Person(id: String, year: Int, address: String)
object Person {
implicit val schema: Schema.CaseClass3[String, Int, String, Person] = DeriveSchema.gen[Person]
val (id, year, address) = ProjectExpression.accessors[Person]
}
final case class Employee(id: String, group: String, address: String)
object Person {
implicit val schema: Schema.CaseClass3[String, Int, String, Person] = DeriveSchema.gen[Person]
val (id, year, address) = ProjectExpression.accessors[Person]
}

The High Level API unifies the two different ways into a single Type Safe API that is accessed by using the ProjectExpression returned by the ProjectExpression.accessors function as a springboard via the partitionKey and sortKey methods.

AWSExampleContext
Primary KeysPerson.id.partitionKey === "1" && Person.year.sortKey === "2020"GetItem, PutItem, DeleteItem
Key Condition Expressions[Query].whereKey(Person.id.partitionKey === "1" && Person.year.sortKey > 2020)[Query].whereKey

Valid operations on primary keys are:

contextoperationapplies to
Person.id.partitionKey
Person.year.sortKey
===GetItem, PutItem, DeleteItem, [Query].whereKey
Person.id.partitionKey === "1"&&GetItem, PutItem, DeleteItem, [Query].whereKey.

Provides conjunction from a partition key to a sort key
Person.year.sortKey>[Query].whereKey only


>=


<


<=


<>


.between(2021, 2023)
Employee.group.sortKey.beginsWith("Group1")Applies to String sort keys only

ZIO Chat

Hi there! ๐Ÿ‘‹ I'm ZIO Chat, your friendly neighborhood ZIO expert. I'm here to help you navigate the world of ZIO. Whether you're a seasoned pro or just starting out, fire away with your ZIO questions. I will hit you back with the best insights I've got. Let's dive into the world of ZIO together!