support db create id (#287)

* support db create id

* add the proto
This commit is contained in:
Asim Aslam
2021-12-01 09:18:20 +00:00
committed by GitHub
parent 3cc3329e29
commit f81ba3c70e
4 changed files with 40 additions and 16 deletions

View File

@@ -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 {