-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[Dev] How to properly return arrays from a driver? #964
Comments
I think I found part of my answer: Lines 342 to 443 in 41dac16
As far as I understand, any subelements of a struct will have their name in the TypeQueue be separated by a dot.
If I am not wrong, this would result, that starting with
That said, there's so much going on in that function alone, I am quite sure I overlooked at least something... |
Changed the title, after doing some tests: package main
import (
"fmt"
_ "github.com/IngwiePhoenix/surrealdb-driver"
"github.com/jmoiron/sqlx"
)
type Book struct {
ID string
Title string
}
type Author struct {
Id string
Name string
Likes []string
}
func main() {
defSql := `
DEFINE NAMESPACE IF NOT EXISTS p2;
USE NS p2;
DEFINE DATABASE IF NOT EXISTS p2;
USE DB p2;
DEFINE TABLE IF NOT EXISTS books SCHEMAFULL;
DEFINE FIELD IF NOT EXISTS title ON books TYPE string;
DEFINE TABLE IF NOT EXISTS authors SCHEMAFULL;
DEFINE FIELD IF NOT EXISTS name ON authors TYPE string;
DEFINE FIELD IF NOT EXISTS likes ON authors TYPE array<string>;
// DEFINE FIELD IF NOT EXISTS written ON author TYPE array<optional<record<books>>>
`
db, err := sqlx.Connect("surrealdb", "ws://db:db@localhost:8000/rpc?method=root&db=p2&ns=p2")
if err != nil {
panic(err.Error())
}
res, err := db.Exec(defSql)
if err != nil {
panic(err.Error())
}
ins, _ := res.RowsAffected()
fmt.Println(ins)
res, _ = db.Exec(`
CREATE authors:chris CONTENT {
name: "Christopher",
likes: ["a", "lot", "of", "stuff"]
}
`) // ignored; exists after rerun
rows, err := db.Queryx("SELECT * FROM authors;")
if err != nil {
panic(err.Error())
}
for rows.Next() {
if rows.Err() != nil {
panic(rows.Err())
}
cols, err := rows.Columns()
if err != nil {
panic(err.Error())
}
a := Author{}
fmt.Println("----------")
fmt.Println(cols)
err = rows.StructScan(&a) // <- implodes here
if err != nil {
panic(err.Error())
}
//a.Likes = make([]string, 4)
//rows.Scan(&a.Id, &a.Likes[0], &a.Likes[1], &a.Likes[2], &a.Likes[3], &a.Name)
fmt.Println(a)
fmt.Println("----------")
}
} As you can see, The driver returned the following:
The records are internally received as a JSON message - so it's nested. So, what do I "tell" sqlx, in the columns, to make it like arrays? Do I just return a blank array for Thanks! |
Hello! Discussions are not enabled, so I am making a ticket for this... apologies. :)
I am currently working on a SurrealDB Driver and I am currently "fighting" with related records.
Basically, SurrealDB has no
JOIN
, instead you useFETCH $field...
inSELECT
to pick fields whose related records should be resolved.To demonstrate:
When querying, a JSON object is returned (or a nested CBOR structure) as a result and currently, my driver's
Next(dest []any) error
method will see any sub-object as being a complex type and return[]byte
to avoid crashes.For instance:
And basically, this would result in:
...but after reading through the docs, I am not so sure if this should be the case or not. Hence, why I would like to ask some people with more experience in this :)
When using sqlx'
ScanStruct
methods (and similiar), how does it actually determine the keys to use to read into the struct? Is there any "post processing" I could possibly do to assist from within my driver?So far, I see that perhaps I should instead use dot-notation for the columns:
ID, author.ID, author.name
. Is that assumption correct?I just want to make sure my driver is well compatible. So far I tried to make REL work - and it kinda does but just fails when automatically attempting to determine foreign keys which just do not exist in SurrealDB - but I think there is still room for improvements.
Thank you very much and kind regards,
Ingwie
The text was updated successfully, but these errors were encountered: