surrealdb / surrealdb.go

SurrealDB SDK for Golang
https://surrealdb.com
Apache License 2.0
231 stars 60 forks source link

Feature Request - Compatibility with go sql #22

Open EtienneBruines opened 1 year ago

EtienneBruines commented 1 year ago

This mostly involves these tasks:

emehrkay commented 1 year ago

This would make using SurrealDB pretty straight forward.

emehrkay commented 1 year ago

I'd like to help out if possible. Anything I can pick up?

emehrkay commented 1 year ago

I've been playing around with this and I noticed a couple of things:

  1. It isn't too difficult to get FETCH support for returned objects/structs

type fetch[T any] []T

func (yne fetch[T]) Scan(value any) error { if value == nil { yne = fetch[T]{} return nil }

bytes, err := json.Marshal(value)
if err != nil {
    return err
}

items := fetch[T]{}
err = json.Unmarshal(bytes, &items)
if err != nil {
    return err
}

*yne = append(*yne, items...)

return nil

}


2. the fields dont necessarily come back in order, this will be kinda difficult to wrangle when it is time to Scan the data into the values. edit: I just noticed that the columns are always sorted alphabetically 

SELECT name, email, age, ->buys->item as bought from user:1 fetch bought /// [ { "time": "101.532µs", "status": "OK", "result": [ { "age": null, "bought": [ { "id": "item:1", "name": "some item", "price": "999.99" } ], "email": null, "name": "SurrealDB" } ] } ]