mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"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 getCollection(location Location) *mongo.Collection {
|
|
client := getNewClient()
|
|
defer client.Disconnect(context.TODO())
|
|
return client.Database(location.Database).Collection(location.Collection)
|
|
}
|
|
|
|
func pushDocument(location Location, document interface{}) error {
|
|
collection := getCollection(location)
|
|
_, err := collection.InsertOne(context.TODO(), document)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
|
collection := getCollection(location)
|
|
var results [][]byte
|
|
|
|
cursor, err := collection.Find(context.TODO(), filter)
|
|
if err != nil {
|
|
return results, errors.New("something is wrong with filter to find document")
|
|
}
|
|
for cursor.TryNext(context.TODO()) {
|
|
results = append(results, cursor.Current)
|
|
}
|
|
return results, nil
|
|
}
|