add id field to update (#159)

This commit is contained in:
Asim Aslam
2021-06-15 11:00:04 +01:00
committed by GitHub
parent 649b2e3f78
commit e1c2c92baf
4 changed files with 60 additions and 22 deletions

View File

@@ -133,9 +133,15 @@ func (e *Db) Update(ctx context.Context, req *db.UpdateRequest, rsp *db.UpdateRe
m := req.Record.AsMap()
// where ID is specified do a single update record update
id, ok := m[idKey].(string)
if !ok {
return fmt.Errorf("update failed: missing id")
id := req.Id
// if the id is blank then check the data
if len(req.Id) == 0 {
var ok bool
id, ok = m[idKey].(string)
if !ok {
return fmt.Errorf("update failed: missing id")
}
}
return db.Transaction(func(tx *gorm.DB) error {

View File

@@ -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 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"
)
@@ -21,11 +21,13 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// Read data from a table. Lookup can be by ID or via querying any field in the record.
type ReadRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Table name is optional
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
// Read by id. Equivalent to 'id == "your-id"'
Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
@@ -132,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() {
@@ -167,21 +169,23 @@ 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
}
return nil
}
// Create a record in the database. Optionally include an "id" field otherwise its set automatically.
type CreateRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Table name is optional
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"`
}
func (x *CreateRequest) Reset() {
@@ -223,7 +227,7 @@ func (x *CreateRequest) GetTable() string {
return ""
}
func (x *CreateRequest) GetRecord() *_struct.Struct {
func (x *CreateRequest) GetRecord() *structpb.Struct {
if x != nil {
return x.Record
}
@@ -235,7 +239,7 @@ type CreateResponse struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// The id of the record
// The id of the record (either specified or automatically created)
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
}
@@ -278,14 +282,18 @@ func (x *CreateResponse) GetId() string {
return ""
}
// Update a record in the database. Include an "id" in the record to update.
type UpdateRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Table name is optional
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
// The id of the record
Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
// record, JSON object
Record *_struct.Struct `protobuf:"bytes,2,opt,name=record,proto3" json:"record,omitempty"`
Record *structpb.Struct `protobuf:"bytes,3,opt,name=record,proto3" json:"record,omitempty"`
}
func (x *UpdateRequest) Reset() {
@@ -327,7 +335,14 @@ func (x *UpdateRequest) GetTable() string {
return ""
}
func (x *UpdateRequest) GetRecord() *_struct.Struct {
func (x *UpdateRequest) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *UpdateRequest) GetRecord() *structpb.Struct {
if x != nil {
return x.Record
}
@@ -372,11 +387,13 @@ func (*UpdateResponse) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{5}
}
// Delete a record in the database by id.
type DeleteRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Table name is optional
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"`
@@ -466,11 +483,13 @@ func (*DeleteResponse) Descriptor() ([]byte, []int) {
return file_proto_db_proto_rawDescGZIP(), []int{7}
}
// Truncate the records in a table
type TruncateRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Table name is optional
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
}
@@ -518,6 +537,7 @@ type TruncateResponse struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// The table truncated
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
}
@@ -588,10 +608,11 @@ var file_proto_db_proto_rawDesc = []byte{
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,
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, 0x56, 0x0a, 0x0d, 0x55, 0x70, 0x64,
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,
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,
0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64,
0x12, 0x2f, 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x03, 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, 0x10, 0x0a, 0x0e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f,
@@ -649,7 +670,7 @@ var file_proto_db_proto_goTypes = []interface{}{
(*DeleteResponse)(nil), // 7: db.DeleteResponse
(*TruncateRequest)(nil), // 8: db.TruncateRequest
(*TruncateResponse)(nil), // 9: db.TruncateResponse
(*_struct.Struct)(nil), // 10: google.protobuf.Struct
(*structpb.Struct)(nil), // 10: google.protobuf.Struct
}
var file_proto_db_proto_depIdxs = []int32{
10, // 0: db.ReadResponse.records:type_name -> google.protobuf.Struct

View File

@@ -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"
)

View File

@@ -14,7 +14,9 @@ service Db {
}
// Read data from a table. Lookup can be by ID or via querying any field in the record.
message ReadRequest {
// Table name is optional
string table = 1;
// Read by id. Equivalent to 'id == "your-id"'
string id = 2;
@@ -39,28 +41,34 @@ message ReadResponse {
repeated google.protobuf.Struct records = 1;
}
// Create a record in the database. Optionally include an "id" field otherwise its set automatically.
message CreateRequest {
// Table name is optional
string table = 1;
// JSON encoded record or records (can be array or object)
google.protobuf.Struct record = 2;
}
message CreateResponse {
// The id of the record
// The id of the record (either specified or automatically created)
string id = 1;
}
// Update a record in the database. Include an "id" in the record to update.
message UpdateRequest {
// Table name is optional
string table = 1;
// The id of the record
string id = 2;
// record, JSON object
google.protobuf.Struct record = 2;
google.protobuf.Struct record = 3;
}
message UpdateResponse {
}
message UpdateResponse {}
// Delete a record in the database by id.
message DeleteRequest {
// Table name is optional
string table = 1;
// id or ids, eg. 'user-1', or comma separated ids 'user-1,user-2'
string id = 2;
@@ -70,10 +78,13 @@ message DeleteResponse {
}
// Truncate the records in a table
message TruncateRequest {
// Table name is optional
string table = 1;
}
message TruncateResponse {
// The table truncated
string table = 1;
}
}