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

added connection interface for non-ws to add in future #108

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import (

"github.com/surrealdb/surrealdb.go/pkg/model"

"github.com/surrealdb/surrealdb.go/pkg/conn"
"github.com/surrealdb/surrealdb.go/pkg/constants"
"github.com/surrealdb/surrealdb.go/pkg/websocket"
)

// DB is a client for the SurrealDB database that holds are websocket connection.
type DB struct {
ws websocket.WebSocket
conn conn.Connection
}

// New creates a new SurrealDB client.
func New(url string, ws websocket.WebSocket) (*DB, error) {
return &DB{ws}, nil
// New creates a new SurrealDB lient.
func New(url string, connection conn.Connection) (*DB, error) {
connection, err := connection.Connect(url)
if err != nil {
return nil, err
}
return &DB{connection}, nil
}

// --------------------------------------------------
Expand All @@ -25,7 +29,7 @@ func New(url string, ws websocket.WebSocket) (*DB, error) {

// Close closes the underlying WebSocket connection.
func (db *DB) Close() {
_ = db.ws.Close()
_ = db.conn.Close()
}

// --------------------------------------------------
Expand Down Expand Up @@ -114,7 +118,7 @@ func (db *DB) Insert(what string, data interface{}) (interface{}, error) {

// LiveNotifications returns a channel for live query.
func (db *DB) LiveNotifications(liveQueryID string) (chan model.Notification, error) {
return db.ws.LiveNotifications(liveQueryID)
return db.conn.LiveNotifications(liveQueryID)
}

// --------------------------------------------------
Expand All @@ -124,7 +128,7 @@ func (db *DB) LiveNotifications(liveQueryID string) (chan model.Notification, er
// send is a helper method for sending a query to the database.
func (db *DB) send(method string, params ...interface{}) (interface{}, error) {
// here we send the args through our websocket connection
resp, err := db.ws.Send(method, params)
resp, err := db.conn.Send(method, params)
if err != nil {
return nil, fmt.Errorf("sending request failed for method '%s': %w", method, err)
}
Expand Down
24 changes: 11 additions & 13 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/surrealdb/surrealdb.go"
"github.com/surrealdb/surrealdb.go/pkg/conn/gorilla"
"github.com/surrealdb/surrealdb.go/pkg/constants"
gorilla "github.com/surrealdb/surrealdb.go/pkg/gorilla"

"github.com/surrealdb/surrealdb.go/pkg/conn"
"github.com/surrealdb/surrealdb.go/pkg/logger"
"github.com/surrealdb/surrealdb.go/pkg/marshal"
"github.com/surrealdb/surrealdb.go/pkg/websocket"
)

// TestDBSuite is a test s for the DB struct
type SurrealDBTestSuite struct {
suite.Suite
db *surrealdb.DB
name string
wsImplementations map[string]websocket.WebSocket
db *surrealdb.DB
name string
connImplementations map[string]conn.Connection
}

// a simple user struct for testing
Expand All @@ -43,15 +43,15 @@ type testUser struct {

func TestSurrealDBSuite(t *testing.T) {
SurrealDBSuite := new(SurrealDBTestSuite)
SurrealDBSuite.wsImplementations = make(map[string]websocket.WebSocket)
SurrealDBSuite.connImplementations = make(map[string]conn.Connection)

// Without options
logData := createLogger(t)
SurrealDBSuite.wsImplementations["gorilla"] = gorilla.Create().Logger(logData)
SurrealDBSuite.connImplementations["gorilla"] = gorilla.Create().Logger(logData)

// With options
logData = createLogger(t)
SurrealDBSuite.wsImplementations["gorilla_opt"] = gorilla.Create().SetTimeOut(time.Minute).SetCompression(true).Logger(logData)
SurrealDBSuite.connImplementations["gorilla_opt"] = gorilla.Create().SetTimeOut(time.Minute).SetCompression(true).Logger(logData)

RunWsMap(t, SurrealDBSuite)
}
Expand All @@ -64,7 +64,7 @@ func createLogger(t *testing.T) logger.Logger {
}

func RunWsMap(t *testing.T, s *SurrealDBTestSuite) {
for wsName := range s.wsImplementations {
for wsName := range s.connImplementations {
// Run the test suite
t.Run(wsName, func(t *testing.T) {
s.name = wsName
Expand Down Expand Up @@ -99,11 +99,9 @@ func (s *SurrealDBTestSuite) openConnection() *surrealdb.DB {
if url == "" {
url = "ws://localhost:8000/rpc"
}
impl := s.wsImplementations[s.name]
impl := s.connImplementations[s.name]
require.NotNil(s.T(), impl)
ws, err := impl.Connect(url)
s.Require().NoError(err)
db, err := surrealdb.New(url, ws)
db, err := surrealdb.New(url, impl)
s.Require().NoError(err)
return db
}
Expand Down
4 changes: 2 additions & 2 deletions internal/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package mock
import (
"errors"

"github.com/surrealdb/surrealdb.go/pkg/conn"
"github.com/surrealdb/surrealdb.go/pkg/model"
"github.com/surrealdb/surrealdb.go/pkg/websocket"
)

type ws struct {
}

func (w *ws) Connect(url string) (websocket.WebSocket, error) {
func (w *ws) Connect(url string) (conn.Connection, error) {
return w, nil
}

Expand Down
10 changes: 4 additions & 6 deletions pkg/websocket/websocket.go → pkg/conn/conn.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package websocket
package conn

import (
"github.com/surrealdb/surrealdb.go/pkg/model"
)
import "github.com/surrealdb/surrealdb.go/pkg/model"

type WebSocket interface {
Connect(url string) (WebSocket, error)
type Connection interface {
Connect(url string) (Connection, error)
Send(method string, params []interface{}) (interface{}, error)
Close() error
LiveNotifications(id string) (chan model.Notification, error)
Expand Down
8 changes: 4 additions & 4 deletions pkg/gorilla/gorilla.go → pkg/conn/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

gorilla "github.com/gorilla/websocket"
"github.com/surrealdb/surrealdb.go/internal/rpc"
"github.com/surrealdb/surrealdb.go/pkg/conn"
"github.com/surrealdb/surrealdb.go/pkg/logger"
"github.com/surrealdb/surrealdb.go/pkg/rand"
"github.com/surrealdb/surrealdb.go/pkg/websocket"
)

const (
Expand Down Expand Up @@ -55,16 +55,16 @@ func Create() *WebSocket {
}
}

func (ws *WebSocket) Connect(url string) (websocket.WebSocket, error) {
func (ws *WebSocket) Connect(url string) (conn.Connection, error) {
dialer := gorilla.DefaultDialer
dialer.EnableCompression = true

conn, _, err := dialer.Dial(url, nil)
connection, _, err := dialer.Dial(url, nil)
if err != nil {
return nil, err
}

ws.Conn = conn
ws.Conn = connection

for _, option := range ws.Option {
if err := option(ws); err != nil {
Expand Down