-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
65 lines (55 loc) · 1.58 KB
/
example_test.go
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
package tutorial_ent
import (
"context"
"fmt"
"log"
"entgo.io/ent/dialect"
_ "github.com/mattn/go-sqlite3"
"github.com/pannpers/tutorial-ent/ent"
"github.com/pannpers/tutorial-ent/ent/todo"
)
func main() {
client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed to open connection to sqlite: %v", err)
}
defer client.Close()
client = client.Debug()
ctx := context.Background()
// Run the automatic migration tool to create all schema resources.
if err := client.Schema.Create(ctx); err != nil {
log.Fatalf("failed to create schema resources: %v", err)
}
task1, err := client.Todo.Create().
SetText("Add GraphQL example").
Save(ctx)
if err != nil {
log.Fatalf("failed to create a todo: %v", err)
}
fmt.Printf("%d: %q\n", task1.ID, task1.Text)
task2, err := client.Todo.Create().
SetText("Add tracing example").
Save(ctx)
if err != nil {
log.Fatalf("failed to create a todo: %v", err)
}
fmt.Printf("%d: %q\n", task2.ID, task2.Text)
if err := task2.Update().SetParent(task1).Exec(ctx); err != nil {
log.Fatalf("failed to connect todo2 to its parent: %v", err)
}
items, err := client.Todo.Query().All(ctx)
if err != nil {
log.Fatalf("failed to query todos: %v", err)
}
for _, t := range items {
fmt.Printf("%d: %q\n", t.ID, t.Text)
}
// Query all todo items that depend on other items.
items, err = client.Todo.Query().Where(todo.HasParent()).All(ctx)
if err != nil {
log.Fatalf("failed to query todos: %v", err)
}
for _, t := range items {
fmt.Printf("%d: %q\n", t.ID, t.Text)
}
}