forked from surrealdb/surrealdb.go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_query_embedded_struct_test.go
More file actions
114 lines (103 loc) · 3.5 KB
/
example_query_embedded_struct_test.go
File metadata and controls
114 lines (103 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package surrealdb_test
import (
"context"
"fmt"
"time"
surrealdb "github.com/surrealdb/surrealdb.go"
"github.com/surrealdb/surrealdb.go/contrib/testenv"
"github.com/surrealdb/surrealdb.go/pkg/models"
)
//nolint:funlen
func ExampleQuery_embedded_struct() {
db := testenv.MustNew("surrealdbexamples", "query", "persons")
type Base struct {
ID *models.RecordID `json:"id,omitempty"`
}
type Profile struct {
Base
City string `json:"city"`
}
type Person struct {
Base
Name string `json:"name"`
Profile Profile
CreatedAt models.CustomDateTime `json:"created_at,omitempty"`
UpdatedAt *models.CustomDateTime `json:"updated_at,omitempty"`
}
createdAt, err := time.Parse(time.RFC3339, "2023-10-01T12:00:00Z")
if err != nil {
panic(err)
}
createQueryResults, err := surrealdb.Query[[]Person](
context.Background(),
db,
`CREATE type::thing($tb, $id) CONTENT $content`,
map[string]any{
"tb": "persons",
"id": "yusuke",
"content": map[string]any{
"name": "Yusuke",
"created_at": models.CustomDateTime{
Time: createdAt,
},
"profile": map[string]any{
"id": models.NewRecordID("profiles", "yusuke"),
"city": "Tokyo",
},
},
})
if err != nil {
panic(err)
}
fmt.Printf("Number of query results: %d\n", len(*createQueryResults))
fmt.Printf("First query result's status: %+s\n", (*createQueryResults)[0].Status)
fmt.Printf("Persons contained in the first query result: %+v\n", (*createQueryResults)[0].Result)
updatedAt, err := time.Parse(time.RFC3339, "2023-10-02T12:00:00Z")
if err != nil {
panic(err)
}
updateQueryResults, err := surrealdb.Query[[]Person](
context.Background(),
db,
`UPDATE $id CONTENT $content`,
map[string]any{
"id": models.NewRecordID("persons", "yusuke"),
"content": map[string]any{
"name": "Yusuke Updated Last",
"created_at": createdAt,
"updated_at": updatedAt,
},
},
)
if err != nil {
panic(err)
}
fmt.Printf("Number of update query results: %d\n", len(*updateQueryResults))
fmt.Printf("Update query result's status: %+s\n", (*updateQueryResults)[0].Status)
fmt.Printf("Persons contained in the update query result: %+v\n", (*updateQueryResults)[0].Result)
selectQueryResults, err := surrealdb.Query[[]Person](
context.Background(),
db,
`SELECT * FROM $id`,
map[string]any{
"id": models.NewRecordID("persons", "yusuke"),
},
)
if err != nil {
panic(err)
}
fmt.Printf("Number of select query results: %d\n", len(*selectQueryResults))
fmt.Printf("Select query result's status: %+s\n", (*selectQueryResults)[0].Status)
fmt.Printf("Persons contained in the select query result: %+v\n", (*selectQueryResults)[0].Result)
//nolint:lll
// Output:
// Number of query results: 1
// First query result's status: OK
// Persons contained in the first query result: [{Base:{ID:persons:yusuke} Name:Yusuke Profile:{Base:{ID:profiles:yusuke} City:Tokyo} CreatedAt:{Time:2023-10-01 12:00:00 +0000 UTC} UpdatedAt:<nil>}]
// Number of update query results: 1
// Update query result's status: OK
// Persons contained in the update query result: [{Base:{ID:persons:yusuke} Name:Yusuke Updated Last Profile:{Base:{ID:<nil>} City:} CreatedAt:{Time:2023-10-01 12:00:00 +0000 UTC} UpdatedAt:2023-10-02T12:00:00Z}]
// Number of select query results: 1
// Select query result's status: OK
// Persons contained in the select query result: [{Base:{ID:persons:yusuke} Name:Yusuke Updated Last Profile:{Base:{ID:<nil>} City:} CreatedAt:{Time:2023-10-01 12:00:00 +0000 UTC} UpdatedAt:2023-10-02T12:00:00Z}]
}