kminiorm

ORM for Kotlin supporting JDBC and MongoDB

License

License

Categories

Categories

Data ORM
GroupId

GroupId

com.soywiz.korlibs.kminiorm
ArtifactId

ArtifactId

kminiorm-metadata
Last Version

Last Version

0.10.2
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

kminiorm
ORM for Kotlin supporting JDBC and MongoDB
Project URL

Project URL

https://github.com/korlibs/kminiorm
Source Code Management

Source Code Management

https://github.com/korlibs/kminiorm

Download kminiorm-metadata

Dependencies

compile (1)

Group / Artifact Type Version
com.soywiz.korlibs.klock : klock jar 2.0.7

Project Modules

There are no modules declared in this project.

kminiorm

Actions Status

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 ...
            }
        }
    }
}
com.soywiz.korlibs.kminiorm
Kotlin cORoutine Libraries for fullstack development - https://bintray.com/korlibs/korlibs - https://github.com/korlibs

Versions

Version
0.10.2