From f078fbb4468193dc95cc1a26adbd6be8b0a7c871 Mon Sep 17 00:00:00 2001 From: Remade Date: Fri, 25 Oct 2024 09:55:33 +0100 Subject: [PATCH] Resolve issues 156 and 157 --- db.go | 27 ++++++++++++++++++++------- db_test.go | 11 ++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/db.go b/db.go index 8fa75e2..8ce17ab 100644 --- a/db.go +++ b/db.go @@ -222,16 +222,29 @@ func Select[TResult any, TWhat TableOrRecord](db *DB, what TWhat) (*TResult, err func Patch(db *DB, what interface{}, patches []PatchData) (*[]PatchData, error) { var patchRes connection.RPCResponse[[]PatchData] - err := db.con.Send(&patchRes, "patch", what, patches, true) - return patchRes.Result, err + if err := db.con.Send(&patchRes, "patch", what, patches, true); err != nil { + return nil, err + } + + return patchRes.Result, nil } -func Delete[TWhat TableOrRecord](db *DB, what TWhat) error { - return db.con.Send(nil, "delete", what) +func Delete[TResult any, TWhat TableOrRecord](db *DB, what TWhat) (*TResult, error) { + var res connection.RPCResponse[TResult] + if err := db.con.Send(&res, "delete", what); err != nil { + return nil, err + } + + return res.Result, nil } -func Upsert[TWhat TableOrRecord](db *DB, what TWhat, data interface{}) error { - return db.con.Send(nil, "upsert", what, data) +func Upsert[TResult any, TWhat TableOrRecord](db *DB, what TWhat, data interface{}) (*TResult, error) { + var res connection.RPCResponse[TResult] + if err := db.con.Send(&res, "upsert", what, data); err != nil { + return nil, err + } + + return res.Result, nil } // Update a table or record in the database like a PUT request. @@ -245,7 +258,7 @@ func Update[TResult any, TWhat TableOrRecord](db *DB, what TWhat, data interface } // Merge a table or record in the database like a PATCH request. -func Merge[TResult any](db *DB, what, data interface{}) (*TResult, error) { +func Merge[TResult any, TWhat TableOrRecord](db *DB, what TWhat, data interface{}) (*TResult, error) { var res connection.RPCResponse[TResult] if err := db.con.Send(&res, "merge", what, data); err != nil { return nil, err diff --git a/db_test.go b/db_test.go index df64152..dcdd5c6 100644 --- a/db_test.go +++ b/db_test.go @@ -69,13 +69,13 @@ func TestSurrealDBSuite(t *testing.T) { // SetupTest is called after each test func (s *SurrealDBTestSuite) TearDownTest() { - err := surrealdb.Delete[models.Table](s.db, "users") + _, err := surrealdb.Delete[[]testUser, models.Table](s.db, "users") s.Require().NoError(err) - err = surrealdb.Delete[models.Table](s.db, "persons") + _, err = surrealdb.Delete[[]testUser, models.Table](s.db, "persons") s.Require().NoError(err) - err = surrealdb.Delete[models.Table](s.db, "knows") + _, err = surrealdb.Delete[[]testUser, models.Table](s.db, "knows") s.Require().NoError(err) } @@ -129,7 +129,7 @@ func (s *SurrealDBTestSuite) TestDelete() { s.Require().NoError(err) // Delete the users... - err = surrealdb.Delete(s.db, "users") + _, err = surrealdb.Delete[[]testUser](s.db, "users") s.Require().NoError(err) } @@ -460,7 +460,4 @@ func (s *SurrealDBTestSuite) TestQueryRaw() { var selected []testPerson err = queries[1].GetResult(&selected) s.Require().NoError(err) - - fmt.Println(created) - fmt.Println(selected) }