Giter Site home page Giter Site logo

djun / protoc-gen-bom Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cjp2600/protoc-gen-bom

0.0 0.0 0.0 4.1 MB

A protobuf compiler plugin designed to generate MongoDB models and APIs for simple object persistence tasks

Go 98.96% Makefile 1.04%

protoc-gen-bom's Introduction

Protoc-gen-bom

A protobuf compiler plugin designed to generate MongoDB models and APIs for simple object persistence tasks

Example

Describe the essence of what we want to work with.

syntax = "proto3";

import "github.com/cjp2600/protoc-gen-bom/plugin/options/bom.proto";

package main;

enum UserTypes {
    user = 0;
    admin = 1;
}

// describe the user model  
// 
message User {

    // define a message as a model
    option (bom.opts) = {
         model: true
         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 = 11;
    UserTypes type = 12;
}

After generation, we have the following methods available

// 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

	query := item.ToMongo()
	p, err := q.InsertOne()
	if err != nil {
		return nil, err
	}

FindOne

    // func (e *UserMongo) FindOne() (*UserMongo, error) {
	user, err := pb.NewUserMongo().WhereId("5f3a4ea2e97e882308d8f5ac").FindOne()
	if err != nil {
		return
	}

List

	// func (e *UserMongo) List() ([]*UserMongo, error) {
	users, err := pb.NewUserMongo().List()
	if err != nil {
		return
	}

ListWithPagination

	// func (e *UserMongo) ListWithPagination() ([]*UserMongo, *bom.Pagination, error) {
	user, pagination, err := pb.NewUserMongo().ListWithPagination()
	if err != nil {
		return
	}

FindOneById

	// func (e *UserMongo) FindOneById(Id string) (*UserMongo, error) {
	user, err := pb.NewUserMongo().FindOneById("5f3a4ea2e97e882308d8f5ac")
	if err != nil {
		return
	}

FindOneById

	// func (e *UserMongo) FindOneById(Id string) (*UserMongo, error) {
	user, err := pb.NewUserMongo().FindOneById("5f3a4ea2e97e882308d8f5ac")
	if err != nil {
		return
	}

GetBulk

	// func (e *UserMongo) GetBulk(ids []string) ([]*UserMongo, error) {
	user, err := pb.NewUserMongo().GetBulk([]string{"5f3a4ea2e97e882308d8f5ac","1f3a4ea2e97e882308d8f5ac"})
	if err != nil {
		return
	}

ListWithLastID

	// func (e *UserMongo) GetBulk(ids []string) ([]*UserMongo, error) {
	user, err := pb.NewUserMongo().GetBulk([]string{"5f3a4ea2e97e882308d8f5ac","1f3a4ea2e97e882308d8f5ac"})
	if err != nil {
		return
	}

protoc-gen-bom's People

Contributors

antsupovsa avatar cjp2600 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.