Kapper 1.1: Bringing Kotlin Coroutines to Your Database Layer

Kapper 1.1: Bringing Kotlin Coroutines to Your Database Layer

I'm excited to announce that Kapper 1.1 now supports Kotlin Coroutines!

When I created Kapper, my goal was to build the most idiomatic lightweight ORM for Kotlin, and this release brings us one step closer.

The secret? Just a single suspending function: `withConnection`.

This extension function on DataSource keeps Kapper's API clean and consistent while unlocking the full power of coroutines for your database operations.

True to Kapper's philosophy, it complements rather than wraps the native JDBC API.

runBlocking {
    val selectJob =
        async {
            // Execute DB query without blocking the main thread
            it.query<SuperHero>("SELECT * FROM super_heroes")
        }
    ... // do something else while query executes
    selectJob.join()
}        

What I love most about this solution is its elegance - adding coroutine support took only 10 lines of code!

I've written a detailed blog post explaining why this minimal API makes perfect sense for handling database connections in a coroutine context.

Check out the blog post to learn about:

  • Why a single suspending function is all you need.
  • How to run queries concurrently.
  • Best practices for database transactions in coroutines.

Leave a comment, check out the Kapper and Kapper-Example repositories for more information and examples, and let me know what you think.

要查看或添加评论,请登录

Dries Samyn的更多文章

社区洞察

其他会员也浏览了