Skip to content

Commit

Permalink
feat(documents/boltdb): Return Key on boltdb queries and gets.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Jul 19, 2021
1 parent c072374 commit b96848f
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pkg/plugins/document/boltdb/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package boltdb_service

import (
"fmt"
"github.com/nitric-dev/membrane/pkg/plugins/document"
"github.com/nitric-dev/membrane/pkg/utils"
"os"
"strconv"
"strings"
"time"

"github.com/nitric-dev/membrane/pkg/plugins/document"
"github.com/nitric-dev/membrane/pkg/utils"

"github.com/Knetic/govaluate"
"github.com/asdine/storm"
"github.com/asdine/storm/q"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (s *BoltDocService) Get(key *sdk.Key) (*sdk.Document, error) {
return nil, err
}

return toSdkDoc(doc), nil
return toSdkDoc(key.Collection, doc), nil
}

func (s *BoltDocService) Set(key *sdk.Key, content map[string]interface{}) error {
Expand Down Expand Up @@ -217,12 +218,12 @@ func (s *BoltDocService) Query(collection *sdk.Collection, expressions []sdk.Que
}
include := eval.(bool)
if include {
sdkDoc := toSdkDoc(doc)
sdkDoc := toSdkDoc(collection, doc)
documents = append(documents, *sdkDoc)
}

} else {
sdkDoc := toSdkDoc(doc)
sdkDoc := toSdkDoc(collection, doc)
documents = append(documents, *sdkDoc)
}

Expand Down Expand Up @@ -281,7 +282,6 @@ func (s *BoltDocService) createdDb(coll sdk.Collection) (*storm.DB, error) {
}

func createDoc(key *sdk.Key) BoltDoc {

parentKey := key.Collection.Parent

// Top Level Collection
Expand All @@ -301,8 +301,25 @@ func createDoc(key *sdk.Key) BoltDoc {
}
}

func toSdkDoc(doc BoltDoc) *sdk.Document {
func toSdkDoc(col *sdk.Collection, doc BoltDoc) *sdk.Document {
keys := strings.Split(doc.Id, "_")

// Translate the boltdb Id into a nitric document key Id
var id string
if len(keys) > 1 {
// sub document
id = keys[len(keys)-1]
} else {
id = doc.Id
}

return &sdk.Document{
Content: doc.Value,
Key: &sdk.Key{

Collection: col,
// TODO: need to split out parent key id...
Id: id,
},
}
}

0 comments on commit b96848f

Please sign in to comment.