Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*.ipr
*.iws

# ignore vs files
.vscode

# ignore local configs
#
*.env
*.env
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.8
- 1.15

install:
- go get github.com/apex/go-apex
Expand All @@ -12,6 +12,7 @@ install:
- go get github.com/go-sql-driver/mysql
- go get github.com/go-sql-driver/mysql
- go get github.com/lib/pq
- go get github.com/boltdb/bolt/...

services:
- mysql
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![GoDoc](https://godoc.org/github.com/altairsix/eventsource?status.svg)](https://godoc.org/github.com/altairsix/eventsource) ![Travis CI](https://travis-ci.org/altairsix/eventsource.svg?branch=master)
[![GoDoc](https://godoc.org/github.com/vancelongwill/eventsource?status.svg)](https://godoc.org/github.com/vancelongwill/eventsource) ![Travis CI](https://travis-ci.org/altairsix/eventsource.svg?branch=master)

```eventsource``` is a Serverless event sourcing library for Go that attempts to
leverage the capabilities of AWS to simplify the development and operational
Expand All @@ -15,7 +15,7 @@ library. What AWS can handle, I'd rather have AWS handle.
## Installation

```
go get github.com/altairsix/eventsource/...
go get github.com/vancelongwill/eventsource/...
```

## Getting Started
Expand Down
2 changes: 2 additions & 0 deletions _examples/boltdb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*debug* # vscode on windows
*.bolt*
122 changes: 122 additions & 0 deletions _examples/boltdb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"context"
"fmt"
"log"
"strconv"
"time"

"github.com/vancelongwill/eventsource"
"github.com/vancelongwill/eventsource/boltdbstore"
)

//Order is an example of state generated from left fold of events
type Order struct {
ID string
Version int
CreatedAt time.Time
UpdatedAt time.Time
State string
}

//OrderCreated event used a marker of order created
type OrderCreated struct {
eventsource.Model
}

//OrderShipped event used a marker of order shipped
type OrderShipped struct {
eventsource.Model
}

//On implements Aggregate interface
func (item *Order) On(event eventsource.Event) error {
switch v := event.(type) {
case *OrderCreated:
item.State = "created"

case *OrderShipped:
item.State = "shipped"

default:
return fmt.Errorf("unable to handle event, %v", v)
}

item.Version = event.EventVersion()
item.ID = event.AggregateID()
item.UpdatedAt = event.EventAt()

return nil
}

//CreateOrder command
type CreateOrder struct {
eventsource.CommandModel
}

//ShipOrder command
type ShipOrder struct {
eventsource.CommandModel
}

//Apply implements the CommandHandler interface
func (item *Order) Apply(ctx context.Context, command eventsource.Command) ([]eventsource.Event, error) {
switch v := command.(type) {
case *CreateOrder:
orderCreated := &OrderCreated{
Model: eventsource.Model{ID: command.AggregateID(), Version: item.Version + 1, At: time.Now()},
}
return []eventsource.Event{orderCreated}, nil

case *ShipOrder:
if item.State != "created" {
return nil, fmt.Errorf("order, %v, has already shipped", command.AggregateID())
}
orderShipped := &OrderShipped{
Model: eventsource.Model{ID: command.AggregateID(), Version: item.Version + 1, At: time.Now()},
}
return []eventsource.Event{orderShipped}, nil

default:
return nil, fmt.Errorf("unhandled command, %v", v)
}
}

func check(err error) {
if err != nil {
log.Fatalln(err)
}
}

func main() {
store, err := boltdbstore.New("orders")
check(err)

repo := eventsource.New(&Order{},
eventsource.WithStore(store),
eventsource.WithSerializer(eventsource.NewJSONSerializer(
OrderCreated{},
OrderShipped{},
)),
)

id := strconv.FormatInt(time.Now().UnixNano(), 36)
ctx := context.Background()

_, err = repo.Apply(ctx, &CreateOrder{
CommandModel: eventsource.CommandModel{ID: id},
})
check(err)

_, err = repo.Apply(ctx, &ShipOrder{
CommandModel: eventsource.CommandModel{ID: id},
})
check(err)

aggregate, err := repo.Load(ctx, id)
check(err)

found := aggregate.(*Order)
fmt.Printf("Order %v [version %v] %v %v\n", found.ID, found.Version, found.State, found.UpdatedAt.Format(time.RFC822))
}
4 changes: 2 additions & 2 deletions _examples/dynamo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"log"
"time"

"github.com/altairsix/eventsource"
"github.com/altairsix/eventsource/dynamodbstore"
"github.com/vancelongwill/eventsource"
"github.com/vancelongwill/eventsource/dynamodbstore"
)

// UserCreated defines a user creation event
Expand Down
6 changes: 6 additions & 0 deletions _examples/fullproto/compiles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main_test

import "testing"

func TestCompiles(t *testing.T) {
}
136 changes: 136 additions & 0 deletions _examples/fullproto/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"context"
"fmt"
"log"
"strconv"
"time"

"github.com/vancelongwill/eventsource"
"github.com/vancelongwill/eventsource/_examples/fullproto/pb"
"github.com/vancelongwill/eventsource/boltdbstore"
"google.golang.org/protobuf/types/known/timestamppb"
)

//Order is an example of state generated from left fold of events
type Order struct {
ID string
Version int
CreatedAt time.Time
UpdatedAt time.Time
State string
}

//OrderCreated event used a marker of order created
type OrderCreated struct {
*pb.OrderCreated
}

func (o OrderCreated) AggregateID() string { return o.OrderId }
func (o OrderCreated) EventVersion() int { return int(o.Version) }
func (o OrderCreated) EventAt() time.Time { return o.At.AsTime() }

// OrderShipped event used a marker of order shipped
type OrderShipped struct {
*pb.OrderShipped
}

func (o OrderShipped) AggregateID() string { return o.OrderId }
func (o OrderShipped) EventVersion() int { return int(o.Version) }
func (o OrderShipped) EventAt() time.Time { return o.At.AsTime() }

//On implements Aggregate interface
func (item *Order) On(event eventsource.Event) error {
switch v := event.(type) {
case *OrderCreated:
item.State = "created"

case *OrderShipped:
item.State = "shipped"

default:
return fmt.Errorf("unable to handle event, %v", v)
}

item.Version = event.EventVersion()
item.ID = event.AggregateID()
item.UpdatedAt = event.EventAt()

return nil
}

//CreateOrder command
type CreateOrder struct {
eventsource.CommandModel
}

//ShipOrder command
type ShipOrder struct {
eventsource.CommandModel
}

//Apply implements the CommandHandler interface
func (item *Order) Apply(ctx context.Context, command eventsource.Command) ([]eventsource.Event, error) {
switch v := command.(type) {
case *CreateOrder:
orderCreated := &OrderCreated{
&pb.OrderCreated{
OrderId: command.AggregateID(), Version: int32(item.Version + 1), At: timestamppb.New(time.Now()),
},
}
return []eventsource.Event{orderCreated}, nil

case *ShipOrder:
if item.State != "created" {
return nil, fmt.Errorf("order, %v, has already shipped", command.AggregateID())
}
orderShipped := &OrderShipped{
&pb.OrderShipped{
OrderId: command.AggregateID(), Version: int32(item.Version + 1), At: timestamppb.New(time.Now()),
},
}
return []eventsource.Event{orderShipped}, nil

default:
return nil, fmt.Errorf("unhandled command, %v", v)
}
}

func check(err error) {
if err != nil {
log.Fatalln(err)
}
}

func main() {
store, err := boltdbstore.New("orders")
check(err)

repo := eventsource.New(&Order{},
eventsource.WithStore(store),
eventsource.WithSerializer(eventsource.NewProtoSerializer(
OrderCreated{},
OrderShipped{},
)),
)

id := strconv.FormatInt(time.Now().UnixNano(), 36)
ctx := context.Background()

_, err = repo.Apply(ctx, &CreateOrder{
CommandModel: eventsource.CommandModel{ID: id},
})
check(err)

_, err = repo.Apply(ctx, &ShipOrder{
CommandModel: eventsource.CommandModel{ID: id},
})
check(err)

aggregate, err := repo.Load(ctx, id)
check(err)

found := aggregate.(*Order)
fmt.Printf("Order %v [version %v] %v %v\n", found.ID, found.Version, found.State, found.UpdatedAt.Format(time.RFC822))
}
5 changes: 5 additions & 0 deletions _examples/fullproto/pb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: build

build:
protoc -I=. --go_out=. --go_opt=paths=source_relative \
./order.proto
Loading