mongowrapper.go 902 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package mongowrapper
  2. import (
  3. "context"
  4. "time"
  5. "go.mongodb.org/mongo-driver/mongo"
  6. "go.mongodb.org/mongo-driver/mongo/options"
  7. )
  8. var Client = MONGO{}
  9. var (
  10. UserCollection *mongo.Collection
  11. MSGCollection *mongo.Collection
  12. )
  13. type MONGO struct {
  14. Connection *mongo.Client
  15. }
  16. func InitCollections() {
  17. UserCollection = Client.Connection.Database("bot").Collection("users")
  18. MSGCollection = Client.Connection.Database("bot").Collection("msg")
  19. }
  20. func Connect(uri string) (err error) {
  21. var maxSize uint64 = 200
  22. var minSize uint64 = 20
  23. var minHeartbeat = time.Duration(1 * time.Second)
  24. opt := options.Client()
  25. opt.MaxPoolSize = &maxSize
  26. opt.MinPoolSize = &minSize
  27. opt.HeartbeatInterval = &minHeartbeat
  28. Client.Connection, err = mongo.Connect(context.TODO(), opt.ApplyURI(uri))
  29. return err
  30. }
  31. func Disconnect() (err error) {
  32. err = Client.Connection.Disconnect(context.TODO())
  33. return err
  34. }