Cleaned, refactored, added db init script

This commit is contained in:
Florian Sylvain
2022-10-14 09:14:45 +02:00
parent 0835726beb
commit 68495b6430
8 changed files with 285 additions and 190 deletions
+39
View File
@@ -0,0 +1,39 @@
package internal
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Location struct {
Database string
Collection string
}
func getNewClient() *mongo.Client {
client, err := mongo.Connect(
context.TODO(),
options.Client().ApplyURI("mongodb://db:27017/"))
if err != nil {
panic(err)
}
return client
}
func pushDocument(location Location, document interface{}) error {
client := getNewClient()
defer client.Disconnect(context.TODO())
coll := client.Database(location.Database).Collection(location.Collection)
_, err := coll.InsertOne(context.TODO(), document)
if err != nil {
return err
}
return nil
}
// func getDocument(database string, collection string) {
// }