Documentation ¶
Index ¶
- 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) Build() mongo.Pipeline
- func (pb *StageBuilder) Count(id string, filter string) *StageBuilder
- func (pb *StageBuilder) Group(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Limit(id string, filters interface{}) *StageBuilder
- func (pb *StageBuilder) Lookup(id string, filters bson.M) *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) Stages() []*stage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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) Build ¶
func (pb *StageBuilder) Build() mongo.Pipeline
func (*StageBuilder) Count ¶
func (pb *StageBuilder) Count(id string, filter string) *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 string, filters bson.M) *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) Stages ¶
func (pb *StageBuilder) Stages() []*stage