Sutando
A simple go ORM framework for mongoDB.
Install / Use
/learn @yanun0323/SutandoREADME
<a href="."><img height="200" src="./document/sutando.png"></a>
Requirement
Required go 1.18 up
Query Parameters
- Exists
- And
- Equal
- NotEqual
- Greater
- GreaterOrEqual
- Less
- LessOrEqual
- Contain
- In
- NotIn
- Sort
- Limit
- Skip
- Count
- Regex
Guide
Installation
$ go get -u github.com/yanun0323/sutando@latest
Example
Connect To MongoDB
- Create a new connection
// connect through host and port.
db, err := sutando.NewDB(ctx, sutando.Conn{
Username: "example",
Password: "example",
Host: "example",
Port: 27017,
DB: "example",
AdminAuth: true,
Pem: "",
ClientOptionsHandler: func(opts *options.ClientOptions) {
opts.SetConnectTimeout(5 * time.Second)
opts.SetTimeout(15 * time.Second)
},
})
// connect through SRV.
db, err := sutando.NewDB(ctx, sutando.ConnSrv{
Username: "example",
Password: "example",
Host: "example.mongo.net",
DB: "example",
AdminAuth: true,
Pem: "",
ClientOptionsHandler: func(opts *options.ClientOptions) {
opts.SetConnectTimeout(5 * time.Second)
opts.SetTimeout(15 * time.Second)
},
})
- Model Declaration
// Supported
type Element struct {
FirstName string // 'firstName' as mongo db field key
lastName string // 'lastName' as mongo db field key
Nickname bool `bson:"nick_name"` // using `bson:"xxx"` tag to assign field key to 'xxx'
Healthy bool `bson:"-"` // using `bson:"-"` tag to ignore this field
Children []string `bson:",omitempty"` // using `bson:",omitempty"` tag to ignore this field when it's empty
CareerPlan CustomStruct // 'careerPlan' as mongo db field key works
Hobbies map[string]string
Live time.Time
Salary decimal.Decimal
}
- Use an exist connection
var client *mongo.Client
...
database := "example"
db := sutando.NewDBFromMongo(ctx, client, database)
Disconnect
err := db.Disconnect(ctx)
Drop
err := db.Collection("Collection").Drop(ctx)
Scalar
// Count
count, err := db.Collection("Collection").Find().Equal("Name", "sutando").Greater("Number", 300).Count(ctx, "_index_id_")
Find
resultOne := struct{}
err := db.Collection("Collection").Find().Equal("Name", "sutando").Greater("Number", 300).First().Exec(ctx, &resultOne)
resultMany := []struct{}
err := db.Collection("Collection").Find().Equal("Name", "sutando").Greater("Number", 300).Exec(ctx, &resultMany)
Create
resultOne, _, err := db.Collection("Collection").Insert(&obj).Exec(ctx)
_, resultMany, err := db.Collection("Collection").Insert(&obj1, &obj2, &obj3).Exec(ctx)
Update with Model (Will update all fields including empty fields)
resultOne, err := db.Collection("Collection").UpdateWith(&data).Equal("Field", "sutando").First().Exec(su.ctx, false)
resultMany, err := db.Collection("Collection").UpdateWith(&data).Equal("Field", "sutando").Exec(su.ctx, false)
Update with Set
resultOne, err := db.Collection("Collection").Update().Equal("Field", "sutando").First().Set("Field", "hello").Exec(su.ctx, false)
resultMany, err := db.Collection("Collection").Update().Equal("Field", "sutando").Set("Field", "hello").Exec(su.ctx, false)
Delete
resultOne, err := db.Collection("Collection").Delete().Equal("Field", "sutando").First().Exec(su.ctx)
resultMany, err := db.Collection("Collection").Delete().Equal("Field", "sutando").Exec(su.ctx)
Use original mongo-driver instance
client := db.RawClient()
database := db.RawDatabase()
Changelog
| Version | Description |
| :-----: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1.4.2 | - Added method Bson into Update Find Delete Scalar |
| 1.4.1 | - Completed comment for all struct, interface, function |
| 1.4.0 | - Remove all db execute functions <br> - Removed Query <br> - Removed method Bitwise <br> - Added ConnSrv connection structure <br> - Added method Drop into Collection() method chain <br> - Added comment for all methods <br> - Added option parameter into Regex method <br> - Rewrite the structure fo filters <br> - Renamed GetDriver to RawClient <br> - Renamed GetDriverDB to RawDatabase <br> - Fixed after invoking Find, didn't call defer cursor.Close() |
| 1.3.7 | - Added Scalar <br> - Moved method Count from Query to Scalar |
| 1.3.6 | - Added method Regex into Update Find Delete Query |
| 1.3.5 | - Fixed Find no document mismatch error |
| 1.3.4 | - Added method Count into Query |
| 1.3.3 | - Added methods Sort Limit Skip into Find |
| 1.3.2 | - Added deprecated comment for DB |
| 1.3.1 | - Renamed OptionHandler to ClientOptionsHandler <br> - Renamed SetupOption to SetupClientOptions |
| 1.3.0 | - Added Execute Chain <br> - Fixed error when input o
