fix(module,store): allow querying on _id, _createdAt, _updatedAt attributes

This commit is contained in:
2023-02-21 15:05:01 +01:00
parent f01b1ef3b2
commit b9f985ab0c
5 changed files with 284 additions and 7 deletions

View File

@ -3,10 +3,36 @@ package sqlite
import (
"fmt"
"forge.cadoles.com/arcad/edge/pkg/storage"
"forge.cadoles.com/arcad/edge/pkg/storage/filter/sql"
)
func transformOperator(operator string, invert bool, key string, value any, option *sql.Option) (string, any, error) {
isDataAttr := true
switch key {
case storage.DocumentAttrCreatedAt:
key = "created_at"
isDataAttr = false
case storage.DocumentAttrUpdatedAt:
key = "updated_at"
isDataAttr = false
case storage.DocumentAttrID:
key = "id"
isDataAttr = false
}
if !isDataAttr {
option = &sql.Option{
PreparedParameter: option.PreparedParameter,
KeyTransform: func(key string) string {
return key
},
ValueTransform: option.ValueTransform,
Transform: option.Transform,
}
}
switch operator {
case sql.OpIn:
return transformInOperator(key, value, option)

View File

@ -6,7 +6,6 @@ import (
"forge.cadoles.com/arcad/edge/pkg/storage"
"forge.cadoles.com/arcad/edge/pkg/storage/filter"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
)
@ -58,6 +57,43 @@ var documentStoreOpsTestCases = []documentStoreOpsTestCase{
return nil
},
},
{
Name: "Query on _id",
Run: func(ctx context.Context, store storage.DocumentStore) error {
collection := "query_on_id"
doc := storage.Document{
"attr1": "Foo",
}
upsertedDoc, err := store.Upsert(ctx, collection, doc)
if err != nil {
return errors.WithStack(err)
}
docID, ok := upsertedDoc.ID()
if !ok {
return errors.Errorf("")
}
filter := filter.New(
filter.NewEqOperator(map[string]interface{}{
"_id": docID,
}),
)
results, err := store.Query(ctx, collection, filter, nil)
if err != nil {
return errors.WithStack(err)
}
if e, g := 1, len(results); e != g {
return errors.Errorf("len(results): expected '%v', got '%v'", e, g)
}
return nil
},
},
{
Name: "Query with 'IN' operator",
Run: func(ctx context.Context, store storage.DocumentStore) error {
@ -160,8 +196,6 @@ var documentStoreOpsTestCases = []documentStoreOpsTestCase{
return errors.WithStack(err)
}
spew.Dump(upsertedDoc, upsertedDoc2)
prevID, _ := upsertedDoc.ID()
newID, _ := upsertedDoc2.ID()