# Exposed — Kotlin SQL Framework with Type-Safe DSL and DAO > Exposed is a Kotlin SQL library by JetBrains offering two flavors: a lightweight type-safe DSL for SQL queries and a full DAO layer with entity mapping, both supporting multiple database backends. ## Install Save in your project root: # Exposed — Kotlin SQL Framework with Type-Safe DSL and DAO ## Quick Use ```kotlin // build.gradle.kts dependencies { implementation("org.jetbrains.exposed:exposed-core:0.56.0") implementation("org.jetbrains.exposed:exposed-dao:0.56.0") implementation("org.jetbrains.exposed:exposed-jdbc:0.56.0") } ``` ```kotlin object Users : Table() { val id = integer("id").autoIncrement() val name = varchar("name", 50) override val primaryKey = PrimaryKey(id) } transaction { SchemaUtils.create(Users) Users.insert { it[name] = "Alice" } Users.selectAll().forEach { println(it[Users.name]) } } ``` ## Introduction Exposed is an open-source SQL framework developed by JetBrains for Kotlin. It provides two complementary access layers: a typesafe DSL that mirrors SQL syntax in Kotlin code, and a DAO layer for lightweight ORM-style entity management. Both approaches leverage Kotlin's type system to catch query errors at compile time. ## What Exposed Does - Provides a typesafe DSL where table columns, joins, and conditions are Kotlin expressions - Offers a DAO layer mapping database rows to Kotlin objects with lazy loading - Manages database transactions with automatic commit, rollback, and retry - Supports schema generation and migration through `SchemaUtils` - Works with PostgreSQL, MySQL, MariaDB, SQLite, H2, Oracle, and SQL Server ## Architecture Overview Exposed defines tables as Kotlin objects extending `Table`. Columns are properties with types that enforce compile-time safety. The DSL layer translates Kotlin expressions into SQL strings via a dialect abstraction, while the DAO layer wraps rows in `Entity` objects backed by an identity map within the transaction scope. Connection pooling integrates with HikariCP or any JDBC `DataSource`. ## Installation & Configuration - Add `exposed-core`, `exposed-dao`, and `exposed-jdbc` modules via Gradle or Maven - Configure a `Database.connect()` call with JDBC URL, driver, and credentials - Optionally add `exposed-java-time` or `exposed-kotlin-datetime` for date column support - Use `exposed-spring-boot-starter` for automatic Spring Boot integration - Set `TransactionManager.defaultDatabase` for implicit transaction binding ## Key Features - Compile-time SQL validation eliminates entire classes of runtime query errors - DSL queries compose naturally with Kotlin's `let`, `apply`, and extension functions - DAO entities support one-to-many and many-to-many relations via `referencedOn` and `via` - Batch insert and upsert operations for efficient bulk data loading - Coroutine-friendly with `newSuspendedTransaction` for non-blocking database access ## Comparison with Similar Tools - **Hibernate/JPA** — annotation-heavy ORM with XML config; Exposed uses Kotlin DSL for lighter, more idiomatic code - **jOOQ** — Java-first SQL builder with code generation; Exposed is Kotlin-native and needs no code generation step - **Ktorm** — similar Kotlin ORM; Exposed has broader database support and is maintained by JetBrains - **SQLDelight** — generates Kotlin from SQL files; Exposed lets you write queries in Kotlin directly - **Spring Data JPA** — repository-based abstraction; Exposed gives finer SQL control with less magic ## FAQ **Q: Can Exposed handle database migrations?** A: `SchemaUtils.createMissingTablesAndColumns()` handles simple schema evolution. For production migrations, pair it with Flyway or Liquibase. **Q: Does Exposed support connection pooling?** A: Yes. Pass a HikariCP `DataSource` to `Database.connect()` for production-grade connection management. **Q: Is Exposed suitable for large-scale applications?** A: Yes. JetBrains uses it in their own products. The DSL layer generates efficient SQL, and batch operations handle high-throughput scenarios. **Q: Can I use raw SQL with Exposed?** A: Yes. `exec()` and `TransactionManager.current().exec()` let you run arbitrary SQL when the DSL does not cover a specific need. ## Sources - https://github.com/JetBrains/Exposed - https://jetbrains.github.io/Exposed/ --- Source: https://tokrepo.com/en/workflows/asset-a8db759c Author: AI Open Source