vmdb

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 17 Imported by: 2

Documentation

Overview

Package vmdb

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrDuplicateKey added in v1.5.4

func ErrDuplicateKey(err error) bool

func ErrNoDocuments added in v1.4.1

func ErrNoDocuments(err error) bool

ErrNoDocuments return true if the error is an mongo.ErrNoDocuments error. Else the function returns false.

func UpdateInc added in v1.4.23

func UpdateInc(value interface{}) bson.D

func UpdateSet added in v1.4.6

func UpdateSet(value interface{}) bson.D

UpdateSet set all values in the database.

MongoDB:

{
	"$set": value,
	"$set": {
		"modified.updated": time.Now().Unix(),
	}
}

Types

type Collection

type Collection struct {
	Name         string
	DatabaseName string
	Collection   *mongo.Collection
}

Collection represents an mongo db database collection

var (
	FSChunkCollection *Collection
	FSFileCollection  *Collection
)

func (*Collection) Aggregate

func (i *Collection) Aggregate(ctx context.Context, filter mongo.Pipeline, value interface{}) (err error)

Aggregate use the mongo.Collection.Aggregate function for select a list of elements using an aggregation pipeline.

func (*Collection) AggregateOne added in v1.4.1

func (i *Collection) AggregateOne(ctx context.Context, pipeline mongo.Pipeline, value interface{}) (err error)

AggregateOne use an aggregation pipeline for creating an struct that contains the information from more than one collection. If the result cursor contains objects, the first one will be decoded in the value param.

func (*Collection) CountDocuments added in v1.4.26

func (i *Collection) CountDocuments(ctx context.Context, filter bson.D) (count int64, err error)

CountDocuments count all documents filtered by the filter object.

func (*Collection) CreateIndex

func (i *Collection) CreateIndex(field string, unique bool) *Collection

CreateIndex creates an index for a given collection.

func (*Collection) CreateMultiIndex added in v1.4.0

func (i *Collection) CreateMultiIndex(filter bson.D, unique bool) *Collection

CreateMultiIndex creates an index for an value combination.

func (*Collection) DeleteMany added in v1.4.0

func (i *Collection) DeleteMany(ctx context.Context, filter bson.D) (err error)

DeleteMany deletes all elements from collection they match the filter object.

func (*Collection) DeleteOne

func (i *Collection) DeleteOne(ctx context.Context, filter bson.D) (err error)

DeleteOne deletes an element from given collection by the bson.M filter.

func (*Collection) Find

func (i *Collection) Find(ctx context.Context, filter bson.D, value interface{}, opts ...*options.FindOptions) (err error)

Find use the mongo.Collection.Find function for select a list of elements from a collection. The result will decode in the value param. So the value need to be a slice struct.

func (*Collection) FindAndCount added in v1.5.2

func (i *Collection) FindAndCount(ctx context.Context, filter bson.D, value interface{}, opts ...*options.FindOptions) (listSize int64, err error)

FindAndCount use the mongo.Collection.Find function for select a list of elements from a collection. The result will decode in the value param. So the value need to be a slice struct. The return value listSize counts all elements in the collection that match the given filter.

func (*Collection) FindOne

func (i *Collection) FindOne(ctx context.Context, filter bson.D, value interface{}) (err error)

FindOne use the mongo.Collection.FindOne function for select one element from collection.

func (*Collection) InsertMany added in v1.4.0

func (i *Collection) InsertMany(ctx context.Context, value []interface{}) (err error)

InsertMany inserts a list of value and return an MongoError as error.

func (*Collection) InsertOne added in v1.4.0

func (i *Collection) InsertOne(ctx context.Context, value interface{}) (err error)

InsertOne inserts a value and return an MongoError as error.

func (*Collection) TryDeleteMany added in v1.4.1

func (i *Collection) TryDeleteMany(ctx context.Context, filter bson.D) (err error)

TryDeleteMany returns no error if no element was deleted.

func (*Collection) TryDeleteOne added in v1.4.1

func (i *Collection) TryDeleteOne(ctx context.Context, filter bson.D) (err error)

DeleteOne deletes an element from given collection by the bson.M filter.

func (*Collection) TryUpdateMany added in v1.4.1

func (i *Collection) TryUpdateMany(ctx context.Context, filter bson.A, value bson.M) (err error)

TryUpdateMany returns no error if no models was updated.

func (*Collection) TryUpdateOne added in v1.4.1

func (i *Collection) TryUpdateOne(ctx context.Context, filter bson.D, value interface{}) (err error)

TryUpdateOne returns no error if the model is not updated.

func (*Collection) UpdateMany added in v1.4.0

func (i *Collection) UpdateMany(ctx context.Context, filter bson.D, value bson.D) (err error)

UpdateMany updates an slice of interfaces. @TODO: create an result.

func (*Collection) UpdateOne

func (i *Collection) UpdateOne(ctx context.Context, filter bson.D, value interface{}, result interface{}) (err error)

UpdateOne use the mongo.Collection.UpdateOne function for update one element in an collection. If the result.MatchedCount == 0, the function returns an mongo.ErrNoDocuments error. If the result.MatchedCount != 0, the i.Collection.FindOne function is used to select the updated element and decode it into the result interface.

func (*Collection) UpdateOneAggregate added in v1.4.2

func (i *Collection) UpdateOneAggregate(ctx context.Context, filter bson.D, value interface{}, result interface{}, pipeline mongo.Pipeline) (err error)

UpdateOneAggregate works the same way than UpdateOne but you can define the pipeline param for decode an aggregated model into the result interface.

type CollectionUpdate added in v1.4.46

type CollectionUpdate struct {
	Collection *Collection
}

func NewCollectionUpdate added in v1.4.46

func NewCollectionUpdate(collection *Collection) *CollectionUpdate

func (CollectionUpdate) Check added in v1.4.46

func (i CollectionUpdate) Check(ctx context.Context, name string) bool

func (CollectionUpdate) Insert added in v1.4.46

func (i CollectionUpdate) Insert(ctx context.Context, name string)

type CollectionUpdateModel added in v1.4.46

type CollectionUpdateModel struct {
	ID   string `bson:"_id"`
	Name string `bson:"name"`
}

type Database

type Database struct {
	Name     string //Database Name
	URI      string //Database URI
	Database *mongo.Database
}

Database represents the initial struct for an Mongo Database connection named Name. Database is an pointer to an mongo.Database.

func NewDatabase added in v1.4.0

func NewDatabase(name string) *Database

func (*Database) Collection added in v1.4.0

func (i *Database) Collection(name string) *Collection

Collection initial an new mongodb collection named by the name parameter. Use NewDatabase for initial an database connection.

func (*Database) Connect added in v1.4.6

func (i *Database) Connect() *Database

Connect creates an mongo db client and initial an connection.

You can use the following parameters as environment variable or as flag to define the connection parameters.

MONGO_DB_HOST=<host>,
MONGO_DB_PORT=<port>,
MONGO_DB_USER=<user>,
MONGO_DB_PASSWORD=<password>

if the username or password is not defined, the Client try to connect without an user.

func (*Database) DeleteFile added in v1.4.44

func (i *Database) DeleteFile(ctx context.Context, id string) (err error)

func (*Database) DownloadFile added in v1.4.44

func (i *Database) DownloadFile(id string) (result []byte, err error)

func (*Database) UploadFile added in v1.4.44

func (i *Database) UploadFile(file *vmod.File, id string) (err error)

type DeletedResult added in v1.4.0

type DeletedResult struct {
	Model string `json:"model"`
	ID    string `json:"id"`
}

type Filter added in v1.4.1

type Filter bson.D

Filter represents an mongo filter object.

func NewFilter added in v1.4.1

func NewFilter() *Filter

NewMatch return an empty Match type.

func (*Filter) Append added in v1.4.1

func (i *Filter) Append(value bson.E)

func (*Filter) Bson added in v1.4.4

func (i *Filter) Bson() bson.D

Bson return the bson.D object.

func (*Filter) ContainsString added in v1.4.28

func (i *Filter) ContainsString(key string, value string)

ContainsString use regex for handling a substring matching.

MongoDB:

{
	key: {"$regex": .*value.*}
}

func (*Filter) ElemMatch added in v1.4.4

func (i *Filter) ElemMatch(list string, key string, value string)

ElemMatch TODO

func (*Filter) ElemMatchList added in v1.4.4

func (i *Filter) ElemMatchList(list string, key string, value []string)

ElemMatchList TODO

func (*Filter) EqualBool added in v1.4.4

func (i *Filter) EqualBool(key string, value string)

EqualBool the value is a string representation of an bool. match if value is equal to the value of the key in a database entry.

MongoDB:

{
	key: value as boolean
}

func (*Filter) EqualInt added in v1.4.1

func (i *Filter) EqualInt(key string, value string)

EqualInt the value is an string representation of an int. match if the value is equal to the value of the given key in an database entry.

MongoDB:

{
	key: value as int
}

func (*Filter) EqualInt64 added in v1.4.4

func (i *Filter) EqualInt64(key string, value string)

EqualInt the value is an string representation of an int64. match if value is equal to the value of the key in a database entry.

MongoDB:

{
	key: value as int64
}

func (*Filter) EqualString added in v1.4.1

func (i *Filter) EqualString(key string, value string)

EqualString match if the value is equal to the value of the key in a database collection.

MongoDB:

{
	key: value
}

func (*Filter) EqualStringList added in v1.4.1

func (i *Filter) EqualStringList(key string, value []string)

EqualStringList match if the the value of the key param is matching an element in the value param slice.

MongoDB:

{
	key: {"$or": value}
}

func (*Filter) ExpIn added in v1.4.4

func (i *Filter) ExpIn(key string, value string)

ExpIn TODO

func (*Filter) GteInt added in v1.4.1

func (i *Filter) GteInt(key string, value string)

GteInt provides $lte for key they have an int6 datatype. If the value element is "" or not an int formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$gte": value as int}
}

func (*Filter) GteInt64 added in v1.4.1

func (i *Filter) GteInt64(key string, value string)

GteInt64 provides $gte for key they have an int64 datatype. If the value element is "" or not an int64 formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$gte": value as int64}
}

func (*Filter) LikeString added in v1.4.4

func (i *Filter) LikeString(key string, value string)

LikeString use regex for handling a substring matching at the start of a string.

MongoDB:

{
	key: {"$regex": ^value}
}

func (*Filter) LteInt added in v1.4.1

func (i *Filter) LteInt(key string, value string)

LteInt provides $lte for key they have an int datatype. If the value element is "" or not an int formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$lte": value as int}
}

func (*Filter) LteInt64 added in v1.4.1

func (i *Filter) LteInt64(key string, value string)

LteInt64 provides $lte for key they have an int64 datatype. If the value element is "" or not an int64 formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$lte": value as int64}
}

func (*Filter) SearchString added in v1.4.28

func (i *Filter) SearchString(fields []string, value string) bson.D

SearchString searchs for a given string in all the given fields If the value search string is "" no search string will be added to the filter object.

MongoDB:

{
	key: {"$or": key: {"$regex": ".*value.*"}}
}

type Pipeline added in v1.4.0

type Pipeline struct {
	Pipe []bson.D
}

Pipeline represents an helper for handling mongodb pipeline. The Pipe param contains an []bson.D that represents an mongo pipeline.

func NewPipeline added in v1.4.0

func NewPipeline() *Pipeline

NewPipeline creates an new Pipeline struct.

func (*Pipeline) Append added in v1.4.17

func (i *Pipeline) Append(entry bson.D)

func (*Pipeline) AppendSlice added in v1.4.18

func (i *Pipeline) AppendSlice(pipe []bson.D)

Append appends the elements in pipe to the the Pipeline object.

func (*Pipeline) Count added in v1.4.32

func (i *Pipeline) Count() *Pipeline

Count adds the value total as $count to the end of the Pipeline struct.

MongoDB:

{"$count": total}

func (*Pipeline) Limit added in v1.4.25

func (i *Pipeline) Limit(value int64, defaultValue int64) *Pipeline

Limit is used for the mongo function $limit

func (*Pipeline) Lookup added in v1.4.0

func (i *Pipeline) Lookup(from string, localField string, foreignField string, as string)

Lookup represents an lookup to join an list of elements from a second collection to the result.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"as": as
	}
}

func (*Pipeline) LookupList added in v1.4.2

func (i *Pipeline) LookupList(from string, localField string, foreignField string, as string)

LookupList represents an lookup to join an list of elements from a second collection to the result. The value of the localField need to be a list of references. If the foreignField value is in the list, the element will joined to the as value.

MongoDB:

{
	"$lookup":{
		"from": from,
		"let": { localField: $localField },
		"foreignField" foreignField,
		"pipeline": [{
			"$match":{
				"$expr":{"$in: ["$foreinField", "$$localField"]}
			}
		}],
		"as": as
	}
}

func (*Pipeline) LookupMatch added in v1.4.1

func (i *Pipeline) LookupMatch(from string, localField string, foreignField string, as string, match bson.D)

LookupMatch represents an lookup to join an list of elements from a second collection to the result. The joined elements can be filtered by the match param.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"pipeline": [{"$match": match}]
		"as": as
	}
}

func (*Pipeline) LookupUnwind added in v1.4.0

func (i *Pipeline) LookupUnwind(from string, localField string, foreignField string, as string)

LockupUnwind represents the lookup and unwind combination to join an element from a second collection to the result.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"as": as
	},
	"$unwind": {
		"path": "$as",
		"preserveNullAndEmptyArrays": true
	}
}

func (*Pipeline) LookupUnwindMatch added in v1.4.1

func (i *Pipeline) LookupUnwindMatch(from string, localField string, foreignField string, as string, match bson.D)

LookupUnwindMatch represents the lookup and unwind combination to join an element from a second collection to the result. The joined element can be filtered by the match param.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"pipeline": [{"$match": match}]
		"as": as
	},
	"$unwind": {
		"path": "$as",
		"preserveNullAndEmptyArrays": true
	}
}

func (*Pipeline) Match added in v1.4.0

func (i *Pipeline) Match(filter bson.D) *Pipeline

Match adds the filter param as $match to the end of the Pipeline struct.

MongoDB:

{"$match": filter}

func (*Pipeline) Skip added in v1.4.25

func (i *Pipeline) Skip(value int64, defaultValue int64) *Pipeline

Skip used for the mongo function $skip

func (*Pipeline) Sort added in v1.4.25

func (i *Pipeline) Sort(value bson.D) *Pipeline

Sort is used for the mongo function $sort. Use Sort object for input. The fields for sorting have to be created via SortFields in first place due to performance (e.g. before lookup)

func (*Pipeline) SortFields added in v1.4.38

func (i *Pipeline) SortFields(sort bson.D) *Pipeline

Create case insensitive fields

type Query added in v1.5.3

type Query struct {
	Search        string `query:"search"`
	SortField     string `query:"sort"`
	SortDirection string `query:"sort_dir"`
	Limit         int64  `query:"limit"`
	Skip          int64  `query:"skip"`
}

Query represents the default query set for database requests.

func (Query) FindOptions added in v1.5.3

func (i Query) FindOptions() *options.FindOptions

FindOptions return mongodb find options they can be used with Collection.Find() or Collection.FindAndCount()

type Sort added in v1.4.25

type Sort struct {
	Value bson.D
}

Sort represents an type for manage sorting in mongo db.

func NewSort added in v1.4.25

func NewSort() *Sort

NewSort creates a new Sort object.

func (*Sort) Add added in v1.4.25

func (i *Sort) Add(key string, value string)

Add adds an sort option to the sort object. The function convert ASC to 1 and DESC to -1.

func (*Sort) Bson added in v1.4.25

func (i *Sort) Bson() bson.D

Bson returns the bson object.

Jump to

Keyboard shortcuts

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