initial commit

This commit is contained in:
2021-03-16 22:04:12 +02:00
commit 287f049058
7 changed files with 480 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
package db
import (
"context"
"fmt"
"log"
"os"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// package-wide variables for the collection and the context
var Kots *mongo.Collection
var Ctx = context.TODO()
func InitializeDB() {
clientOptions := options.Client().ApplyURI(os.Getenv("DB_URI"))
client, err := mongo.Connect(Ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(Ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to mongo")
Kots = client.Database("kotapi").Collection("kots")
// creates a unique index on the _id field, which also auto generates it
Kots.Indexes().CreateOne(Ctx, mongo.IndexModel{
Keys: bson.D{{Key: "_id", Value: 1}},
Options: options.Index().SetUnique(true),
})
// creates a unique index on the id field
Kots.Indexes().CreateOne(Ctx, mongo.IndexModel{
Keys: bson.D{{Key: "id", Value: 1}},
Options: options.Index().SetUnique(true),
})
// creates a unique index on the id field
Kots.Indexes().CreateOne(Ctx, mongo.IndexModel{
Keys: bson.D{{Key: "url", Value: 1}},
Options: options.Index().SetUnique(true),
})
// creates a unique index on the id field
Kots.Indexes().CreateOne(Ctx, mongo.IndexModel{
Keys: bson.D{{Key: "compressed_url", Value: 1}},
Options: options.Index().SetUnique(true),
})
}