common

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Client *mongo.Client
)

Functions

func CacheWrapper

func CacheWrapper(ctx context.Context, collectionName string, cacheKey string, filter interface{}, projection interface{}, result interface{}) error

CacheWrapper is a helper function to fetch data from MongoDB and cache the result

func ConnectDatabase

func ConnectDatabase(cfg models.DatabaseConfig) error

func GenerateCacheKey

func GenerateCacheKey(collectionName string, filter interface{}) (string, error)

func GetCollection

func GetCollection(name string) *mongo.Collection

Helper function to simplify getting data. example: clientsCollection := GetCollection("clients")

func InvalidateCache

func InvalidateCache(ctx context.Context, collectionName string, cacheKey string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) error

Cache Invalidation - Deletes the cached entry after updating the database

func UpdateCache

func UpdateCache(ctx context.Context, collectionName string, cacheKey string, filter interface{}, update interface{}, result interface{}) error

UpdateCache is responsible for updating the cache after a successful update in MongoDB

Types

type CollectionInterface

type CollectionInterface interface {
	InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
	FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
	UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (cur *mongo.Cursor, err error)
}

CollectionInterface defines the methods used from mongo.Collection

type SingoleResultInterface

type SingoleResultInterface interface {
	Decode(v interface{}) error
	Err() error
}

CollectionInterface defines the methods used from mongo.Collection

Jump to

Keyboard shortcuts

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