io.github.mbannour.mongo.codecs
Members list
Type members
Classlikes
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 Objecttrait Matchableclass Any
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 Objecttrait Matchableclass Any
- Self type
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
- noneHandling
-
Strategy for handling
Nonevalues inOptionfields. ===Example Usage===val config = CodecConfig( noneHandling = NoneHandling.Ignore )
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow 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 Objecttrait Matchableclass Any
- Self type
-
CodecProviderMacro.type
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 Objecttrait Matchableclass Any
- Self type
-
CodecTestKit.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 Objecttrait Matchableclass 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 Enumtrait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
RegistryBuilder.type
Types
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
- Efficient caching - temporary derivation registry cached across chained operations
- Choose between encode
Noneasnullor ignoreNonefields - Add individual codecs with
withCodecor many at once withwithCodecs - Automatically derive codecs for case classes with
register[T] - Batch registration with
registerAll[(Type1, Type2, ...)] - Extension methods for fluent, idiomatic Scala 3 API
===Common Patterns===
// Register a single type and build immediately
given CodecRegistry = MongoClient.DEFAULT_CODEC_REGISTRY
.newBuilder
.just[User]
// Register multiple types with configuration
val registry = MongoClient.DEFAULT_CODEC_REGISTRY
.newBuilder
.ignoreNone
.withTypes[(User, Order, Product)]
// Conditional registration
val builder = baseRegistry.newBuilder
.registerIf[AdminUser](isProduction)
.registerIf[DebugInfo](!isProduction)
// Merge builders
val commonTypes = baseBuilder.register[Address].register[Person]
val fullBuilder = commonTypes ++ specificTypesBuilder
===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
))
.withCodec(employeeIdCodec)
.register[Person]
.build
// Batch registration with functional configuration
val registry = MongoClient.DEFAULT_CODEC_REGISTRY
.newBuilder
.configure { config =>
config
.withIgnoreNone
}
.registerAll[(Address, Person, Department)]
.build
===Performance Notes===
- The builder maintains a cached temporary registry used for codec derivation
- Chaining
register[A].register[B]...is O(N) total, not O(N²) - The cache is preserved across register calls and only rebuilt when base/codecs change
- The final registry is assembled once in
build()with all accumulated providers