Skip to content

zaihui/mongoent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


mongoent

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. TodoList
  5. Contributing
  6. License

About The Project

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
}

(back to top)

Built With

  • golang1.19+

  • goimport

  • gofumpt

  • gofmt

(back to top)

Getting Started

Prerequisites

Make sure you have Go programming language and related tools installed correctly.

Installation

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

(back to top)

Usage

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

(back to top)

  • 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

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)