remongo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2025 License: MIT Imports: 4 Imported by: 0

README

REMONGO

REMONGO - Simple and Extendable Repository Pattern with Go and Mongodb.

Using the repository pattern in Go, especially with the Mongodb package (go.mongodb.org/mongo-driver), can help maintain a clean and organized data layer that is easy to extend. The repository pattern abstracts database operations, allowing you to handle CRUD functions and isolate data access from business logic.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBson

func ToBson(v interface{}) (doc *bson.D, err error)

Types

type IMongoModel

type IMongoModel interface {
	Collection() string
}

type IMongoRepository

type IMongoRepository[T IMongoModel] interface {
	GetDB() *mongo.Database
	FindOne(model *T, filter interface{}, opts ...*options.FindOneOptions) error
	Find(
		models []*T,
		filter interface{},
		aggregate interface{},
		opts ...*options.FindOptions,
	) error
	InsertOne(model *T, opts ...*options.InsertOneOptions) (error, interface{})
	InsertMany(models *[]T, opts ...*options.InsertManyOptions) (error, interface{})
	ReplaceOne(filter interface{}, model *T, opts ...*options.ReplaceOptions) (error, int64)
	UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (error, int64)
	UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (error, int64)
	DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (error, int64)
	DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (error, int64)
}

func InitRepository

func InitRepository[T IMongoModel](database *mongo.Database, model IMongoModel) IMongoRepository[T]

type MongoRepository

type MongoRepository[T IMongoModel] struct {
	IMongoRepository[T]
	Model    T
	Database *mongo.Database
}

func (*MongoRepository[T]) DeleteMany

func (mr *MongoRepository[T]) DeleteMany(
	filter interface{},
	opts ...*options.DeleteOptions,
) (error, int64)

func (*MongoRepository[T]) DeleteOne

func (mr *MongoRepository[T]) DeleteOne(
	filter interface{},
	opts ...*options.DeleteOptions,
) (error, int64)

func (*MongoRepository[T]) Find

func (mr *MongoRepository[T]) Find(
	models []*T,
	filter interface{},
	aggregate interface{},
	opts ...*options.FindOptions,
) error

func (*MongoRepository[T]) FindOne

func (mr *MongoRepository[T]) FindOne(
	model *T,
	filter interface{},
	opts ...*options.FindOneOptions,
) error

func (*MongoRepository[T]) GetCollection

func (mr *MongoRepository[T]) GetCollection() *mongo.Collection

func (*MongoRepository[T]) GetDB

func (mr *MongoRepository[T]) GetDB() *mongo.Database

func (*MongoRepository[T]) InsertMany

func (mr *MongoRepository[T]) InsertMany(
	models *[]T,
	opts ...*options.InsertManyOptions,
) (error, interface{})

func (*MongoRepository[T]) InsertOne

func (mr *MongoRepository[T]) InsertOne(
	model *T,
	opts ...*options.InsertOneOptions,
) (error, interface{})

func (*MongoRepository[T]) ReplaceOne

func (mr *MongoRepository[T]) ReplaceOne(
	filter interface{},
	model *T,
	opts ...*options.ReplaceOptions,
) (error, int64)

func (*MongoRepository[T]) UpdateMany

func (mr *MongoRepository[T]) UpdateMany(
	filter interface{},
	update interface{},
	opts ...*options.UpdateOptions,
) (error, int64)

func (*MongoRepository[T]) UpdateOne

func (mr *MongoRepository[T]) UpdateOne(
	filter interface{},
	update interface{},
	opts ...*options.UpdateOptions,
) (error, int64)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL