Skip to content

Commit

Permalink
refact - mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Oct 17, 2023
1 parent 9c5985e commit b1cb0f1
Show file tree
Hide file tree
Showing 3 changed files with 498 additions and 6 deletions.
26 changes: 26 additions & 0 deletions examples/mongodb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"

"github.com/debeando/go-common/mongodb"
)

func main() {
m := mongodb.New("example", "mongodb://admin:pass@127.0.0.1:27017")
if err := m.Connect(); err == nil {
databases := m.Databases()
for _, database := range databases.Databases {
fmt.Println(">", database.Name)
collections := m.Collections(database.Name)
for _, collection := range collections {
fmt.Println(" -", collection)
colStats := m.CollectionStats(database.Name, collection)
fmt.Printf("%+v\n", colStats)
}
}

serverstatus := m.ServerStatus()
fmt.Printf("\nServer Status: %+v\n", serverstatus)
}
}
48 changes: 42 additions & 6 deletions mongodb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,59 @@ func (c *Connection) Connect() (err error) {
clientOptions := options.Client().ApplyURI(c.DSN)
c.Client, err = mongo.Connect(c.Context, clientOptions)
if err != nil {
log.ErrorWithFields("MongoDB", log.Fields{"message": err})
log.ErrorWithFields("MongoDB:Connect", log.Fields{"message": err})
return err
}

err = c.Client.Ping(c.Context, nil)
if err != nil {
log.ErrorWithFields("MongoDB", log.Fields{"message": err})
log.ErrorWithFields("MongoDB:Connect:Ping", log.Fields{"message": err})
return err
}

return nil
}

func (c *Connection) GetServerStatus() (ss map[string]interface{}) {
db := c.Client.Database("admin")
func (c *Connection) Close() {
if err := c.Client.Disconnect(c.Context); err != nil {
log.ErrorWithFields("MongoDB:Close", log.Fields{"message": err})
}
}

func (c *Connection) RunCommand(database string, cmd interface{}, result interface{}) error {
db := c.Client.Database(database)
return db.RunCommand(context.TODO(), cmd).Decode(result)
}

func (c *Connection) ServerStatus() *ServerStatus {
serverstatus := &ServerStatus{}
cmd := bson.D{primitive.E{Key: "serverStatus", Value: "1"}}
db.RunCommand(context.TODO(), cmd).Decode(&ss)
c.RunCommand("admin", cmd, serverstatus)

return serverstatus
}

func (c *Connection) Databases() *Databases {
databases := &Databases{}
cmd := bson.D{primitive.E{Key: "listDatabases", Value: "1"}}
c.RunCommand("admin", cmd, databases)

return databases
}

func (c *Connection) Collections(database string) []string {
colls, err := c.Client.Database(database).ListCollectionNames(context.TODO(), bson.M{})
if err != nil {
log.ErrorWithFields("MongoDB:Collections", log.Fields{"message": err})
}

return colls
}

func (c *Connection) CollectionStats(dbName, colName string) *CollectionStats {
collectionStats := &CollectionStats{}
cmd := bson.D{primitive.E{Key: "collStats", Value: colName}}
c.RunCommand(dbName, cmd, collectionStats)

return ss
return collectionStats
}
Loading

0 comments on commit b1cb0f1

Please sign in to comment.