Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The implementation of the interfaces for the official MongoDB driver in Go

License

NotificationsYou must be signed in to change notification settings

sv-tools/mongoifc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Code AnalysisGo ReferencecodecovGitHub tag (latest SemVer)OpenSSF Best PracticesOpenSSF Scorecard

The Interfaces for theMongoDB driver.

go get -u github.com/sv-tools/mongoifc/v2

Versioning Policy

Themongoifc code is stabilized, so now the version will match the version of the MongoDB driver sincev1.8.0.

In case of need for bug fixes inmongoifc, the version will be in this formatv1.8.1+N, wherev1.8.1 is theversion of MongoDB driver andN is a patch ofmongoifc. The new version for changes in README.md, tests, examples,GitHub workflows is not required.

‼️Important

It is not a simple drop in replacement because of the limitations in Go. You should rewrite your code to use thislibrary instead of mongo driver.

conn:= mongoifc.Connect(...)

instead of

conn:= mongo.Connect(...)

or if you have a special code that returns the mongo object then you need to use one ofWrap functions to wrapthemongo.Client ormongo.Database ormongo.Collection ormongo.Session objects:

orig:= mongo.Connect(...)...conn:= mongoifc.WrapClient(orig)

or

funcGetTenantDB(ctx context.Context,tenantID,dbNamestring) (*mongo.Database,error) {// a code that returns a special database for a given tenant and database name}...orig,err:=GetTenantDB(ctx,tenant,"users")iferr!=nil {...}db=mongoifc.WrapDatabase(orig)

Now let's dig a bit into the limitations. Assume that you have a function to return a list of admin users, and yourewrote it usingmongoifc:

package users// Original: func GetAdmins(ctx context.Context, db *mongo.Database) ([]*User, error)funcGetAdmins(ctx context.Context,db mongoifc.Database) ([]User,error) {varusers []Usercur,err:=db.Collection(UsersCollection).Find(ctx,User{Active:true,IsAdmin:true,})iferr!=nil {returnnil,err}iferr:=cur.All(ctx,&users);err!=nil {returnnil,err}returnusers,err}

and if you pass an object of*mongo.Database type instead ofmongoifc.Database

conn,_:= mongo.Connect(context.Background(),...)db:= conn.Database(...)users.GetAdmins(context.Background(),db)

then compilation fails with such error:

 cannot use db (type *mongo.Database) as type mongoifc.Database in argument to simple.GetAdmins:     *mongo.Database does not implement mongoifc.Database (wrong type for Aggregate method)         have Aggregate(context.Context, interface {}, ...*"go.mongodb.org/mongo-driver/mongo/options".AggregateOptions) (*mongo.Cursor, error)         want Aggregate(context.Context, interface {}, ...*"go.mongodb.org/mongo-driver/mongo/options".AggregateOptions) (mongoifc.Cursor, error)

This is the main reason of wrapping the original objects and using themongoifc instead.

Wrapped Interfaces

Mocks

Themocks folder contains the mocks generated bymockeryandgomock tools.

The examples of how to use the mocks can be found in theexamples folder or check any of the*_test.go files aswell.

Simple Example

user workflow

  1. Create 4 users, with two admins, usingInsertMany function.
  2. Get the admin users only usingFind function
  3. Delete all users usingDeleteMany function
  • users.go is a file with a set of functions, like:
    • Create to create the users usingInsertMany
    • Delete to delete the users by given IDs
    • GetAdmins to return the list of admin users
  • users_test.go is a file withTestUsersWorkflow unit tests:
    • mockery tests the workflow usingmockery mocks
    • gomock tests the workflow usinggomock mocks
    • docker tests the workflow using real mongo database run by docker

collection workflow

  1. Create a collection with random name.
  2. Check that the collection exists.
  3. Check that another collection does not exist.
  4. Drop collection.
  5. Check that the original collection does not exist.
  • collections.go is a file with a set of functions, like:
    • CreateCollection to create a collection usingCreateCollection
    • DropCollection to delete a collection by given name
    • CollectionExists to check that a collection exists
  • collections_test.go is a file withTestCollectionsWorkflow unit tests:
    • mockery tests the workflow usingmockery mocks
    • gomock tests the workflow usinggomock mocks
    • docker tests the workflow using real mongo database run by docker

About

The implementation of the interfaces for the official MongoDB driver in Go

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors5

Languages


[8]ページ先頭

©2009-2025 Movatter.jp