From f81ba3c70eec09d8420460a2b9bbb6b75d4c416c Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 1 Dec 2021 09:18:20 +0000 Subject: [PATCH] support db create id (#287) * support db create id * add the proto --- db/handler/db.go | 17 ++++++++++++++--- db/proto/db.pb.go | 35 +++++++++++++++++++++++------------ db/proto/db.pb.micro.go | 2 +- db/proto/db.proto | 2 ++ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/db/handler/db.go b/db/handler/db.go index cdf60ae..b5d7a14 100644 --- a/db/handler/db.go +++ b/db/handler/db.go @@ -112,13 +112,24 @@ func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateRe } m := req.Record.AsMap() - if _, ok := m[idKey].(string); !ok { - m[idKey] = uuid.New().String() + id := req.Id + + // check the record for an id field + if len(id) == 0 { + if mid, ok := m[idKey].(string); ok { + id = mid + } else { + // set id as uuid + id = uuid.New().String() + // inject id into record + m[idKey] = id + } } + bs, _ := json.Marshal(m) err = db.Table(tableName).Create(&Record{ - ID: m[idKey].(string), + ID: id, Data: bs, }).Error if err != nil { diff --git a/db/proto/db.pb.go b/db/proto/db.pb.go index 95d51c6..a71c6d6 100644 --- a/db/proto/db.pb.go +++ b/db/proto/db.pb.go @@ -1,15 +1,15 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.26.0 -// protoc v3.6.1 +// protoc-gen-go v1.27.1 +// protoc v3.15.6 // source: proto/db.proto package db import ( - _struct "github.com/golang/protobuf/ptypes/struct" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + structpb "google.golang.org/protobuf/types/known/structpb" reflect "reflect" sync "sync" ) @@ -134,7 +134,7 @@ type ReadResponse struct { unknownFields protoimpl.UnknownFields // JSON encoded records - Records []*_struct.Struct `protobuf:"bytes,1,rep,name=records,proto3" json:"records,omitempty"` + Records []*structpb.Struct `protobuf:"bytes,1,rep,name=records,proto3" json:"records,omitempty"` } func (x *ReadResponse) Reset() { @@ -169,7 +169,7 @@ func (*ReadResponse) Descriptor() ([]byte, []int) { return file_proto_db_proto_rawDescGZIP(), []int{1} } -func (x *ReadResponse) GetRecords() []*_struct.Struct { +func (x *ReadResponse) GetRecords() []*structpb.Struct { if x != nil { return x.Records } @@ -185,7 +185,9 @@ type CreateRequest struct { // Optional table name. Defaults to 'default' Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"` // JSON encoded record or records (can be array or object) - Record *_struct.Struct `protobuf:"bytes,2,opt,name=record,proto3" json:"record,omitempty"` + Record *structpb.Struct `protobuf:"bytes,2,opt,name=record,proto3" json:"record,omitempty"` + // optional record id to use + Id string `protobuf:"bytes,3,opt,name=id,proto3" json:"id,omitempty"` } func (x *CreateRequest) Reset() { @@ -227,13 +229,20 @@ func (x *CreateRequest) GetTable() string { return "" } -func (x *CreateRequest) GetRecord() *_struct.Struct { +func (x *CreateRequest) GetRecord() *structpb.Struct { if x != nil { return x.Record } return nil } +func (x *CreateRequest) GetId() string { + if x != nil { + return x.Id + } + return "" +} + type CreateResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -293,7 +302,7 @@ type UpdateRequest struct { // The id of the record. If not specified it is inferred from the 'id' field of the record Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"` // record, JSON object - Record *_struct.Struct `protobuf:"bytes,3,opt,name=record,proto3" json:"record,omitempty"` + Record *structpb.Struct `protobuf:"bytes,3,opt,name=record,proto3" json:"record,omitempty"` } func (x *UpdateRequest) Reset() { @@ -342,7 +351,7 @@ func (x *UpdateRequest) GetId() string { return "" } -func (x *UpdateRequest) GetRecord() *_struct.Struct { +func (x *UpdateRequest) GetRecord() *structpb.Struct { if x != nil { return x.Record } @@ -849,6 +858,7 @@ func (x *ListTablesResponse) GetTables() []string { return nil } +// Drop a table in the DB type DropTableRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -955,12 +965,13 @@ var file_proto_db_proto_rawDesc = []byte{ 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52, 0x07, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x73, 0x22, - 0x56, 0x0a, 0x0d, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x66, 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, 0x2f, 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52, - 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x22, 0x20, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74, + 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x20, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x66, 0x0a, 0x0d, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x61, @@ -1064,7 +1075,7 @@ var file_proto_db_proto_goTypes = []interface{}{ (*ListTablesResponse)(nil), // 15: db.ListTablesResponse (*DropTableRequest)(nil), // 16: db.DropTableRequest (*DropTableResponse)(nil), // 17: db.DropTableResponse - (*_struct.Struct)(nil), // 18: google.protobuf.Struct + (*structpb.Struct)(nil), // 18: google.protobuf.Struct } var file_proto_db_proto_depIdxs = []int32{ 18, // 0: db.ReadResponse.records:type_name -> google.protobuf.Struct diff --git a/db/proto/db.pb.micro.go b/db/proto/db.pb.micro.go index ede31e0..d0bd297 100644 --- a/db/proto/db.pb.micro.go +++ b/db/proto/db.pb.micro.go @@ -6,7 +6,7 @@ package db import ( fmt "fmt" proto "github.com/golang/protobuf/proto" - _ "github.com/golang/protobuf/ptypes/struct" + _ "google.golang.org/protobuf/types/known/structpb" math "math" ) diff --git a/db/proto/db.proto b/db/proto/db.proto index 8490940..6e23bff 100644 --- a/db/proto/db.proto +++ b/db/proto/db.proto @@ -51,6 +51,8 @@ message CreateRequest { string table = 1; // JSON encoded record or records (can be array or object) google.protobuf.Struct record = 2; + // optional record id to use + string id = 3; } message CreateResponse {