-
-
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
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
//> 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