io.github.mbannour.mongo.codecs

Members list

Type members

Classlikes

trait BsonCodec[T]

Type class for BSON encoding and decoding.

Type class for BSON encoding and decoding.

This provides a more functional and composable alternative to directly working with MongoDB codecs. It allows for better type safety and easier testing.

Type parameters

T

The type to encode/decode

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object BsonCodec

Attributes

Companion
trait
Supertypes
class Object
trait Matchable
class Any
Self type
BsonCodec.type

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
case class CodecConfig(noneHandling: NoneHandling, discriminatorField: String)

Configuration for BSON codec generation behavior.

Configuration for BSON codec generation behavior.

This configuration object encapsulates all codec generation options, making the API more type-safe and extensible than using boolean flags.

Value parameters

discriminatorField

Field name used to store type discriminators for sealed hierarchies (default: "_t"). ===Example Usage===

 val config = CodecConfig(
   noneHandling = NoneHandling.Ignore,
   discriminatorField = "_type"
 )
noneHandling

Strategy for handling None values in Option fields.

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

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.

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

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

object Person: private val config = CodecConfig(noneHandling = NoneHandling.Encode) private val provider = CodecProviderMacro.createCodecProvider[Person]

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

Attributes

See also

CaseClassCodecGenerator.generateCodec for the underlying codec generator.

Supertypes
class Object
trait Matchable
class Any
Self type
object CodecTestKit

Testing utilities for BSON codecs.

Testing utilities for BSON codecs.

Provides helper methods for testing codec symmetry and round-trip encoding/decoding.

Attributes

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

Strategy for handling None values in Option fields during BSON encoding.

Strategy for handling None values in Option fields during BSON encoding.

Attributes

Supertypes
trait Enum
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type

Types

opaque type RegistryBuilder

Type-safe, immutable registry builder using Scala 3 opaque types and extension methods.

Type-safe, immutable registry builder using Scala 3 opaque types and extension methods.

RegistryBuilder provides a fluent API for constructing MongoDB org.bson.codecs.configuration.CodecRegistry instances with compile-time type safety and functional programming patterns.

===Features===

  • Opaque types for enhanced type safety without runtime overhead
  • Immutable by design - all operations return new instances
  • Lazy registry building - registries are only built once at the end
  • Choose between encode None as null or ignore None fields
  • Add individual codecs with withCodec or many at once with withCodecs
  • Automatically derive codecs for case classes with register[T]
  • Batch registration with registerAll[(Type1, Type2, ...)]
  • Configure discriminator field names for sealed hierarchies
  • Extension methods for fluent, idiomatic Scala 3 API

===Example Usage===

 import org.mongodb.scala.MongoClient

 // Simple usage
 val registry = MongoClient.DEFAULT_CODEC_REGISTRY
   .newBuilder
   .ignoreNone
   .register[Address]
   .register[Person]
   .build

 // With custom configuration
 val registry = RegistryBuilder
   .from(MongoClient.DEFAULT_CODEC_REGISTRY)
   .configure(_.copy(
     noneHandling = NoneHandling.Ignore,
     discriminatorField = "_type"
   ))
   .withCodec(employeeIdCodec)
   .register[Person]
   .build

 // Batch registration with functional configuration
 val registry = MongoClient.DEFAULT_CODEC_REGISTRY
   .newBuilder
   .configure { config =>
     config
       .withIgnoreNone
       .withDiscriminator("_type")
   }
   .registerAll[(Address, Person, Department)]
   .build

Attributes