Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
NoBypass committed Nov 15, 2024
2 parents c229788 + 92197e9 commit 895fb4d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,38 @@ Unmarshal and Scan functions will automatically convert the SurrealDB formats ba
If you don't like using the `db` tag, or your struct already uses it for something else, you can use the `fallback` tag.
For example if most of your structs use the `json` tag, you can set the fallback tag to `json`. This way for the fields
which don't have a `db` tag, the library will look for a `json` tag. Here is an example:
````go
type User struct {
// `dbName` will be used for marshaling since the db tag takes priority over the fallback tag
Name string `json:"name,omitempty" db:"dbName"`
// `birthday` will be used for marshaling since no db tag was provided
Birthday time.Time `json:"birthday"`
}

db, err := surgo.Connect("ws://localhost:8000", &surgo.Credentials{
// enter your credentials here
}, surgo.WithFallbackTag("json"))
if err != nil {
// handle error
}

err := db.Query("CREATE $john CONTENT $data", map[string]any{
"john": "users:john",
"data": User{
Name: "John",
Birthday: time.Date(1980, 1, 1, 0, 0, 0, 0, time.UTC),
},
}).Error
````

Running this will result in this entry in SurrealDB:
```
{
id: 'someRandomString',
dbName: 'John',
birthday: '1980-01-01T00:00:00Z'
}
```

### Tracing & Context
You can use the `WithLogger` option to pass a custom logger/tracer to the `Connect` function. The logger/tracer must
Expand All @@ -170,4 +202,15 @@ result := db.WithContext(ctx).Query("SELECT * FROM ONLY $john", map[string]any{
})
```

Of course, you can also use a cancelable context or timeout context.
Of course, you can also use a cancelable context or timeout context.\
These are the possible traces (They will be called in the order they are listed):

| TraceType | Description |
| --- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| TraceQuery | This signals the start of a query and the data will be the query `string` |
| TraceVars | At this point all vars were computed and the data will be a `map[string]any` |
| TraceResponse | This signals that SurrealDB responded and the data it responded with will be a `map[string]any` |
| TraceEnd | This signals the end of the query and the data will be the value which the called function returned * |

\* This will be either of type `*surgo.Result` for a `DB.Query` call or simply `error` for a `DB.Scan` call.\
If an error occurs between steps, these traces will be skipped and `TraceEnd` will be called immediately.
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (db *DB) Query(query string, vars map[string]any) (result *Result) {
// If multiple results are expected, a pointer to a slice of structs or maps can be passed.
// NOTE: Only the last result (the last query if multiple are present) is scanned into the
// given object. If any of the queries fail, the error is returned.
func (db DB) Scan(dest any, query string, vars map[string]any) error {
func (db DB) Scan(dest any, query string, vars map[string]any) (err error) {
db.ctx = context.WithValue(safeContext(db.ctx), scanCtxKey, true)
defer func() {
db.logger.Trace(db.ctx, TraceEnd, nil)
db.logger.Trace(db.ctx, TraceEnd, err)
}()

result := db.Query(query, vars)
Expand Down
2 changes: 1 addition & 1 deletion rpc/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func (c *WebsocketConn) listen() {
for {
_, msg, err := c.Read(context.TODO())
_, msg, err := c.Read(context.Background())
if err != nil {
c.logger.Error(err)
return
Expand Down
2 changes: 1 addition & 1 deletion rpc/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Logger interface {
}

func NewWebsocketConn(url string, logger Logger) (*WebsocketConn, error) {
c, _, err := websocket.Dial(context.TODO(), url, nil)
c, _, err := websocket.Dial(context.Background(), url, nil)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions surgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func Connect(url string, creds *Credentials, opts ...Option) (*DB, error) {

ctx, cancel := context.WithTimeout(context.Background(), db.timeout)
defer cancel()

_, err = c.Send(ctx, "signin", []any{creds})
if err != nil {
return nil, errs.ErrInvalidCredentials.With(err)
Expand Down

0 comments on commit 895fb4d

Please # to comment.