commonMain.com.bselzer.ktx.db.transaction.DBTransaction.kt Maven / Gradle / Ivy
The newest version!
package com.bselzer.ktx.db.transaction
import org.kodein.db.*
import org.kodein.memory.use
import kotlin.reflect.KClass
/**
* Represents an abstraction for writing a batch.
*
* @param reader the reader
* @param writer the batch writer
*/
class DBTransaction(
private val reader: DBRead,
private val writer: Batch
) : Transaction, DBRead by reader, Batch by writer {
/**
* Initializes a new instance of the [DBTransaction] class with the [database] as the [reader] and a new batch as the [writer].
*/
constructor(database: DB) : this(database, database.newBatch())
/**
* Executes the [block], commits the changes, and closes the writer.
*/
inline fun use(vararg options: Options.BatchWrite, block: Transaction.() -> R): R = (this as Batch).use {
val result = block(this)
write(*options)
result
}
// Both DBRead and DBWrite are KeyMaker.
override fun keyById(type: KClass, vararg id: Any): Key = reader.keyById(type, *id)
override fun keyFrom(model: M, vararg options: Options.Puts): Key = reader.keyFrom(model, *options)
override fun keyFromB64(type: KClass, b64: String): Key = reader.keyFromB64(type, b64)
}
/**
* Creates a new [DBTransaction] for the given database.
*/
fun DB.transaction() = DBTransaction(this)