Last active
October 29, 2015 03:35
-
-
Save igorlukanin/e24098c891ecbce301c0 to your computer and use it in GitHub Desktop.
Using RethinkDB Java driver with Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Database.kt | |
*/ | |
import com.rethinkdb.RethinkDB | |
import com.rethinkdb.gen.ast.ReqlExpr | |
import com.rethinkdb.net.Connection | |
import com.rethinkdb.net.ConnectionInstance | |
object Database { | |
private lateinit var db: String | |
lateinit var connection: Connection<ConnectionInstance> | |
fun connect(db: String) { | |
this.db = db | |
connection = RethinkDB.r.connection().connect() | |
} | |
fun query(table: String) = RethinkDB.r.db(db).table(table) | |
} | |
fun ReqlExpr.run() = this.run<Any>(Database.connection) | |
inline fun <reified T : Any> ReqlExpr.one(): T? = this.run(Database.connection, T::class.java) | |
/** | |
* Client code | |
*/ | |
Database.query("tags").insert(pojo).run() | |
val selected = Database.query("tags").get(id).one<PojoClass>() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment