/
database.go
51 lines (41 loc) · 1.16 KB
/
database.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
package database
import (
"context"
"fmt"
"github.com/ugcompsoc/apid/internal/config"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Datastore struct {
Client *mongo.Client
Database *mongo.Database
}
/*
* Database Setup
*/
func NewDatastore(config *config.Config) (*Datastore, error) {
datastore := &Datastore{}
if err := datastore.connect(config); err != nil {
return nil, err
}
return datastore, nil
}
func (ds *Datastore) connect(config *config.Config) error {
credential := options.Credential{
Username: config.Database.Username,
Password: config.Database.Password,
}
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI(config.Database.Host).SetAuth(credential).SetServerAPIOptions(serverAPI)
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
return fmt.Errorf("failed to connect to/create session with database host: %w", err)
}
err = client.Ping(context.TODO(), nil)
if err != nil {
return fmt.Errorf("failed to ping the database host: %w", err)
}
ds.Client = client
ds.Database = client.Database(config.Database.Name)
return err
}