Learn production-ready Kotlin Exposed ORM patterns for queries, transactions, migrations, and repositories.
Copy the install command and let the AI configure it · recommended for beginners
Please install the "kotlin-exposed-patterns" skill from askskill: 1. Download https://raw.githubusercontent.com/affaan-m/ECC/main/skills/kotlin-exposed-patterns/SKILL.md 2. Save it as ~/.claude/skills/kotlin-exposed-patterns/SKILL.md 3. Reload skills and tell me it's ready
Give me a Kotlin Exposed DSL example that fetches a single user by UUID and returns a nullable result inside a coroutine-safe transaction.
A Kotlin example using Exposed DSL, a where clause, and a suspended transaction.
Generate a Kotlin DatabaseFactory example that configures a PostgreSQL connection pool with HikariCP, including driver, jdbcUrl, username, password, and max pool size.
A database initialization example with HikariConfig and Database.connect.
Write a Kotlin function that runs Flyway migrations at application startup using database config values and the classpath:db/migration location.
Startup code that calls Flyway.configure and sets the data source and migration path.
Developers building a new backend service can use this skill to structure Exposed DSL, DAO, transactions, and connection pooling into a production-ready data access layer.
When a team needs to manage schema changes, they can follow the Flyway patterns here to run versioned migration scripts at startup and keep the schema in sync.
When better testability and separation are needed, Exposed queries can be wrapped behind repository interfaces and tested with an in-memory H2 database.
The document outlines a practical database access setup for Kotlin applications using JetBrains Exposed ORM. It covers the two main query styles, DSL and DAO, transaction handling with newSuspendedTransaction, HikariCP connection pool configuration, and Flyway-based schema migrations. It also describes using the repository pattern to decouple business logic from persistence and mentions in-memory H2 for testing.
Comprehensive patterns for database access with JetBrains Exposed ORM, including DSL queries, DAO, transactions, and production-ready configuration.
Exposed provides two query styles: DSL for direct SQL-like expressions and DAO for entity lifecycle management. HikariCP manages a pool of reusable database connections configured via HikariConfig. Flyway runs versioned SQL migration scripts at startup to keep the schema in sync. All database operations run inside newSuspendedTransaction blocks for coroutine safety and atomicity. The repository pattern wraps Exposed queries behind an interface so business logic stays decoupled from the data layer and tests can use an in-memory H2 database.
suspend fun findUserById(id: UUID): UserRow? =
newSuspendedTransaction {
UsersTable.selectAll()
.where { UsersTable.id eq id }
.map { it.toUser() }
.singleOrNull()
}
suspend fun createUser(request: CreateUserRequest): User =
newSuspendedTransaction {
UserEntity.new {
name = request.name
email = request.email
role = request.role
}.toModel()
}
val hikariConfig = HikariConfig().apply {
driverClassName = config.driver
jdbcUrl = config.url
username = config.username
password = config.password
maximumPoolSize = config.maxPoolSize
isAutoCommit = false
transactionIsolation = "TRANSACTION_READ_COMMITTED"
validate()
}
// DatabaseFactory.kt
object DatabaseFactory {
fun create(config: DatabaseConfig): Database {
val hikariConfig = HikariConfig().apply {
driverClassName = config.driver
jdbcUrl = config.url
username = config.username
password = config.password
maximumPoolSize = config.maxPoolSize
isAutoCommit = false
transactionIsolation = "TRANSACTION_READ_COMMITTED"
validate()
}
return Database.connect(HikariDataSource(hikariConfig))
}
}
data class DatabaseConfig(
val url: String,
val driver: String = "org.postgresql.Driver",
val username: String = "",
val password: String = "",
val maxPoolSize: Int = 10,
)
// FlywayMigration.kt
fun runMigrations(config: DatabaseConfig) {
Flyway.configure()
.dataSource(config.url, config.username, config.password)
.locations("classpath:db/migration")
.baselineOnMigrate(true)
.load()
.migrate()
}
// Application startup
fun Application.module() {
val config = DatabaseConfig(
url = environment.config.property("database.url").getString(),
username = environment.config.property("database.username").getString(),
password = environment.config.property("database.password").getString(),
)
runMigrations(config)
val database = DatabaseFactory.create(config)
// ...
}
-- src/main/resources/db/migration/V1__create_users.sql
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
role VARCHAR(20) NOT NULL DEFAULT 'USER',
metadata JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_role ON users(role);
…
It focuses on common JetBrains Exposed ORM practices, including DSL queries, the DAO pattern, transaction handling, HikariCP connection pooling, Flyway migrations, and the repository pattern.
The excerpt states that database operations run inside newSuspendedTransaction blocks for coroutine safety and atomicity.
Yes. The excerpt mentions that the repository pattern helps decouple business logic and allows tests to use an in-memory H2 database. For more details, see the source repository.
Coordinate behavior-preserving code refactors with tests, review, and gated commits.
Operate long-running agent workloads with monitoring, security, and lifecycle control.
Build a motion foundation for React/Next.js with safe, accessible performance rules.
Unify multi-channel notifications for routing, deduplication, escalation, and inbox consolidation.
Generate or audit design systems and review styling consistency changes.
Learn SwiftUI architecture, state management, navigation, and performance best practices.
Apply idiomatic Kotlin patterns to build robust, efficient, maintainable applications.
Learn practical Kotlin Ktor server patterns for building and testing backend apps.
Learn practical Kotlin Coroutines and Flow patterns for Android and KMP.
Learn Kotlin conventions, result handling, and session patterns for DAT SDK Android.
Get production-ready MySQL and MariaDB schema, query, and operations patterns.
Get PostgreSQL best practices for optimization, schema design, indexing, and security.