KandraBatchScope

DSL scope for building a caller-controlled LOGGED batch.

All saveInBatch() and deleteInBatch() calls inside a KandraRuntime.batch or KandraRuntime.batchBlocking block are collected and executed as a single atomic LOGGED BATCH when the block exits.

Deliberately not named save/delete: Kotlin resolves a member function of the extension receiver over an extension function with a matching name unconditionally, even when the extension is a member-extension of an implicit receiver in closer scope (as these are, being declared inside KandraBatchScope itself). Since every repository already has its own real save/delete member, repo.save(entity) (or with(repo) { save(entity) }) inside a batch block would always silently call the repository's own immediately-executing method — never this class's statement-collecting one — with no compiler warning. Distinct names route the call correctly and make it a compile error to reach for the wrong one.

Restrictions:

  • findAll, findById, and all read operations are not available — reads cannot be batched.

  • saveIfNotExists throws KandraQueryException — LWT cannot be mixed with regular statements.

  • EVENTUAL lookup writes are not included in the batch (they fire separately after commit).

Functions

Link copied to clipboard

Adds the entity delete (primary + all lookup tables) to this batch.

Link copied to clipboard

Always throws — LWT cannot be mixed with regular batch statements. Named distinctly (not saveIfNotExists) for the same shadowing reason as saveInBatch/deleteInBatch: a same-named guard here would never actually be reachable, since repo.saveIfNotExists(...) would always resolve to the repository's own real, immediately-executing member instead — defeating the guard silently rather than enforcing it.

Link copied to clipboard
fun <T : Any> KandraRepository<T>.saveInBatch(entity: T, ttlSeconds: Int? = null)
fun <T : Any> KandraSuspendRepository<T>.saveInBatch(entity: T, ttlSeconds: Int? = null)

Adds the entity save (primary + BATCH lookups) to this batch.