Initial commit
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"pet-house.com/core/g"
|
||||
"pet-house.com/core/helper/str"
|
||||
"pet-house.com/core/server/viper_server"
|
||||
)
|
||||
|
||||
// init initialize
|
||||
func init() {
|
||||
viper_server.Init(getViperConfig())
|
||||
}
|
||||
|
||||
var CONFIG = MongoDB{
|
||||
DB: "mongo_test",
|
||||
Timeout: 10,
|
||||
Addr: "localhost:27017",
|
||||
}
|
||||
|
||||
type MongoDB struct {
|
||||
Timeout time.Duration `mapstructure:"timeout" json:"timeout" yaml:"timeout"`
|
||||
DB string `mapstructure:"db" json:"db" yaml:"db"`
|
||||
Addr string `mapstructure:"addr" json:"addr" yaml:"addr"`
|
||||
}
|
||||
|
||||
func (md *MongoDB) GetApplyURI() string {
|
||||
return str.Join("mongodb://", md.Addr)
|
||||
}
|
||||
|
||||
// IsExist config file is exist
|
||||
func IsExist() bool {
|
||||
return getViperConfig().IsFileExist()
|
||||
}
|
||||
|
||||
// Remove remove config file
|
||||
func Remove() error {
|
||||
return getViperConfig().Remove()
|
||||
}
|
||||
|
||||
// Recover
|
||||
func Recover() error {
|
||||
b, err := json.Marshal(CONFIG)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return getViperConfig().Recover(b)
|
||||
}
|
||||
|
||||
// getViperConfig get viper config
|
||||
func getViperConfig() viper_server.ViperConfig {
|
||||
configName := "mongo"
|
||||
return viper_server.ViperConfig{
|
||||
Debug: true,
|
||||
Directory: g.ConfigDir,
|
||||
Name: configName,
|
||||
Type: g.ConfigType,
|
||||
Watch: func(vi *viper.Viper) error {
|
||||
if err := vi.Unmarshal(&CONFIG); err != nil {
|
||||
return fmt.Errorf("get Unarshal error: %v", err)
|
||||
}
|
||||
// watch config file change
|
||||
vi.SetConfigName(configName)
|
||||
return nil
|
||||
},
|
||||
//
|
||||
Default: []byte(`
|
||||
{
|
||||
"timeout": "` + CONFIG.Timeout.String() + `",
|
||||
"db": "` + CONFIG.DB + `",
|
||||
"addr": "` + CONFIG.Addr + `"
|
||||
}`),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsExist(t *testing.T) {
|
||||
t.Run("test mongodb config IsExist function", func(t *testing.T) {
|
||||
if !IsExist() {
|
||||
t.Errorf("config's files is not exist.")
|
||||
}
|
||||
})
|
||||
t.Run("Test GetApplyURI function", func(t *testing.T) {
|
||||
want := "mongodb://localhost:27017"
|
||||
if applyUrl := CONFIG.GetApplyURI(); applyUrl != want {
|
||||
t.Errorf("applyURI want %s but get %s", want, applyUrl)
|
||||
}
|
||||
})
|
||||
t.Run("Test Remove function", func(t *testing.T) {
|
||||
if err := Remove(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if IsExist() {
|
||||
t.Errorf("config's files remove is fail.")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
mc *mongo.Client
|
||||
}
|
||||
|
||||
// GetClient
|
||||
func GetClient() (*Client, error) {
|
||||
mc, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(CONFIG.GetApplyURI()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := &Client{mc: mc}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// Ping
|
||||
func (c *Client) Ping() error {
|
||||
return c.mc.Ping(context.TODO(), readpref.Primary())
|
||||
}
|
||||
|
||||
// getCollection
|
||||
func (c *Client) getCollection(name string) *mongo.Collection {
|
||||
return c.mc.Database(CONFIG.DB).Collection(name)
|
||||
}
|
||||
|
||||
// Aggregate
|
||||
func (c *Client) Aggregate(name string, groupStage mongo.Pipeline) ([]bson.M, error) {
|
||||
// pass the stage into a pipeline
|
||||
// pass the pipeline as the second paramter in the Aggregate() method
|
||||
cursor, err := c.getCollection(name).Aggregate(context.TODO(), groupStage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// display the results
|
||||
results := []bson.M{}
|
||||
if err = cursor.All(context.TODO(), &results); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(context.TODO())
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// Find
|
||||
func (c *Client) Find(name string, filters ...interface{}) ([]bson.M, error) {
|
||||
var cursor *mongo.Cursor
|
||||
var err error
|
||||
if len(filters) == 0 {
|
||||
cursor, err = c.getCollection(name).Find(context.TODO(), bson.D{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
cursor, err = c.getCollection(name).Find(context.TODO(), filters[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var results []bson.M
|
||||
for cursor.Next(context.TODO()) {
|
||||
b := bson.M{}
|
||||
err := cursor.Decode(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, b)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// FindOne
|
||||
func (c *Client) FindOne(name string, filter interface{}) *mongo.SingleResult {
|
||||
return c.getCollection(name).FindOne(context.TODO(), filter)
|
||||
}
|
||||
|
||||
// Disconnect
|
||||
func (c *Client) Disconnect() error {
|
||||
return c.mc.Disconnect(context.TODO())
|
||||
}
|
||||
|
||||
// InsertOne
|
||||
func (c *Client) InsertOne(name string, filter interface{}) (interface{}, error) {
|
||||
cur, err := c.getCollection(name).InsertOne(context.TODO(), filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cur.InsertedID, nil
|
||||
}
|
||||
|
||||
// DeleteOne
|
||||
func (c *Client) DeleteOne(name string, filter interface{}) error {
|
||||
_, err := c.getCollection(name).DeleteOne(context.TODO(), filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateByID
|
||||
func (c *Client) UpdateOne(name string, filter, update interface{}) (*mongo.UpdateResult, error) {
|
||||
return c.getCollection(name).UpdateOne(context.TODO(), filter, update)
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"pet-house.com/core/g"
|
||||
"pet-house.com/core/server/cache"
|
||||
_ "pet-house.com/core/server/cache"
|
||||
)
|
||||
|
||||
func TestGetClient(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb getClient", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
})
|
||||
|
||||
}
|
||||
func TestPing(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb ping", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
err = client.Ping()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
func TestInsertOne(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb InsertOne", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
res, err := client.InsertOne("testing", bson.D{
|
||||
{Key: "name", Value: "pi"}, {Key: "value", Value: 3.14159},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if res == "" {
|
||||
t.Error("inserted id is empty")
|
||||
}
|
||||
})
|
||||
}
|
||||
func TestGetCollection(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb GetCollection", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
res := client.getCollection("testing")
|
||||
if res == nil {
|
||||
t.Error("Collection return empty")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAggregate(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb Aggregate", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
pipeline := mongo.Pipeline{
|
||||
{
|
||||
{"$match", bson.D{
|
||||
{"items.fruit", "banana"},
|
||||
}},
|
||||
},
|
||||
{
|
||||
{"$sort", bson.D{
|
||||
{"date", 1},
|
||||
}},
|
||||
},
|
||||
}
|
||||
res, err := client.Aggregate("testing", pipeline)
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if res == nil {
|
||||
t.Error("Collection return empty")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb Find", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
res, err := client.Find("testing", bson.D{{"end", nil}})
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if res == nil {
|
||||
t.Error("Collection return empty")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestFindOne(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb FindOne", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
res := client.FindOne("testing", bson.D{{"end", nil}})
|
||||
if res == nil {
|
||||
t.Error("Collection return empty")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteOne(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb DeleteOne", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
err = client.DeleteOne("testing", bson.D{{"end", nil}})
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateOne(t *testing.T) {
|
||||
CONFIG.Addr = g.TestMongoAddr
|
||||
defer Remove()
|
||||
defer cache.Remove()
|
||||
t.Run("test mongodb UpdateOne", func(t *testing.T) {
|
||||
client, err := GetClient()
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if client == nil {
|
||||
t.Error("mongodb clinet is nil")
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err = client.Disconnect(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
id, err := client.InsertOne("testing", bson.D{
|
||||
{Key: "name", Value: "pi"}, {Key: "value", Value: 3.14159},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
b := bson.D{
|
||||
{Key: "$set", Value: bson.D{
|
||||
{Key: "name", Value: "pi"},
|
||||
{Key: "value", Value: 3.1415926},
|
||||
}},
|
||||
}
|
||||
res, err := client.UpdateOne("testing", bson.D{{"_id", id}}, b)
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if res == nil {
|
||||
t.Error("Collection return empty")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user