Capstone/ui/db/db.go

149 lines
3.6 KiB
Go
Raw Normal View History

2023-10-28 14:42:29 -04:00
package db
import (
"context"
"errors"
2023-11-19 21:51:05 -05:00
"time"
2023-10-28 14:42:29 -04:00
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
2023-10-28 14:42:29 -04:00
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2023-11-19 21:51:05 -05:00
"go.mongodb.org/mongo-driver/mongo/readpref"
2023-10-28 14:42:29 -04:00
)
2023-11-16 20:45:48 -05:00
// Interface for any object that wants to take advantage of the DB package
2023-10-28 14:42:29 -04:00
type Model interface {
2023-11-14 13:58:08 -05:00
//Should return the _id field of the object if it exits
//if it is new it should generate a new objectId
MongoId() primitive.ObjectID
2023-11-14 13:58:08 -05:00
//It is expected that this will update the CommonFields part of the model
UpdateObjectInfo()
2023-10-28 14:42:29 -04:00
}
type DB struct {
client *mongo.Client
}
func NewClient(uri string) (*DB, error) {
2023-11-19 21:51:05 -05:00
opts := options.Client().ApplyURI(uri).SetConnectTimeout(60 * time.Second)
client, err := mongo.Connect(context.Background(), opts)
2023-10-28 14:42:29 -04:00
if err != nil {
return nil, err
}
2023-11-19 21:51:05 -05:00
if err := client.Ping(context.Background(), readpref.Primary()); err != nil {
return nil, err
}
2023-10-28 14:42:29 -04:00
return &DB{client: client}, nil
}
2023-10-28 17:50:44 -04:00
2023-11-16 20:45:48 -05:00
// Upserts
2023-10-28 17:50:44 -04:00
func (db *DB) SaveModel(m Model) error {
conf := config.Config()
2023-11-02 19:23:07 -04:00
m.UpdateObjectInfo()
opts := options.Update().SetUpsert(true)
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).UpdateOne(context.Background(), bson.M{"_id": m.MongoId()}, bson.M{"$set": m}, opts)
if err != nil {
return err
}
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
2023-11-16 20:43:34 -05:00
return errors.New("Failed to save model properly")
}
return nil
}
func (db *DB) SaveModels(m ...Model) error {
conf := config.Config()
writeEntry := make([]mongo.WriteModel, len(m))
for index, model := range m {
entry := mongo.NewUpdateOneModel()
entry.SetFilter(bson.M{"_id": model.MongoId})
entry.SetUpsert(true)
entry.SetUpdate(bson.M{"$set": model})
model.UpdateObjectInfo()
writeEntry[index] = entry
}
opts := options.BulkWrite()
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).BulkWrite(context.Background(), writeEntry, opts)
if err != nil {
return err
}
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
return errors.New("Failed to save models properly")
}
return nil
}
2023-11-18 21:16:44 -05:00
// For allowing more varidaic like things
func saveModels[T Model](db *DB, m ...T) error {
conf := config.Config()
writeEntry := make([]mongo.WriteModel, len(m))
for index, model := range m {
entry := mongo.NewUpdateOneModel()
entry.SetFilter(bson.M{"_id": model.MongoId})
entry.SetUpsert(true)
entry.SetUpdate(bson.M{"$set": model})
model.UpdateObjectInfo()
writeEntry[index] = entry
}
opts := options.BulkWrite()
2023-11-16 20:43:34 -05:00
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).BulkWrite(context.Background(), writeEntry, opts)
if err != nil {
return err
}
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
return errors.New("Failed to save models properly")
}
return nil
}
2023-11-16 20:45:48 -05:00
// Doesn't upsert
func (db *DB) InsertModel(m Model) error {
conf := config.Config()
m.UpdateObjectInfo()
opts := options.InsertOne()
_, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).InsertOne(context.Background(), m, opts)
if err != nil {
return err
}
return nil
}
func (db *DB) DeleteModel(m Model) error {
conf := config.Config()
opts := options.Delete()
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).DeleteOne(context.Background(), bson.M{"_id": m.MongoId()}, opts)
if err != nil {
return err
}
if res.DeletedCount == 0 {
return errors.New("There was no item to delete")
}
return nil
2023-10-28 17:50:44 -04:00
}