mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-19 05:55:19 +00:00
add id field to update (#159)
This commit is contained in:
@@ -133,9 +133,15 @@ func (e *Db) Update(ctx context.Context, req *db.UpdateRequest, rsp *db.UpdateRe
|
|||||||
m := req.Record.AsMap()
|
m := req.Record.AsMap()
|
||||||
|
|
||||||
// where ID is specified do a single update record update
|
// where ID is specified do a single update record update
|
||||||
id, ok := m[idKey].(string)
|
id := req.Id
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("update failed: missing 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 {
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// protoc-gen-go v1.26.0
|
// protoc-gen-go v1.26.0
|
||||||
// protoc v3.6.1
|
// protoc v3.15.6
|
||||||
// source: proto/db.proto
|
// source: proto/db.proto
|
||||||
|
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_struct "github.com/golang/protobuf/ptypes/struct"
|
|
||||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
|
structpb "google.golang.org/protobuf/types/known/structpb"
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
sync "sync"
|
sync "sync"
|
||||||
)
|
)
|
||||||
@@ -21,11 +21,13 @@ const (
|
|||||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
_ = 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 {
|
type ReadRequest struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// Table name is optional
|
||||||
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
||||||
// Read by id. Equivalent to 'id == "your-id"'
|
// Read by id. Equivalent to 'id == "your-id"'
|
||||||
Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
@@ -132,7 +134,7 @@ type ReadResponse struct {
|
|||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
// JSON encoded records
|
// 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() {
|
func (x *ReadResponse) Reset() {
|
||||||
@@ -167,21 +169,23 @@ func (*ReadResponse) Descriptor() ([]byte, []int) {
|
|||||||
return file_proto_db_proto_rawDescGZIP(), []int{1}
|
return file_proto_db_proto_rawDescGZIP(), []int{1}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *ReadResponse) GetRecords() []*_struct.Struct {
|
func (x *ReadResponse) GetRecords() []*structpb.Struct {
|
||||||
if x != nil {
|
if x != nil {
|
||||||
return x.Records
|
return x.Records
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a record in the database. Optionally include an "id" field otherwise its set automatically.
|
||||||
type CreateRequest struct {
|
type CreateRequest struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// Table name is optional
|
||||||
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
||||||
// JSON encoded record or records (can be array or object)
|
// 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() {
|
func (x *CreateRequest) Reset() {
|
||||||
@@ -223,7 +227,7 @@ func (x *CreateRequest) GetTable() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *CreateRequest) GetRecord() *_struct.Struct {
|
func (x *CreateRequest) GetRecord() *structpb.Struct {
|
||||||
if x != nil {
|
if x != nil {
|
||||||
return x.Record
|
return x.Record
|
||||||
}
|
}
|
||||||
@@ -235,7 +239,7 @@ type CreateResponse struct {
|
|||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
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"`
|
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,14 +282,18 @@ func (x *CreateResponse) GetId() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update a record in the database. Include an "id" in the record to update.
|
||||||
type UpdateRequest struct {
|
type UpdateRequest struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// Table name is optional
|
||||||
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
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, 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() {
|
func (x *UpdateRequest) Reset() {
|
||||||
@@ -327,7 +335,14 @@ func (x *UpdateRequest) GetTable() string {
|
|||||||
return ""
|
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 {
|
if x != nil {
|
||||||
return x.Record
|
return x.Record
|
||||||
}
|
}
|
||||||
@@ -372,11 +387,13 @@ func (*UpdateResponse) Descriptor() ([]byte, []int) {
|
|||||||
return file_proto_db_proto_rawDescGZIP(), []int{5}
|
return file_proto_db_proto_rawDescGZIP(), []int{5}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete a record in the database by id.
|
||||||
type DeleteRequest struct {
|
type DeleteRequest struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// Table name is optional
|
||||||
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
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 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"`
|
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}
|
return file_proto_db_proto_rawDescGZIP(), []int{7}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Truncate the records in a table
|
||||||
type TruncateRequest struct {
|
type TruncateRequest struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// Table name is optional
|
||||||
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -518,6 +537,7 @@ type TruncateResponse struct {
|
|||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// The table truncated
|
||||||
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
|
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,
|
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, 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,
|
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,
|
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,
|
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,
|
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,
|
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,
|
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
|
(*DeleteResponse)(nil), // 7: db.DeleteResponse
|
||||||
(*TruncateRequest)(nil), // 8: db.TruncateRequest
|
(*TruncateRequest)(nil), // 8: db.TruncateRequest
|
||||||
(*TruncateResponse)(nil), // 9: db.TruncateResponse
|
(*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{
|
var file_proto_db_proto_depIdxs = []int32{
|
||||||
10, // 0: db.ReadResponse.records:type_name -> google.protobuf.Struct
|
10, // 0: db.ReadResponse.records:type_name -> google.protobuf.Struct
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ package db
|
|||||||
import (
|
import (
|
||||||
fmt "fmt"
|
fmt "fmt"
|
||||||
proto "github.com/golang/protobuf/proto"
|
proto "github.com/golang/protobuf/proto"
|
||||||
_ "github.com/golang/protobuf/ptypes/struct"
|
_ "google.golang.org/protobuf/types/known/structpb"
|
||||||
math "math"
|
math "math"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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 {
|
message ReadRequest {
|
||||||
|
// Table name is optional
|
||||||
string table = 1;
|
string table = 1;
|
||||||
// Read by id. Equivalent to 'id == "your-id"'
|
// Read by id. Equivalent to 'id == "your-id"'
|
||||||
string id = 2;
|
string id = 2;
|
||||||
@@ -39,28 +41,34 @@ message ReadResponse {
|
|||||||
repeated google.protobuf.Struct records = 1;
|
repeated google.protobuf.Struct records = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a record in the database. Optionally include an "id" field otherwise its set automatically.
|
||||||
message CreateRequest {
|
message CreateRequest {
|
||||||
|
// Table name is optional
|
||||||
string table = 1;
|
string table = 1;
|
||||||
// JSON encoded record or records (can be array or object)
|
// JSON encoded record or records (can be array or object)
|
||||||
google.protobuf.Struct record = 2;
|
google.protobuf.Struct record = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message CreateResponse {
|
message CreateResponse {
|
||||||
// The id of the record
|
// The id of the record (either specified or automatically created)
|
||||||
string id = 1;
|
string id = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update a record in the database. Include an "id" in the record to update.
|
||||||
message UpdateRequest {
|
message UpdateRequest {
|
||||||
|
// Table name is optional
|
||||||
string table = 1;
|
string table = 1;
|
||||||
|
// The id of the record
|
||||||
|
string id = 2;
|
||||||
// record, JSON object
|
// 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 {
|
message DeleteRequest {
|
||||||
|
// Table name is optional
|
||||||
string table = 1;
|
string table = 1;
|
||||||
// id or ids, eg. 'user-1', or comma separated ids 'user-1,user-2'
|
// id or ids, eg. 'user-1', or comma separated ids 'user-1,user-2'
|
||||||
string id = 2;
|
string id = 2;
|
||||||
@@ -70,10 +78,13 @@ message DeleteResponse {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Truncate the records in a table
|
||||||
message TruncateRequest {
|
message TruncateRequest {
|
||||||
|
// Table name is optional
|
||||||
string table = 1;
|
string table = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message TruncateResponse {
|
message TruncateResponse {
|
||||||
|
// The table truncated
|
||||||
string table = 1;
|
string table = 1;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user