2021-10-25 23:33:00 +02:00
|
|
|
package db
|
|
|
|
|
2021-10-27 15:39:21 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lyx0/nourybot/pkg/config"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
2021-10-28 16:16:11 +02:00
|
|
|
type Channel struct {
|
|
|
|
Name string `bson:"name,omitempty"`
|
|
|
|
Connect bool `bson:"connect,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-10-28 17:48:18 +02:00
|
|
|
func Connect(cfg *config.Config) *mongo.Client {
|
2021-10-27 15:39:21 +02:00
|
|
|
|
2021-10-28 17:48:18 +02:00
|
|
|
client, err := mongo.NewClient(options.Client().ApplyURI(cfg.MongoURI))
|
2021-10-27 15:39:21 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-10-28 15:22:19 +02:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-10-27 15:39:21 +02:00
|
|
|
err = client.Connect(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-10-27 19:27:35 +02:00
|
|
|
// defer client.Disconnect(ctx)
|
2021-10-27 15:39:21 +02:00
|
|
|
|
2021-10-28 16:16:11 +02:00
|
|
|
err = client.Ping(context.TODO(), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Connected to MongoDB!")
|
|
|
|
|
2021-10-27 15:39:21 +02:00
|
|
|
databases, err := client.ListDatabaseNames(ctx, bson.M{})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-10-28 16:16:11 +02:00
|
|
|
_ = databases
|
|
|
|
// log.Info(databases)
|
2021-10-27 15:39:21 +02:00
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
List databases
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Iterate a cursor
|
|
|
|
*/
|
|
|
|
|
|
|
|
// log.Info(channels)
|
|
|
|
|
2021-10-26 17:03:16 +02:00
|
|
|
// import (
|
|
|
|
// "context"
|
|
|
|
// "fmt"
|
|
|
|
// "time"
|
|
|
|
|
|
|
|
// "github.com/lyx0/nourybot/pkg/config"
|
|
|
|
// log "github.com/sirupsen/logrus"
|
|
|
|
// "go.mongodb.org/mongo-driver/bson"
|
|
|
|
// "go.mongodb.org/mongo-driver/mongo"
|
|
|
|
// "go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
// )
|
|
|
|
|
|
|
|
// func Connect() {
|
|
|
|
// conf := config.LoadConfig()
|
|
|
|
// client, err := mongo.NewClient(options.Client().ApplyURI(conf.MongoURI))
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatal(err)
|
|
|
|
// }
|
|
|
|
// ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
// err = client.Connect(ctx)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatal(err)
|
|
|
|
// }
|
|
|
|
// defer client.Disconnect(ctx)
|
|
|
|
// /*
|
|
|
|
// List databases
|
|
|
|
// */
|
|
|
|
// databases, err := client.ListDatabaseNames(ctx, bson.M{})
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatal(err)
|
|
|
|
// }
|
|
|
|
// fmt.Println(databases)
|
|
|
|
|
|
|
|
// // Interact with data
|
|
|
|
// type Channel struct {
|
|
|
|
// Name string `bson:"name,omitempty"`
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /*
|
|
|
|
// Get my collection instance
|
|
|
|
// */
|
|
|
|
// collection := client.Database("nourybot").Collection("channels")
|
|
|
|
|
|
|
|
// /*
|
|
|
|
// Insert channel
|
|
|
|
// */
|
|
|
|
// // chnl := []interface{}{
|
|
|
|
// // bson.D{{"name", "nouryqt"}},
|
|
|
|
// // bson.D{{"name", "nourybot"}},
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// // res, insertErr := collection.InsertMany(ctx, chnl)
|
|
|
|
// // if insertErr != nil {
|
|
|
|
// // log.Fatal(insertErr)
|
|
|
|
// // }
|
|
|
|
// // log.Info(res)
|
|
|
|
|
|
|
|
// /*
|
|
|
|
// Iterate a cursor
|
|
|
|
// */
|
|
|
|
|
|
|
|
// cur, currErr := collection.Find(ctx, bson.D{})
|
|
|
|
|
|
|
|
// if currErr != nil {
|
|
|
|
// panic(currErr)
|
|
|
|
// }
|
|
|
|
// defer cur.Close(ctx)
|
|
|
|
|
|
|
|
// var channels []Channel
|
|
|
|
// if err = cur.All(ctx, &channels); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// log.Info(channels)
|
|
|
|
|
|
|
|
// }
|