我正在尝试使用 doobie、http4s 和 circe 设置一个带有数据库的简单 scala 应用程序。
如何将 java.util.UUID 转换为 doobie.syntax.SqlInterpolator.SingleFragment ?
final case class User(id: UUID, details: UserDetails)
Run Code Online (Sandbox Code Playgroud)
implicit val userDecoder: Decoder[User] = deriveDecoder[User]
implicit def userEntityDecoder[F[_]: Sync]: EntityDecoder[F, User] = jsonOf
implicit val userEncoder: Encoder[User] = deriveEncoder[User]
implicit def userEntityEncoder[F[_]: Applicative]: EntityEncoder[F, User] = jsonEncoderOf
implicit val put: Put[User] =
Put[Json].contramap(_.asJson)
implicit val get: Get[User] =
Get[Json].temap(_.as[User].left.map(_.show))
Run Code Online (Sandbox Code Playgroud)
[info] welcome to sbt 1.3.12 (N/A Java 14.0.1)
[info] loading global plugins from /Users/ryan/.sbt/1.0/plugins
[info] loading settings for project bobbymoore-build from …Run Code Online (Sandbox Code Playgroud) 假设我有一个Doobie程序列表(全部带有Unit类型参数,fwiw):
val progList: List[ConnectionIO[Unit]] = prog1 :: prog2 :: ... :: Nil
Run Code Online (Sandbox Code Playgroud)
我有什么办法可以在一个事务中运行它们?理解在这里行不通,因为我只知道运行时程序列表的确切组成。基本上,我想我应该将它们折叠在一起。
我想这个问题通常适用于Cats中的Free Monad,因此我也将Cats标记为。谢谢。
我正在阅读 Doobie 文档并尝试在事务中进行简单的获取或创建。我从第一个查询中获得了一个选项,并尝试getOrElse在 else 中执行一个并运行一个插入,但是我一直value map is not a member of Any在getOrElse调用中获得一个。获取现有行或创建新行instances并在事务中返回该结果的正确方法是什么?
import doobie._
import doobie.implicits._
import cats._
import cats.effect._
import cats.implicits._
import org.joda.time.DateTime
import scala.concurrent.ExecutionContext
case class Instance(id : Int, hostname : String)
case class User(id : Int, instanceId: Int, username : String, email : String, created : DateTime)
class Database(dbUrl : String, dbUser: String, dbPass: String) {
implicit val cs = IO.contextShift(ExecutionContext.global)
val xa = Transactor.fromDriverManager[IO](
"org.postgresql.Driver", dbUrl, dbUser, dbPass …Run Code Online (Sandbox Code Playgroud) 我有 3 个 vals,每个类型 Array[String]
它们的长度都相等
val1.length == val2.length // 真
接下来,我创建了一个案例类,如下所示:
case class resource(name: String, count: Int, location: String)
Run Code Online (Sandbox Code Playgroud)
我想建立一个列表,List[resource]使得该列表中的每个对象是从相应的元件创建的valS,即val1,val2,val3
像这样的东西:
val newList: List[resource] = (val1(0), val2(0).toInt, val3(0)),
(val1(1), val2(1).toInt, val3(1)),
...
(val1(val1.length), val2(val2.length).toInt, val3(val3.length)
Run Code Online (Sandbox Code Playgroud)
我不知道如何继续。我是使用 flatMap、foreach、for-loops 还是其他什么?
我们的想法是创建上述内容newList并将其与使用 doobie 从 SQL 数据库获得的结果进行比较。
val comparator = sql"sql statment".query[resource]
comparator.to[List].transact(xa).unsafeRunSync()
Run Code Online (Sandbox Code Playgroud)