Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cjp2600 committed Nov 27, 2020
2 parents 6e72a3a + 0c38d79 commit 5d45664
Showing 1 changed file with 107 additions and 56 deletions.
163 changes: 107 additions & 56 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,134 @@
### Example
## Protoc-gen-bom
A protobuf compiler plugin designed to generate MongoDB models and APIs for simple object persistence tasks

```$xslt
## Example

Describe the essence of what we want to work with.

```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;
}
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;
user = 0;
admin = 1;
}
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;
string email = 11;
UserTypes type = 12;
}
message Token {
option (bom.opts) = {model: true};
string accessToken = 3;
string refreshToken = 4;
}
```

After generation, we have the following methods available

message ProviderUsers {
option (bom.opts) = {
model: true
crud: true
};
```go

string providerId = 1 [(bom.field).tag = {mongoObjectId: true}];
string userId = 2 [(bom.field).tag = {mongoObjectId: true}];
// 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
}

```
```
### 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
// 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
}
```
### 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
}
```

0 comments on commit 5d45664

Please sign in to comment.