Skip to content

Commit

Permalink
Update README.md (#474)
Browse files Browse the repository at this point in the history
Co-authored-by: ZIO Assistant <zio-assistant[bot]@users.noreply.github.com>
  • Loading branch information
zio-assistant[bot] and zio-assistant[bot] authored Apr 12, 2024
1 parent 6e2a3fe commit 37d358a
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,27 @@ object ZQueryExample extends ZIOAppDefault {
new DataSource.Batched[Any, GetUserName] {
val identifier: String = "UserDataSource"

def run(requests: Chunk[GetUserName])(implicit trace: Trace): ZIO[Any, Nothing, CompletedRequestMap] = {
val resultMap = CompletedRequestMap.empty
def run(requests: Chunk[GetUserName])(implicit trace: Trace): ZIO[Any, Nothing, CompletedRequestMap] =
requests.toList match {
case request :: Nil =>
val result: Task[String] = {
// get user by ID e.g. SELECT name FROM users WHERE id = $id
ZIO.succeed(???)
}

result.exit.map(resultMap.insert(request, _))
result.exit.map(CompletedRequestMap.single(request, _))

case batch: Seq[GetUserName] =>
val result: Task[List[(Int, String)]] = {
// get multiple users at once e.g. SELECT id, name FROM users WHERE id IN ($ids)
ZIO.succeed(???)
}

result.fold(
err =>
requests.foldLeft(resultMap) { case (map, req) =>
map.insert(req, Exit.fail(err))
},
_.foldLeft(resultMap) { case (map, (id, name)) =>
map.insert(GetUserName(id), Exit.succeed(name))
}
result.foldCause(
CompletedRequestMap.failCause(requests, _),
CompletedRequestMap.fromIterableWith(_)(kv => GetUserName(kv._1), kv => Exit.succeed(kv._2))
)
}
}

}

def getUserNameById(id: Int): ZQuery[Any, Throwable, String] =
Expand Down

0 comments on commit 37d358a

Please sign in to comment.