50 lines
1007 B
Go
50 lines
1007 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type Database struct {
|
|
client *mongo.Client
|
|
users *mongo.Collection
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewDatabase(ctx context.Context, mongoUri string) Database {
|
|
var db Database
|
|
|
|
clientOptions := options.Client().ApplyURI(mongoUri)
|
|
client, err := mongo.Connect(ctx, clientOptions)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
db.client = client
|
|
|
|
log.Println("Trying to ping Mongo database...")
|
|
err = client.Ping(ctx, nil)
|
|
if err != nil {
|
|
log.Fatal("Couldn't connect to MongoDB --> ", err)
|
|
}
|
|
|
|
log.Println("Connected to MongoDB-->", mongoUri)
|
|
|
|
db.users = client.Database("account-mngr").Collection("users")
|
|
indexField := bson.M{"email": 1}
|
|
_, err = db.users.Indexes().CreateOne(ctx, mongo.IndexModel{
|
|
Keys: indexField,
|
|
Options: options.Index().SetUnique(true),
|
|
})
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
db.ctx = ctx
|
|
|
|
return db
|
|
}
|