Skip to content

Commit

Permalink
add data jsonpath and querying support
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG committed Apr 22, 2024
1 parent 10a4f79 commit 2db5ff5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
52 changes: 52 additions & 0 deletions internal/data/data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package data

import (
"encoding/json"
"fmt"

"github.com/TanmoySG/wunderDB/internal/filter"
Expand All @@ -9,12 +10,20 @@ import (
"github.com/TanmoySG/wunderDB/pkg/schema"
"github.com/TanmoySG/wunderDB/pkg/utils/maps"
er "github.com/TanmoySG/wunderDB/pkg/wdb/errors"
"github.com/spyzhov/ajson"
)

const (
defaultPrimaryKeyField = "recordId"
)

var (
JsonPathQuery QueryType = "jsonpath"
EvaluateQuery QueryType = "evaluate"
)

type QueryType string

type Data struct {
Data map[model.Identifier]*model.Record
Schema model.Schema
Expand Down Expand Up @@ -69,6 +78,7 @@ func (d Data) Read(filters interface{}) (map[model.Identifier]*model.Record, *er
filteredData := f.Filter(*d.PrimaryKey, d.Data)
return filteredData, nil
}

return d.Data, nil
}

Expand Down Expand Up @@ -127,6 +137,48 @@ func (d Data) Delete(filters interface{}) *er.WdbError {
return &er.FilterMissingError
}

func (d Data) Query(query string, mode QueryType) (interface{}, *er.WdbError) {

jsonData, err := json.Marshal(d.Data)
if err != nil {
return nil, nil
}

var queryResultNodes []*ajson.Node
var queryResults []interface{}

root, err := ajson.Unmarshal(jsonData)
if err != nil {
return nil, nil
}

switch mode {
case JsonPathQuery:
jpqResult, err := root.JSONPath(query)
if err != nil {
fmt.Println("ths", err)
}
queryResultNodes = jpqResult
case EvaluateQuery:
evqResult, err := ajson.Eval(root, query)
if err != nil {
fmt.Println("thsd", err)
}

queryResultNodes = []*ajson.Node{evqResult}
}

for _, node := range queryResultNodes {
marshaledNode, err := ajson.Marshal(node)
if err != nil {
return nil, nil
}
queryResults = append(queryResults, string(marshaledNode))
}

return queryResults, nil
}

func (d Data) getPrimaryKey(recordId model.Identifier, data interface{}) model.Identifier {
primaryKeyValue := recordId.String()

Expand Down
26 changes: 26 additions & 0 deletions pkg/wdb/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ func (wdb wdbClient) GetData(databaseId, collectionId model.Identifier, filters
return fetchedData, nil
}

func (wdb wdbClient) QueryData(databaseId, collectionId model.Identifier, query string, mode d.QueryType) (interface{}, *er.WdbError) {
if !wdb.safeName.Check(databaseId.String()) {
return nil, &er.DatabaseNameFormatError
}

dbExists, database := wdb.Databases.CheckIfExists(databaseId)
if !dbExists {
return nil, &er.DatabaseDoesNotExistsError
}

if !wdb.safeName.Check(collectionId.String()) {
return nil, &er.CollectionNameFormatError
}

collections := c.UseDatabase(database)

collectionExists, collection := collections.CheckIfExists(collectionId)
if !collectionExists {
return nil, &er.CollectionDoesNotExistsError
}

data := d.UseCollection(collection)

return data.Query(query, mode)
}

func (wdb wdbClient) UpdateData(databaseId, collectionId model.Identifier, updatedData, filters interface{}) *er.WdbError {
if !wdb.safeName.Check(databaseId.String()) {
return &er.DatabaseNameFormatError
Expand Down
2 changes: 2 additions & 0 deletions pkg/wdb/wdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wdbClient

import (
"github.com/TanmoySG/wunderDB/internal/config"
"github.com/TanmoySG/wunderDB/internal/data"
d "github.com/TanmoySG/wunderDB/internal/databases"
r "github.com/TanmoySG/wunderDB/internal/roles"
u "github.com/TanmoySG/wunderDB/internal/users"
Expand Down Expand Up @@ -41,6 +42,7 @@ type Client interface {
GetData(databaseId model.Identifier, collectionId model.Identifier, filters interface{}) (map[model.Identifier]*model.Record, *er.WdbError)
UpdateData(databaseId model.Identifier, collectionId model.Identifier, updatedData interface{}, filters interface{}) *er.WdbError
DeleteData(databaseId model.Identifier, collectionId model.Identifier, filters interface{}) *er.WdbError
QueryData(databaseId, collectionId model.Identifier, query string, mode data.QueryType) (interface{}, *er.WdbError)

// Users Methods
CreateUser(userID model.Identifier, password string) *er.WdbError
Expand Down

0 comments on commit 2db5ff5

Please sign in to comment.