Kotlin

Exposed — Kotlin-First ORM

admin by @admin ADMIN
5h ago
Jun 1, 2026
Public
0 0 up · 0 down Sign in to vote
JetBrains' Exposed combines a SQL DSL with a lightweight ORM. Either style is fully type-safe; both compile to plain SQL.
Kotlin
Raw
// build.gradle.kts:
//   implementation("org.jetbrains.exposed:exposed-core:0.45.+")
//   implementation("org.jetbrains.exposed:exposed-jdbc:0.45.+")

import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

// Schema
object Users : Table("users") {
    val id        = integer("id").autoIncrement()
    val name      = varchar("name", 255)
    val email     = varchar("email", 255).uniqueIndex()
    val createdAt = long("created_at")
    override val primaryKey = PrimaryKey(id)
}

fun main() {
    Database.connect(
        "jdbc:postgresql://localhost/myapp",
        driver = "org.postgresql.Driver",
        user   = "user", password = "secret",
    )

    transaction {
        SchemaUtils.create(Users)

        // INSERT — get the generated id
        val newId = Users.insert {
            it[name]      = "Alice"
            it[email]     = "a@x.com"
            it[createdAt] = System.currentTimeMillis()
        } get Users.id

        // SELECT
        Users.select { Users.email like "%@x.com" }
            .forEach { row ->
                println("${row[Users.id]} ${row[Users.name]}")
            }

        // UPDATE
        Users.update({ Users.id eq newId }) {
            it[name] = "Alyce"
        }
    }
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.