Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/handle live query diff #105

Merged
merged 12 commits into from
Nov 28, 2023
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,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(table string, diff bool) (string, error) {
id, err := db.send("live", table, diff)
return id.(string), err
}

Expand Down
34 changes: 32 additions & 2 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ func signin(s *SurrealDBTestSuite) interface{} {
}

func (s *SurrealDBTestSuite) TestLiveViaMethod() {
live, err := s.db.Live("users")

live, err := s.db.Live("users", false)
defer func() {
_, err = s.db.Kill(live)
s.Require().NoError(err)
Expand All @@ -148,6 +147,37 @@ 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)

live, err := s.db.Live("users", true)
defer func() {
_, err = s.db.Kill(live)
Atoo35 marked this conversation as resolved.
Show resolved Hide resolved
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/conn/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,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