Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example improvements #503

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import zio.schema.annotation.discriminatorName
import zio.schema.Schema
import zio.schema.DeriveSchema

// An example of using a model that uses only sum and product types
// An example of using a model that uses only sum and product types to represent a deep hierarchy - this the recommended
// approach for modelling deeply nested hierarchies.
object TypeSafeApiAlternateModeling extends DynamoDBLocalSpec {

object dynamo {
Expand All @@ -31,7 +32,7 @@ object TypeSafeApiAlternateModeling extends DynamoDBLocalSpec {
val (fixed, otp) = ProjectionExpression.accessors[GroupBody]
}

@discriminatorName("invoiceType")
@discriminatorName("contractType")
sealed trait ContractBody
object ContractBody {
final case class Simple(sku: String) extends ContractBody // simple types just have fields
Expand Down Expand Up @@ -72,7 +73,7 @@ object TypeSafeApiAlternateModeling extends DynamoDBLocalSpec {

val modelExperimentSuite = suite("alternate model")(
test("crud operations") {
withIdAndAccountIdGsiTable { invoiceTable =>
withIdAndAccountIdGsiTable { contractTable =>
import dynamo._
val abc = Contract(
"1",
Expand All @@ -84,11 +85,12 @@ object TypeSafeApiAlternateModeling extends DynamoDBLocalSpec {
val accountIdKey = Contract.accountId.partitionKey === Some("a1")
for {
_ <- DynamoDBQuery
.put(invoiceTable, abc)
.put(contractTable, abc)
.where(!Contract.id.exists) // expressions operate on top level fields
.execute
contract <- DynamoDBQuery.get(invoiceTable)(Contract.id.partitionKey === "1").execute.absolve
stream <- DynamoDBQuery.queryAll[Contract](invoiceTable).indexName("accountId").whereKey(accountIdKey).execute
contract <- DynamoDBQuery.get(contractTable)(Contract.id.partitionKey === "1").execute.absolve
stream <-
DynamoDBQuery.queryAll[Contract](contractTable).indexName("accountId").whereKey(accountIdKey).execute
xs <- stream.runCollect
} yield assertTrue(contract.contractType == "group:abc" && xs.size == 1)

Expand Down
Loading