Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:23
Show Gist options
  • Select an option

  • Save dacr/f69159308f971361a2643393d4b9bf3f to your computer and use it in GitHub Desktop.

Select an option

Save dacr/f69159308f971361a2643393d4b9bf3f to your computer and use it in GitHub Desktop.
ZIO LMDB simple transaction example / published by https://github.com/dacr/code-examples-manager #a763b457-5b4c-4cdb-897c-23bb321ec2f9/a8e64022131ec339134c2ed4ef5665fefadddb77
// summary : ZIO LMDB simple transaction example
// keywords : scala, zio, lmdb, transaction, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : a763b457-5b4c-4cdb-897c-23bb321ec2f9
// created-on : 2026-02-03T08:55:25+01:00
// managed-by : https://github.com/dacr/code-people-manager
// run-with : scala-cli $file
// ---------------------
//> using scala 3.8.1
//> using dep fr.janalyse::zio-lmdb:2.3.2
//> using javaOpt --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED
// ---------------------
import zio.*, zio.json.*, zio.lmdb.*, zio.lmdb.json.*
import java.io.File, java.util.UUID, java.time.OffsetDateTime
case class Record(uuid: UUID, name: String, age: Int, addedOn: OffsetDateTime) derives LMDBCodecJson
object SimpleExample extends ZIOAppDefault {
override def run = example.provide(LMDB.liveWithDatabaseName("lmdb-data-basic-transaction-example"), Scope.default)
val collectionName = "people"
val example = for {
people <- LMDB.collectionCreate[UUID, Record](collectionName, failIfExists = false)
dateTime <- Clock.currentDateTime
record1 <- Random.nextUUID.map(id => Record(id, "John Doe", 42, dateTime))
record2 <- Random.nextUUID.map(id => Record(id, "Sarah Connors", 24, dateTime))
_ <- people.readWrite { peopleTX =>
peopleTX.upsertOverwrite(record1.uuid, record1) *>
peopleTX.upsertOverwrite(record2.uuid, record2)
}
collected <- people.collect()
_ <- ZIO.foreachDiscard(collected)(record => Console.printLine(record))
} yield ()
}
SimpleExample.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment