mirror of
https://github.com/IllusionMan1212/kotapi-go.git
synced 2026-10-24 10:12:29 +00:00
initial commit
This commit is contained in:
@@ -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),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user