Documentation
¶
Index ¶
- Variables
- func Add(data *bson.D, key string, value interface{})
- func AddExcludeDeletedFilter(filter bson.D) bson.D
- func AddIfNotNil(data *bson.D, key string, value interface{})
- func CountDistinctResources(collection *mongo.Collection, field string, filter bson.D) (int, error)
- func CountResources(collection *mongo.Collection, filter bson.D) (int, error)
- func CreateIfUniqueResource[T Resource](collection *mongo.Collection, id string, data *T, filter bson.D) error
- func DeleteResources(collection *mongo.Collection, filter bson.D) error
- func GetCollectionDefinitions() map[string]CollectionDefinition
- func GetResource[T Resource](collection *mongo.Collection, filter bson.D, projection bson.D) (*T, error)
- func GroupFilters(filter bson.D, extraFilter bson.M, searchParams *SearchParams, ...) bson.D
- func ListResources[T any](collection *mongo.Collection, filter bson.D, projection bson.D, ...) ([]T, error)
- func Setup() error
- func Shutdown()
- func UpdateOneResource(collection *mongo.Collection, filter bson.D, update bson.D) error
- type CollectionDefinition
- type Context
- type Pagination
- type Resource
- type SearchParams
- type SortBy
Constants ¶
This section is empty.
Variables ¶
var ErrUniqueConstraint = errors.New("unique constraint error")
Functions ¶
func AddIfNotNil ¶
func CountDistinctResources ¶
func CountResources ¶
func CreateIfUniqueResource ¶
func DeleteResources ¶
func DeleteResources(collection *mongo.Collection, filter bson.D) error
func GetCollectionDefinitions ¶
func GetCollectionDefinitions() map[string]CollectionDefinition
GetCollectionDefinitions returns a map of all collections and their definitions, including any indexes that should be created on the collection.
func GetResource ¶
func GroupFilters ¶
func ListResources ¶
func ListResources[T any](collection *mongo.Collection, filter bson.D, projection bson.D, pagination *Pagination, sortBy *SortBy) ([]T, error)
func Setup ¶
func Setup() error
Setup initializes the database context. It should be called once at the start of the application.
func Shutdown ¶
func Shutdown()
Shutdown closes the database connections. It should be called once at the end of the application.
func UpdateOneResource ¶
Types ¶
type CollectionDefinition ¶
type CollectionDefinition struct { Name string Indexes []string // unique only for non-deleted documents UniqueIndexes [][]string // unique even for deleted documents TotallyUniqueIndexes [][]string TextIndexFields []string }
CollectionDefinition is a struct that defines a collection. It is used as an abstraction layer between MongoDB and the application. It contains the collection name, as well as any indexes that should be created on the collection.
type Context ¶
type Context struct { MongoClient *mongo.Client RedisClient *redis.Client CollectionMap map[string]*mongo.Collection CollectionDefinitionMap map[string]CollectionDefinition }
Context is the database context for the application. It contains the mongo client and redis client, as well as a map of all collections and their definitions. It is used as a singleton, and should be initialized with the Setup() function.
var DB Context
func (*Context) GetCollection ¶
func (dbCtx *Context) GetCollection(collectionName string) *mongo.Collection
GetCollection returns the collection with the given name. If the collection is not found, the application will exit.