-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (66 loc) · 1.81 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
gogm "github.com/codingfinest/neo4j-go-ogm"
)
//Define models
type Movie struct {
gogm.Node `gogm:"label:FILM,label:PICTURE"`
Title string
Released int64
Tagline string
Characters []*Character `gogm:"direction:<-"`
Directors []*Director `gogm:"direction:<-"`
}
type Director struct {
gogm.Node
Name string
Movies []*Movie
}
type Actor struct {
gogm.Node
Name string
Characters []*Character
}
type Character struct {
gogm.Relationship `gogm:"reltype:ACTED_IN"`
Roles []string
Name string
NumberOfScenes int64
Actor *Actor `gogm:"startNode"`
Movie *Movie `gogm:"endNode"`
}
func main() {
var config = &gogm.Config{
"uri",
"username",
"password",
gogm.NONE/*log level*/,
false/*allow cyclic ref*/}
var ogm = gogm.New(config)
var session, err = ogm.NewSession(true/*isWriteMode*/)
if err != nil {
panic(err)
}
theMatrix := &Movie{}
theMatrix.Title = "The Matrix"
theMatrix.Released = 1999
carrieAnne := &Actor{}
carrieAnne.Name = "Carrie-Anne Moss"
keanu := &Actor{}
keanu.Name = "Keanu Reeves"
carrieAnneMatrixCharacter := &Character{Movie: theMatrix, Actor: carrieAnne, Roles: []string{"Trinity"}}
keanuReevesMatrixCharacter := &Character{Movie: theMatrix, Actor: keanu, Roles: []string{"Neo"}}
theMatrix.Characters = append(theMatrix.Characters, carrieAnneMatrixCharacter, keanuReevesMatrixCharacter)
//Persist the movie. This persists the actors as well and creates the relationships with their associated properties
if err := session.Save(&theMatrix, nil); err != nil {
panic(err)
}
var loadedMatrix *Movie
if err := session.Load(&loadedMatrix, *theMatrix.ID, nil); err != nil {
panic(err)
}
for _, character := range loadedMatrix.Characters {
fmt.Println("Actor: " + character.Actor.Name)
}
}