R2Dbi is a Kotlin library inspired by JDBI and built on Reactive Relational Database Connectivity (R2DBC) SPI.
R2DBC brings Reactive Programming APIs to relational databases. R2DBI simplifies database operations by offering a
declarative style similar to JDBI SqlObjects.
It provides easy-to-use interfaces for executing SQL queries
and mapping data
to objects
.
While primarily designed for Kotlin, it may also work with Java, although it hasn't been tested yet.
R2dbi started out to be used in declarative mode (ie define an annotated interface
). The fluent interface is still
being developed and hence not ready to use. It is available to experiment and play with.
R2Dbi is licensed under the commercial friendly Apache 2.0 license.
define the SQL to execute and the shape of the results - by creating an annotated interface
.
import kotlinx.coroutines.flow.Flow
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
// Declare the API using annotations on a Java interface
interface UserDao {
// Using kotlinx.coroutines.flow.Flow
@SqlQuery("SELECT 1")
fun getOne(): Flow<Long>
@SqlQuery("SELECT * FROM 'user' where name = :name")
fun findUser(@Bind("name") name: String): Flow<User>;
// Using reactor.core.publisher.Flux
@SqlQuery("SELECT * FROM 'user'")
fun getAllUsers(): Flux<User>;
}
This library supports both kotlinx.coroutines.flow.Flow
and org.reactivestreams.Publisher
/ reactor.core.publisher.Flux
as the return type. Note that no other return type (especially blocking) is supported.
You look at TestQueryDao.kt and TestDynamicInterfaceBase.kt to checkout more examples.
🚧
Before utilizing this library, it's crucial to grasp the fundamentals of Coroutines and Project Reactor.
For Instance, without subscribing to aPublisher
or attempting to materialize a value of aFlow
, the underlying SQL query won't execute.
see CONTRIBUTING.md
TODO