简体   繁体   中英

Scala Tuple of seq to seq of object

I am having tuples of format as (DBIO[Seq[Person]], DBIO[Seq[Address]]) as one to one mapping. Person and Address is separate table in RDBMS. Profile definition is Profile(person: Person, address: Address) . Now I want to convert the former into DBIO[Seq[Profile]] . Following is code snippet for how I have got (DBIO[Seq[Person]], DBIO[Seq[Address]])

        for {
          person <- personQuery if person.personId === personId
          address <- addressQuery if address.addressId === profile.addressId
        } yield (person.result, address.result)

Need help with this transformation to DBIO[Seq[Profile] .

Assuming you can't use a join and you need to work with two actions (two DBIO s), what you can do is combine the two actions into a single one:

// Combine two actions into a single action
val pairs: DBIO[ ( Seq[Person], Seq[Address] ) ] = 
  (person.result).zip(address.result)

( zip is just one of many combinators you can use to manipulate DBIO ).

From there you can use DBIO.map to convert the pair into the datastructure you want.

For example:

// Use Slick's DBIO.map to map the DBIO value into a sequence of profiles:
val profiles: DBIO[Seq[Profile]] = pairs.map { case (ppl, places) => 
  // We now use a regular Scala `zip` on two sequences:
  ppl.zip(places).map { case (person, place) => Profile(person, place) }
}

I am unfamiliar with whatever DBIO is. Assuming it is a case class of some type T:

val (DBIO(people), DBIO(addresses)) = for {
  person <- personQuery if person.personId === personId
  address <- addressQuery if address.addressId === profile.addressId
} yield (person.result, address.result)

val profiles = DBIO(people.zip(addresses).map{ case (person, address) => Profile(person, address)})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM