Skip to content

Commit 2db5ff5

Browse files
committed
add data jsonpath and querying support
1 parent 10a4f79 commit 2db5ff5

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

internal/data/data.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package data
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/TanmoySG/wunderDB/internal/filter"
@@ -9,12 +10,20 @@ import (
910
"github.com/TanmoySG/wunderDB/pkg/schema"
1011
"github.com/TanmoySG/wunderDB/pkg/utils/maps"
1112
er "github.com/TanmoySG/wunderDB/pkg/wdb/errors"
13+
"github.com/spyzhov/ajson"
1214
)
1315

1416
const (
1517
defaultPrimaryKeyField = "recordId"
1618
)
1719

20+
var (
21+
JsonPathQuery QueryType = "jsonpath"
22+
EvaluateQuery QueryType = "evaluate"
23+
)
24+
25+
type QueryType string
26+
1827
type Data struct {
1928
Data map[model.Identifier]*model.Record
2029
Schema model.Schema
@@ -69,6 +78,7 @@ func (d Data) Read(filters interface{}) (map[model.Identifier]*model.Record, *er
6978
filteredData := f.Filter(*d.PrimaryKey, d.Data)
7079
return filteredData, nil
7180
}
81+
7282
return d.Data, nil
7383
}
7484

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

140+
func (d Data) Query(query string, mode QueryType) (interface{}, *er.WdbError) {
141+
142+
jsonData, err := json.Marshal(d.Data)
143+
if err != nil {
144+
return nil, nil
145+
}
146+
147+
var queryResultNodes []*ajson.Node
148+
var queryResults []interface{}
149+
150+
root, err := ajson.Unmarshal(jsonData)
151+
if err != nil {
152+
return nil, nil
153+
}
154+
155+
switch mode {
156+
case JsonPathQuery:
157+
jpqResult, err := root.JSONPath(query)
158+
if err != nil {
159+
fmt.Println("ths", err)
160+
}
161+
queryResultNodes = jpqResult
162+
case EvaluateQuery:
163+
evqResult, err := ajson.Eval(root, query)
164+
if err != nil {
165+
fmt.Println("thsd", err)
166+
}
167+
168+
queryResultNodes = []*ajson.Node{evqResult}
169+
}
170+
171+
for _, node := range queryResultNodes {
172+
marshaledNode, err := ajson.Marshal(node)
173+
if err != nil {
174+
return nil, nil
175+
}
176+
queryResults = append(queryResults, string(marshaledNode))
177+
}
178+
179+
return queryResults, nil
180+
}
181+
130182
func (d Data) getPrimaryKey(recordId model.Identifier, data interface{}) model.Identifier {
131183
primaryKeyValue := recordId.String()
132184

pkg/wdb/data.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ func (wdb wdbClient) GetData(databaseId, collectionId model.Identifier, filters
7575
return fetchedData, nil
7676
}
7777

78+
func (wdb wdbClient) QueryData(databaseId, collectionId model.Identifier, query string, mode d.QueryType) (interface{}, *er.WdbError) {
79+
if !wdb.safeName.Check(databaseId.String()) {
80+
return nil, &er.DatabaseNameFormatError
81+
}
82+
83+
dbExists, database := wdb.Databases.CheckIfExists(databaseId)
84+
if !dbExists {
85+
return nil, &er.DatabaseDoesNotExistsError
86+
}
87+
88+
if !wdb.safeName.Check(collectionId.String()) {
89+
return nil, &er.CollectionNameFormatError
90+
}
91+
92+
collections := c.UseDatabase(database)
93+
94+
collectionExists, collection := collections.CheckIfExists(collectionId)
95+
if !collectionExists {
96+
return nil, &er.CollectionDoesNotExistsError
97+
}
98+
99+
data := d.UseCollection(collection)
100+
101+
return data.Query(query, mode)
102+
}
103+
78104
func (wdb wdbClient) UpdateData(databaseId, collectionId model.Identifier, updatedData, filters interface{}) *er.WdbError {
79105
if !wdb.safeName.Check(databaseId.String()) {
80106
return &er.DatabaseNameFormatError

pkg/wdb/wdb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wdbClient
22

33
import (
44
"github.com/TanmoySG/wunderDB/internal/config"
5+
"github.com/TanmoySG/wunderDB/internal/data"
56
d "github.com/TanmoySG/wunderDB/internal/databases"
67
r "github.com/TanmoySG/wunderDB/internal/roles"
78
u "github.com/TanmoySG/wunderDB/internal/users"
@@ -41,6 +42,7 @@ type Client interface {
4142
GetData(databaseId model.Identifier, collectionId model.Identifier, filters interface{}) (map[model.Identifier]*model.Record, *er.WdbError)
4243
UpdateData(databaseId model.Identifier, collectionId model.Identifier, updatedData interface{}, filters interface{}) *er.WdbError
4344
DeleteData(databaseId model.Identifier, collectionId model.Identifier, filters interface{}) *er.WdbError
45+
QueryData(databaseId, collectionId model.Identifier, query string, mode data.QueryType) (interface{}, *er.WdbError)
4446

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

0 commit comments

Comments
 (0)