-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
135 lines (106 loc) · 3.47 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package db
import (
"time"
"github.com/allanassis/reddere/src/config"
"github.com/allanassis/reddere/src/observability/logging"
"github.com/allanassis/reddere/src/storages"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"context"
)
type Database struct {
client mongo.Client
instance *mongo.Database
logger *logging.Logger
timeout time.Duration
}
func NewDatabase(logger *logging.Logger, config *config.Config) storages.Storage {
uri := config.GetString("database.uri")
name := config.GetString("database.name")
timeout := config.GetDuration("database.timeout")
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(uri))
instance := client.Database(name)
db := &Database{
client: *client,
instance: instance,
logger: logger,
timeout: timeout,
}
if err != nil {
panic(err)
}
err = db.Healthcheck()
if err != nil {
panic(err)
}
return db
}
func (db *Database) Save(document interface{}, collectionName string) (string, error) {
collection := db.instance.Collection(collectionName)
logger := db.logger.With(logging.String("collection", collectionName))
logger.Debug("Retrive collection from database")
insertedResult, err := collection.InsertOne(context.Background(), document)
if err != nil {
return "", err
}
stringObjectID := insertedResult.InsertedID.(primitive.ObjectID).Hex()
logger.Debug("Succefully inserted document into database", logging.String("id", stringObjectID))
return stringObjectID, nil
}
func (db *Database) Get(id string, collectionName string) (*mongo.SingleResult, error) {
collection := db.instance.Collection(collectionName)
logger := db.logger.With(logging.String("collection", collectionName))
logger.Debug("Retrive collection from database")
objectId, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
logger.Debug("Succefully parsed id into object id")
result := collection.FindOne(context.Background(), bson.D{primitive.E{Key: "_id", Value: objectId}})
if result.Err() == mongo.ErrNoDocuments {
logger.Warn("No document found in database", logging.String("id", id))
return nil, nil
}
if result.Err() != nil {
return nil, result.Err()
}
logger.Debug("Succefully found item in database", logging.String("id", id))
return result, nil
}
func (db *Database) Delete(id string, collectionName string) (*mongo.DeleteResult, error) {
collection := db.instance.Collection(collectionName)
logger := db.logger.With(logging.String("collection", collectionName))
objectId, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
result, err := collection.DeleteOne(context.Background(), bson.D{primitive.E{Key: "_id", Value: objectId}})
if err != nil {
return nil, err
}
logger.Debug("Succefully deleted item in database", logging.String("id", id))
return result, nil
}
func (db *Database) Bind(result *mongo.SingleResult, instance interface{}) error {
err := result.Decode(instance)
if err == mongo.ErrNoDocuments {
db.logger.Warn("No document to bind")
return nil
}
if err != nil {
return err
}
db.logger.Debug("Succefully bind item", logging.Any("item", instance))
return nil
}
func (db *Database) Healthcheck() error {
err := db.client.Ping(context.Background(), &readpref.ReadPref{})
if err != nil {
return err
}
db.logger.Info("Succefully ping to database")
return nil
}