mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 19:53:21 +02:00
Cleaned, refactored, added db init script
This commit is contained in:
@@ -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) {
|
||||
// }
|
||||
Reference in New Issue
Block a user