Documentation ¶
Index ¶
- Variables
- func NewMongoRepository[T any](db *mongo.Database, collectionName string) *mongoRepository[T]
- type FilterFunc
- func And(filters ...FilterFunc) FilterFunc
- func Eq(field string, value interface{}) FilterFunc
- func Exists(field string, exists bool) FilterFunc
- func Gt(field string, value interface{}) FilterFunc
- func Gte(field string, value interface{}) FilterFunc
- func In(field string, values interface{}) FilterFunc
- func Lt(field string, value interface{}) FilterFunc
- func Lte(field string, value interface{}) FilterFunc
- func Ne(field string, value interface{}) FilterFunc
- func Or(filters ...FilterFunc) FilterFunc
- func Regex(field string, pattern string, options string) FilterFunc
- func TextSearch(searchTerm string) FilterFunc
- type IndexOption
- func ExpireAfterSeconds(expireAfterSeconds int32) IndexOption
- func Name(name string) IndexOption
- func PartialFilterExpression(partialFilterExpression interface{}) IndexOption
- func SetCollation(collation *options.Collation) IndexOption
- func SetHidden(hidden bool) IndexOption
- func SetWildcardProjection(wildcardProjection interface{}) IndexOption
- func Sparse(sparse bool) IndexOption
- func TTL(duration time.Duration) IndexOption
- func TextIndex(config TextIndexConfig) IndexOption
- func Unique(unique bool) IndexOption
- type Repository
- type TextIndexConfig
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("document not found") ErrDuplicate = errors.New("document already exists") ErrFailedToFindByID = errors.New("failed to find document by id") ErrFailedToFindByIDs = errors.New("failed to find documents by ids") ErrInvalidDocumentID = errors.New("invalid document id") ErrFailedToCreate = errors.New("failed to create document") ErrFailedToUpdate = errors.New("failed to update document") ErrFailedToUpdateMany = errors.New("failed to update documents") ErrFailedToDelete = errors.New("failed to delete document") ErrFailedToFindOneByFilter = errors.New("failed to find a document by the given filter") ErrFailedToFindManyByFilter = errors.New("failed to find any documents by the given filter") ErrFailedToCreateIndex = errors.New("failed to create collection index") ErrFailedToDeleteMany = errors.New("failed to delete documents") )
Predefined errors
Functions ¶
func NewMongoRepository ¶
NewMongoRepository creates a new instance of the mongoRepository[T] struct. It takes a mongo.Database and a collectionName as parameters and returns a pointer to the mongoRepository[T] struct. The mongoRepository[T] struct represents a repository for working with a specific MongoDB collection. The collection field of the struct is initialized with the specified collectionName from the provided database.
Types ¶
type FilterFunc ¶
FilterFunc is a function type that takes a BSON document and modifies it.
func And ¶
func And(filters ...FilterFunc) FilterFunc
And combines multiple filters with a logical AND
func Gte ¶
func Gte(field string, value interface{}) FilterFunc
Gte creates a greater-than-or-equal filter
func Lte ¶
func Lte(field string, value interface{}) FilterFunc
Lte creates a less-than-or-equal filter
func Regex ¶
func Regex(field string, pattern string, options string) FilterFunc
Regex creates a filter for regular expression matching
func TextSearch ¶
func TextSearch(searchTerm string) FilterFunc
TextSearch creates a full-text search filter
type IndexOption ¶
type IndexOption func(*options.IndexOptions)
IndexOption wraps the MongoDB IndexOptions for extensibility and ease of use
func ExpireAfterSeconds ¶
func ExpireAfterSeconds(expireAfterSeconds int32) IndexOption
ExpireAfterSeconds specifies the expireAfterSeconds option for an index
func PartialFilterExpression ¶
func PartialFilterExpression(partialFilterExpression interface{}) IndexOption
PartialFilterExpression specifies the partialFilterExpression option for an index
func SetCollation ¶
func SetCollation(collation *options.Collation) IndexOption
SetCollation specifies the collation option for an index
func SetHidden ¶
func SetHidden(hidden bool) IndexOption
SetHidden specifies the hidden option for an index
func SetWildcardProjection ¶
func SetWildcardProjection(wildcardProjection interface{}) IndexOption
SetWildcardProjection specifies the wildcardProjection option for an index
func TTL ¶ added in v0.3.0
func TTL(duration time.Duration) IndexOption
TTL sets the Time-To-Live for an index
func TextIndex ¶ added in v0.3.0
func TextIndex(config TextIndexConfig) IndexOption
TextIndex creates a text index option with specified fields and weights
type Repository ¶
type Repository[T any] interface { // CreateIndex creates an index in the MongoDB collection based on the specified key and options. // It takes a context.Context as the first argument, the key for the index as the second argument, // and optional IndexOption(s) as the third argument(s). // The function returns an error if the index creation fails. CreateIndex(ctx context.Context, key interface{}, opts ...IndexOption) error // Create inserts a new document into the MongoDB collection. // It takes a context.Context and a model of type T as input parameters. // It returns the ID of the newly created document as a string and an error, if any. Create(ctx context.Context, model T) (string, error) // FindByID retrieves a document from the MongoDB collection by its ID. // It takes a context.Context and the ID of the document as parameters. // It returns the retrieved document of type T and an error, if any. FindByID(ctx context.Context, id string) (T, error) // FindByIDs retrieves multiple documents from the MongoDB collection by their IDs. // It takes a context.Context and a slice of IDs as parameters. // It returns a slice of documents of type T and an error, if any. FindByIDs(ctx context.Context, ids ...string) ([]T, error) // Update updates a document in the MongoDB collection with the specified ID. // It takes a context, ID string, and model as input parameters. // It returns the number of modified documents and an error, if any. Update(ctx context.Context, id string, model T) (int64, error) // UpdateMany updates multiple documents in the MongoDB collection based on the provided filters. // It takes a context.Context, a map of update fields, and optional filter functions as parameters. // The update fields specify the changes to be made to the documents. // The filter functions are used to build the filter for selecting the documents to be updated. // It returns the number of documents modified and an error if any. UpdateMany(ctx context.Context, update map[string]interface{}, filters ...FilterFunc) (int64, error) // Delete deletes a document from the MongoDB collection based on the provided ID. // It returns the number of deleted documents and an error, if any. Delete(ctx context.Context, id string) (int64, error) // DeleteMany deletes multiple documents from the MongoDB collection based on the provided filters. // It returns the number of deleted documents and an error, if any. DeleteMany(ctx context.Context, filters ...FilterFunc) (int64, error) // FindManyByFilter retrieves multiple documents from the collection based on the provided filters. // It allows skipping a certain number of documents and limiting the number of documents to be returned. // The filters are applied in the order they are passed. // If no documents match the filters, it returns an error with the ErrNotFound error code. // If an error occurs during the retrieval process, it returns an error with the ErrFailedToFindManyByFilter error code. // The function returns a slice of documents of type T and an error. FindManyByFilter(ctx context.Context, skip int64, limit int64, filters ...FilterFunc) ([]T, error) // FindOneByFilter finds a single document in the collection based on the provided filters. // It accepts one or more FilterFunc functions that modify the filter criteria. // The function returns the found document of type T and an error, if any. // If no document is found, it returns an error of type ErrNotFound. // If an error occurs during the find operation, it returns the error. FindOneByFilter(ctx context.Context, filters ...FilterFunc) (T, error) // Exists checks if a document exists in the collection based on the provided filters. // It accepts one or more FilterFunc functions that modify the filter criteria. // The function returns true if a document exists and false otherwise. // If an error occurs during the find operation, it returns the error. Exists(ctx context.Context, filters ...FilterFunc) (bool, error) // Count returns the number of documents in the collection based on the provided filters. // It accepts one or more FilterFunc functions that modify the filter criteria. // The function returns the number of documents and an error, if any. Count(ctx context.Context, filters ...FilterFunc) (int64, error) }
Repository is an interface that defines the methods for interacting with a MongoDB collection.
type TextIndexConfig ¶ added in v0.3.0
type TextIndexConfig struct { Fields map[string]int32 // Fields with weights DefaultLang string // Default language for the index Name string // Optional custom name for the index }
TextIndexConfig configures the text index
func NewTextIndexConfig ¶ added in v0.3.0
func NewTextIndexConfig(fields map[string]int32) TextIndexConfig
NewTextIndexConfig creates a new text index config with specified fields and weights Panics if no fields are provided