From 013158aac99df3ff4184737102ec7e369428593d Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Fri, 6 Nov 2020 15:54:11 +0300 Subject: [PATCH 1/6] Update Readme.md --- Readme.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 5f00a7c..2373a5d 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,6 @@ +### protoc-gen-bom + + ### Example ```$xslt @@ -80,4 +83,4 @@ message ProviderUsers { string userId = 2 [(bom.field).tag = {mongoObjectId: true}]; } -``` \ No newline at end of file +``` From 271e655481fdfbdedbc16c5e5ebdb275ee38e03c Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Fri, 6 Nov 2020 17:23:59 +0300 Subject: [PATCH 2/6] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 2373a5d..49a108a 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,4 @@ -### protoc-gen-bom +## protoc-gen-bom ### Example From dcab810215759318b539b0340cc12e0fca892099 Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Sat, 7 Nov 2020 17:56:04 +0300 Subject: [PATCH 3/6] Update Readme.md --- Readme.md | 72 ++++++++++--------------------------------------------- 1 file changed, 12 insertions(+), 60 deletions(-) diff --git a/Readme.md b/Readme.md index 49a108a..a10f8c6 100644 --- a/Readme.md +++ b/Readme.md @@ -3,84 +3,36 @@ ### Example -```$xslt +```protobuf syntax = "proto3"; import "github.com/cjp2600/protoc-gen-bom/plugin/options/bom.proto"; -import "google/protobuf/timestamp.proto"; -import "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api/annotations.proto"; package main; -// user types existing in the system enum UserTypes { - pupil = 0; // the main user type is set by default, type is issued for customers of the service who perform training - teacher = 1; // type of teacher - admin = 4; + user = 0; + admin = 1; } -message Role { - option (bom.opts) = { - model: true - crud: true - }; - - string id = 1 [(bom.field).tag = {mongoObjectId:true isID:true}]; - string name = 2; - repeated Permission role = 3; -} - -message Permission { - option (bom.opts) = { - model: true - crud: true - }; - string id = 1 [(bom.field).tag = {mongoObjectId:true isID:true}]; - - string service = 3; - bool create = 4; - bool read = 5; - bool update = 6; - bool delete = 7; -} - -// user base model +// describe the user model +// message User { + + // define a message as a model option (bom.opts) = { model: true - crud: true - collection: "user" + crud: true // methods are needed to redefine }; + // Set the basic id using ObjectID string id = 1 [(bom.field).tag = {mongoObjectId:true isID:true}]; - bool active = 2; string firstName = 3; string lastName = 4; + string secondName = 5; string phone = 6; - string email = 7; - Role role = 9; - bool EmailConfirm = 10; - UserTypes type = 11; - Token token = 12; - google.protobuf.Timestamp createdAt = 13; - google.protobuf.Timestamp updatedAt = 14; -} - -message Token { - option (bom.opts) = {model: true}; - string accessToken = 3; - string refreshToken = 4; -} - - -message ProviderUsers { - option (bom.opts) = { - model: true - crud: true - }; - - string providerId = 1 [(bom.field).tag = {mongoObjectId: true}]; - string userId = 2 [(bom.field).tag = {mongoObjectId: true}]; + string email = 11; + UserTypes type = 12; } ``` From 1a7c8187f6461f1c265b1a0b4abf1501e4498511 Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Sat, 7 Nov 2020 18:05:31 +0300 Subject: [PATCH 4/6] Update Readme.md --- Readme.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index a10f8c6..6a44fb8 100644 --- a/Readme.md +++ b/Readme.md @@ -1,7 +1,9 @@ ## protoc-gen-bom -### Example +## Example + +Describe the essence of what we want to work with. ```protobuf syntax = "proto3"; @@ -36,3 +38,32 @@ message User { } ``` + +After generation, we have the following methods available + +### FindOne +```go + // func (e *UserMongo) FindOne() (*UserMongo, error) { + user, err := pb.NewUserMongo().WhereId("5f3a4ea2e97e882308d8f5ac").FindOne() + if err != nil { + return + } +``` + +### List +```go + // func (e *UserMongo) List() ([]*UserMongo, error) { + users, err := pb.NewUserMongo().List() + if err != nil { + return + } +``` + +### ListWithPagination +```go + // func (e *UserMongo) ListWithPagination() ([]*UserMongo, *bom.Pagination, error) { + user, pagination, err := pb.NewUserMongo().ListWithPagination() + if err != nil { + return + } +``` From 01ca6ee5968329f4afca15c1777f30059b2ad8e6 Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Sat, 7 Nov 2020 18:08:20 +0300 Subject: [PATCH 5/6] Update Readme.md --- Readme.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 6a44fb8..b035207 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,4 @@ -## protoc-gen-bom +## Protoc-gen-bom ## Example @@ -29,6 +29,7 @@ message User { // Set the basic id using ObjectID string id = 1 [(bom.field).tag = {mongoObjectId:true isID:true}]; + bool active = 2; string firstName = 3; string lastName = 4; string secondName = 5; @@ -41,6 +42,23 @@ message User { After generation, we have the following methods available +```go + +// create MongoDB Model from protobuf (UserMongo) +type UserMongo struct { + Id primitive.ObjectID `_id, omitempty` + Active bool `json:"active"` + FirstName string `json:"firstname"` + LastName string `json:"lastname"` + SecondName string `json:"secondname"` + Phone string `json:"phone"` + Email string `json:"email"` + Type UserTypes `json:"type"` + bom *bom.Bom +} + +``` + ### FindOne ```go // func (e *UserMongo) FindOne() (*UserMongo, error) { From 0c38d79ebae37e50f0176ac6278661408573031b Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Sat, 7 Nov 2020 18:19:08 +0300 Subject: [PATCH 6/6] Update Readme.md --- Readme.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index b035207..370e94e 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,5 @@ ## Protoc-gen-bom - +A protobuf compiler plugin designed to generate MongoDB models and APIs for simple object persistence tasks ## Example @@ -58,6 +58,20 @@ type UserMongo struct { } ``` +### ToMongo +method of conversion from protobuf of an object to a mongo object + +### ToPB +method of conversion from mongo of an object to a protobuf object + +### InsertOne +```go + query := item.ToMongo() + p, err := q.InsertOne() + if err != nil { + return nil, err + } +``` ### FindOne ```go @@ -85,3 +99,36 @@ type UserMongo struct { return } ``` +### FindOneById +```go + // func (e *UserMongo) FindOneById(Id string) (*UserMongo, error) { + user, err := pb.NewUserMongo().FindOneById("5f3a4ea2e97e882308d8f5ac") + if err != nil { + return + } +``` +### FindOneById +```go + // func (e *UserMongo) FindOneById(Id string) (*UserMongo, error) { + user, err := pb.NewUserMongo().FindOneById("5f3a4ea2e97e882308d8f5ac") + if err != nil { + return + } +``` +### GetBulk +```go + // func (e *UserMongo) GetBulk(ids []string) ([]*UserMongo, error) { + user, err := pb.NewUserMongo().GetBulk([]string{"5f3a4ea2e97e882308d8f5ac","1f3a4ea2e97e882308d8f5ac"}) + if err != nil { + return + } +``` + +### ListWithLastID +```go + // func (e *UserMongo) GetBulk(ids []string) ([]*UserMongo, error) { + user, err := pb.NewUserMongo().GetBulk([]string{"5f3a4ea2e97e882308d8f5ac","1f3a4ea2e97e882308d8f5ac"}) + if err != nil { + return + } +```