db

package
v0.0.0-...-dd4be11 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package db provides MongoDB database operations with context integration and caching support.

Index

Constants

View Source
const (
	// DatabaseContextKey is the context key for accessing the MongoDB database instance.
	DatabaseContextKey = "database"
	// CollectionContextKey is the context key for accessing the collection name.
	CollectionContextKey = "collection"
)

Variables

View Source
var ErrDecodeNotPointer = wlerrors.New("decode: value must be a pointer")

ErrDecodeNotPointer indicates that a decode operation was attempted with a non-pointer value.

View Source
var ErrNoDatabase = wlerrors.New("context is not a DatabaseContext")

ErrNoDatabase indicates that the context does not contain a database instance.

Functions

func ConnectToMongo

func ConnectToMongo(ctx context.Context, mongoURI, mongoDbName string) (*mongo.Database, error)

ConnectToMongo establishes a connection to MongoDB with automatic retries and context-aware cleanup.

func InstallTransactionHook

func InstallTransactionHook(hook TransactionHook)

InstallTransactionHook registers a hook to be called before each transaction callback.

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists checks if an error is an AlreadyExistsError.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if an error is a NotFoundError.

func NewAlreadyExistsError

func NewAlreadyExistsError(message string) error

NewAlreadyExistsError creates a new AlreadyExistsError with the given message.

func NewNotFoundError

func NewNotFoundError(message string) error

NewNotFoundError creates a new NotFoundError with the given message.

func SetupTestDB

func SetupTestDB(t *testing.T, collectionKey string, indexModels ...mongo.IndexModel) context.Context

SetupTestDB creates a test database context with a clean collection and optional indexes.

func SetupTestDBWithCache

func SetupTestDBWithCache(t *testing.T, collectionKey string, indexModels ...mongo.IndexModel) context.Context

SetupTestDBWithCache creates a test database context with caching enabled. This returns a context that implements context_mod.Z so that the ContextualizedCollection caching code path can be exercised.

func WithTransaction

func WithTransaction(ctx context.Context, fn func(ctx context.Context) error) error

WithTransaction executes a function within a database transaction.

func WrapError

func WrapError(err error, format string, a ...any) error

WrapError wraps a database error with additional context and converts it to a domain-specific error type.

Types

type AlreadyExistsError

type AlreadyExistsError struct {
	// Message provides additional context about the error.
	Message string
}

AlreadyExistsError represents a generic "already exists" conflict error in the database.

func (*AlreadyExistsError) Error

func (e *AlreadyExistsError) Error() string

Error implements the error interface for AlreadyExistsError.

type ContextualizedCollection

type ContextualizedCollection[T any] struct {
	// contains filtered or unexported fields
}

ContextualizedCollection wraps a MongoDB collection with context for logging and caching.

func GetCollection

func GetCollection[T any](ctx context.Context, collectionName string) (*ContextualizedCollection[T], error)

GetCollection retrieves a contextualized collection from the database in the context.

func (*ContextualizedCollection[T]) Aggregate

func (c *ContextualizedCollection[T]) Aggregate(_ context.Context, pipeline any, opts ...*options.AggregateOptions) (*mongo.Cursor, error)

Aggregate executes an aggregation pipeline and returns a cursor with the results.

func (*ContextualizedCollection[T]) CountDocuments

func (c *ContextualizedCollection[T]) CountDocuments(_ context.Context, filter any, opts ...*options.CountOptions) (int64, error)

CountDocuments counts the number of documents matching the filter.

func (*ContextualizedCollection[T]) DeleteMany

func (c *ContextualizedCollection[T]) DeleteMany(_ context.Context, filter any, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteMany deletes all documents matching the filter.

func (*ContextualizedCollection[T]) DeleteOne

func (c *ContextualizedCollection[T]) DeleteOne(_ context.Context, filter any, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteOne deletes a single document matching the filter.

func (*ContextualizedCollection[T]) Drop

Drop removes the entire collection and invalidates the cache.

func (*ContextualizedCollection[T]) Find

func (c *ContextualizedCollection[T]) Find(_ context.Context, filter any, opts ...*options.FindOptions) (*mongo.Cursor, error)

Find finds all documents matching the filter and returns a cursor.

func (*ContextualizedCollection[T]) FindOne

func (c *ContextualizedCollection[T]) FindOne(_ context.Context, filter any, opts ...*options.FindOneOptions) Decoder[T]

FindOne finds a single document matching the filter with cache support.

func (*ContextualizedCollection[T]) FindOneAs

func (c *ContextualizedCollection[T]) FindOneAs(_ context.Context, filter any, opts ...*options.FindOneOptions) (T, error)

FindOneAs finds a single document matching the filter and decodes it into the result type.

func (*ContextualizedCollection[T]) GetCollection

func (c *ContextualizedCollection[T]) GetCollection() *mongo.Collection

GetCollection returns the underlying MongoDB collection.

func (*ContextualizedCollection[T]) InsertMany

func (c *ContextualizedCollection[T]) InsertMany(_ context.Context, documents []any, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

InsertMany inserts multiple documents into the collection.

func (*ContextualizedCollection[T]) InsertOne

func (c *ContextualizedCollection[T]) InsertOne(_ context.Context, document any, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

InsertOne inserts a single document into the collection and invalidates the cache.

func (*ContextualizedCollection[T]) NewIndex

NewIndex creates a new index on the collection if it does not already exist.

func (*ContextualizedCollection[T]) NewSearchIndex

NewSearchIndex creates a new search index on the collection if it does not already exist.

func (*ContextualizedCollection[T]) ReplaceOne

func (c *ContextualizedCollection[T]) ReplaceOne(_ context.Context, filter, replacement any, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)

ReplaceOne replaces a single document matching the filter and invalidates the cache.

func (*ContextualizedCollection[T]) UpdateMany

func (c *ContextualizedCollection[T]) UpdateMany(_ context.Context, filter, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateMany updates all documents matching the filter and invalidates the cache.

func (*ContextualizedCollection[T]) UpdateOne

func (c *ContextualizedCollection[T]) UpdateOne(_ context.Context, filter, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateOne updates a single document matching the filter and invalidates the cache.

type DB

type DB interface {
	mongo.Database
}

DB is an interface that wraps the mongo.Database type.

type Decoder

type Decoder[T any] interface {
	Decode(v T) error
	Err() error
}

Decoder provides an interface for decoding database results into typed values.

type NotFoundError

type NotFoundError struct {
	// Message provides additional context about the error.
	Message string
}

NotFoundError represents a generic "not found" error in the database.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

Error implements the error interface for NotFoundError.

func (*NotFoundError) Status

func (e *NotFoundError) Status() int

Status returns the HTTP status code for a NotFoundError.

type TransactionHook

type TransactionHook func(context.Context) context.Context

TransactionHook modifies the context before a transaction callback is executed.

Jump to

Keyboard shortcuts

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