0
# Slick
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: slick
7
- **Package Type**: maven
8
- **Language**: Scala
9
- **Installation**: `libraryDependencies += "com.typesafe.slick" %% "slick" % "3.6.1"`
10
11
## Core Imports
12
13
The primary way to access Slick functionality is through database profile imports:
14
15
```scala
16
import slick.jdbc.H2Profile.api._
17
```
18
19
For PostgreSQL:
20
```scala
21
import slick.jdbc.PostgresProfile.api._
22
```
23
24
For MySQL:
25
```scala
26
import slick.jdbc.MySQLProfile.api._
27
```
28
29
Alternative specific imports:
30
```scala
31
import slick.lifted.{TableQuery, Tag}
32
import slick.dbio.DBIO
33
import slick.jdbc.JdbcBackend.Database
34
```
35
36
## Basic Usage
37
38
```scala
39
import slick.jdbc.H2Profile.api._
40
import scala.concurrent.ExecutionContext.Implicits.global
41
import scala.concurrent.Future
42
43
// Define a case class for your data
44
final case class Coffee(name: String, price: Double)
45
46
// Define how the database table maps to the case class
47
class Coffees(tag: Tag) extends Table[Coffee](tag, "COFFEES") {
48
def name = column[String]("NAME")
49
def price = column[Double]("PRICE")
50
def * = (name, price).mapTo[Coffee]
51
}
52
53
// Create a TableQuery to access the table
54
val coffees = TableQuery[Coffees]
55
56
// Connect to the database
57
val db = Database.forConfig("mydb")
58
59
// Create some queries
60
val expensiveCoffees = coffees.filter(_.price > 3.0)
61
val coffeeNames = coffees.map(_.name)
62
63
// Execute queries asynchronously
64
val future: Future[Seq[Coffee]] = db.run(expensiveCoffees.result)
65
val insertAction = coffees += Coffee("Latte", 2.50)
66
val insertFuture: Future[Int] = db.run(insertAction)
67
68
// Execute queries with streaming
69
val coffeeStream = db.stream(coffees.result)
70
```
71
72
## Architecture
73
74
Slick is built around several key architectural components:
75
76
- **Profiles**: Database-specific implementations that provide the complete API through their `api` objects
77
- **Lifted Embedding**: Type-safe query DSL that "lifts" Scala expressions into database queries
78
- **DBIO Actions**: Composable, asynchronous database operations with effect tracking
79
- **Backend System**: Connection management, transaction handling, and query execution
80
- **Type System**: Compile-time type safety through Scala's type system and custom type classes
81
82
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.
83
84
## Capabilities
85
86
### Table Definitions
87
88
Define database tables with strongly-typed columns, constraints, and schema operations. Includes table classes, column definitions, primary keys, foreign keys, and DDL operations.
89
90
```scala { .api }
91
abstract class Table[T](tag: Tag, tableName: String)
92
def column[T](name: String, options: ColumnOption[T]*): Rep[T]
93
object O {
94
val PrimaryKey: ColumnOption[Nothing]
95
val AutoInc: ColumnOption[Nothing]
96
val Unique: ColumnOption[Nothing]
97
}
98
```
99
100
[Table Definitions](./table-definitions.md)
101
102
### Query Language
103
104
Type-safe query composition with monadic operations, joins, aggregations, and filtering. Provides the core lifted embedding DSL for building SQL queries using Scala syntax.
105
106
```scala { .api }
107
class Query[+E, U, C[_]]
108
def filter(f: E => Rep[Boolean]): Query[E, U, C]
109
def map[F, G, T](f: E => F)(implicit shape: Shape[_ <: FlatShapeLevel, F, G, T]): Query[G, T, C]
110
def sortBy[T](f: E => T)(implicit ord: Ordering[T]): Query[E, U, C]
111
def join[E2, U2, D[_]](q2: Query[E2, U2, D]): Query[(E, E2), (U, U2), C]
112
```
113
114
[Query Language](./queries.md)
115
116
### Database I/O Actions
117
118
Composable, asynchronous database actions with effect tracking and transaction support. Provides the foundation for executing queries and managing database operations.
119
120
```scala { .api }
121
trait DBIOAction[+R, +S <: NoStream, -E <: Effect]
122
def map[R2](f: R => R2): DBIOAction[R2, S, E]
123
def flatMap[R2, S2 <: NoStream, E2 <: Effect](f: R => DBIOAction[R2, S2, E2]): DBIOAction[R2, S2, E with E2]
124
def transactionally: DBIOAction[R, S, E with Transactional]
125
```
126
127
[Database I/O Actions](./database-io.md)
128
129
### Database Profiles
130
131
Database-specific profiles providing connection management, SQL generation, and database-specific features. Includes all supported database backends and configuration options.
132
133
```scala { .api }
134
trait JdbcProfile extends RelationalProfile
135
object H2Profile extends JdbcProfile
136
object PostgresProfile extends JdbcProfile
137
object MySQLProfile extends JdbcProfile
138
def Database.forConfig(path: String): Database
139
```
140
141
[Database Profiles](./database-profiles.md)
142
143
### Plain SQL
144
145
Direct SQL query execution with parameter interpolation and result mapping. Allows mixing type-safe queries with raw SQL when needed.
146
147
```scala { .api }
148
implicit class SQLInterpolation(val sc: StringContext)
149
def sql"...": SQLActionBuilder
150
def sqlu"...": SQLActionBuilder
151
trait GetResult[T]
152
trait SetParameter[T]
153
```
154
155
[Plain SQL](./plain-sql.md)
156
157
### Type Mappings
158
159
Column type mappings between Scala types and database types, including custom type mappings and implicit conversions.
160
161
```scala { .api }
162
trait ColumnType[T] extends TypedType[T]
163
trait BaseColumnType[T] extends ColumnType[T]
164
case class MappedColumnType[T, U](tmap: T => U, tcomap: U => T)(implicit tm: ColumnType[U]) extends ColumnType[T]
165
implicit def optionColumnType[T](implicit tm: ColumnType[T]): ColumnType[Option[T]]
166
```
167
168
[Type Mappings](./type-mappings.md)
169
170
## Types
171
172
```scala { .api }
173
type DBIO[+R] = DBIOAction[R, NoStream, Effect.All]
174
type StreamingDBIO[+R, +T] = DBIOAction[R, Streaming[T], Effect.All]
175
176
trait Rep[T]
177
type Query[+E, U, C[_]] <: Rep[C[U]]
178
type TableQuery[E <: AbstractTable[_]] <: Query[E, E#TableElementType, Seq]
179
180
trait Tag
181
abstract class AbstractTable[T](tag: Tag, tableName: String)
182
class Table[T](tag: Tag, tableName: String) extends AbstractTable[T]
183
184
trait Database {
185
def run[R](a: DBIOAction[R, NoStream, Nothing]): Future[R]
186
def stream[T](a: StreamingDBIO[_, T]): DatabasePublisher[T]
187
}
188
```