https://github.com/pull-vert/kotysa
The idiomatic way to write type-safe SQL in Kotlin
https://github.com/pull-vert/kotysa
Last synced: 8 months ago
JSON representation
The idiomatic way to write type-safe SQL in Kotlin
- Host: GitHub
- URL: https://github.com/pull-vert/kotysa
- Owner: pull-vert
- License: unlicense
- Archived: true
- Created: 2019-05-12T12:45:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-31T13:37:08.000Z (over 5 years ago)
- Last Synced: 2024-08-04T01:28:28.502Z (over 1 year ago)
- Language: Kotlin
- Homepage:
- Size: 823 KB
- Stars: 17
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - kotysa - safe SQL in Kotlin | pull-vert | 17 | (Kotlin)
README
[](http://unlicense.org/)
[ ](https://bintray.com/pull-vert/kotysa/kotysa/_latestVersion)
# Kotysa
The idiomatic way to write **Ko**tlin **ty**pe-**sa**fe SQL.
:exclamation: **This project has migrated to [new location](https://github.com/ufoss-org/kotysa) :exclamation:
## 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: UUID = UUID.randomUUID()
)
```
### step 2 -> Describe database model with [type-safe DSL](docs/table-modelling.md), based on these entities
```kotlin
val tables =
tables().postgresql { // choose database type
table {
name = "roles"
column { it[Role::id].uuid().primaryKey }
column { it[Role::label].varchar() }
}
table {
name = "users"
column { it[User::id].uuid().primaryKey }
column { it[User::firstname].varchar().name("first-name") }
column { it[User::roleId].uuid().foreignKey() }
column { it[User::alias].varchar() }
}
}
```
### step 3 -> Write SQL queries with [type-safe SqlClient DSL](docs/sql-queries.md)
Kotysa will generate SQL for you !
```kotlin
// return all admin users
val admins = sqlClient.select()
.innerJoin().on { it[User::roleId] }
.where { it[Role::label] eq "admin" }
.fetchAll()
```
**No annotations, no code generation, just regular Kotlin code ! No JPA, just pure SQL !**
## Getting started
Kotysa is agnostic from Sql Engine (SqLite on Android, R2DBC, JDBC in the future) :
* use Kotysa with [Spring data R2DBC](kotysa-spring-data-r2dbc/README.md)
* use Kotysa with [SqLite on Android](kotysa-android/README.md)
See sample projects [here](samples).
Kotysa provides [Kotlin Coroutines first class support with R2DBC](kotysa-spring-data-r2dbc/README.md#coroutines-first-class-support)
Kotysa is **not production ready yet**, some key features are still missing. Regular early releases will provide new features (see [next milestones](https://github.com/pull-vert/kotysa/milestones)).
Type safety relies on type and nullability of the Entity property (or getter).
## Build from sources
* Clone Kotysa repository
* Use a JDK 1.8
* You need a local docker, like docker-ce. Some integration tests use testcontainers to start real databases like PostgreSQL
* Kotysa can be easily built with the gradle wrapper
```bash
$ ./gradlew clean buildNeeded
```