DB service (#132)

This commit is contained in:
Janos Dobronszki
2021-06-02 15:29:52 +01:00
committed by GitHub
parent 4d12230338
commit 7157db9aaa
15 changed files with 1389 additions and 4 deletions

2
db/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
db

3
db/Dockerfile Normal file
View File

@@ -0,0 +1,3 @@
FROM alpine
ADD db /db
ENTRYPOINT [ "/db" ]

22
db/Makefile Normal file
View File

@@ -0,0 +1,22 @@
GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/db.proto
.PHONY: build
build:
go build -o db *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t db:latest

23
db/README.md Normal file
View File

@@ -0,0 +1,23 @@
# Db Service
This is the Db service
Generated with
```
micro new db
```
## Usage
Generate the proto code
```
make proto
```
Run the service
```
micro run .
```

2
db/generate.go Normal file
View File

@@ -0,0 +1,2 @@
package main
//go:generate make proto

108
db/handler/db.go Normal file
View File

@@ -0,0 +1,108 @@
package handler
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
db "github.com/micro/services/db/proto"
gorm2 "github.com/micro/services/pkg/gorm"
"gorm.io/datatypes"
)
type Record struct {
ID string
Data datatypes.JSON `json:"data"`
}
type Db struct {
gorm2.Helper
}
// Call is a single request handler called via client.Call or the generated client code
func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateResponse) error {
db, err := e.GetDBConn(ctx)
if err != nil {
return err
}
m := map[string]interface{}{}
err = json.Unmarshal([]byte(req.Record), &m)
if err != nil {
return err
}
if _, ok := m["ID"].(string); !ok {
m["ID"] = uuid.New().String()
}
bs, _ := json.Marshal(m)
return db.Table(req.Table).Create(Record{
ID: m["ID"].(string),
Data: bs,
}).Error
}
func (e *Db) Update(ctx context.Context, req *db.UpdateRequest, rsp *db.UpdateResponse) error {
return nil
}
func (e *Db) Read(ctx context.Context, req *db.ReadRequest, rsp *db.ReadResponse) error {
recs := []Record{}
queries, err := Parse(req.Query)
if err != nil {
return err
}
db, err := e.GetDBConn(ctx)
if err != nil {
return err
}
db = db.Table(req.Table)
for _, query := range queries {
typ := "text"
switch query.Value.(type) {
case int64:
typ = "int"
case bool:
typ = "boolean"
}
op := ""
switch query.Op {
case itemEquals:
op = "="
case itemGreaterThan:
op = ">"
case itemGreaterThanEquals:
op = ">="
case itemLessThan:
op = "<"
case itemLessThanEquals:
op = "<="
case itemNotEquals:
op = "!="
}
db = db.Where(fmt.Sprintf("(data ->> '%v')::%v %v ?", query.Field, typ, op), query.Value)
}
err = db.Find(&recs).Error
if err != nil {
return err
}
ret := []map[string]interface{}{}
for _, rec := range recs {
m, err := rec.Data.MarshalJSON()
if err != nil {
return err
}
ma := map[string]interface{}{}
json.Unmarshal(m, &ma)
ma["ID"] = rec.ID
ret = append(ret, ma)
}
bs, _ := json.Marshal(ret)
rsp.Records = string(bs)
return nil
}
func (e *Db) Delete(ctx context.Context, req *db.DeleteRequest, rsp *db.DeleteResponse) error {
return nil
}

134
db/handler/parse.go Normal file
View File

@@ -0,0 +1,134 @@
package handler
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/crufter/lexer"
)
var quoteEscape = fmt.Sprint(0x10FFFF)
const (
itemIgnore = iota
// nouns
itemAnd
itemInt
itemFieldName
itemString
itemBoolTrue
itemBoolFalse
// ops
itemEquals
itemNotEquals
itemLessThan
itemGreaterThan
itemLessThanEquals
itemGreaterThanEquals
)
var opToString = map[int]string{
itemEquals: "==",
itemNotEquals: "!=",
itemLessThan: "<",
itemGreaterThan: ">",
itemLessThanEquals: "<=",
itemGreaterThanEquals: ">=",
}
var expressions = []lexer.TokenExpr{
{`[ ]+`, itemIgnore}, // Whitespace
{`==`, itemEquals},
{`!=`, itemNotEquals},
{`false`, itemBoolFalse},
{`true`, itemBoolTrue},
{`and`, itemAnd},
{`<=`, itemLessThanEquals},
{`>=`, itemGreaterThanEquals},
{`<`, itemLessThan},
{`>`, itemGreaterThan},
{`[0-9]+`, itemInt},
{`"(?:[^"\\]|\\.)*"`, itemString},
{`[\<\>\!\=\+\-\|\&\*\/A-Za-z][A-Za-z0-9_]*`, itemFieldName},
}
type Query struct {
Field string
Op int
Value interface{}
}
func Parse(q string) ([]Query, error) {
if strings.Contains(q, quoteEscape) {
return nil, errors.New("query contains illegal max rune")
}
q = strings.Replace(q, `""`, quoteEscape, -1)
tokens, err := lexer.Lex(q, expressions)
if err != nil {
return nil, err
}
queries := []Query{}
current := Query{}
for i, token := range tokens {
// and tokens should trigger a query
// save and reset
if token.Typ == itemAnd {
queries = append(queries, current)
current = Query{}
continue
}
// is an op
if token.Typ >= itemEquals {
current.Op = token.Typ
continue
}
// is a value
switch token.Typ {
case itemFieldName:
current.Field = token.Text
case itemString:
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with strings", opToString[token.Typ])
}
if len(token.Text) < 2 {
return nil, fmt.Errorf("string literal too short: '%v'", token.Text)
}
current.Value = strings.Replace(token.Text[1:len(token.Text)-1], quoteEscape, `"`, -1)
case itemBoolTrue:
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[token.Typ])
}
current.Value = true
case itemBoolFalse:
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[token.Typ])
}
current.Value = false
case itemInt:
num, err := strconv.ParseInt(token.Text, 10, 64)
if err != nil {
return nil, err
}
current.Value = num
}
// if we are at last position, save last query
if i == len(tokens)-1 {
queries = append(queries, current)
}
}
return queries, nil
}

141
db/handler/parse_test.go Normal file
View File

@@ -0,0 +1,141 @@
package handler
import (
"fmt"
"reflect"
"testing"
"github.com/crufter/lexer"
)
func TestLexing(t *testing.T) {
tokens, err := lexer.Lex("a == 12", expressions)
if err != nil {
t.Fatal(err)
}
if len(tokens) != 3 {
t.Fatal(tokens)
}
if tokens[0].Typ != itemFieldName || tokens[1].Typ != itemEquals || tokens[2].Typ != itemInt {
t.Fatal(tokens)
}
tokens, err = lexer.Lex(`a == 12 and name != "nandos"`, expressions)
if tokens[0].Typ != itemFieldName ||
tokens[1].Typ != itemEquals ||
tokens[2].Typ != itemInt ||
tokens[3].Typ != itemAnd ||
tokens[4].Typ != itemFieldName ||
tokens[5].Typ != itemNotEquals ||
tokens[6].Typ != itemString {
t.Fatal(tokens)
}
}
type tCase struct {
Q string
E []Query
Err error
}
func TestParsing(t *testing.T) {
tCases := []tCase{
tCase{
Q: `a == 12 and name != "nandos"`,
E: []Query{
Query{
Field: "a",
Value: int64(12),
Op: itemEquals,
},
Query{
Field: "name",
Value: "nandos",
Op: itemNotEquals,
},
},
},
// test escaping quotes
tCase{
Q: `a == 12 and name != "He said ""yes""!"`,
E: []Query{
Query{
Field: "a",
Value: int64(12),
Op: itemEquals,
},
Query{
Field: "name",
Value: `He said "yes"!`,
Op: itemNotEquals,
},
},
},
tCase{
Q: `a == false and b == true`,
E: []Query{
Query{
Field: "a",
Value: false,
Op: itemEquals,
},
Query{
Field: "b",
Value: true,
Op: itemEquals,
},
},
},
// a < 20
tCase{
Q: `a < 20`,
E: []Query{
Query{
Field: "a",
Value: int64(20),
Op: itemLessThan,
},
},
},
tCase{
Q: `a <= 20`,
E: []Query{
Query{
Field: "a",
Value: int64(20),
Op: itemLessThanEquals,
},
},
},
tCase{
Q: `a > 20`,
E: []Query{
Query{
Field: "a",
Value: int64(20),
Op: itemGreaterThan,
},
},
},
tCase{
Q: `a >= 20`,
E: []Query{
Query{
Field: "a",
Value: int64(20),
Op: itemGreaterThanEquals,
},
},
},
}
for _, tCase := range tCases {
fmt.Println("Parsing", tCase.Q)
qs, err := Parse(tCase.Q)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(qs, tCase.E) {
t.Fatal("Expected", tCase.E, "got", qs)
}
}
}

50
db/main.go Normal file
View File

@@ -0,0 +1,50 @@
package main
import (
pb "github.com/micro/services/db/proto"
"github.com/micro/services/db/handler"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
"database/sql"
"github.com/micro/micro/v3/service/config"
_ "github.com/jackc/pgx/v4/stdlib"
)
var dbAddress = "postgresql://postgres:postgres@localhost:5432/db?sslmode=disable"
func main() {
// Create service
srv := service.New(
service.Name("db"),
service.Version("latest"),
)
// Connect to the database
cfg, err := config.Get("micro.db.database")
if err != nil {
logger.Fatalf("Error loading config: %v", err)
}
addr := cfg.String(dbAddress)
sqlDB, err := sql.Open("pgx", addr)
if err != nil {
logger.Fatalf("Failed to open connection to DB %s", err)
}
h := &handler.Db{}
h.DBConn(sqlDB).Migrations(&handler.Record{})
// Register handler
pb.RegisterDbHandler(srv.Server(), h)
// Register handler
pb.RegisterDbHandler(srv.Server(), new(handler.Db))
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

1
db/micro.mu Normal file
View File

@@ -0,0 +1 @@
service db

654
db/proto/db.pb.go Normal file
View File

@@ -0,0 +1,654 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.6.1
// source: proto/db.proto
package db
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type ReadRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
// eg. 'age >= 18', 'age >= 18 and verified == true'
// comparison operators: '==', '!=', '<', '>', '<=', '>='
// logical operator: 'and'
Query string `protobuf:"bytes,2,opt,name=query,proto3" json:"query,omitempty"`
Offset int32 `protobuf:"varint,3,opt,name=offset,proto3" json:"offset,omitempty"`
Limit int32 `protobuf:"varint,4,opt,name=limit,proto3" json:"limit,omitempty"`
// field name to order by
OrderBy string `protobuf:"bytes,5,opt,name=orderBy,proto3" json:"orderBy,omitempty"`
// 'asc' (default), 'desc'
Order string `protobuf:"bytes,6,opt,name=order,proto3" json:"order,omitempty"`
}
func (x *ReadRequest) Reset() {
*x = ReadRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ReadRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ReadRequest) ProtoMessage() {}
func (x *ReadRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ReadRequest.ProtoReflect.Descriptor instead.
func (*ReadRequest) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{0}
}
func (x *ReadRequest) GetTable() string {
if x != nil {
return x.Table
}
return ""
}
func (x *ReadRequest) GetQuery() string {
if x != nil {
return x.Query
}
return ""
}
func (x *ReadRequest) GetOffset() int32 {
if x != nil {
return x.Offset
}
return 0
}
func (x *ReadRequest) GetLimit() int32 {
if x != nil {
return x.Limit
}
return 0
}
func (x *ReadRequest) GetOrderBy() string {
if x != nil {
return x.OrderBy
}
return ""
}
func (x *ReadRequest) GetOrder() string {
if x != nil {
return x.Order
}
return ""
}
type ReadResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// JSON encoded records
Records string `protobuf:"bytes,1,opt,name=records,proto3" json:"records,omitempty"`
}
func (x *ReadResponse) Reset() {
*x = ReadResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ReadResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ReadResponse) ProtoMessage() {}
func (x *ReadResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ReadResponse.ProtoReflect.Descriptor instead.
func (*ReadResponse) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{1}
}
func (x *ReadResponse) GetRecords() string {
if x != nil {
return x.Records
}
return ""
}
type CreateRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
// JSON encoded record or records (can be array or object)
Record string `protobuf:"bytes,2,opt,name=record,proto3" json:"record,omitempty"`
}
func (x *CreateRequest) Reset() {
*x = CreateRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *CreateRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CreateRequest) ProtoMessage() {}
func (x *CreateRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CreateRequest.ProtoReflect.Descriptor instead.
func (*CreateRequest) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{2}
}
func (x *CreateRequest) GetTable() string {
if x != nil {
return x.Table
}
return ""
}
func (x *CreateRequest) GetRecord() string {
if x != nil {
return x.Record
}
return ""
}
type CreateResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
}
func (x *CreateResponse) Reset() {
*x = CreateResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *CreateResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CreateResponse) ProtoMessage() {}
func (x *CreateResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CreateResponse.ProtoReflect.Descriptor instead.
func (*CreateResponse) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{3}
}
type UpdateRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
// JSON encoded record or records (can be array or object)
Record string `protobuf:"bytes,2,opt,name=record,proto3" json:"record,omitempty"`
}
func (x *UpdateRequest) Reset() {
*x = UpdateRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UpdateRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UpdateRequest) ProtoMessage() {}
func (x *UpdateRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UpdateRequest.ProtoReflect.Descriptor instead.
func (*UpdateRequest) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{4}
}
func (x *UpdateRequest) GetTable() string {
if x != nil {
return x.Table
}
return ""
}
func (x *UpdateRequest) GetRecord() string {
if x != nil {
return x.Record
}
return ""
}
type UpdateResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
}
func (x *UpdateResponse) Reset() {
*x = UpdateResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UpdateResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UpdateResponse) ProtoMessage() {}
func (x *UpdateResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[5]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UpdateResponse.ProtoReflect.Descriptor instead.
func (*UpdateResponse) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{5}
}
type DeleteRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
// id or ids, eg. 'user-1', or comma separated ids 'user-1,user-2'
Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
}
func (x *DeleteRequest) Reset() {
*x = DeleteRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *DeleteRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*DeleteRequest) ProtoMessage() {}
func (x *DeleteRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[6]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use DeleteRequest.ProtoReflect.Descriptor instead.
func (*DeleteRequest) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{6}
}
func (x *DeleteRequest) GetTable() string {
if x != nil {
return x.Table
}
return ""
}
func (x *DeleteRequest) GetId() string {
if x != nil {
return x.Id
}
return ""
}
type DeleteResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
}
func (x *DeleteResponse) Reset() {
*x = DeleteResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_db_proto_msgTypes[7]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *DeleteResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*DeleteResponse) ProtoMessage() {}
func (x *DeleteResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_db_proto_msgTypes[7]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use DeleteResponse.ProtoReflect.Descriptor instead.
func (*DeleteResponse) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{7}
}
var File_proto_db_proto protoreflect.FileDescriptor
var file_proto_db_proto_rawDesc = []byte{
0x0a, 0x0e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x12, 0x02, 0x64, 0x62, 0x22, 0x97, 0x01, 0x0a, 0x0b, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x71, 0x75,
0x65, 0x72, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x71, 0x75, 0x65, 0x72, 0x79,
0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05,
0x52, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69,
0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x18,
0x0a, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x42, 0x79, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52,
0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x42, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x72, 0x64, 0x65,
0x72, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x22, 0x28,
0x0a, 0x0c, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18,
0x0a, 0x07, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x07, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x73, 0x22, 0x3d, 0x0a, 0x0d, 0x43, 0x72, 0x65, 0x61,
0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x61, 0x62,
0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x12,
0x16, 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x22, 0x10, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74,
0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x3d, 0x0a, 0x0d, 0x55, 0x70, 0x64,
0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x61,
0x62, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65,
0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
0x52, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x22, 0x10, 0x0a, 0x0e, 0x55, 0x70, 0x64, 0x61,
0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x35, 0x0a, 0x0d, 0x44, 0x65,
0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74,
0x61, 0x62, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c,
0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69,
0x64, 0x22, 0x10, 0x0a, 0x0e, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x32, 0xca, 0x01, 0x0a, 0x02, 0x44, 0x62, 0x12, 0x31, 0x0a, 0x06, 0x43, 0x72,
0x65, 0x61, 0x74, 0x65, 0x12, 0x11, 0x2e, 0x64, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x64, 0x62, 0x2e, 0x43, 0x72, 0x65,
0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x2b, 0x0a,
0x04, 0x52, 0x65, 0x61, 0x64, 0x12, 0x0f, 0x2e, 0x64, 0x62, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x64, 0x62, 0x2e, 0x52, 0x65, 0x61, 0x64,
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x06, 0x55, 0x70,
0x64, 0x61, 0x74, 0x65, 0x12, 0x11, 0x2e, 0x64, 0x62, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x64, 0x62, 0x2e, 0x55, 0x70, 0x64,
0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x31, 0x0a,
0x06, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x11, 0x2e, 0x64, 0x62, 0x2e, 0x44, 0x65, 0x6c,
0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x64, 0x62, 0x2e,
0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00,
0x42, 0x24, 0x5a, 0x22, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d,
0x69, 0x63, 0x72, 0x6f, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2f, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x3b, 0x64, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_db_proto_rawDescOnce sync.Once
file_proto_db_proto_rawDescData = file_proto_db_proto_rawDesc
)
func file_proto_db_proto_rawDescGZIP() []byte {
file_proto_db_proto_rawDescOnce.Do(func() {
file_proto_db_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_db_proto_rawDescData)
})
return file_proto_db_proto_rawDescData
}
var file_proto_db_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
var file_proto_db_proto_goTypes = []interface{}{
(*ReadRequest)(nil), // 0: db.ReadRequest
(*ReadResponse)(nil), // 1: db.ReadResponse
(*CreateRequest)(nil), // 2: db.CreateRequest
(*CreateResponse)(nil), // 3: db.CreateResponse
(*UpdateRequest)(nil), // 4: db.UpdateRequest
(*UpdateResponse)(nil), // 5: db.UpdateResponse
(*DeleteRequest)(nil), // 6: db.DeleteRequest
(*DeleteResponse)(nil), // 7: db.DeleteResponse
}
var file_proto_db_proto_depIdxs = []int32{
2, // 0: db.Db.Create:input_type -> db.CreateRequest
0, // 1: db.Db.Read:input_type -> db.ReadRequest
4, // 2: db.Db.Update:input_type -> db.UpdateRequest
6, // 3: db.Db.Delete:input_type -> db.DeleteRequest
3, // 4: db.Db.Create:output_type -> db.CreateResponse
1, // 5: db.Db.Read:output_type -> db.ReadResponse
5, // 6: db.Db.Update:output_type -> db.UpdateResponse
7, // 7: db.Db.Delete:output_type -> db.DeleteResponse
4, // [4:8] is the sub-list for method output_type
0, // [0:4] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_proto_db_proto_init() }
func file_proto_db_proto_init() {
if File_proto_db_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ReadRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_db_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ReadResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_db_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CreateRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_db_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CreateResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_db_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UpdateRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_db_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UpdateResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_db_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*DeleteRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_db_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*DeleteResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_db_proto_rawDesc,
NumEnums: 0,
NumMessages: 8,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_db_proto_goTypes,
DependencyIndexes: file_proto_db_proto_depIdxs,
MessageInfos: file_proto_db_proto_msgTypes,
}.Build()
File_proto_db_proto = out.File
file_proto_db_proto_rawDesc = nil
file_proto_db_proto_goTypes = nil
file_proto_db_proto_depIdxs = nil
}

144
db/proto/db.pb.micro.go Normal file
View File

@@ -0,0 +1,144 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/db.proto
package db
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Db service
func NewDbEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Db service
type DbService interface {
Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error)
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error)
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
}
type dbService struct {
c client.Client
name string
}
func NewDbService(name string, c client.Client) DbService {
return &dbService{
c: c,
name: name,
}
}
func (c *dbService) Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error) {
req := c.c.NewRequest(c.name, "Db.Create", in)
out := new(CreateResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *dbService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
req := c.c.NewRequest(c.name, "Db.Read", in)
out := new(ReadResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *dbService) Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error) {
req := c.c.NewRequest(c.name, "Db.Update", in)
out := new(UpdateResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *dbService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
req := c.c.NewRequest(c.name, "Db.Delete", in)
out := new(DeleteResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Db service
type DbHandler interface {
Create(context.Context, *CreateRequest, *CreateResponse) error
Read(context.Context, *ReadRequest, *ReadResponse) error
Update(context.Context, *UpdateRequest, *UpdateResponse) error
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
}
func RegisterDbHandler(s server.Server, hdlr DbHandler, opts ...server.HandlerOption) error {
type db interface {
Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
}
type Db struct {
db
}
h := &dbHandler{hdlr}
return s.Handle(s.NewHandler(&Db{h}, opts...))
}
type dbHandler struct {
DbHandler
}
func (h *dbHandler) Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error {
return h.DbHandler.Create(ctx, in, out)
}
func (h *dbHandler) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
return h.DbHandler.Read(ctx, in, out)
}
func (h *dbHandler) Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error {
return h.DbHandler.Update(ctx, in, out)
}
func (h *dbHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
return h.DbHandler.Delete(ctx, in, out)
}

62
db/proto/db.proto Normal file
View File

@@ -0,0 +1,62 @@
syntax = "proto3";
package db;
option go_package = "github.com/micro/services/proto;db";
service Db {
rpc Create(CreateRequest) returns (CreateResponse) {}
rpc Read(ReadRequest) returns (ReadResponse) {}
rpc Update(UpdateRequest) returns (UpdateResponse) {}
rpc Delete(DeleteRequest) returns (DeleteResponse) {}
}
message ReadRequest {
string table = 1;
// eg. 'age >= 18', 'age >= 18 and verified == true'
// comparison operators: '==', '!=', '<', '>', '<=', '>='
// logical operator: 'and'
string query = 2;
int32 offset = 3;
int32 limit = 4;
// field name to order by
string orderBy = 5;
// 'asc' (default), 'desc'
string order = 6;
}
message ReadResponse {
// JSON encoded records
string records = 1;
}
message CreateRequest {
string table = 1;
// JSON encoded record or records (can be array or object)
string record = 2;
}
message CreateResponse {
}
message UpdateRequest {
string table = 1;
// JSON encoded record or records (can be array or object)
string record = 2;
}
message UpdateResponse {
}
message DeleteRequest {
string table = 1;
// id or ids, eg. 'user-1', or comma separated ids 'user-1,user-2'
string id = 2;
}
message DeleteResponse {
}