oyvindberg / typo

Typed Postgresql integration for Scala. Hopes to avoid typos
https://oyvindberg.github.io/typo/
MIT License
101 stars 11 forks source link

Add `selectByIdsTracked` (fix #45 ) #97

Closed oyvindberg closed 6 months ago

oyvindberg commented 6 months ago

This is built on top of #94 . Enables very convenient usage with things like ZQuery

  override def selectByIdsTracked(stateprovinceids: Array[StateprovinceId])(implicit c: Connection): Map[StateprovinceId, Option[StateprovinceRow]] = {
    val byId = selectByIds(stateprovinceids).view.map(x => (x.stateprovinceid, x)).toMap
    stateprovinceids.view.map(id => (id, byId.get(id))).toMap
  }

  override def selectByIdsTracked(stateprovinceids: Array[StateprovinceId]): ConnectionIO[Map[StateprovinceId, Option[StateprovinceRow]]] = {
    selectByIds(stateprovinceids).compile.toList.map { rows =>
      val byId = rows.view.map(x => (x.stateprovinceid, x)).toMap
      stateprovinceids.view.map(id => (id, byId.get(id))).toMap
    }
  }

  override def selectByIdsTracked(stateprovinceids: Array[StateprovinceId]): ZIO[ZConnection, Throwable, Map[StateprovinceId, Option[StateprovinceRow]]] = {
    selectByIds(stateprovinceids).runCollect.map { rows =>
      val byId = rows.view.map(x => (x.stateprovinceid, x)).toMap
      stateprovinceids.view.map(id => (id, byId.get(id))).toMap
    }
  }