kminiorm
ORM for Kotlin supporting JDBC and MongoDB
Repository:
https://bintray.com/korlibs/korlibs/kminiorm
repositories {
// ...
jcenter()
maven { url = uri("https://dl.bintray.com/korlibs/korlibs") }
}
dependencies {
// Core:
implementation("com.soywiz.korlibs.kminiorm:kminiorm-jvm:0.9.0")
// JDBC:
implementation("com.soywiz.korlibs.kminiorm:kminiorm-jdbc-jvm:0.9.0")
implementation("org.xerial:sqlite-jdbc:3.30.1")
implementation("com.h2database:h2:1.4.200")
// Mongo:
implementation("com.soywiz.korlibs.kminiorm:kminiorm-mongo-jvm:0.9.0")
}
Sample:
You can run ./sample.main.kts
to get it working.
import com.soywiz.kminiorm.*
import com.soywiz.kminiorm.dialect.*
import com.soywiz.kminiorm.where.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.*
import java.io.*
fun main() = runBlocking {
data class MyTable(
@DbPrimary val key: String,
@DbIndex val value: Long
) : DbBaseModel
val sqliteFile = File("sample.sq3")
val db = JdbcDb(
"jdbc:sqlite:${sqliteFile.absoluteFile.toURI()}",
debugSQL = System.getenv("DEBUG_SQL") == "true",
dialect = SqliteDialect,
async = true
)
val table = db.table<MyTable>()
table.insert(
MyTable("hello", 10L),
MyTable("world", 20L),
MyTable("this", 30L),
MyTable("is", 40L),
MyTable("a", 50L),
MyTable("test", 60L),
onConflict = DbOnConflict.IGNORE
)
table.where { it::value ge 20L }.limit(10).collect {
println(it)
}
Unit
}
Defining Tables
You can use normal Kotlin fields
data class MyTable(
@DbPrimary val key: String,
@DbIndex val value: Long
) : DbBaseModel
Multi-column indices
data class MyTable(
@DbUnique("a_b") val a: String,
@DbUnique("a_b") val b: String
) : DbBaseModel
Creating a Repository
Migrations
If you change a table adding a new field to it, you can register a DbMigration that will be executed when the ALTER TABLE is automatically performed.
data class MyTable(
val a: String,
@DbPerformMigration(MyAddColumnMigration::class) val newlyAddedField: String //
) : DbBaseModel {
class MyAddColumnMigration : DbMigration<MyTable> {
override suspend fun migrate(table: DbTable<MyTable>, action: DbMigration.Action, column: ColumnDef<MyTable>?) {
table.where.collect { item ->
// Update item here ...
}
}
}
}