-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathexample_insert_relation_test.go
More file actions
156 lines (140 loc) · 3.57 KB
/
Copy pathexample_insert_relation_test.go
File metadata and controls
156 lines (140 loc) · 3.57 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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/connection"
"github.com/surrealdb/surrealdb.go/pkg/models"
)
func ExampleInsertRelation() {
db := testenv.MustNew("surrealdbexamples", "query", "person", "follow")
type Person struct {
ID models.RecordID `json:"id,omitempty"`
}
type Follow struct {
In *models.RecordID `json:"in,omitempty"`
Out *models.RecordID `json:"out,omitempty"`
Since models.CustomDateTime `json:"since"`
}
first, err := surrealdb.Create[Person](
context.Background(),
db,
"person",
map[string]any{
"id": models.NewRecordID("person", "first"),
})
if err != nil {
panic(err)
}
second, err := surrealdb.Create[Person](
context.Background(),
db,
"person",
map[string]any{
"id": models.NewRecordID("person", "second"),
})
if err != nil {
panic(err)
}
since, err := time.Parse(time.RFC3339, "2023-10-01T12:00:00Z")
if err != nil {
panic(err)
}
persons, err := surrealdb.Query[[]Person](
context.Background(),
db,
"SELECT * FROM person ORDER BY id.id",
nil,
)
if err != nil {
panic(err)
}
for _, person := range (*persons)[0].Result {
fmt.Printf("Person: %+v\n", person)
}
res, relateErr := surrealdb.InsertRelation[[]connection.ResponseID[models.RecordID]](
context.Background(),
db,
&surrealdb.Relationship{
ID: &models.RecordID{Table: "follow", ID: "first_second"},
In: first.ID,
Out: second.ID,
Relation: "follow",
Data: map[string]any{
"since": models.CustomDateTime{
Time: since,
},
},
},
)
if relateErr != nil {
panic(relateErr)
}
if res == nil {
panic("relation response is nil")
}
if (*res)[0].ID.ID != "first_second" {
panic("relation ID must be set to 'first_second'")
}
//nolint:lll
/// Here's an alternative way to insert a relation using a query.
//
// if res, err := surrealdb.Query[any](
// db,
// "INSERT RELATION INTO follow $content",
// map[string]any{
// "content": map[string]any{
// "id": "first_second",
// "in": first.ID,
// "out": second.ID,
// "since": models.CustomDateTime{Time: since},
// },
// },
// ); err != nil {
// panic(err)
// } else {
// fmt.Printf("Relation: %+v\n", (*res)[0].Result)
// }
// The output will be:
// Relation: [map[id:{Table:follow ID:first_second} in:{Table:person ID:first} out:{Table:person ID:second} since:{Time:2023-10-01 12:00:00 +0000 UTC}]]
type PersonWithFollows struct {
Person
Follows []models.RecordID `json:"follows,omitempty"`
}
selected, err := surrealdb.Query[[]PersonWithFollows](
context.Background(),
db,
"SELECT id, name, ->follow->person AS follows FROM $id",
map[string]any{
"id": first.ID,
},
)
if err != nil {
panic(err)
}
for _, person := range (*selected)[0].Result {
fmt.Printf("PersonWithFollows: %+v\n", person)
}
// Note we can select the relationships themselves because
// RELATE creates a record in the relation table.
follows, err := surrealdb.Query[[]Follow](
context.Background(),
db,
"SELECT * from follow",
nil,
)
if err != nil {
panic(err)
}
for _, follow := range (*follows)[0].Result {
fmt.Printf("Follow: %+v\n", follow)
}
//nolint:lll
// Output:
// Person: {ID:{Table:person ID:first}}
// Person: {ID:{Table:person ID:second}}
// PersonWithFollows: {Person:{ID:{Table:person ID:first}} Follows:[{Table:person ID:second}]}
// Follow: {In:person:first Out:person:second Since:{Time:2023-10-01 12:00:00 +0000 UTC}}
}