Documentation
¶
Overview ¶
Package db provides MongoDB database operations with context integration and caching support.
Index ¶
- Constants
- Variables
- func ConnectToMongo(ctx context.Context, mongoURI, mongoDbName string) (*mongo.Database, error)
- func InstallTransactionHook(hook TransactionHook)
- func IsAlreadyExists(err error) bool
- func IsNotFound(err error) bool
- func NewAlreadyExistsError(message string) error
- func NewNotFoundError(message string) error
- func SetupTestDB(t *testing.T, collectionKey string, indexModels ...mongo.IndexModel) context.Context
- func SetupTestDBWithCache(t *testing.T, collectionKey string, indexModels ...mongo.IndexModel) context.Context
- func WithTransaction(ctx context.Context, fn func(ctx context.Context) error) error
- func WrapError(err error, format string, a ...any) error
- type AlreadyExistsError
- type ContextualizedCollection
- func (c *ContextualizedCollection[T]) Aggregate(_ context.Context, pipeline any, opts ...*options.AggregateOptions) (*mongo.Cursor, error)
- func (c *ContextualizedCollection[T]) CountDocuments(_ context.Context, filter any, opts ...*options.CountOptions) (int64, error)
- func (c *ContextualizedCollection[T]) DeleteMany(_ context.Context, filter any, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (c *ContextualizedCollection[T]) DeleteOne(_ context.Context, filter any, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (c *ContextualizedCollection[T]) Drop(ctx context.Context) error
- func (c *ContextualizedCollection[T]) Find(_ context.Context, filter any, opts ...*options.FindOptions) (*mongo.Cursor, error)
- func (c *ContextualizedCollection[T]) FindOne(_ context.Context, filter any, opts ...*options.FindOneOptions) Decoder[T]
- func (c *ContextualizedCollection[T]) FindOneAs(_ context.Context, filter any, opts ...*options.FindOneOptions) (T, error)
- func (c *ContextualizedCollection[T]) GetCollection() *mongo.Collection
- func (c *ContextualizedCollection[T]) InsertMany(_ context.Context, documents []any, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
- func (c *ContextualizedCollection[T]) InsertOne(_ context.Context, document any, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
- func (c *ContextualizedCollection[T]) NewIndex(model mongo.IndexModel, opts ...*options.CreateIndexesOptions) error
- func (c *ContextualizedCollection[T]) NewSearchIndex(model mongo.SearchIndexModel, opts ...*options.CreateSearchIndexesOptions) error
- func (c *ContextualizedCollection[T]) ReplaceOne(_ context.Context, filter, replacement any, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
- func (c *ContextualizedCollection[T]) UpdateMany(_ context.Context, filter, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
- func (c *ContextualizedCollection[T]) UpdateOne(_ context.Context, filter, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
- type DB
- type Decoder
- type NotFoundError
- type TransactionHook
Constants ¶
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 ¶
var ErrDecodeNotPointer = wlerrors.New("decode: value must be a pointer")
ErrDecodeNotPointer indicates that a decode operation was attempted with a non-pointer value.
var ErrNoDatabase = wlerrors.New("context is not a DatabaseContext")
ErrNoDatabase indicates that the context does not contain a database instance.
Functions ¶
func ConnectToMongo ¶
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 ¶
IsAlreadyExists checks if an error is an AlreadyExistsError.
func NewAlreadyExistsError ¶
NewAlreadyExistsError creates a new AlreadyExistsError with the given message.
func NewNotFoundError ¶
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 ¶
WithTransaction executes a function within a database transaction.
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 ¶
func (c *ContextualizedCollection[T]) Drop(ctx context.Context) error
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 ¶
func (c *ContextualizedCollection[T]) NewIndex(model mongo.IndexModel, opts ...*options.CreateIndexesOptions) error
NewIndex creates a new index on the collection if it does not already exist.
func (*ContextualizedCollection[T]) NewSearchIndex ¶
func (c *ContextualizedCollection[T]) NewSearchIndex(model mongo.SearchIndexModel, opts ...*options.CreateSearchIndexesOptions) error
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 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.