diff --git a/aggregate.go b/aggregate.go index 5a23133..6413ebb 100644 --- a/aggregate.go +++ b/aggregate.go @@ -7,8 +7,10 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) +// Pipeline define the pipeline for aggregate type Pipeline []bson.D +// Aggregate is a handle to a aggregate type Aggregate struct { ctx context.Context pipeline interface{} @@ -37,7 +39,7 @@ func (a *Aggregate) One(result interface{}) error { } defer cr.Close() if !cr.Next(result) { - return ERR_NO_SUCH_RECORD + return ErrNoSuchDocuments } return err } diff --git a/base.go b/base.go index 6655998..5315183 100644 --- a/base.go +++ b/base.go @@ -2,9 +2,10 @@ package qmgo import ( "strings" + "time" ) -// QmgoConfig for initial mongodb instance +// Config for initial mongodb instance type Config struct { // URI example: [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options] // URI Reference: https://docs.mongodb.com/manual/reference/connection-string/ @@ -30,10 +31,10 @@ func IsDup(err error) bool { return strings.Contains(err.Error(), "E11000") } -//// Now return Millisecond current time -//func Now() time.Time { -// return time.Unix(0, time.Now().UnixNano()/1e6*1e6) -//} +// Now return Millisecond current time +func Now() time.Time { + return time.Unix(0, time.Now().UnixNano()/1e6*1e6) +} // SplitSortField handle sort symbol: "+"/"-" in front of field // if "+", return sort as 1 diff --git a/base_test.go b/base_test.go new file mode 100644 index 0000000..0320dc8 --- /dev/null +++ b/base_test.go @@ -0,0 +1,13 @@ +package qmgo + +import ( + "fmt" + "testing" + "time" +) + +func TestNow(t *testing.T) { + t1 := time.Unix(0, time.Now().UnixNano()/1e6*1e6) + t2 := Now() + fmt.Println(t1, t2) +} diff --git a/client.go b/client.go index 1c54da7..fcef308 100644 --- a/client.go +++ b/client.go @@ -10,7 +10,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/readpref" ) -// Client specifies the instance to operate mongoDB +// QmgoClient specifies the instance to operate mongoDB type QmgoClient struct { *Collection *Database diff --git a/collection.go b/collection.go index 0ea64b1..1e29d45 100644 --- a/collection.go +++ b/collection.go @@ -10,7 +10,7 @@ import ( "go.mongodb.org/mongo-driver/x/bsonx" ) -// Collection +// Collection is a handle to a MongoDB collection type Collection struct { collection *mongo.Collection } @@ -24,7 +24,7 @@ func (c *Collection) Find(ctx context.Context, filter interface{}) QueryI { } } -// Insert insert one document into the collection +// InsertOne insert one document into the collection // Reference: https://docs.mongodb.com/manual/reference/command/insert/ func (c *Collection) InsertOne(ctx context.Context, doc interface{}) (result *InsertOneResult, err error) { res, err := c.collection.InsertOne(ctx, doc) @@ -68,7 +68,7 @@ func (c *Collection) Update(ctx context.Context, filter interface{}, update inte } if res.MatchedCount == 0 { - err = ERR_NO_SUCH_RECORD + err = ErrNoSuchDocuments } return err @@ -96,7 +96,7 @@ func (c *Collection) Remove(ctx context.Context, filter interface{}) (err error) return err } if res.DeletedCount == 0 { - err = ERR_NO_SUCH_RECORD + err = ErrNoSuchDocuments } return err diff --git a/collection_test.go b/collection_test.go index 4aae78e..c257103 100644 --- a/collection_test.go +++ b/collection_test.go @@ -237,7 +237,7 @@ func TestCollection_Update(t *testing.T) { }, } err = cli.Update(context.Background(), filter2, update2) - ast.Equal(err, ERR_NO_SUCH_RECORD) + ast.Equal(err, ErrNoSuchDocuments) // filter is nil or wrong BSON Document format update3 := bson.M{ @@ -374,17 +374,19 @@ func TestCollection_Remove(t *testing.T) { "name": "Lily", } err = cli.Remove(context.Background(), filter2) - ast.Equal(err, ERR_NO_SUCH_RECORD) + ast.Equal(err, ErrNoSuchDocuments) // filter is bson.M{},delete one document filter3 := bson.M{} preCnt, err := cli.Find(context.Background(), filter3).Count() + ast.NoError(err) ast.Equal(int64(2), preCnt) err = cli.Remove(context.Background(), filter3) ast.NoError(err) afterCnt, err := cli.Find(context.Background(), filter3).Count() + ast.NoError(err) ast.Equal(preCnt-1, afterCnt) // filter is nil or wrong BSON Document format diff --git a/database.go b/database.go index 7e22ba5..029b14d 100644 --- a/database.go +++ b/database.go @@ -2,30 +2,14 @@ package qmgo import ( "context" - "fmt" - "go.mongodb.org/mongo-driver/mongo" ) +// Database is a handle to a MongoDB database type Database struct { database *mongo.Database } -// NewDatabase creates new database connection -func NewDatabase(ctx context.Context, conf *Config) (cli *Database, err error) { - client, err := client(ctx, conf) - if err != nil { - fmt.Println("new client fail", err) - return - } - db := client.Database(conf.Database) - - cli = &Database{ - database: db, - } - return -} - // Collection gets collection from database func (d *Database) Collection(name string) *Collection { var cp *mongo.Collection diff --git a/database_test.go b/database_test.go index 06365cd..321b26e 100644 --- a/database_test.go +++ b/database_test.go @@ -15,19 +15,8 @@ func TestDatabase(t *testing.T) { var maxPoolSize uint64 = 3000 collName := "testopen" dbName := "mongoxtest" - cfg := Config{ - Uri: "://localhost:27017", - Database: dbName, - Coll: collName, - ConnectTimeoutMS: &cTimeout, - SocketTimeoutMS: &sTimeout, - MaxPoolSize: &maxPoolSize, - } - cli, err := NewDatabase(context.Background(), &cfg) - ast.NotNil(err) - - cfg = Config{ + cfg := Config{ Uri: "mongodb://localhost:27017", Database: dbName, Coll: collName, @@ -36,7 +25,9 @@ func TestDatabase(t *testing.T) { MaxPoolSize: &maxPoolSize, } - cli, err = NewDatabase(context.Background(), &cfg) + c, err := NewClient(context.Background(), &cfg) + ast.NoError(err) + cli := c.Database(cfg.Database) ast.Nil(err) ast.Equal(dbName, cli.GetDatabaseName()) coll := cli.Collection(collName) diff --git a/errors.go b/errors.go index 1c372ec..763e54a 100644 --- a/errors.go +++ b/errors.go @@ -1,11 +1,20 @@ package qmgo -import "errors" +import ( + "errors" + + "go.mongodb.org/mongo-driver/mongo" +) var ( - ERR_QUERY_NOT_SLICE_POINTER = errors.New("result argument must be a pointer to a slice") - ERR_QUERY_NOT_SLICE_TYPE = errors.New("result argument must be a slice address") - ERR_QUERY_RESULT_TYPE_INCONSISTEN = errors.New("result type is not equal mongodb value type") - ERR_QUERY_RESULT_VAL_CAN_NOT_CHANGE = errors.New("the value of result can not be changed") - ERR_NO_SUCH_RECORD = errors.New("no such record") + // ErrQueryNotSlicePointer return if result argument is not a pointer to a slice + ErrQueryNotSlicePointer = errors.New("result argument must be a pointer to a slice") + // ErrQueryNotSliceType return if result argument is not slice address + ErrQueryNotSliceType = errors.New("result argument must be a slice address") + // ErrQueryResultTypeInconsistent return if result type is not equal mongodb value type + ErrQueryResultTypeInconsistent = errors.New("result type is not equal mongodb value type") + // ErrQueryResultValCanNotChange return if the value of result can not be changed + ErrQueryResultValCanNotChange = errors.New("the value of result can not be changed") + // ErrNoSuchDocuments return if no document found + ErrNoSuchDocuments = errors.New(mongo.ErrNoDocuments.Error()) ) diff --git a/interface.go b/interface.go index 276e788..d0ddbb4 100644 --- a/interface.go +++ b/interface.go @@ -37,6 +37,7 @@ type QueryI interface { Cursor() CursorI } +// AggregateI define the interface of aggregate type AggregateI interface { All(results interface{}) error One(result interface{}) error diff --git a/query.go b/query.go index d676f85..540a427 100644 --- a/query.go +++ b/query.go @@ -10,7 +10,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -// Query +// Query struct definition type Query struct { ctx context.Context collection *mongo.Collection @@ -167,7 +167,7 @@ func (q *Query) Distinct(key string, result interface{}) error { resultVal := reflect.ValueOf(result) if resultVal.Kind() != reflect.Ptr { - return ERR_QUERY_NOT_SLICE_POINTER + return ErrQueryNotSlicePointer } sliceVal := resultVal.Elem() @@ -175,11 +175,11 @@ func (q *Query) Distinct(key string, result interface{}) error { sliceVal = sliceVal.Elem() } if sliceVal.Kind() != reflect.Slice { - return ERR_QUERY_NOT_SLICE_TYPE + return ErrQueryNotSliceType } if !resultVal.Elem().CanSet() { - return ERR_QUERY_RESULT_VAL_CAN_NOT_CHANGE + return ErrQueryResultValCanNotChange } sliceVal = sliceVal.Slice(0, 0) @@ -197,7 +197,7 @@ func (q *Query) Distinct(key string, result interface{}) error { if vType != elementType { fmt.Printf("mongo type: %s, result type: %s\n", vType.Name(), elementType.Name()) - return ERR_QUERY_RESULT_TYPE_INCONSISTEN + return ErrQueryResultTypeInconsistent } sliceVal = reflect.Append(sliceVal, vValue) } diff --git a/query_test.go b/query_test.go index 2302a37..4bfd7dc 100644 --- a/query_test.go +++ b/query_test.go @@ -412,17 +412,17 @@ func TestQuery_Distinct(t *testing.T) { var res3 []int32 err = cli.Find(context.Background(), filter2).Distinct("age", res3) - ast.EqualError(err, ERR_QUERY_NOT_SLICE_POINTER.Error()) + ast.EqualError(err, ErrQueryNotSlicePointer.Error()) var res4 int err = cli.Find(context.Background(), filter2).Distinct("age", &res4) - ast.EqualError(err, ERR_QUERY_NOT_SLICE_TYPE.Error()) + ast.EqualError(err, ErrQueryNotSliceType.Error()) var res5 []string err = cli.Find(context.Background(), filter2).Distinct("age", &res5) - ast.EqualError(err, ERR_QUERY_RESULT_TYPE_INCONSISTEN.Error()) + ast.EqualError(err, ErrQueryResultTypeInconsistent.Error()) var res6 []int32