Documentation
¶
Overview ¶
Package factory provides a fluent builder for creating MongoDB clients from configuration.
MongoBuilder uses a fluent API with deferred error accumulation: errors from any step are collected and returned at MongoBuilder.Build time.
m, err := factory.New(cfg.Mongodb).
UseLogger(logger).
UseTlsConfig(tlsConfig).
UseKmsProvider(kmsProvider).
UseHealthCoordinator(coordinator).
Build(ctx)
The builder integrates with config.Mongodb to produce driver-level go.mongodb.org/mongo-driver/v2/mongo/options.ClientOptions and higher-level mongo.Mongo wrappers with authentication, TLS, connection pooling, compression, and client-side field level encryption (CSFLE).
The builder supports two configuration modes: connection-URI based (when config.Mongodb.ConnectionURI is set) and field-based (individual host, credential, and pool settings). Both paths converge in MongoBuilder.ClientOptions.
When a health.Coordinator is provided via MongoBuilder.UseHealthCoordinator, the builder automatically registers a health checker that pings the primary on each check.
Index ¶
- type MongoBuilder
- func (b *MongoBuilder) Build(_ context.Context) (*mongo.Mongo, error)
- func (b *MongoBuilder) ClientOptions() (*mongoOptions.ClientOptions, error)
- func (b *MongoBuilder) UseCollector(v metrics.Collector) *MongoBuilder
- func (b *MongoBuilder) UseConverterOptions(opts ...converter.Option) *MongoBuilder
- func (b *MongoBuilder) UseDefaultLogger() *MongoBuilder
- func (b *MongoBuilder) UseHealthCoordinator(v *health.Coordinator) *MongoBuilder
- func (b *MongoBuilder) UseHealthServiceName(v string) *MongoBuilder
- func (b *MongoBuilder) UseKmsProvider(v kms.Provider) *MongoBuilder
- func (b *MongoBuilder) UseLogger(v *slog.Logger) *MongoBuilder
- func (b *MongoBuilder) UseTlsConfig(v *tls.Config) *MongoBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MongoBuilder ¶
type MongoBuilder struct {
corefactory.Base
// contains filtered or unexported fields
}
MongoBuilder assembles a mongo.Mongo wrapper step by step using a fluent API. Create instances with New. Errors are accumulated and reported at MongoBuilder.Build time. The builder is not safe for concurrent use.
func New ¶
func New(cfg *config.Mongodb) *MongoBuilder
New creates a MongoBuilder for the given MongoDB config. Config can be nil — the error surfaces at MongoBuilder.Build time.
func (*MongoBuilder) Build ¶
Build creates a mongo.Mongo wrapper from configuration. If a health.Coordinator was provided via MongoBuilder.UseHealthCoordinator, a health checker is registered under the service name "mongo". The returned instance is not connected; call mongo.Mongo.Connect to establish the connection.
func (*MongoBuilder) ClientOptions ¶
func (b *MongoBuilder) ClientOptions() (*mongoOptions.ClientOptions, error)
ClientOptions creates MongoDB driver mongoOptions.ClientOptions from the builder's configuration. When config.Mongodb.ConnectionURI is set, ApplyURI is used as the base and pool/timeout/retry/TLS fields are applied on top. Otherwise, options are built from individual config fields including hosts, credentials, and compressors. Returns an error if TLS setup fails.
func (*MongoBuilder) UseCollector ¶
func (b *MongoBuilder) UseCollector(v metrics.Collector) *MongoBuilder
UseCollector sets the metrics.Collector for recording MongoDB metrics.
func (*MongoBuilder) UseConverterOptions ¶
func (b *MongoBuilder) UseConverterOptions(opts ...converter.Option) *MongoBuilder
UseConverterOptions registers extra converter.Option values that the builder forwards to mongo.WithConverterOptions when constructing the mongo.Mongo client. Successive calls accumulate options.
Useful for plugging in a codec that bridges custom field types between the Mongo model and the domain entity, for example optionalcodec.Codec to convert optional.Optional[T] ↔ *T.
func (*MongoBuilder) UseDefaultLogger ¶
func (b *MongoBuilder) UseDefaultLogger() *MongoBuilder
UseDefaultLogger sets the logger to slog.Default.
func (*MongoBuilder) UseHealthCoordinator ¶
func (b *MongoBuilder) UseHealthCoordinator(v *health.Coordinator) *MongoBuilder
UseHealthCoordinator sets the health coordinator used to register a health checker for the created MongoDB client.
func (*MongoBuilder) UseHealthServiceName ¶
func (b *MongoBuilder) UseHealthServiceName(v string) *MongoBuilder
UseHealthServiceName sets the service name used when registering the health checker with the coordinator. Defaults to "mongo" if not set.
func (*MongoBuilder) UseKmsProvider ¶
func (b *MongoBuilder) UseKmsProvider(v kms.Provider) *MongoBuilder
UseKmsProvider sets the KMS provider used for client-side field level encryption.
func (*MongoBuilder) UseLogger ¶
func (b *MongoBuilder) UseLogger(v *slog.Logger) *MongoBuilder
UseLogger sets the logger for the builder and all created components.
func (*MongoBuilder) UseTlsConfig ¶
func (b *MongoBuilder) UseTlsConfig(v *tls.Config) *MongoBuilder
UseTlsConfig sets the TLS configuration for the MongoDB connection.