Skip to content

Commit

Permalink
fix: update change and modify method (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
youkoulayley authored Sep 20, 2023
1 parent 135edbe commit 4e3ec82
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
26 changes: 8 additions & 18 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func (db *DB) Update(what string, data interface{}) (interface{}, error) {
return db.send("update", what, data)
}

// Change a table or record in the database like a PATCH request.
func (db *DB) Change(what string, data interface{}) (interface{}, error) {
return db.send("change", what, data)
// Merge a table or record in the database like a PATCH request.
func (db *DB) Merge(what string, data interface{}) (interface{}, error) {
return db.send("merge", what, data)
}

// Modify applies a series of JSONPatches to a table or record.
func (db *DB) Modify(what string, data []Patch) (interface{}, error) {
return db.send("modify", what, data)
// Patch applies a series of JSONPatches to a table or record.
func (db *DB) Patch(what string, data []Patch) (interface{}, error) {
return db.send("patch", what, data)
}

// Delete a table or a row from the database like a DELETE request.
Expand All @@ -122,20 +122,10 @@ func (db *DB) send(method string, params ...interface{}) (interface{}, error) {
}

switch method {
case "select", "create", "update", "merge", "patch", "insert":
return db.resp(method, params, resp)
case "delete":
return nil, nil
case "select":
return db.resp(method, params, resp)
case "create":
return db.resp(method, params, resp)
case "update":
return db.resp(method, params, resp)
case "change":
return db.resp(method, params, resp)
case "modify":
return db.resp(method, params, resp)
case "insert":
return db.resp(method, params, resp)
default:
return resp, nil
}
Expand Down
31 changes: 28 additions & 3 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,30 @@ func (s *SurrealDBTestSuite) TestUnmarshalRaw() {
s.Empty(userSlice[0].Result)
}

func (s *SurrealDBTestSuite) TestModify() {
func (s *SurrealDBTestSuite) TestMerge() {
_, err := s.db.Create("users:999", map[string]interface{}{
"username": "john999",
"password": "123",
})
s.NoError(err)

// Update the user
_, err = s.db.Merge("users:999", map[string]string{
"password": "456",
})
s.Require().NoError(err)

user2, err := s.db.Select("users:999")
s.Require().NoError(err)

username := (user2).(map[string]interface{})["username"].(string)
password := (user2).(map[string]interface{})["password"].(string)

s.Equal("john999", username) // Ensure username hasn't change.
s.Equal("456", password)
}

func (s *SurrealDBTestSuite) TestPatch() {
_, err := s.db.Create("users:999", map[string]interface{}{
"username": "john999",
"password": "123",
Expand All @@ -387,15 +410,17 @@ func (s *SurrealDBTestSuite) TestModify() {
}

// Update the user
_, err = s.db.Modify("users:999", patches)
_, err = s.db.Patch("users:999", patches)
s.Require().NoError(err)

user2, err := s.db.Select("users:999")
s.Require().NoError(err)

username := (user2).(map[string]interface{})["username"].(string)
data := (user2).(map[string]interface{})["age"].(float64)

s.Equal(patches[1].Value, int(data))
s.Equal("john999", username) // Ensure username hasn't change.
s.EqualValues(patches[1].Value, data)
}

func (s *SurrealDBTestSuite) TestNonRowSelect() {
Expand Down

0 comments on commit 4e3ec82

Please sign in to comment.