{"id":17605941,"url":"https://github.com/lyearn/mgod","last_synced_at":"2025-04-30T10:48:01.614Z","repository":{"id":209840444,"uuid":"722421207","full_name":"Lyearn/mgod","owner":"Lyearn","description":"MongoDB ODM for Go - Designed to work efficiently with Go models and official Mongo Go driver","archived":false,"fork":false,"pushed_at":"2024-11-06T14:49:25.000Z","size":729,"stargazers_count":38,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-30T10:47:41.178Z","etag":null,"topics":["go","models","mongo","mongo-go-models","mongodb","mongodb-go-odm","mongodb-odm","odm"],"latest_commit_sha":null,"homepage":"https://lyearn.github.io/mgod/","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/Lyearn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-23T05:35:22.000Z","updated_at":"2025-03-01T10:20:19.000Z","dependencies_parsed_at":"2025-03-09T03:32:08.586Z","dependency_job_id":"f0fa89bc-d4df-44b3-8aec-1bf066422d0a","html_url":"https://github.com/Lyearn/mgod","commit_stats":null,"previous_names":["lyearn/mgod"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyearn%2Fmgod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyearn%2Fmgod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyearn%2Fmgod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyearn%2Fmgod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lyearn","download_url":"https://codeload.github.com/Lyearn/mgod/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251685078,"owners_count":21627233,"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":["go","models","mongo","mongo-go-models","mongodb","mongodb-go-odm","mongodb-odm","odm"],"created_at":"2024-10-22T15:12:23.447Z","updated_at":"2025-04-30T10:48:01.571Z","avatar_url":"https://github.com/Lyearn.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable-next-line MD041 --\u003e\n![logo](./images/logo.png)\n\u003c!-- prettier-ignore-end --\u003e\n\n# mgod\n\n`mgod` is a MongoDB ODM specifically designed for Go. It provides a structured way to map Go models to MongoDB collections, simplifying database interactions in Go applications.\n\n- [Features](#features)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Basic Usage](#basic-usage)\n- [Motivation](#motivation)\n- [Future Scope](#future-scope)\n- [Documentation](#documentation)\n- [Contribute](#contribute)\n- [License](#license)\n\n## Features\n- **Reuse existing Go structs** to define models and perform Mongo operations.\n- Automatic **field transformation** between Go and MongoDB using struct tags.\n- Easily manage **meta fields** in models without cluttering Go structs.\n- Supports **union types**, expanding data capabilities.\n- Implement strict field requirements with struct tags for **data integrity**.\n- Built-in support for **multi-tenant** systems.\n- Wrapper around the **official** Mongo Go Driver.\n\n## Requirements\n- Go 1.18 or higher.\n- MongoDB 3.6 and higher.\n\n\u003e [!WARNING]\n\u003e For MongoDB version **\u003c4.4**, please create the collection in MongoDB before creating an `EntityMongoModel` using `mgod` for the same.\n\u003e Refer to [this MongoDB limitations](https://www.mongodb.com/docs/manual/reference/limits/#operations) for more information.\n\n## Installation\n```\ngo get github.com/Lyearn/mgod\n```\n\n## Basic Usage\nUse existing MongoDB connection, or setup a new one to register a default database connection.\n\nFor existing database connection,\n```go\nimport \"github.com/Lyearn/mgod\"\n\nfunc init() {\n\t// client is the MongoDB client obtained using Go Mongo Driver's Connect method.\n\tmgod.SetDefaultClient(client)\n}\n```\n\nTo setup a new connection,\n```go\nimport (\n\t\"time\"\n\n\t\"github.com/Lyearn/mgod\"\n\t\"go.mongodb.org/mongo-driver/mongo/options\"\n)\n\nfunc init() {\n\t// `cfg` is optional. Can rely on default configurations by providing `nil` value in argument.\n\tcfg := \u0026mgod.ConnectionConfig{Timeout: 5 * time.Second}\n\topts := options.Client().ApplyURI(\"mongodb://root:mgod123@localhost:27017\")\n\n\terr := mgod.ConfigureDefaultClient(cfg, opts)\n}\n```\n\nAdd tags _(wherever applicable)_ in existing struct _(or define a new model)_.\n```go\ntype User struct {\n\tName     string\n\tEmailID  string `bson:\"emailId\"`\n\tAge      *int32 `bson:\",omitempty\"`\n\tJoinedOn string `bson:\"joinedOn\" mgoType:\"date\"`\n}\n```\n\nUse `mgod` to get the entity ODM.\n```go\nimport (\n\t\"github.com/Lyearn/mgod\"\n\t\"github.com/Lyearn/mgod/schema/schemaopt\"\n)\n\nmodel := User{}\ndbName := \"mgoddb\"\ncollection := \"users\"\n\nopts := mgod.NewEntityMongoModelOptions(dbName, collection, nil)\nuserModel, _ := mgod.NewEntityMongoModel(model, *opts)\n```\n\nUse the entity ODM to perform CRUD operations with ease.\n\nInsert a new document.\n```go\njoinedOn, _ := dateformatter.New(time.Now()).GetISOString()\nuserDoc := User{\n\tName: \"Gopher\",\n\tEmailID: \"gopher@mgod.com\",\n\tJoinedOn: joinedOn,\n}\nuser, _ := userModel.InsertOne(context.TODO(), userDoc)\n```\n\n**Output:**\n```js\n{\n\t\"_id\": ObjectId(\"65697705d4cbed00e8aba717\"),\n\t\"name\": \"Gopher\",\n\t\"emailId\": \"gopher@mgod.com\",\n\t\"joinedOn\": ISODate(\"2023-12-01T11:32:19.290Z\"),\n\t\"__v\": 0\n}\n```\n---\n\nFind documents using model properties.\n\n```go\nusers, _ := userModel.Find(context.TODO(), bson.M{\"name\": userDoc.Name})\n```\n\n**Output:**\n```go\n[]User{\n\tUser{\n\t\tName: \"Gopher\",\n\t\tEmailID: \"gopher@mgod.com\",\n\t\tJoinedOn: \"2023-12-01T11:32:19.290Z\",\n\t}\n}\n```\n\n## Motivation\nCreating `mgod` was driven by the need to **simplify MongoDB interactions** while keeping **one schema for all** in Go. Traditionally, working with MongoDB in Go involved either using separate structs for database and service logic or manually converting service structs to MongoDB documents, a process that was both time-consuming and error-prone. This lack of integration often led to redundant coding, especially when dealing with union types or adding meta fields for each MongoDB operation.\n\nInspired by the easy interface of MongoDB handling using [Mongoose](https://github.com/Automattic/mongoose) and [Typegoose](https://github.com/typegoose/typegoose) libraries available in Node, `mgod` aims to streamline these processes. It offers a more integrated approach, reducing the need to duplicate code and enhancing type safety, making MongoDB operations more intuitive and efficient in Go.\n\n## Future Scope\nThe current version of mgod is a stable release. However, there are plans to add a lot more features like -\n- [x] Implement a setup step for storing a default Mongo connection, eliminating the need to pass it during EntityMongoModel creation.\n- [x] Provide support for transactions following the integration of default Mongo connection logic.\n- [ ] Enable functionality to opt out of the default conversion of date fields to ISOString format.\n- [ ] Develop easy to use wrapper functions around MongoDB Aggregation operation.\n- [ ] Introduce automatic MongoDB collection selection based on Go struct names as a default behavior.\n- [ ] Add test cases to improve code coverage.\n\nIf you have any interesting feature requests, feel free to open an issue on [GitHub issue tracker](https://github.com/Lyearn/mgod/issues). We will be more than happy to discuss that!\n\n## Documentation\nFor user facing documentation, check out [docs](docs/README.md).\n\n## Contribute\nFor contribution guidelines, check out [CONTRIBUTING](CONTRIBUTING.md).\n\n\u003c!-- ## Documentation --\u003e\n\n## License\n`mgod` is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flyearn%2Fmgod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flyearn%2Fmgod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flyearn%2Fmgod/lists"}