db

package
v0.0.0-...-e71a76f Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUniqueConstraint = errors.New("unique constraint error")

Functions

func Add

func Add(data *bson.D, key string, value interface{})

func AddExcludeDeletedFilter

func AddExcludeDeletedFilter(filter bson.D) bson.D

func AddIfNotNil

func AddIfNotNil(data *bson.D, key string, value interface{})

func CountDistinctResources

func CountDistinctResources(collection *mongo.Collection, field string, filter bson.D) (int, error)

func CountResources

func CountResources(collection *mongo.Collection, filter bson.D) (int, error)

func CreateIfUniqueResource

func CreateIfUniqueResource[T Resource](collection *mongo.Collection, id string, data *T, filter bson.D) error

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 GetResource[T Resource](collection *mongo.Collection, filter bson.D, projection bson.D) (*T, error)

func GroupFilters

func GroupFilters(filter bson.D, extraFilter bson.M, searchParams *SearchParams, includeDeleted bool) bson.D

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

func UpdateOneResource(collection *mongo.Collection, filter bson.D, update bson.D) error

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.

type Pagination

type Pagination struct {
	Page     int
	PageSize int
}

type Resource

type Resource interface {
}

type SearchParams

type SearchParams struct {
	Query  string   `json:"query"`
	Fields []string `json:"fields"`
}

type SortBy

type SortBy struct {
	Field string
	Order int
}

Jump to

Keyboard shortcuts

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