factory

package
v0.0.0-...-b788499 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 15 Imported by: 0

README

factory

import "github.com/altessa-s/go-atlas/infrastructure/mongo/factory"

Package factory provides a fluent builder for creating MongoDB clients from configuration. MongoBuilder uses deferred error accumulation — errors from any step are collected and returned at Build() time.

Quick Start

m, err := factory.New(cfg.Mongodb).
    UseLogger(logger).
    UseTlsConfig(tlsConfig).
    UseHealthCoordinator(healthCoordinator).
    Build(ctx)
if err != nil {
    return err
}
if err := m.Connect(ctx); err != nil {
    return err
}

The returned *mongo.Mongo is not yet connected — call Connect to establish the connection.

Authentication

Mechanism Config field Description
SCRAM-SHA-1 Credentials.Scram Username, password, and auth source
SCRAM-SHA-256 Credentials.Scram Same fields, stronger hash
X.509 Credentials.AuthMechanism Certificate-based, no username/password needed
PLAIN Credentials.Plain LDAP proxy authentication

Methods

Constructor
Method Description
New(cfg) Creates a MongoBuilder for the given MongoDB config
Dependencies
Method Description
UseLogger Sets the logger for the builder and all created components
UseTlsConfig Sets the TLS configuration for secure connections
UseKmsProvider Sets the KMS provider for client-side field level encryption
UseHealthCoordinator Registers a health checker under service name "mongo"
UseConverterOptions Forwards extra converter.Options to mongo.WithConverterOptions (codecs)
Terminal
Method Description
Build(ctx) Assembles and returns the *mongo.Mongo wrapper (not yet connected)

Configuration Modes

When ConnectionURI is set, ApplyURI is used as the base and pool/timeout/retry/TLS fields are layered on top. Otherwise, options are built from individual config fields including hosts, credentials, replica set, compressors, and direct connection flag.

When Encryption is configured, CSFLE auto-encryption is applied in bypass mode so reads transparently decrypt while writes use the explicit encryption path.

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

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

func (b *MongoBuilder) Build(_ context.Context) (*mongo.Mongo, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL