Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions common/database/database.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

/*
Database interface exposing the methods necessary to querying, inserting, updating, upserting, and removing records
*/
type Database interface {
Connect(host string) error
Close()
FindOne(collection_name string, query interface{}, result interface{}) error
FindAll(collection_name string, query interface{}, result interface{}) error
FindAllSorted(collection_name string, query interface{}, sort_fields []SortField, result interface{}) error
RemoveOne(collection_name string, query interface{}) error
RemoveAll(collection_name string, query interface{}) (*ChangeResults, error)
Insert(collection_name string, item interface{}) error
Upsert(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)
Update(collection_name string, selector interface{}, update interface{}) error
UpdateAll(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)
DropDatabase() error
GetStats(collection_name string, fields []string) (map[string]interface{}, error)
GetRaw() *mongo.Client
StartSession() (*mongo.Session, error)
GetNewContext() (context.Context, context.CancelFunc)
FindOne(collection_name string, query interface{}, result interface{}, session *mongo.SessionContext) error
FindOneAndDelete(collection_name string, query interface{}, result interface{}, session *mongo.SessionContext) error
FindOneAndUpdate(collection_name string, query interface{}, update interface{}, result interface{}, return_new_doc bool, upsert bool, session *mongo.SessionContext) error
FindOneAndReplace(collection_name string, query interface{}, update interface{}, result interface{}, return_new_doc bool, upsert bool, session *mongo.SessionContext) error
FindAll(collection_name string, query interface{}, result interface{}, session *mongo.SessionContext) error
FindAllSorted(collection_name string, query interface{}, sort_fields bson.D, result interface{}, session *mongo.SessionContext) error
RemoveOne(collection_name string, query interface{}, session *mongo.SessionContext) error
RemoveAll(collection_name string, query interface{}, session *mongo.SessionContext) (*ChangeResults, error)
Insert(collection_name string, item interface{}, session *mongo.SessionContext) error
Upsert(collection_name string, selector interface{}, update interface{}, session *mongo.SessionContext) (*ChangeResults, error)
Update(collection_name string, selector interface{}, update interface{}, session *mongo.SessionContext) error
UpdateAll(collection_name string, selector interface{}, update interface{}, session *mongo.SessionContext) (*ChangeResults, error)
Replace(collection_name string, selector interface{}, update interface{}, upsert bool, session *mongo.SessionContext) error
DropDatabase(session *mongo.SessionContext) error
GetStats(collection_name string, fields []string, session *mongo.SessionContext) (map[string]interface{}, error)
}

/*
Expand Down
40 changes: 32 additions & 8 deletions common/database/db_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,48 @@ package database

import (
"errors"
"gopkg.in/mgo.v2"
"fmt"

"go.mongodb.org/mongo-driver/mongo"
)

var (
ErrNotFound = errors.New("Error: NOT_FOUND")
ErrConnection = errors.New("Error: CONNECTION_FAILED")
ErrUnknown = errors.New("Error: UNKNOWN")
ErrNotFound = errors.New("Error: NOT_FOUND")
ErrDuplicateKey = errors.New("Error: DUPLICATE_KEY")
ErrConnection = errors.New("Error: CONNECTION_FAILED")
ErrDisconnected = errors.New("Error: CLIENT_DISCONNECTED")
ErrUnknown = errors.New("Error: UNKNOWN")
ErrNilPassedToCRUD = errors.New("Error: NIL_PASSED_TO_CRUD") // this is an error the user should never get and is merely here for debugging
)

/*
Converts internal mgo errors to external presented errors
*/
func convertMgoError(err error) error {
if err == nil {
// TODO: Need to try and encompass all possible errors
switch err {
case nil:
return nil
} else if err == mgo.ErrNotFound {
case mongo.ErrClientDisconnected:
return ErrDisconnected
case mongo.ErrNilDocument:
return ErrNilPassedToCRUD
case mongo.ErrNoDocuments:
return ErrNotFound
default:
{
var e mongo.WriteException
if errors.As(err, &e) {
for _, we := range e.WriteErrors {
if we.Code == 11000 { // Error code for duplicate key error
return ErrDuplicateKey
}
}
}
}
fmt.Println("Unhandled error: ", err)
// TODO: How can we embed error information into here?
// It'll help a lot if an unexpected error comes up
return ErrUnknown
}

return ErrUnknown
}
Loading