Last active
February 3, 2026 20:25
-
-
Save dacr/9d2c4171d1b1e7a40a244ef456725d25 to your computer and use it in GitHub Desktop.
ZIO LMDB simple example for scala-2 / published by https://github.com/dacr/code-examples-manager #1c3bed7d-d01d-422d-b299-5c1fa03c5294/858c27809af102e84594f3bf52bcfa5f0397f15b
This file contains hidden or 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
| // summary : ZIO LMDB simple example for scala-2 | |
| // keywords : scala, zio, lmdb, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 1c3bed7d-d01d-422d-b299-5c1fa03c5294 | |
| // created-on : 2023-03-25T11:47:27+01:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala 2.13.16 | |
| //> using dep fr.janalyse::zio-lmdb:1.8.4 | |
| //> using javaOpt --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED | |
| // --------------------- | |
| import zio._ | |
| import zio.lmdb._ | |
| import zio.json._ | |
| import java.io.File | |
| import java.util.UUID | |
| import java.time.OffsetDateTime | |
| case class Record(uuid: UUID, name: String, age: Int, addedOn: OffsetDateTime) | |
| object Record { | |
| implicit val jsonCodecRecord: JsonCodec[Record] = DeriveJsonCodec.gen | |
| } | |
| object SimpleExample extends ZIOAppDefault { | |
| override def run = example.provide(LMDB.liveWithDatabaseName("lmdb-data-simple-example"), Scope.default) | |
| val collectionName = "examples" | |
| val example = for { | |
| examples <- LMDB.collectionCreate[Record](collectionName, failIfExists = false) | |
| recordId <- Random.nextUUID | |
| dateTime <- Clock.currentDateTime | |
| record = Record(recordId, "John Doe", 42, dateTime) | |
| _ <- examples.upsertOverwrite(recordId.toString, record) | |
| gotten <- examples.fetch(recordId.toString).some | |
| collected <- examples.collect() | |
| _ <- Console.printLine(s"collection $collectionName contains ${collected.size} records") | |
| _ <- ZIO.foreachDiscard(collected)(record => Console.printLine(record)) | |
| lmdb <- ZIO.service[LMDB] | |
| _ <- Console.printLine("""LMDB standard tools can be used to manage the database content : sudo apt-get install lmdb-utils""") | |
| _ <- Console.printLine(s"""To get some statistics : mdb_stat -s $collectionName ${lmdb.databasePath}/""") | |
| _ <- Console.printLine(s"""To dump collection content : mdb_dump -p -s $collectionName ${lmdb.databasePath}/""") | |
| } yield () | |
| } | |
| SimpleExample.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment