Skip to content
KantisPublic

About

MicroORM for Kotlin Multiplatform

Resources

Stars

11 stars

Watchers

0 watching

Forks

Repository files navigation

Stability Status - Alpha GitHub Actions Workflow Status

What is Mikrom?

Mikrom is inspired by Dapper, a popular micro ORM for .NET.

Usage

Note: This library is in an early phase. The API is highly likely to have breaking changes sometimes, but I would value your feedback on the library design.

Kotlin version compatibility

Mikrom version Kotlin version required
0.7.x+ 2.4.21
0.6.x 2.3.20
0.5.x 2.3.10

JDBC

Add the Gradle plugin and dependency:

plugins {
  id("io.github.kantis.mikrom") version "0.7.0"
}

dependencies {
  implementation("io.github.kantis:mikrom-jdbc:0.7.0")
}

Create a JDBC connection and construct a Mikrom instance:

fun main() {
  val dbConnection = DriverManager.getConnection("jdbc:your_database_url")
  val dataSource = JdbcDataSource(dbConnection)
  val mikrom = Mikrom {}

  dataSource.transaction {
    // Using named parameters with a generated ParameterMapper
    mikrom.execute(
      """
        INSERT INTO books(author, title, number_of_pages)
        VALUES (:author, :title, :numberOfPages)
    """,
      CreateBookCommand("JRR Tolkien", BookTitle("The Hobbit"), 310),
      CreateBookCommand("George Orwell", BookTitle("1984"), 328),
    )

    // or, using positional parameters
    mikrom.execute(
      "INSERT INTO books(author, title, number_of_pages) VALUES (?, ?, ?)",
      listOf("JRR Tolkien", "The Hobbit", 310),
      listOf("George Orwell", "1984", 328),
    )

    mikrom.queryFor<Book>(
      "SELECT * FROM books WHERE number_of_pages > ?", 311
    ) // Book("George Orwell", BookTitle("1984"), 328)
  }
}

@MikromParameter
data class CreateBookCommand(val author: String, val title: BookTitle, val numberOfPages: Int)

@JvmInline
value class BookTitle(val title: String)

@MikromResult
data class Book(val author: String, val title: BookTitle, val numberOfPages: Int)

R2DBC

The same API is available as suspending functions on top of an R2DBC ConnectionPool:

dependencies {
  implementation("io.github.kantis:mikrom-r2dbc:0.7.0")
}
import io.github.kantis.mikrom.suspend.execute
import io.github.kantis.mikrom.suspend.queryFor
import io.github.kantis.mikrom.suspend.queryForFlow

suspend fun main() {
  val dataSource = PooledR2dbcDataSource(connectionPool) // an io.r2dbc.pool.ConnectionPool
  val mikrom = Mikrom {}

  dataSource.suspendingTransaction {
    mikrom.execute(
      "INSERT INTO books(author, title, number_of_pages) VALUES (:author, :title, :numberOfPages)",
      CreateBookCommand("JRR Tolkien", BookTitle("The Hobbit"), 310),
    )

    // Loads the full result, like the blocking API
    mikrom.queryFor<Book>("SELECT * FROM books WHERE number_of_pages > :minPages", mapOf("minPages" to 300))

    // Or stream it; the Flow must be collected before the transaction block returns
    mikrom.queryForFlow<Book>("SELECT * FROM books").collect { println(it) }
  }
}

See the getting started guide for details on placeholders, null handling and streaming.

Philosophy

  • Multiplatform design, with JDBC and R2DBC support
    • JS and Native variants not currently planned, but possible. Raise an issue if you want them, also include which underlying driver would make sense to use for that platform.
  • Explicit transaction management
  • Explicit SQL, instead of generated SQL which might not perform well
  • Explicit updates, instead of automated tracking
  • Convenient DSLs, so explicitness doesn't become tedious
  • Code generation through compiler plugins, to support basic Row/Parameter mapping
  • Minimal reflection

License

Copyright (C) 2025 Emil Kantis

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Code from other parties

  • A ton of code, including entire infrastructure setup, related to compiler plugins has been copied from Brian Norman's Buildable project
  • Some code related to compiler plugins has been copied from Zac Sweers' Metro project

About

MicroORM for Kotlin Multiplatform

Resources

Stars

11 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages