Introduce

What is Kronos?

Kronos is a modern ORM framework designed for Kotlin. It uses a compiler plugin to provide type-safe SQL DSL support and works with multiple databases.

We support both Code First and Database First schemas, which provide auto-creation of database table structures, auto-synchronization, as well as support for table structures, indexes and code generation.

Kronos analyzes Kotlin expressions and keeps common database operations close to Kotlin syntax. You can use ==, >, <, in, field references, projection DSL, and table operations without writing string SQL for ordinary CRUD flows.

New users should start with 📚Installation, then follow 📚First Query for the smallest model, table creation, insert, and select flow. After that, read 📚KPojo for entity mapping, 📚Select for reads, 📚Insert for writes, and 📚Connect to DB for production data source setup.

graph LR
    A[Kronos] --> B[Kronos-core]
    B --> I[ORM for KPojo]
    I --> J[Query、Insert、Update、Delete,etc.]
    A --> C[Kronos-compiler-plugin]
    C --> L[Kronos-compiler-kotlin-plugin]
    C --> D[Kronos-maven-plugin]
    C --> E[Kronos-gradle-plugin]
    A --> F[Kronos-data-source-wrapper]
    F --> G[DataSource]
    G --> H[MySQL, PostgreSQL, SQLite, H2, SQL Server, Oracle, DM8]
    A --> K[Kronos-logging, Kronos-codegen and Other Plugins]

Why Kronos?

Simple examples

Here is a simple example.

import com.kotlinorm.Kronos
import com.kotlinorm.wrappers.KronosJdbcWrapper
import org.apache.commons.dbcp2.BasicDataSource

val wrapper = KronosJdbcWrapper(
    BasicDataSource().apply {
        driverClassName = "com.mysql.cj.jdbc.Driver"
        url = "jdbc:mysql://localhost:3306/kronos?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"
        username = "user"
        password = "******"
    }
)

Kronos.dataSource = { wrapper }

// Create a User object
val user: User = User(
    id = 1,
    name = "Kronos",
    age = 18
)

// Create the table if it does not exist, otherwise synchronize columns and indexes.
wrapper.table.syncTable(user)

// Insert data
user.insert().execute()

// Update the name field according to the id.
user.update().set { it.name = "Kronos ORM" }.by { it.id }.execute()
// or
user.update { it.name }.by { it.id }.execute()

// Query records based on object value
val selectedUser: User = user.select().by { it.id }.first()

// Query name field by id
val selectedName: String = user.select { it.name }.where { it.id == 1 }.first<String>()

// Delete data with id 1
User().delete().where { it.id == 1 }.execute()
// or
User(id = 1).delete().by { it.id }.execute()
Open interactive example