[英]Returning a Stream of Entities using a Transactor inside a Resource in Doobie
I'm trying to implement a query that returns the extracted information inside a fs2.Stream
.我正在尝试实现一个查询,该查询返回
fs2.Stream
中提取的信息。 I defined the Jobs
algebra:我定义了
Jobs
代数:
trait Jobs[F[_]] {
def all(): fs2.Stream[F, Job]
}
Then, I implemented an interpreter for the algebra:然后,我为代数实现了一个解释器:
final class LiveJobs[F[_]: MonadCancelThrow](postgres: Resource[F, Transactor[F]]) extends Jobs[F] {
override def all(): fs2.Stream[F, Job] = for {
jobs <- postgres.use { xa =>
sql"SELECT * FROM jobs".query[Job].stream.transact(xa)
}
} yield jobs
}
However, the compiler yells because the types are not aligned:但是,编译器会因为类型未对齐而大喊:
type mismatch;
[error] found : fs2.Stream[[_]F[_],Job]
[error] required: F[?]
[error] sql"SELECT * FROM jobs".query[Job].stream.transact(xa)
[error] ^
[error] one error found
The Resource.use
method needs a function that produces an F[*]
, not an fs2.Stream[F, Job]
. Resource.use
方法需要一个 function 产生一个F[*]
,而不是一个fs2.Stream[F, Job]
。 I cannot find anything that lets me convert between the two types or a different way to use the postgres
resource.我找不到任何可以让我在这两种类型之间进行转换或使用
postgres
资源的不同方式的东西。
The following is probably the design you want to follow:以下可能是您要遵循的设计:
trait Jobs[F[_]] {
def all: fs2.Stream[F, Job] =
}
object Jobs {
// I am not exactly sure which typeclass you require here, so i will use Async
def live[F[_]](implicit ev: Async[F]): Resource[F, Jobs[F]] = {
val transactor: Resource[F, Transactor[F]] = ... // Whatever you already have here.
transactor.map(xa => new LiveJobs(xa))
}
}
private[pckg] final class LiveJobs[F[_]](xa: Transactor[F])(implicit ev: MonadCancelThrow[F]) extends Jobs[F] {
override final val all: fs2.Stream[F, Job] =
sql"SELECT * FROM jobs".query[Job].stream.transact(xa)
}
Also, my personal advice, stick to concrete IO
while learning;另外,我个人的建议,在学习的同时坚持具体
IO
; and maybe even after.甚至之后。 The whole
F[_]
thing will just cause more trouble than worth originally.整个
F[_]
事情只会造成比原本价值更多的麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.