zio/zio-dynamodb

Make `ConditionCheck` type public

Open

#550 opened on Dec 8, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Scala (23 forks)auto 404
good first issue

Repository metrics

Stars
 (48 stars)
PR merge metrics
 (PR metrics pending)

Description

Reported by @calvinlfer - make ConditionCheck type (which is part of the transaction API) public so that users can use it to ascribe types.

def conditionalCheckAddTransaction(givenP1: DynamoPerson, givenP2: DynamoPerson)(
    write: DynamoPerson
  ): IO[DynamoDBError, Option[DynamoPerson]] =
    def checkPrimaryKeyAndName(p: DynamoPerson) = // can't type ConditionCheck here 
      val primaryKey = (DynamoPerson.pk.partitionKey === p.pk && DynamoPerson.sk.sortKey === p.sk).asAttrMap
      DynamoDBQuery.conditionCheck(table, primaryKey)(
        DynamoPerson.name === p.name
      )

    // given the presence of persons #1 and #2 in the table, we want to write a new person
    val cond1 = checkPrimaryKeyAndName(givenP1)
    val cond2 = checkPrimaryKeyAndName(givenP2)
    val add   = DynamoDBQuery.put(tableName = table, write)
    val txn   = cond1.zipRight(cond2).zipRight(add).transaction
    executor
      .execute(txn)
      .tapSomeError:
        case DynamoDBError.AWSError(cause)
            if cause.awsErrorDetails().errorCode() == "ConditionalCheckFailedException" =>
          ZIO.debug(cause.awsErrorDetails())

Contributor guide