113 lines
2.8 KiB
Go
113 lines
2.8 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"servicebase/pkg/common/req"
|
|
|
|
"github.com/spf13/viper"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type MongoInitStarter struct {
|
|
}
|
|
|
|
var Client *mongo.Client
|
|
var DB *mongo.Database
|
|
|
|
func (s *MongoInitStarter) Init() error {
|
|
var (
|
|
ctx = context.TODO()
|
|
host = viper.GetString("db.mongo.host")
|
|
port = viper.GetString("db.mongo.port")
|
|
username = viper.GetString("db.mongo.username")
|
|
password = viper.GetString("db.mongo.password")
|
|
dbname = viper.GetString("db.mongo.dbname")
|
|
e error
|
|
)
|
|
// log.InfoF(ctx, "host=%s port=%s db=%s", host, port, dbname)
|
|
if Client, e = mongo.Connect(ctx, options.Client().
|
|
SetAuth(options.Credential{Username: username, Password: password}).
|
|
ApplyURI(fmt.Sprintf("mongodb://%s:%s", host, port))); e != nil {
|
|
return e
|
|
}
|
|
DB = Client.Database(dbname)
|
|
return nil
|
|
}
|
|
|
|
func CheckTable(c context.Context, table string) error {
|
|
if r, e := DB.ListCollectionNames(c, bson.M{"name": "table"}); e != nil {
|
|
return e
|
|
} else if len(r) == 0 {
|
|
return DB.CreateCollection(c, table)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func InsertOne(c context.Context, table string, record interface{}) (*mongo.InsertOneResult, error) {
|
|
if e := CheckTable(c, table); e != nil {
|
|
return nil, e
|
|
}
|
|
return DB.Collection(table).InsertOne(c, record)
|
|
}
|
|
|
|
func SaveOne(c context.Context, table string, filter, record interface{}) (*mongo.UpdateResult, error) {
|
|
if e := CheckTable(c, table); e != nil {
|
|
return nil, e
|
|
}
|
|
return DB.Collection(table).ReplaceOne(c, filter, record, options.Replace().SetUpsert(true))
|
|
}
|
|
|
|
func DeleteOne(c context.Context, table string, record interface{}) (*mongo.DeleteResult, error) {
|
|
if e := CheckTable(c, table); e != nil {
|
|
return nil, e
|
|
}
|
|
return DB.Collection(table).DeleteOne(c, record)
|
|
}
|
|
|
|
func UpdateByID(c context.Context, table string, id interface{}, record interface{}) (*mongo.UpdateResult, error) {
|
|
if e := CheckTable(c, table); e != nil {
|
|
return nil, e
|
|
}
|
|
return DB.Collection(table).UpdateByID(c, id, bson.D{{Key: "$set", Value: record}})
|
|
}
|
|
|
|
func Page(c context.Context, table string, page req.Page, filter bson.D, results interface{}) (int64, error) {
|
|
if e := CheckTable(c, table); e != nil {
|
|
return 0, e
|
|
}
|
|
cursor, e := DB.Collection(table).Find(
|
|
c,
|
|
filter,
|
|
options.Find().SetSkip(int64(page.Offset())),
|
|
options.Find().SetLimit(int64(page.Limit())),
|
|
)
|
|
if e != nil {
|
|
// log.ErrorF(c, "db Page error: %v", e)
|
|
return 0, e
|
|
}
|
|
defer func() {
|
|
if e := cursor.Close(c); e != nil {
|
|
// log.Error(c, e.Error())
|
|
}
|
|
}()
|
|
if e != nil {
|
|
return 0, e
|
|
}
|
|
cnt, e := DB.Collection(table).CountDocuments(c, filter)
|
|
if e != nil {
|
|
return 0, e
|
|
}
|
|
return cnt, cursor.All(c, results)
|
|
}
|
|
|
|
func MToD(m bson.M) (d bson.D) {
|
|
for key, value := range m {
|
|
d = append(d, bson.E{Key: key, Value: value})
|
|
}
|
|
return
|
|
}
|