io.github.mbannour.mongo.codecs

Members list

Type members

Classlikes

Macro-based codec generator for BSON serialization/deserialization of case classes, supporting nested and sealed hierarchies.

Macro-based codec generator for BSON serialization/deserialization of case classes, supporting nested and sealed hierarchies.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type

CodecProviderMacro is a utility object that provides inline macros for generating MongoDB CodecProvider instances for Scala case classes.

CodecProviderMacro is a utility object that provides inline macros for generating MongoDB CodecProvider instances for Scala case classes.

A CodecProvider wraps a generated Codec[T] so it can be plugged into the MongoDB driver’s CodecRegistry, allowing seamless serialization and deserialization of your domain models.

Two variants are supported:

  • createCodecProviderEncodeNone encodes None fields as BSON null.
  • createCodecProviderIgnoreNone omits None fields entirely from the document.

==Quick Start== In your case class companion: {{ import io.github.mbannour.mongo.codecs.CodecProviderMacro import org.bson.codecs.configuration.CodecRegistries import org.mongodb.scala.MongoClient

case class Person(name: String, age: Int, nickname: Option[String])

object Person: // Choose encode-or-ignore None private val provider = CodecProviderMacro.createCodecProviderEncodeNone[Person]

given registry: CodecRegistry = CodecRegistries.fromRegistries( MongoClient.DEFAULT_CODEC_REGISTRY, CodecRegistries.fromProviders(provider) ) end Person }}

Then attach this registry to your database: {{ val client = MongoClient("mongodb://localhost") val db = client.getDatabase("mydb").withCodecRegistry(summon) val coll = db.getCollectionPerson coll.insertOne(Person("Alice", 30, None)).toFuture() }}

Attributes

See also

CaseClassCodecGenerator.generateCodec for the underlying codec generator.

Supertypes
class Object
trait Matchable
class Any
Self type

EnumValueCodecProvider is a helper object to generate a MongoDB org.bson.codecs.configuration.CodecProvider for Scala 3 enums that can be uniquely represented by a single primitive value such as an Int, String, or Boolean.

EnumValueCodecProvider is a helper object to generate a MongoDB org.bson.codecs.configuration.CodecProvider for Scala 3 enums that can be uniquely represented by a single primitive value such as an Int, String, or Boolean.

This allows seamless BSON serialization/deserialization of Scala enums by mapping them to their primitive representation when writing to and reading from MongoDB.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type