Documentation ¶
Index ¶
- func AddToSet(exp map[string]interface{}) bson.M
- func ArrayToObject(exp interface{}) bson.M
- func Each(values interface{}) bson.M
- func First(exp interface{}) bson.M
- func IncludeIndex(index string) unwindOptsFunc
- func Map(input, as, k, v string) bson.M
- func PreserveNullEmpty(preserve bool) unwindOptsFunc
- func Push(exp ...interface{}) bson.M
- type BaseModel
- type BaseRepository
- func (r *BaseRepository[T]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, result interface{}) error
- func (r *BaseRepository[T]) Delete(ctx context.Context, filter interface{}) (int64, error)
- func (r *BaseRepository[T]) Find(ctx context.Context, filter interface{}) ([]T, error)
- func (r *BaseRepository[T]) FindOne(ctx context.Context, filter interface{}) (T, error)
- func (r *BaseRepository[T]) InsertMany(ctx context.Context, documents []T) error
- func (r *BaseRepository[T]) InsertOne(ctx context.Context, document T) error
- func (r *BaseRepository[Model]) ReplaceOne(ctx context.Context, filter interface{}, replacement Model) error
- func (r *BaseRepository[T]) UpdateOne(ctx context.Context, filters interface{}, update interface{}) (T, error)
- type Model
- type MongoClient
- type MongoRepository
- type StageBuilder
- func (pb *StageBuilder) AddStage(id string, stageType string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Append(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Bucket(id string, groupBy string, boundaries []any, def any, output bson.M) *StageBuilder
- func (pb *StageBuilder) Build() mongo.Pipeline
- func (pb *StageBuilder) Count(id string, filter string) *StageBuilder
- func (pb *StageBuilder) Facet(id string, aggregations map[string]*StageBuilder) *StageBuilder
- func (pb *StageBuilder) Group(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Limit(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Lookup(id, from, localField, foreignField, as string) *StageBuilder
- func (pb *StageBuilder) Match(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Project(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Skip(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Sort(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) SortByCount(id string, expr interface{}) *StageBuilder
- func (pb *StageBuilder) Stages() []*stage
- func (pb *StageBuilder) Unwind(id, fieldPath string, opts ...unwindOptsFunc) *StageBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArrayToObject ¶ added in v0.3.0
func IncludeIndex ¶ added in v0.2.0
func IncludeIndex(index string) unwindOptsFunc
func PreserveNullEmpty ¶ added in v0.2.0
func PreserveNullEmpty(preserve bool) unwindOptsFunc
Types ¶
type BaseModel ¶
type BaseModel struct { // ID must have bson tag `omitempty` to allow the document to either have a database generated ID or // a custom one, and being elegible for `ReplaceOne`. See method `ReplaceOne` in `BaseRepository` for more info. ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` CreatedAt time.Time `json:"createdAt" bson:"createdAt"` UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` }
func NewBaseModel ¶
func NewBaseModel() *BaseModel
type BaseRepository ¶
type BaseRepository[T Model] struct { // contains filtered or unexported fields }
BaseRepository is a base implementation of the MongoRepository interface.
func NewBaseRepository ¶
func NewBaseRepository[T Model](db *mongo.Database, collectionName string, t T) *BaseRepository[T]
NewBaseRepository creates a new instance of BaseRepository.
func (*BaseRepository[T]) Aggregate ¶
func (r *BaseRepository[T]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, result interface{}) error
Aggregate runs an aggregation framework pipeline on the collection.
func (*BaseRepository[T]) Delete ¶
func (r *BaseRepository[T]) Delete(ctx context.Context, filter interface{}) (int64, error)
Delete deletes multiple documents from the collection.
func (*BaseRepository[T]) Find ¶
func (r *BaseRepository[T]) Find(ctx context.Context, filter interface{}) ([]T, error)
Find finds multiple documents in the collection.
func (*BaseRepository[T]) FindOne ¶
func (r *BaseRepository[T]) FindOne(ctx context.Context, filter interface{}) (T, error)
FindOne finds a single document in the collection.
func (*BaseRepository[T]) InsertMany ¶
func (r *BaseRepository[T]) InsertMany(ctx context.Context, documents []T) error
InsertMany inserts multiple documents into the collection.
func (*BaseRepository[T]) InsertOne ¶
func (r *BaseRepository[T]) InsertOne(ctx context.Context, document T) error
InsertOne inserts a single document into the collection.
The document parameter must be a pointer to a struct that implements the Model interface.
func (*BaseRepository[Model]) ReplaceOne ¶
func (r *BaseRepository[Model]) ReplaceOne(ctx context.Context, filter interface{}, replacement Model) error
ReplaceOne replaces a single document in the collection. Replaced document must have the same ID as the one being replaced or not have it serializible at all. It is strongly suggested to have the ID field with the `omitempty` bson tag in case of structs.
type MongoClient ¶
type MongoClient struct {
// contains filtered or unexported fields
}
MongoClient is a struct to manage the database connection
func GetInstance ¶
func GetInstance() *MongoClient
GetInstance returns the singleton instance of DatabaseConnection
func SetInstance ¶
func SetInstance(uri string) *MongoClient
SetInstance initializes a new database connection
func (*MongoClient) Client ¶
func (c *MongoClient) Client() *mongo.Client
func (*MongoClient) Connect ¶
func (c *MongoClient) Connect() error
Connect opens a connection to the database
func (*MongoClient) Disconnect ¶
func (c *MongoClient) Disconnect() error
type MongoRepository ¶
type MongoRepository[T Model] interface { // InsertOne inserts a single document into the collection. InsertOne(ctx context.Context, document Model) error // InsertMany inserts multiple documents into the collection. InsertMany(ctx context.Context, documents []Model) error // FindOne finds a single document in the collection. FindOne(ctx context.Context, filter interface{}) (Model, error) // Find finds multiple documents in the collection. Find(ctx context.Context, filter interface{}) ([]Model, error) // UpdateOne finds a single document and updates it. UpdateOne(ctx context.Context, filters interface{}, update interface{}) (Model, error) // Delete deletes multiple documents from the collection. Delete(ctx context.Context, filter interface{}) (int64, error) // ReplaceOne replaces a single document in the collection. ReplaceOne(ctx context.Context, filter interface{}, replacement T) error // Aggregate runs an aggregation framework pipeline on the collection. Aggregate(ctx context.Context, pipeline mongo.Pipeline, result interface{}) error }
MongoRepository is an interface that defines the methods for interacting with a MongoDB collection.
type StageBuilder ¶
type StageBuilder struct {
// contains filtered or unexported fields
}
func NewStageBuilder ¶
func NewStageBuilder() *StageBuilder
func (*StageBuilder) AddStage ¶
func (pb *StageBuilder) AddStage(id string, stageType string, filters interface{}) *StageBuilder
func (*StageBuilder) Append ¶
func (pb *StageBuilder) Append(id string, filters interface{}) *StageBuilder
func (*StageBuilder) Bucket ¶ added in v0.2.0
func (pb *StageBuilder) Bucket(id string, groupBy string, boundaries []any, def any, output bson.M) *StageBuilder
Bucket categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries and outputs a document per each bucket. Each output document contains an _id field whose value specifies the inclusive lower bound of the bucket. The output option specifies the fields included in each output document.
$bucket only produces output documents for buckets that contain at least one input document.
func (*StageBuilder) Build ¶
func (pb *StageBuilder) Build() mongo.Pipeline
func (*StageBuilder) Count ¶
func (pb *StageBuilder) Count(id string, filter string) *StageBuilder
func (*StageBuilder) Facet ¶ added in v0.2.0
func (pb *StageBuilder) Facet(id string, aggregations map[string]*StageBuilder) *StageBuilder
func (*StageBuilder) Group ¶
func (pb *StageBuilder) Group(id string, filters interface{}) *StageBuilder
func (*StageBuilder) Limit ¶
func (pb *StageBuilder) Limit(id string, filters interface{}) *StageBuilder
func (*StageBuilder) Lookup ¶
func (pb *StageBuilder) Lookup(id, from, localField, foreignField, as string) *StageBuilder
func (*StageBuilder) Match ¶
func (pb *StageBuilder) Match(id string, filters interface{}) *StageBuilder
func (*StageBuilder) Project ¶
func (pb *StageBuilder) Project(id string, filters interface{}) *StageBuilder
func (*StageBuilder) Skip ¶
func (pb *StageBuilder) Skip(id string, filters interface{}) *StageBuilder
func (*StageBuilder) Sort ¶
func (pb *StageBuilder) Sort(id string, filters interface{}) *StageBuilder
func (*StageBuilder) SortByCount ¶ added in v0.2.0
func (pb *StageBuilder) SortByCount(id string, expr interface{}) *StageBuilder
func (*StageBuilder) Stages ¶
func (pb *StageBuilder) Stages() []*stage
func (*StageBuilder) Unwind ¶ added in v0.2.0
func (pb *StageBuilder) Unwind(id, fieldPath string, opts ...unwindOptsFunc) *StageBuilder