Skip to main content
Version: 2.x

ZIO LMDB

ZIO LMDB is an embedded, ACID, key-value database for ZIO. It needs no infrastructure: the data lives in a single file.

Introductionโ€‹

ZIO LMDB is built on top of lmdbjava and offers a type-safe, ZIO-native API.

Key features of ZIO LMDB:

  • Three Collection Kinds โ€” LMDBCollection (one key to one value), LMDBMulti (one key to many values), and LMDBIndex (one key to many keys).
  • Customizable Codecs โ€” Use the JSON codec (derives LMDBCodecJson), the bytes-optimized codecs for keys, or our own.
  • Atomic Transactions โ€” Run transactions on a single collection or across collections.
  • Lexicographic Ordering โ€” Keys are sorted, so range scans and pagination come for free.

Installationโ€‹

In order to use this library, we need to add the following line to our build.sbt file:

libraryDependencies += "fr.janalyse" %% "zio-lmdb" % "2.8.6"

Exampleโ€‹

In the following example, we create a collection of Person values, insert one, update it, and fetch it back:

import zio.*, zio.lmdb.*, zio.lmdb.json.*
import java.util.UUID

case class Person(name: String, age: Int) derives LMDBCodecJson

object Example extends ZIOAppDefault:
def run = (for {
people <- LMDB.collectionCreate[UUID, Person]("people", failIfExists = false)
id <- Random.nextUUID
_ <- people.insert(id, Person("Alice", 30))
_ <- people.update(id, previous => previous.copy(age = previous.age + 1))
result <- people.fetch(id)
_ <- Console.printLine(result)
} yield ()).provide(LMDB.liveWithDatabaseName("my-app"), Scope.default)

When running with Scala CLI, the JVM needs the following options to give LMDB access to NIO internals:

//> using dep fr.janalyse::zio-lmdb:2.8.6
//> using javaOpt --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED

For the full API reference, including transactions, codecs, indexes, and configuration, see the ZIO LMDB documentation.

ZIO Chat

Hi, fellow coder! ๐ŸŒŸ Welcome to your ZIO adventure with the ZIO Chat Bot. ๐Ÿš€ Let's master functional Scala together. From referential transparency to concurrent prowess, I've got your back. Ready to dive in? Your questions, my insights โ€“ let's conquer ZIO! ๐Ÿ’ก๐Ÿค–