Skip to content

EVENFinancial/slick-cats

 
 

Repository files navigation

SlickCats (Fork)

Note: This is a fork of the original SlickCats project with updated dependencies and extended compatibility matrix.

Cats instances for Slick's DBIO including:

  • Monad
  • MonadError
  • CoflatMap
  • Group
  • Monoid
  • Semigroup
  • Comonad
  • Order
  • PartialOrder
  • Equals

Using

This fork provides separate artifacts for different Slick versions to ensure compatibility. Add the appropriate dependency to your build definition:

// For Slick 3.3.x
libraryDependencies += "tech.engine" %% "slickcats-slick3-3" % "0.11.0-SNAPSHOT" // Scala 2.12/2.13

// For Slick 3.4.x
libraryDependencies += "tech.engine" %% "slickcats-slick3-4" % "0.11.0-SNAPSHOT" // Scala 2.12/2.13

// For Slick 3.5.x
libraryDependencies += "tech.engine" %% "slickcats-slick3-5" % "0.11.0-SNAPSHOT" // Scala 2.12/2.13/3.3.x (LTS)

// For Slick 3.6.x
libraryDependencies += "tech.engine" %% "slickcats-slick3-6" % "0.11.0-SNAPSHOT" // Scala 2.12/2.13/3.3.x (LTS)

Because of possible binary incompatibilities, here are the dependency versions used in each release:

This fork supports multiple Slick and Scala version combinations:

Artifact Release Slick Version Scala Versions Cats Version
slickcats-slick3-3 0.11.0-SNAPSHOT 3.3.3 2.12.19, 2.13.16 2.13.0
slickcats-slick3-4 0.11.0-SNAPSHOT 3.4.1 2.12.19, 2.13.16 2.13.0
slickcats-slick3-5 0.11.0-SNAPSHOT 3.5.2 2.12.19, 2.13.16, 3.3.6 2.13.0
slickcats-slick3-6 0.11.0-SNAPSHOT 3.6.1 2.12.19, 2.13.16, 3.3.6 2.13.0

Artifacts are publicly available on Maven Central starting from version 0.6.

Accessing the Instances

Some or all of the following imports may be needed:

import cats._
import slick.dbio._
import com.rms.miu.slickcats.DBIOInstances._

Additionally, be sure to have an implicit ExecutionContext in scope. The implicit conversions require it and will fail with non-obvious errors if it's missing.

implicitly[Monad[DBIO]]
// error:
// No given instance of type cats.Monad[slick.dbio.DBIO] was found for parameter e of method implicitly in object Predef.
// I found:
// 
//     com.rms.miu.slickcats.DBIOInstances.dbioInstance(
//       /* missing */summon[scala.concurrent.ExecutionContext])
// 
// But no implicit values were found that match type scala.concurrent.ExecutionContext.
// def monad[F[_] : Monad, A](fa: F[A]): F[A] = fa
//
import scala.concurrent.ExecutionContext.Implicits.global

instances will be available for:

implicitly[Monad[DBIO]]
implicitly[MonadError[DBIO, Throwable]]
implicitly[CoflatMap[DBIO]]
implicitly[Functor[DBIO]]
implicitly[Applicative[DBIO]]

If a Monoid exists for A, here taken as Int, then the following is also available

implicitly[Group[DBIO[Int]]]
implicitly[Semigroup[DBIO[Int]]]
implicitly[Monoid[DBIO[Int]]]

Known Issues

Instances are supplied for DBIO[A] only. Despite being the same thing, type aliases will not match for implicit conversion. This means that the following

def monad[F[_] : Monad, A](fa: F[A]): F[A] = fa

val fail1: DBIOAction[String, NoStream, Effect.All] = DBIO.successful("hello")
// fail1: DBIOAction[String, NoStream, All] = SuccessAction(value = "hello")
val fail2 = DBIO.successful("hello")
// fail2: DBIOAction[String, NoStream, Effect] = SuccessAction(value = "hello")
val success: DBIO[String] = DBIO.successful("hello")
// success: DBIOAction[String, NoStream, All] = SuccessAction(value = "hello")

will not compile

monad(fail1)
monad(fail2)
// error:
// Found:    (repl.MdocSession.MdocApp.fail1 :
//   slick.dbio.DBIOAction[String, slick.dbio.NoStream, slick.dbio.Effect.All])
// Required: ([_] =>> Any)[Any]
// Note that implicit conversions were not tried because the result of an implicit conversion
// must be more specific than ([_] =>> Any)[Any]
// monad(fail1)
//       ^^^^^
// error:
// Found:    (repl.MdocSession.MdocApp.fail2 :
//   slick.dbio.DBIOAction[String, slick.dbio.NoStream, slick.dbio.Effect])
// Required: ([_] =>> Any)[Any]
// Note that implicit conversions were not tried because the result of an implicit conversion
// must be more specific than ([_] =>> Any)[Any]
// monad(fail2)
//       ^^^^^

but

monad(success)
// res10: DBIOAction[String, NoStream, All] = SuccessAction(value = "hello")

will compile fine.

Extras

This README is compiled using mdoc to ensure that only working examples are given.

About

Cats instances for Slick DBIO

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Scala 100.0%