Add method to retrieve collection

This commit is contained in:
Anton Zadvorny 2024-04-08 05:14:12 +03:00
parent 947d01e7df
commit a5822f1508
1 changed files with 6 additions and 0 deletions

View File

@ -12,6 +12,8 @@ import (
) )
type RepositoryInterface[T Document] interface { type RepositoryInterface[T Document] interface {
Collection() *mongo.Collection
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]T, error) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]T, error)
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (T, error) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (T, error)
FindByID(ctx context.Context, id string, opts ...*options.FindOneOptions) (T, error) FindByID(ctx context.Context, id string, opts ...*options.FindOneOptions) (T, error)
@ -36,6 +38,10 @@ func NewRepository[T Document](collection *mongo.Collection, indexes []mongo.Ind
return &Repository[T]{collection: collection, indexes: withTimestampIndexes(indexes)} return &Repository[T]{collection: collection, indexes: withTimestampIndexes(indexes)}
} }
func (s *Repository[T]) Collection() *mongo.Collection {
return s.collection
}
func (s *Repository[T]) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]T, error) { func (s *Repository[T]) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]T, error) {
cursor, err := s.collection.Find(ctx, filter, opts...) cursor, err := s.collection.Find(ctx, filter, opts...)
if err != nil { if err != nil {