Last active
February 3, 2026 20:23
-
-
Save dacr/dcb8a11f095ef0a2a95c24701e6eb804 to your computer and use it in GitHub Desktop.
ZIO LMDB simple example / published by https://github.com/dacr/code-examples-manager #5c5fabdd-7f05-48d6-ae67-c8618b02284e/d70f0fc7a34e5ed50b874732d34d5a570aa6b3aa
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 | |
| // 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 : 5c5fabdd-7f05-48d6-ae67-c8618b02284e | |
| // created-on : 2022-08-09T20:01:37+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-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-simple-example"), Scope.default) | |
| val collectionName = "examples" | |
| val example = for { | |
| examples <- LMDB.collectionCreate[UUID,Record](collectionName, failIfExists = false) | |
| recordId <- Random.nextUUID | |
| dateTime <- Clock.currentDateTime | |
| record = Record(recordId, "John Doe", 42, dateTime) | |
| _ <- examples.upsertOverwrite(recordId, record) | |
| gotten <- examples.fetch(recordId).some | |
| collected <- examples.collect() | |
| _ <- Console.printLine(s"collection $collectionName contains ${collected.size} records") | |
| _ <- ZIO.foreachDiscard(collected)(record => Console.printLine(record)) | |
| _ <- 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 ${examples.lmdb.databasePath}/""") | |
| _ <- Console.printLine(s"""To dump collection content : mdb_dump -p -s $collectionName ${examples.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