Skip to content

Instantly share code, notes, and snippets.

@trickster
Forked from dacr/zio-lmdb-basic.sc
Last active September 23, 2022 03:35
Show Gist options
  • Save trickster/f11d3dffd3baa97d2bb9bb23f9109ed1 to your computer and use it in GitHub Desktop.
Save trickster/f11d3dffd3baa97d2bb9bb23f9109ed1 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/cc000abfc125180c0fd31e1573008fe900473514
//> using scala "3.2.0"
//> using javaOpt "--add-opens", "java.base/java.nio=ALL-UNNAMED", "--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED"
//> using lib "fr.janalyse::zio-lmdb:0.0.1"
//> using lib "fr.janalyse::zio-worksheet:2.0.2.0"
// ---------------------
import zio.*
import zio.lmdb.*
import zio.json.*
import java.io.File
import java.util.UUID
//import zio.worksheet.*
case class Record(uuid: UUID, name: String, age: Int)
object Record {
given JsonCodec[Record] = DeriveJsonCodec.gen
}
object SimpleExample extends ZIOAppDefault {
val dbConfig = ZLayer.fromZIO(
for {
dataPath <- System.env("LMDB_DATA_PATH").someOrElse("lmdb-data-simple-example")
dataPathFile = new File(dataPath)
_ <- ZIO.attempt(dataPathFile.mkdirs())
} yield LMDBConfig(dataPathFile)
)
override def run = example.provide(dbConfig, LMDB.live, Scope.default)
val example = for {
_ <- LMDB.databaseCreate("example")
recordId <- Random.nextUUID
record = Record(recordId, "John Doe", 42)
_ <- LMDB.upsertOverwrite("example", recordId.toString, record)
gotten <- LMDB.fetch[Record]("example", recordId.toString).some
_ <- LMDB.delete("example", recordId.toString)
deleted <- LMDB.fetch[Record]("example", recordId.toString)
_ <- Console.printLine(gotten)
} yield ()
}
SimpleExample.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment