Table of Contents
This project uses CRUD syntax similar to ent
's SQL format to invoke the go.mongodb.org/mongo-driver
package. When the query conditions become too complex, code snippets like the following will be generated in the mongo-driver
.
filter := bson.D{
{"$or", bson.A{
bson.D{
{"$and", bson.A{
bson.D{{"age", bson.D{{"$gte", 18}}}},
bson.D{{"name", bson.D{{"$regex", "John"}}}},
}},
},
bson.D{
{"city", "NY"},
},
}},
}
Mongoent utilizes the syntax of the ent
framework to implement CRUD operations. Here's an example of a query(Please refer to the cmd/generate_test.go
file for a specific demo.):
newClient := mongoschema.NewClient(mongoschema.Driver(*client))
all, err := newClient.User.SetDBName("my_mongo").Query().
Where(user.UserNameRegex("c*")).
All(ctx)
if err != nil {
log.Fatal(err)
return
}
-
golang1.19+
-
goimport
-
gofumpt
-
gofmt
Make sure you have Go programming language and related tools installed correctly.
1.Install gofmt、goimports、gofumpt
go install golang.org/x/tools/cmd/gofmt@latest
go install golang.org/x/tools/cmd/goimports@latest
go install mvdan.cc/gofumpt/gofumports@latest
To generate the relevant code, simply run the following command:
go run -mod=mod github.com/zaihui/mongoent/cmd generate --schemaFile {$YourModelPath} --outputPath {$outputPath} --projectPath {$projectPath} --goModPath {$goModPath}
--schemaFile:The schema corresponding to MongoDB is represented as a struct type.
--outputPath:The generated code is typically placed in a directory specified by the project's configuration or convention. Please refer to your project's documentation or configuration files to determine the specific path where the code will be generated.
--projectPath:project path
--goModPath: go mod path
- The query conditions typically support the following operators:$eq、$ne、$gt、$lt、$gte、$lte、$regex、$in、$in
- create operator
- update operator
- delete Feature
- multiple operators
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
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 with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.