{"id":20802973,"url":"https://github.com/sv-tools/mongoifc","last_synced_at":"2025-07-02T23:33:47.069Z","repository":{"id":38357490,"uuid":"356914590","full_name":"sv-tools/mongoifc","owner":"sv-tools","description":"The implementation of the interfaces for the official MongoDB driver in Go","archived":false,"fork":false,"pushed_at":"2025-05-01T21:37:23.000Z","size":404,"stargazers_count":35,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-01T21:38:42.994Z","etag":null,"topics":["database","database-layer","go","golang","interfaces","mongo","mongodb","mongodb-wrapper"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sv-tools.png","metadata":{"files":{"readme":"README.md","changelog":"change_stream.go","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-04-11T16:06:54.000Z","updated_at":"2025-05-01T21:37:25.000Z","dependencies_parsed_at":"2023-11-27T04:23:45.456Z","dependency_job_id":"bf48315f-ea32-489d-9afe-0de11eb343ef","html_url":"https://github.com/sv-tools/mongoifc","commit_stats":null,"previous_names":[],"tags_count":36,"template":false,"template_full_name":"sv-tools/go-repo-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sv-tools%2Fmongoifc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sv-tools%2Fmongoifc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sv-tools%2Fmongoifc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sv-tools%2Fmongoifc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sv-tools","download_url":"https://codeload.github.com/sv-tools/mongoifc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252794295,"owners_count":21805174,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","database-layer","go","golang","interfaces","mongo","mongodb","mongodb-wrapper"],"created_at":"2024-11-17T18:39:38.799Z","updated_at":"2025-07-02T23:33:47.050Z","avatar_url":"https://github.com/sv-tools.png","language":"Go","readme":"# mongoifc\n\n[![Code Analysis](https://github.com/sv-tools/mongoifc/actions/workflows/code.yaml/badge.svg)](https://github.com/sv-tools/mongoifc/actions/workflows/code.yaml)\n[![Go Reference](https://pkg.go.dev/badge/github.com/sv-tools/mongoifc.svg)](https://pkg.go.dev/github.com/sv-tools/mongoifc)\n[![codecov](https://codecov.io/gh/sv-tools/mongoifc/branch/main/graph/badge.svg?token=0XVOTDR1CW)](https://codecov.io/gh/sv-tools/mongoifc)\n[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/sv-tools/mongoifc?style=flat)](https://github.com/sv-tools/mongoifc/releases)\n[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/8803/badge)](https://www.bestpractices.dev/projects/8803)\n[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/sv-tools/mongoifc/badge)](https://securityscorecards.dev/viewer/?uri=github.com/sv-tools/mongoifc)\n\nThe Interfaces for the [MongoDB driver](https://github.com/mongodb/mongo-go-driver).\n\n```shell\ngo get -u github.com/sv-tools/mongoifc/v2\n```\n\n## Versioning Policy\n\nThe `mongoifc` code is stabilized, so now the version will match the version of the MongoDB driver since `v1.8.0`.\n\nIn case of need for bug fixes in `mongoifc`, the version will be in this format `v1.8.1+N`, where `v1.8.1` is the\nversion of MongoDB driver and `N` is a patch of `mongoifc`. The new version for changes in README.md, tests, examples,\nGitHub workflows is not required.\n\n## :bangbang: **Important**\n\nIt is not a simple drop in replacement because of the limitations in Go. You should rewrite your code to use this\nlibrary instead of mongo driver.\n\n```go\nconn := mongoifc.Connect(...)\n```\n\ninstead of\n\n```go\nconn := mongo.Connect(...)\n```\n\nor if you have a special code that returns the mongo object then you need to use one of `Wrap` functions to wrap\nthe `mongo.Client` or `mongo.Database` or `mongo.Collection` or `mongo.Session` objects:\n\n```go\norig := mongo.Connect(...)\n\n...\n\nconn := mongoifc.WrapClient(orig)\n```\n\nor\n\n```go\nfunc GetTenantDB(ctx context.Context, tenantID, dbName string) (*mongo.Database, error) {\n// a code that returns a special database for a given tenant and database name\n}\n\n...\n\norig, err := GetTenantDB(ctx, tenant, \"users\")\nif err != nil {\n...\n}\ndb = mongoifc.WrapDatabase(orig)\n```\n\nNow let's dig a bit into the limitations. Assume that you have a function to return a list of admin users, and you\nrewrote it using `mongoifc`:\n\n```go\npackage users\n\n// Original: func GetAdmins(ctx context.Context, db *mongo.Database) ([]*User, error)\nfunc GetAdmins(ctx context.Context, db mongoifc.Database) ([]User, error) {\n\tvar users []User\n\tcur, err := db.Collection(UsersCollection).Find(ctx, User{\n\t\tActive:  true,\n\t\tIsAdmin: true,\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif err := cur.All(ctx, \u0026users); err != nil {\n\t\treturn nil, err\n\t}\n\treturn users, err\n}\n```\n\nand if you pass an object of `*mongo.Database` type instead of `mongoifc.Database`\n\n```go\nconn, _ := mongo.Connect(context.Background(), ...)\ndb := conn.Database(...)\n\nusers.GetAdmins(context.Background(), db)\n```\n\nthen compilation fails with such error:\n\n     cannot use db (type *mongo.Database) as type mongoifc.Database in argument to simple.GetAdmins:\n         *mongo.Database does not implement mongoifc.Database (wrong type for Aggregate method)\n             have Aggregate(context.Context, interface {}, ...*\"go.mongodb.org/mongo-driver/mongo/options\".AggregateOptions) (*mongo.Cursor, error)\n             want Aggregate(context.Context, interface {}, ...*\"go.mongodb.org/mongo-driver/mongo/options\".AggregateOptions) (mongoifc.Cursor, error)\n\nThis is the main reason of wrapping the original objects and using the `mongoifc` instead.\n\n## Wrapped Interfaces\n\n- [x] ChangeStream: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#ChangeStream\n- [x] Client: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Client\n- [x] ClientEncryption: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#ClientEncryption\n- [x] Collection: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Collection\n- [x] Cursor: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Cursor\n- [x] Database: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Database\n- [x] IndexView: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#IndexView\n- [x] Session: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Session\n- [x] SingleResult: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#SingleResult\n- [x] DistinctResult: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#DistinctResult\n- [x] GridFSBucket: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#GridFSBucket\n- [x] GridFSUploadStream: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#GridFSUploadStream\n- [x] GridFSDownloadStream: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#GridFSDownloadStream\n\n## Mocks\n\nThe `mocks` folder contains the mocks generated by [mockery](https://github.com/vektra/mockery)\nand [gomock](https://github.com/uber-go/mock) tools.\n\nThe examples of how to use the mocks can be found in the `examples` folder or check any of the `*_test.go` files as\nwell.\n\n## Simple Example\n\n### user workflow\n1. Create 4 users, with two admins, using `InsertMany` function.\n2. Get the admin users only using `Find` function\n3. Delete all users using `DeleteMany` function\n\n* [users.go](https://github.com/sv-tools/mongoifc/blob/main/examples/simple/users.go) is a file with a set of functions, like:\n  * `Create` to create the users using `InsertMany`\n  * `Delete` to delete the users by given IDs\n  * `GetAdmins` to return the list of admin users\n* [users_test.go](https://github.com/sv-tools/mongoifc/blob/main/examples/simple/users_test.go) is a file with `TestUsersWorkflow` unit tests:\n  * `mockery` tests the workflow using `mockery` mocks\n  * `gomock` tests the workflow using `gomock` mocks\n  * `docker` tests the workflow using real mongo database run by docker\n\n### collection workflow\n1. Create a collection with random name.\n2. Check that the collection exists.\n3. Check that another collection does not exist.\n4. Drop collection.\n5. Check that the original collection does not exist.\n\n* [collections.go](https://github.com/sv-tools/mongoifc/blob/main/examples/simple/collections.go) is a file with a set of functions, like:\n  * `CreateCollection` to create a collection using `CreateCollection`\n  * `DropCollection` to delete a collection by given name\n  * `CollectionExists` to check that a collection exists\n* [collections_test.go](https://github.com/sv-tools/mongoifc/blob/main/examples/simple/collections_test.go) is a file with `TestCollectionsWorkflow` unit tests:\n  * `mockery` tests the workflow using `mockery` mocks\n  * `gomock` tests the workflow using `gomock` mocks\n  * `docker` tests the workflow using real mongo database run by docker\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsv-tools%2Fmongoifc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsv-tools%2Fmongoifc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsv-tools%2Fmongoifc/lists"}