Skip to content

Commit

Permalink
added support for diff in live queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Atoo35 committed Oct 17, 2023
1 parent 3e16bce commit 36d3419
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (db *DB) Authenticate(token string) (interface{}, error) {

// --------------------------------------------------

func (db *DB) Live(table string) (string, error) {
id, err := db.send("live", table)
func (db *DB) Live(query *LiveQuery) (string, error) {
id, err := db.send("live", query.Table, query.Diff)
return id.(string), err
}

Expand Down
41 changes: 40 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ func signin(s *SurrealDBTestSuite) interface{} {
}

func (s *SurrealDBTestSuite) TestLiveViaMethod() {
live, err := s.db.Live("users")
query := &surrealdb.LiveQuery{
Table: "users",
}

live, err := s.db.Live(query)
defer func() {
_, err = s.db.Kill(live)
s.Require().NoError(err)
Expand All @@ -148,6 +151,42 @@ func (s *SurrealDBTestSuite) TestLiveViaMethod() {
s.Require().Equal(live, notification.ID)
}

func (s *SurrealDBTestSuite) TestLiveWithOptionsViaMethod() {
// create a user
userData, e := s.db.Create("users", map[string]interface{}{
"username": "johnny",
"password": "123",
})
s.Require().NoError(e)
var user []testUser
err := marshal.Unmarshal(userData, &user)
s.Require().NoError(err)

query := &surrealdb.LiveQuery{
Table: "users",
Diff: true,
}

live, err := s.db.Live(query)
defer func() {
_, err = s.db.Kill(live)
s.Require().NoError(err)
}()

notifications, er := s.db.LiveNotifications(live)
s.Require().NoError(er)

// update the user
_, e = s.db.Update(user[0].ID, map[string]interface{}{
"password": "456",
})
s.Require().NoError(e)

notification := <-notifications
s.Require().Equal(model.UpdateAction, notification.Action)
s.Require().Equal(live, notification.ID)
}

func (s *SurrealDBTestSuite) TestLiveViaQuery() {
liveResponse, err := s.db.Query("LIVE SELECT * FROM users", map[string]interface{}{})
assert.NoError(s.T(), err)
Expand Down
9 changes: 2 additions & 7 deletions pkg/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,8 @@ func unmarshalMapToStruct(data map[string]interface{}, outStruct interface{}) er
return err
}
fieldValue.SetBool(boolVal)
case reflect.Map:
mapVal, ok := mapValue.(map[string]interface{})
if !ok {
return fmt.Errorf("mapValue for property %s is not a map[string]interface{}", fieldName)
}
fieldValue.Set(reflect.ValueOf(mapVal))

case reflect.Interface:
fieldValue.Set(reflect.ValueOf(mapValue))
// Add cases for other types as needed
default:
return fmt.Errorf("unsupported field type: %s", fieldName)
Expand Down
7 changes: 3 additions & 4 deletions pkg/model/notification.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package model

type Notification struct {
ID string `json:"id"`
Action Action `json:"action"`
Result map[string]interface{} `json:"result"`
ID string `json:"id"`
Action Action `json:"action"`
Result interface{} `json:"result"`
}

type Action string

const (
Expand Down
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ type Patch struct {
Path string `json:"path"`
Value any `json:"value"`
}

type LiveQuery struct {
Table string
Diff bool
}

0 comments on commit 36d3419

Please sign in to comment.