Slick: combine actions with a Seq of DBIOAction

☆樱花仙子☆ 提交于 2019-12-04 09:45:24

DBIO.sequence

Use DBIO.sequence to convert List[DBIO[_]] to DBIO[List[_]] and use for-comprehension.

DBIO.sequence will convert the sequence of DBIO's into DBIO[Seq[_]].

For example let say we have a function getUser

def getUser(userId: Long): DBIO[User]

def getAllUsers(userIds: List[Long]): DB[List[User]] = {
  DBIO.sequence(userIds.map(getUser))
}

DBIO.Sequence converts List[DBIO[_]] to DBIO[List[_]]

Now your code becomes

val actions = (for {
  _ <- slickUsers.insertOrUpdate(dbUser)
  loginInfoActionList <- DBIO.sequence(loginInfoActions)
  _ <- DBIO.sequence { loginInfoActionList.map { loginInfo =>
           slickUserLoginInfos += DBUserLoginInfo(dbUser.userID, loginInfo.id.get) }
      }
} yield ()).transactionally
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!