diff --git a/.travis.yml b/.travis.yml index 39c01fb..361d2ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - "1.14" + - "1.16" env: - GO111MODULE=on services: diff --git a/README.md b/README.md index 718a31a..ad56891 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/qiniu/qmgo)](https://goreportcard.com/report/github.com/qiniu/qmgo) [![GitHub release](https://img.shields.io/github/v/tag/qiniu/qmgo.svg?label=release)](https://github.com/qiniu/qmgo/releases) [![GoDoc](https://pkg.go.dev/badge/github.com/qiniu/qmgo?status.svg)](https://pkg.go.dev/github.com/qiniu/qmgo?tab=doc) -[![Join the chat at https://gitter.im/qiniu/qmgo](https://badges.gitter.im/qiniu/qmgo.svg)](https://gitter.im/qiniu/qmgo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) English | [简体中文](README_ZH.md) diff --git a/aggregate.go b/aggregate.go index 5feb162..28cc320 100644 --- a/aggregate.go +++ b/aggregate.go @@ -72,7 +72,13 @@ func (a *Aggregate) One(result interface{}) error { } // Iter return the cursor after aggregate +// Deprecated, please use Cursor func (a *Aggregate) Iter() CursorI { + return a.Cursor() +} + +// Cursor return the cursor after aggregate +func (a *Aggregate) Cursor() CursorI { opts := options.Aggregate() if len(a.options) > 0 { opts = a.options[0].AggregateOptions diff --git a/client.go b/client.go index 5c76b57..f9c056d 100644 --- a/client.go +++ b/client.go @@ -24,7 +24,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/mongo" - opts "go.mongodb.org/mongo-driver/mongo/options" + officialOpts "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" ) @@ -153,7 +153,7 @@ func NewClient(ctx context.Context, conf *Config, o ...options.ClientOptions) (c } // client creates connection to MongoDB -func client(ctx context.Context, opt *opts.ClientOptions) (client *mongo.Client, err error) { +func client(ctx context.Context, opt *officialOpts.ClientOptions) (client *mongo.Client, err error) { client, err = mongo.Connect(ctx, opt) if err != nil { fmt.Println(err) @@ -173,10 +173,10 @@ func client(ctx context.Context, opt *opts.ClientOptions) (client *mongo.Client, // Qmgo will follow this way official mongodb driver do: // - the configuration in uri takes precedence over the configuration in the setter // - Check the validity of the configuration in the uri, while the configuration in the setter is basically not checked -func newConnectOpts(conf *Config, o ...options.ClientOptions) (*opts.ClientOptions, error) { - option := opts.Client() +func newConnectOpts(conf *Config, o ...options.ClientOptions) (*officialOpts.ClientOptions, error) { + option := officialOpts.Client() for _, apply := range o { - option = opts.MergeClientOptions(apply.ClientOptions) + option = officialOpts.MergeClientOptions(apply.ClientOptions) } if conf.ConnectTimeoutMS != nil { timeoutDur := time.Duration(*conf.ConnectTimeoutMS) * time.Millisecond @@ -215,7 +215,7 @@ func newConnectOpts(conf *Config, o ...options.ClientOptions) (*opts.ClientOptio } // newAuth create options.Credential from conf.Auth -func newAuth(auth Credential) (credential opts.Credential, err error) { +func newAuth(auth Credential) (credential officialOpts.Credential, err error) { if auth.AuthMechanism != "" { credential.AuthMechanism = auth.AuthMechanism } @@ -288,19 +288,18 @@ func (c *Client) Ping(timeout int64) error { // Database create connection to database func (c *Client) Database(name string, options ...*options.DatabaseOptions) *Database { - opts := opts.Database() - if len(options) > 0 { - if options[0].DatabaseOptions != nil { - opts = options[0].DatabaseOptions - } + opts := make([]*officialOpts.DatabaseOptions, 0, len(options)) + for _, o := range options { + opts = append(opts, o.DatabaseOptions) } - return &Database{database: c.client.Database(name, opts), registry: c.registry} + databaseOpts := officialOpts.MergeDatabaseOptions(opts...) + return &Database{database: c.client.Database(name, databaseOpts), registry: c.registry} } // Session create one session on client // Watch out, close session after operation done func (c *Client) Session(opt ...*options.SessionOptions) (*Session, error) { - sessionOpts := opts.Session() + sessionOpts := officialOpts.Session() if len(opt) > 0 && opt[0].SessionOptions != nil { sessionOpts = opt[0].SessionOptions } diff --git a/client_test.go b/client_test.go index 427b23f..a771a99 100644 --- a/client_test.go +++ b/client_test.go @@ -130,7 +130,8 @@ func TestClient(t *testing.T) { ast.Equal(nil, err) opts := &options.DatabaseOptions{DatabaseOptions: officialOpts.Database().SetReadPreference(readpref.PrimaryPreferred())} - coll := c.Database("qmgotest", opts).Collection("testopen") + cOpts := &options.CollectionOptions{CollectionOptions: officialOpts.Collection().SetReadPreference(readpref.PrimaryPreferred())} + coll := c.Database("qmgotest", opts).Collection("testopen", cOpts) res, err := coll.InsertOne(context.Background(), bson.D{{Key: "x", Value: 1}}) ast.NoError(err) diff --git a/database.go b/database.go index b03a457..3546e39 100644 --- a/database.go +++ b/database.go @@ -16,10 +16,10 @@ package qmgo import ( "context" - opts "github.com/qiniu/qmgo/options" + "github.com/qiniu/qmgo/options" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + officialOpts "go.mongodb.org/mongo-driver/mongo/options" ) // Database is a handle to a MongoDB database @@ -30,9 +30,14 @@ type Database struct { } // Collection gets collection from database -func (d *Database) Collection(name string) *Collection { +func (d *Database) Collection(name string, opts ...*options.CollectionOptions) *Collection { var cp *mongo.Collection - cp = d.database.Collection(name) + var opt = make([]*officialOpts.CollectionOptions, 0, len(opts)) + for _, o := range opts { + opt = append(opt, o.CollectionOptions) + } + collOpt := officialOpts.MergeCollectionOptions(opt...) + cp = d.database.Collection(name, collOpt) return &Collection{ collection: cp, @@ -57,8 +62,8 @@ func (d *Database) DropDatabase(ctx context.Context) error { // If the command document contains a session ID or any transaction-specific fields, the behavior is undefined. // // The opts parameter can be used to specify options for this operation (see the options.RunCmdOptions documentation). -func (d *Database) RunCommand(ctx context.Context, runCommand interface{}, opts ...opts.RunCommandOptions) *mongo.SingleResult { - option := options.RunCmd() +func (d *Database) RunCommand(ctx context.Context, runCommand interface{}, opts ...options.RunCommandOptions) *mongo.SingleResult { + option := officialOpts.RunCmd() if len(opts) > 0 && opts[0].RunCmdOptions != nil { option = opts[0].RunCmdOptions } @@ -71,12 +76,12 @@ func (d *Database) RunCommand(ctx context.Context, runCommand interface{}, opts // // The opts parameter can be used to specify options for the operation (see the options.CreateCollectionOptions // documentation). -func (db *Database) CreateCollection(ctx context.Context, name string, opts ...opts.CreateCollectionOptions) error { - var option = make([]*options.CreateCollectionOptions,0,len(opts)) - for _,opt := range opts{ - if opt.CreateCollectionOptions != nil{ - option = append(option,opt.CreateCollectionOptions) +func (db *Database) CreateCollection(ctx context.Context, name string, opts ...options.CreateCollectionOptions) error { + var option = make([]*officialOpts.CreateCollectionOptions, 0, len(opts)) + for _, opt := range opts { + if opt.CreateCollectionOptions != nil { + option = append(option, opt.CreateCollectionOptions) } } - return db.database.CreateCollection(ctx,name,option...) -} \ No newline at end of file + return db.database.CreateCollection(ctx, name, option...) +} diff --git a/go.mod b/go.mod index ee2df0a..2cb352e 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,5 @@ go 1.16 require ( github.com/go-playground/validator/v10 v10.4.1 github.com/stretchr/testify v1.6.1 - go.mongodb.org/mongo-driver v1.9.0 + go.mongodb.org/mongo-driver v1.11.6 ) diff --git a/go.sum b/go.sum index 1350751..f2a486a 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,6 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= -github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= @@ -24,6 +22,7 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -38,39 +37,41 @@ github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= -github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w= -github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= -github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc= -github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= +github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= +github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= +github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs= +github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -go.mongodb.org/mongo-driver v1.9.0 h1:f3aLGJvQmBl8d9S40IL+jEyBC6hfLPbJjv9t5hEM9ck= -go.mongodb.org/mongo-driver v1.9.0/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= +go.mongodb.org/mongo-driver v1.11.6 h1:XM7G6PjiGAO5betLF13BIa5TlLUUE3uJ/2Ox3Lz1K+o= +go.mongodb.org/mongo-driver v1.11.6/go.mod h1:G9TgswdsWjX4tmDA5zfs2+6AEPpYJwqblyjsfuh8oXY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU= -golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= -golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/interface.go b/interface.go index aef0d32..f69e750 100644 --- a/interface.go +++ b/interface.go @@ -72,5 +72,6 @@ type QueryI interface { type AggregateI interface { All(results interface{}) error One(result interface{}) error - Iter() CursorI + Iter() CursorI // Deprecated, please use Cursor instead + Cursor() CursorI } diff --git a/options/collection_options.go b/options/collection_options.go new file mode 100644 index 0000000..cdd4c0c --- /dev/null +++ b/options/collection_options.go @@ -0,0 +1,7 @@ +package options + +import "go.mongodb.org/mongo-driver/mongo/options" + +type CollectionOptions struct { + *options.CollectionOptions +}