Support primary key ID and unique key Name where api uses name

This commit is contained in:
breel
2020-08-02 09:59:47 -06:00
parent 37fe9415e7
commit 8e6e86955e
14 changed files with 68 additions and 49 deletions

View File

@@ -124,9 +124,9 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
if _, ok := m[entity.Name]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name)
} else if _, ok := m[entity.Name].(string); !ok {
if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string")
}
return bdb.db.Update(func(tx *bolt.Tx) error {
@@ -134,7 +134,7 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err != nil {
return err
}
k := []byte(m[entity.Name].(string))
k := []byte(m[entity.ID].(string))
v := bucket.Get(k)
if len(v) > 0 {
return errors.New("cannot insert: collision on primary key")
@@ -272,7 +272,7 @@ func apply(doc bson.M, operator interface{}) (bson.M, error) {
func applyUnset(doc, operator bson.M) (bson.M, error) {
for k := range operator {
if k == entity.Name {
if k == entity.ID {
continue
}
nesting := strings.Split(k, ".")
@@ -305,7 +305,7 @@ func applyUnset(doc, operator bson.M) (bson.M, error) {
func applySet(doc, operator bson.M) (bson.M, error) {
for k, v := range operator {
if k == entity.Name {
if k == entity.ID {
continue
}
nesting := strings.Split(k, ".")