https://github.com/ufoss-org/kotysa
The idiomatic way to write type-safe SQL in Kotlin
https://github.com/ufoss-org/kotysa
android coroutines jdbc kotlin ktor orm quarkus r2dbc spring-boot sql sqlite vertx-sql-client
Last synced: 10 months ago
JSON representation
The idiomatic way to write type-safe SQL in Kotlin
- Host: GitHub
- URL: https://github.com/ufoss-org/kotysa
- Owner: ufoss-org
- License: unlicense
- Created: 2020-05-29T17:24:06.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-14T23:04:00.000Z (about 2 years ago)
- Last Synced: 2024-11-25T14:53:45.853Z (over 1 year ago)
- Topics: android, coroutines, jdbc, kotlin, ktor, orm, quarkus, r2dbc, spring-boot, sql, sqlite, vertx-sql-client
- Language: Kotlin
- Homepage:
- Size: 3.82 MB
- Stars: 121
- Watchers: 5
- Forks: 2
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-java - Kotysa
README
[](http://unlicense.org/)
[](https://search.maven.org/artifact/org.ufoss.kotysa/kotysa-core)
[](http://kotlinlang.org)
# Kotysa
> A light ORM that offers the idiomatic way to write **Ko**tlin **ty**pe-**sa**fe 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](https://ufoss.org/kotysa/) 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 !
```kotlin
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
```kotlin
object Roles : H2Table("roles") {
val id = uuid(Role::id)
.primaryKey()
val label = varchar(Role::label)
.unique()
}
object Users : H2Table("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 !
```kotlin
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
```bash
git clone git@github.com:ufoss-org/kotysa.git
```
2. Build project
```bash
./gradlew clean build
```