Kotysa Save

The idiomatic way to write type-safe SQL in Kotlin

Project README

License: Unlicense Maven Central Kotlin

Kotysa

A light ORM that offers the idiomatic way to write Kotlin type-safe SQL for JVM and Android

  • Kotysa supports various drivers : JDBC, R2DBC, Vertx sqlclient
  • Kotysa supports various database engines : PostgreSQL, MySQL, Microsoft SQL Server, MariaDB, Oracle, H2
  • Kotysa supports SqLite on Android

See the project website for documentation and APIs.

Basic example

Kotysa is easy to use : 3 steps only

step 1 -> Create Kotlin entities

data classes are great for that !

data class Role(
        val label: String,
        val id: UUID = UUID.randomUUID()
)

data class User(
        val firstname: String,
        val roleId: UUID,
        val alias: String? = null,
        val id: Int? = null
)

step 2 -> Describe database model

Use our type-safe Tables DSL to map your entities with the database tables, this is the ORM (object-relational mapping) step

object Roles : H2Table<Role>("roles") {
    val id = uuid(Role::id)
        .primaryKey()
    val label = varchar(Role::label)
        .unique()
}

object Users : H2Table<User>("users") {
    val id = autoIncrementInteger(User::id)
        .primaryKey("PK_users")
    val firstname = varchar(User::firstname, "f_name")
    val roleId = uuid(User::roleId)
        .foreignKey(Roles.id, "FK_users_roles")
    val alias = varchar(User::alias)
}

// List all your mapped tables
private val tables = tables().h2(Roles, Users)

step 3 -> Write SQL queries

Use our type-safe SqlClient DSL, Kotysa executes SQL query for you !

val admins = (sqlClient selectFrom Users
        innerJoin Roles on Users.roleId eq Roles.id
        where Roles.label eq "admin"
        ).fetchAll() // returns all admin users

No annotations, no code generation, no proxy, no additional plugin, just regular Kotlin code ! No JPA, just pure SQL !

Contributors

Contributions are welcome.

  • Compile Kotysa with a JDK 17.
  • You need a local docker, like docker-ce : some tests use testcontainers to start real databases like PostgreSQL, MySQL...
  1. Clone this repo
git clone [email protected]:ufoss-org/kotysa.git
  1. Build project
./gradlew clean build
Open Source Agenda is not affiliated with "Kotysa" Project. README Source: ufoss-org/kotysa
Stars
109
Open Issues
14
Last Commit
1 month ago
Repository

Open Source Agenda Badge

Open Source Agenda Rating