Skip to content

Instantly share code, notes, and snippets.

@igorlukanin
Last active October 29, 2015 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorlukanin/e24098c891ecbce301c0 to your computer and use it in GitHub Desktop.
Save igorlukanin/e24098c891ecbce301c0 to your computer and use it in GitHub Desktop.
Using RethinkDB Java driver with Kotlin
/**
* 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