From 6b367dbd7a0365fc58754fc18956953899d22fce Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Wed, 9 Feb 2022 20:09:13 +0100 Subject: [PATCH] db methods --- crud.go | 16 ++++++++-------- lookup.go | 4 ++-- setup.go | 2 +- validator.go | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crud.go b/crud.go index a7f7ac4..67fb175 100644 --- a/crud.go +++ b/crud.go @@ -27,7 +27,7 @@ func (e *ErrorWithStack) Stack() string { } // CreateDocument creates a document from specified model -func CreateDocument(db *Database, m ModelInterface) error { +func (db *Database) CreateDocument(m ModelInterface) error { m.PrepareInsert() c := db.C(GetCollectionName(m)) @@ -37,7 +37,7 @@ func CreateDocument(db *Database, m ModelInterface) error { } // ReadDocument gets one document via its id -func ReadDocument(db *Database, m ModelInterface, selector bson.M) error { +func (db *Database) ReadDocument(m ModelInterface, selector bson.M) error { c := db.C(GetCollectionName(m)) q := c.FindId(m.GetID()) @@ -136,7 +136,7 @@ func idToObjectID(filter interface{}) { } // ReadCollection gets the filtered collection of the model -func ReadCollection(db *Database, results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err error) { +func (db *Database) ReadCollection(results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err error) { defer func() { if r := recover(); r != nil { err = &ErrorWithStack{Message: fmt.Sprintf("%v", r), StackTrace: string(debug.Stack())} @@ -243,7 +243,7 @@ func ReadCollection(db *Database, results interface{}, filter bson.M, selector b } // ReadCollectionCount gets the count of elements in filtered collection -func ReadCollectionCount(db *Database, m ModelInterface, filter bson.M) (count int, err error) { +func (db *Database) ReadCollectionCount(m ModelInterface, filter bson.M) (count int, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("%v", r) @@ -255,7 +255,7 @@ func ReadCollectionCount(db *Database, m ModelInterface, filter bson.M) (count i } // UpdateDocument updates a document from specified model -func UpdateDocument(db *Database, m ModelInterface, changes bson.M) error { +func (db *Database) UpdateDocument(m ModelInterface, changes bson.M) error { m.PrepareUpdate() changes["updateTime"] = time.Now() @@ -266,7 +266,7 @@ func UpdateDocument(db *Database, m ModelInterface, changes bson.M) error { } // UpsertDocument updates a document from specified model or inserts it, of not found -func UpsertDocument(db *Database, m ModelInterface, changes bson.M) error { +func (db *Database) UpsertDocument(m ModelInterface, changes bson.M) error { m.PrepareUpdate() changes["updateTime"] = time.Now() @@ -277,7 +277,7 @@ func UpsertDocument(db *Database, m ModelInterface, changes bson.M) error { } // DeleteDocument deletes one document via its id -func DeleteDocument(db *Database, m ModelInterface) error { +func (db *Database) DeleteDocument(m ModelInterface) error { c := db.C(GetCollectionName(m)) err := c.RemoveId(m.GetID()) @@ -286,7 +286,7 @@ func DeleteDocument(db *Database, m ModelInterface) error { } // DeleteDocuments deletes documents found by filter -func DeleteDocuments(db *Database, m ModelInterface, filter bson.M) (removed int, err error) { +func (db *Database) DeleteDocuments(m ModelInterface, filter bson.M) (removed int, err error) { c := db.C(GetCollectionName(m)) info, err := c.RemoveAll(filter) diff --git a/lookup.go b/lookup.go index a118993..e1fed59 100644 --- a/lookup.go +++ b/lookup.go @@ -8,7 +8,7 @@ import ( ) // Lookup extends results with data for inline structs -func Lookup(db *Database, structField string, results interface{}, selector bson.M) error { +func (db *Database) Lookup(structField string, results interface{}, selector bson.M) error { t := reflect.TypeOf(results) v := reflect.ValueOf(results) @@ -122,7 +122,7 @@ func Lookup(db *Database, structField string, results interface{}, selector bson return fmt.Errorf("ID type in objects struct %+v is not bson.ObjectId", fieldType) } - err := ReadCollection(db, objectResults, sQuery, selector, 0, 0, nil, nil) + err := db.ReadCollection(objectResults, sQuery, selector, 0, 0, nil, nil) if err != nil { panic(err) } diff --git a/setup.go b/setup.go index 878c8c9..1df161a 100644 --- a/setup.go +++ b/setup.go @@ -9,7 +9,7 @@ import ( ) // EnsureIndex ensured mongodb index reflecting model struct index tag -func EnsureIndex(db *Database, m ModelInterface) error { +func (db *Database) EnsureIndex(m ModelInterface) error { colName := GetCollectionName(m) col := db.C(colName) diff --git a/validator.go b/validator.go index e9f321f..1ac7e5d 100644 --- a/validator.go +++ b/validator.go @@ -6,7 +6,7 @@ import ( ) // ValidateObject validates object via validator tag and custom method -func ValidateObject(db *Database, m ModelInterface, changes bson.M) error { +func (db *Database) ValidateObject(m ModelInterface, changes bson.M) error { // first validate via struct tag validator := validator.New(&validator.Config{ TagName: "validator",