Scala Language-Integrated Connection Kit - Advanced database access library for Scala with strongly-typed APIs
npx @tessl/cli install tessl/maven-com-typesafe-slick--slick_2-12@3.6.0Slick is an advanced, comprehensive database access library for Scala with strongly-typed, highly composable APIs. It enables developers to work with relational databases almost as if they were using Scala collections, while maintaining full control over SQL generation and execution.
libraryDependencies += "com.typesafe.slick" %% "slick" % "3.6.1"The primary way to access Slick functionality is through database profile imports:
import slick.jdbc.H2Profile.api._For PostgreSQL:
import slick.jdbc.PostgresProfile.api._For MySQL:
import slick.jdbc.MySQLProfile.api._Alternative specific imports:
import slick.lifted.{TableQuery, Tag}
import slick.dbio.DBIO
import slick.jdbc.JdbcBackend.Databaseimport slick.jdbc.H2Profile.api._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
// Define a case class for your data
final case class Coffee(name: String, price: Double)
// Define how the database table maps to the case class
class Coffees(tag: Tag) extends Table[Coffee](tag, "COFFEES") {
def name = column[String]("NAME")
def price = column[Double]("PRICE")
def * = (name, price).mapTo[Coffee]
}
// Create a TableQuery to access the table
val coffees = TableQuery[Coffees]
// Connect to the database
val db = Database.forConfig("mydb")
// Create some queries
val expensiveCoffees = coffees.filter(_.price > 3.0)
val coffeeNames = coffees.map(_.name)
// Execute queries asynchronously
val future: Future[Seq[Coffee]] = db.run(expensiveCoffees.result)
val insertAction = coffees += Coffee("Latte", 2.50)
val insertFuture: Future[Int] = db.run(insertAction)
// Execute queries with streaming
val coffeeStream = db.stream(coffees.result)Slick is built around several key architectural components:
api objectsThe lifted embedding allows you to write database queries that look like Scala collection operations while generating efficient SQL. The DBIO action system provides composable, asynchronous operations with proper resource management.
Define database tables with strongly-typed columns, constraints, and schema operations. Includes table classes, column definitions, primary keys, foreign keys, and DDL operations.
abstract class Table[T](tag: Tag, tableName: String)
def column[T](name: String, options: ColumnOption[T]*): Rep[T]
object O {
val PrimaryKey: ColumnOption[Nothing]
val AutoInc: ColumnOption[Nothing]
val Unique: ColumnOption[Nothing]
}Type-safe query composition with monadic operations, joins, aggregations, and filtering. Provides the core lifted embedding DSL for building SQL queries using Scala syntax.
class Query[+E, U, C[_]]
def filter(f: E => Rep[Boolean]): Query[E, U, C]
def map[F, G, T](f: E => F)(implicit shape: Shape[_ <: FlatShapeLevel, F, G, T]): Query[G, T, C]
def sortBy[T](f: E => T)(implicit ord: Ordering[T]): Query[E, U, C]
def join[E2, U2, D[_]](q2: Query[E2, U2, D]): Query[(E, E2), (U, U2), C]Composable, asynchronous database actions with effect tracking and transaction support. Provides the foundation for executing queries and managing database operations.
trait DBIOAction[+R, +S <: NoStream, -E <: Effect]
def map[R2](f: R => R2): DBIOAction[R2, S, E]
def flatMap[R2, S2 <: NoStream, E2 <: Effect](f: R => DBIOAction[R2, S2, E2]): DBIOAction[R2, S2, E with E2]
def transactionally: DBIOAction[R, S, E with Transactional]Database-specific profiles providing connection management, SQL generation, and database-specific features. Includes all supported database backends and configuration options.
trait JdbcProfile extends RelationalProfile
object H2Profile extends JdbcProfile
object PostgresProfile extends JdbcProfile
object MySQLProfile extends JdbcProfile
def Database.forConfig(path: String): DatabaseDirect SQL query execution with parameter interpolation and result mapping. Allows mixing type-safe queries with raw SQL when needed.
implicit class SQLInterpolation(val sc: StringContext)
def sql"...": SQLActionBuilder
def sqlu"...": SQLActionBuilder
trait GetResult[T]
trait SetParameter[T]Column type mappings between Scala types and database types, including custom type mappings and implicit conversions.
trait ColumnType[T] extends TypedType[T]
trait BaseColumnType[T] extends ColumnType[T]
case class MappedColumnType[T, U](tmap: T => U, tcomap: U => T)(implicit tm: ColumnType[U]) extends ColumnType[T]
implicit def optionColumnType[T](implicit tm: ColumnType[T]): ColumnType[Option[T]]type DBIO[+R] = DBIOAction[R, NoStream, Effect.All]
type StreamingDBIO[+R, +T] = DBIOAction[R, Streaming[T], Effect.All]
trait Rep[T]
type Query[+E, U, C[_]] <: Rep[C[U]]
type TableQuery[E <: AbstractTable[_]] <: Query[E, E#TableElementType, Seq]
trait Tag
abstract class AbstractTable[T](tag: Tag, tableName: String)
class Table[T](tag: Tag, tableName: String) extends AbstractTable[T]
trait Database {
def run[R](a: DBIOAction[R, NoStream, Nothing]): Future[R]
def stream[T](a: StreamingDBIO[_, T]): DatabasePublisher[T]
}