or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

database-io.mddatabase-profiles.mdindex.mdplain-sql.mdqueries.mdtable-definitions.mdtype-mappings.md
tile.json

tessl/maven-com-typesafe-slick--slick_2-12

Scala Language-Integrated Connection Kit - Advanced database access library for Scala with strongly-typed APIs

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.typesafe.slick/slick_2.12@3.6.x

To install, run

npx @tessl/cli install tessl/maven-com-typesafe-slick--slick_2-12@3.6.0

index.mddocs/

Slick

Slick 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.

Package Information

  • Package Name: slick
  • Package Type: maven
  • Language: Scala
  • Installation: libraryDependencies += "com.typesafe.slick" %% "slick" % "3.6.1"

Core Imports

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.Database

Basic Usage

import 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)

Architecture

Slick is built around several key architectural components:

  • Profiles: Database-specific implementations that provide the complete API through their api objects
  • Lifted Embedding: Type-safe query DSL that "lifts" Scala expressions into database queries
  • DBIO Actions: Composable, asynchronous database operations with effect tracking
  • Backend System: Connection management, transaction handling, and query execution
  • Type System: Compile-time type safety through Scala's type system and custom type classes

The 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.

Capabilities

Table Definitions

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]
}

Table Definitions

Query Language

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]

Query Language

Database I/O Actions

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 I/O Actions

Database Profiles

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): Database

Database Profiles

Plain SQL

Direct 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]

Plain SQL

Type Mappings

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 Mappings

Types

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]
}