Back to list

Exposed-style Table Definition

Lv.51031@mukitaro0 playsJan 2, 2026

Exposed SQL DSL pattern. JetBrains' type-safe SQL library for Kotlin.

preview.kotlin
Kotlin
1import org.jetbrains.exposed.sql.*
2import org.jetbrains.exposed.sql.transactions.transaction
3import org.jetbrains.exposed.dao.*
4import org.jetbrains.exposed.dao.id.*
5
6object Users : IntIdTable() {
7 val name = varchar("name", 50)
8 val email = varchar("email", 100).uniqueIndex()
9 val age = integer("age").nullable()
10 val createdAt = datetime("created_at")
11}
12
13object Posts : IntIdTable() {
14 val title = varchar("title", 200)
15 val content = text("content")
16 val author = reference("author_id", Users)
17 val published = bool("published").default(false)
18}
19
20fun queryExample() = transaction {
21 val users = Users.selectAll()
22 .where { Users.age greaterEq 18 }
23 .orderBy(Users.name)
24 .map { row ->
25 mapOf(
26 "id" to row[Users.id].value,
27 "name" to row[Users.name],
28 "email" to row[Users.email]
29 )
30 }
31
32 Users.insert {
33 it[name] = "Alice"
34 it[email] = "alice@example.com"
35 it[age] = 25
36 }
37}

Custom problems are not included in rankings