Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: about 1 month ago
JSON representation

Golang ODM based on the official MongoDB driver.

Awesome Lists containing this project

README

        

# Goodm


  1. About The Project

  2. Installation

  3. Connection

  4. Define Document

  5. Save a Document

  6. Save and serialize output

  7. List documents

  8. Operators

  9. Usage

  10. Contributing

  11. 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.