https://github.com/angrypufferfish/goodm
Golang ODM based on the official MongoDB driver.
https://github.com/angrypufferfish/goodm
go golang goodm mongo mongo-driver mongodb odm
Last synced: 10 months ago
JSON representation
Golang ODM based on the official MongoDB driver.
- Host: GitHub
- URL: https://github.com/angrypufferfish/goodm
- Owner: angrypufferfish
- License: apache-2.0
- Created: 2023-09-25T22:30:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-13T09:51:10.000Z (over 2 years ago)
- Last Synced: 2025-04-14T06:12:00.589Z (about 1 year ago)
- Topics: go, golang, goodm, mongo, mongo-driver, mongodb, odm
- Language: Go
- Homepage:
- Size: 5.83 MB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Goodm
- About The Project
- Installation
- Connection
- Define Document
- Save a Document
- Save and serialize output
- List documents
- Operators
- Usage
- Contributing
- License
## About The Project
Golang ODM based on the official MongoDB driver.
## Installation
```sh
go get github.com/angrypufferfish/goodm
```
## Connection
```go
import (
"context"
"github.com/angrypufferfish/goodm"
)
func Connect() {
var iGoodm goodm.Goodm
ctx := context.Background()
client, err := iGoodm.Connect("mongodb://localhost:27017", 10000)
///!
defer iGoodm.Disconnect()
if err != nil {
panic(err)
}
client.UseDatabase("test", &ctx)
}
```
## Define Document
```go
package example
import (
"github.com/angrypufferfish/goodm/src/base"
)
type UserInfo struct {
Address string `json:"address" bson:"address"`
}
type User struct {
//Define collection name on goodm tag
base.BaseDocument `json:",inline" bson:",inline" goodm:"users"`
LastName string `json:"lastName" bson:"lastName"`
FirstName string `json:"firstName" bson:"firstName"`
Info UserInfo `json:"info" bson:"info"`
}
```
## Usage
### Save a document
```go
package example
import (
"github.com/angrypufferfish/goodm"
)
user := &User{
FirstName: "Mario",
LastName: "Rossi",
Info: UserInfo{
Address: "via campo",
},
}
savedUser := goodm.Create[User](user)
```
### Save and serialize output
```go
package example
import (
"github.com/angrypufferfish/goodm"
)
type UserName struct {
FirstName string `json:"firstName" bson:"firstName"`
LastName string `json:"lastName" bson:"lastName"`
}
user := &User{
FirstName: "Mario",
LastName: "Rossi",
Info: UserInfo{
Address: "via campo",
},
}
savedUser := goodm.CreateAndSerialize[User, UserName](user)
//output usr
//{
// "firstName": "Mario",
// "lastName": "Rossi",
//}
```
### List
```go
package example
import (
"github.com/angrypufferfish/goodm"
)
type UserName struct {
FirstName string `json:"firstName" bson:"firstName"`
LastName string `json:"lastName" bson:"lastName"`
}
//Output users: []User
users := goodm.FindAll[User]()
//Output serializedUsers: []UserName
serializedUsers := goodm.FindAllAndSerialize[User, UserName]()
```
### Operators
```go
package example
import (
"github.com/angrypufferfish/goodm"
"github.com/angrypufferfish/goodm/src/query"
)
/// {"$and": [
/// {"lastName": {"$ne": "Doe"}},
/// {"firstName": {"$eq": "Mario"}}
/// ]}
users := goodm.Find[User](
query.And(
///{"lastName": {"$ne": "Doe"}}
query.Ne("lastName", "Doe"),
///{"firstName": {"$eq": "Mario"}}
query.Eq("firstName", "Mario"),
),
)
```
## Contributing
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue.
## License
Distributed under the APACHE LICENSE 2.0 See `LICENSE.txt` for more information.