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
- Host: GitHub
- URL: https://github.com/korlibs/kminiorm
- Owner: korlibs
- License: mit
- Created: 2019-09-26T13:09:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-02-12T06:14:55.000Z (over 4 years ago)
- Last Synced: 2024-05-02T02:09:52.536Z (about 2 years ago)
- Language: Kotlin
- Homepage: https://bintray.com/soywiz/soywiz/kminiorm
- Size: 408 KB
- Stars: 9
- Watchers: 11
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# kminiorm
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 ...
}
}
}
}
```