An open API service indexing awesome lists of open source software.

https://github.com/korlibs/kminiorm

ORM for Kotlin supporting JDBC and MongoDB
https://github.com/korlibs/kminiorm

Last synced: about 1 year ago
JSON representation

ORM for Kotlin supporting JDBC and MongoDB

Awesome Lists containing this project

README

          

# kminiorm


Build Status
Maven Central
Discord

ORM for Kotlin supporting JDBC and MongoDB

### Full Documentation:

## Gradle:

```kotlin
def kminiOrmVersion = "..." // Find latest version on this README

repositories {
// ...
mavenCentral()
}
dependencies {
// Core:
implementation("com.soywiz.korlibs.kminiorm:kminiorm-jvm:$kminiOrmVersion")
// JDBC:
implementation("com.soywiz.korlibs.kminiorm:kminiorm-jdbc-jvm:$kminiOrmVersion")
implementation("org.xerial:sqlite-jdbc:3.30.1")
implementation("com.h2database:h2:1.4.200")
// Mongo:
implementation("com.soywiz.korlibs.kminiorm:kminiorm-mongo-jvm:$kminiOrmVersion")
}
```

## Sample:

You can run `./sample.main.kts` to get it working.

```kotlin
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()
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

```kotlin
data class MyTable(
@DbPrimary val key: String,
@DbIndex val value: Long
) : DbBaseModel
```

### Multi-column indices

```kotlin
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.

```kotlin
data class MyTable(
val a: String,
@DbPerformMigration(MyAddColumnMigration::class) val newlyAddedField: String //
) : DbBaseModel {
class MyAddColumnMigration : DbMigration {
override suspend fun migrate(table: DbTable, action: DbMigration.Action, column: ColumnDef?) {
table.where.collect { item ->
// Update item here ...
}
}
}
}
```