This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
/
example.go
104 lines (85 loc) · 2.93 KB
/
example.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"context"
"fmt"
"github.com/alexzimmer96/eventing"
"github.com/alexzimmer96/eventing/provider"
uuid "github.com/satori/go.uuid"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)
func main() {
database := connectToMongo("localhost", "27017", "root", "root", "testEventing")
storage := provider.NewMongoStorageProvider(database, "events", eventing.BasicProjectionGenerator).
WithEvent("ExampleEvent", exampleEventFromCursor)
projectionController := eventing.NewController(&ExampleProjection{}, buildExampleProjection, storage)
exampleEvent := NewExampleEvent(ExampleEventData{SomeString: "test"})
err := projectionController.SaveEvent(context.Background(), exampleEvent)
if err != nil {
log.Fatal("could not save event")
}
_, err = projectionController.GetLatestProjection(context.Background(), exampleEvent.EntityID)
if err != nil {
log.Fatal("could not save event")
}
}
//======================================================================================================================
type ExampleEvent struct {
eventing.BasicEvent `bson:",inline"`
Data ExampleEventData `json:"data" bson:"data"`
}
type ExampleEventData struct {
SomeString string `json:"some_string" bson:"some_string"`
}
func NewExampleEvent(data ExampleEventData) *ExampleEvent {
return &ExampleEvent{
BasicEvent: eventing.NewBasicEvent(uuid.NewV4().String(), "ExampleEvent"),
Data: data,
}
}
func exampleEventFromCursor(raw *mongo.Cursor) (eventing.Event, error) {
e := &ExampleEvent{}
err := raw.Decode(e)
if err != nil {
return nil, err
}
return e, nil
}
//======================================================================================================================
type ExampleProjection struct {
eventing.BasicProjection `bson:",inline"`
SomeString string `json:"some_string" bson:"some_string"`
}
func buildExampleProjection(ctx context.Context, events []eventing.Event) (eventing.Projection, error) {
ex := &ExampleProjection{}
for _, event := range events {
ex.Apply(event)
}
return ex, nil
}
func (ex *ExampleProjection) Apply(event eventing.Event) {
ex.BasicProjection.Apply(event)
switch v := event.(type) {
case *ExampleEvent:
ex.SomeString = v.Data.SomeString
default:
fmt.Println("could not handle event")
}
}
//======================================================================================================================
func connectToMongo(host, port, user, password, dbName string) *mongo.Database {
opts := options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s", host, port))
opts.SetAuth(options.Credential{
Username: user,
Password: password,
})
client, err := mongo.Connect(context.Background(), opts)
if err != nil {
log.Fatal("could not connect to database")
}
if err := client.Ping(context.Background(), nil); err != nil {
log.Fatal("could not ping database")
}
return client.Database(dbName)
}