Skip to content

Commit

Permalink
breaking: Changed the id of the event to be a string (so it can be se…
Browse files Browse the repository at this point in the history
…t in code and not rely on auto increment).

* Added code to event repository to add a ksuid if no id was set
  • Loading branch information
akeemphilbert committed Aug 16, 2024
1 parent 6891d6b commit 83deafe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 9 additions & 6 deletions rest/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"gorm.io/datatypes"
"gorm.io/gorm"
"net/http"
"time"
)

type Event struct {
gorm.Model
Type string `json:"type"`
Payload datatypes.JSONMap `json:"payload"`
Meta EventMeta `json:"meta" gorm:"embedded"`
Version int `json:"version"`
errors []error
ID string `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
Type string `json:"type"`
Payload datatypes.JSONMap `json:"payload"`
Meta EventMeta `json:"meta" gorm:"embedded"`
Version int `json:"version"`
errors []error
}

type EventMeta struct {
Expand Down
6 changes: 6 additions & 0 deletions rest/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go-v2/feature/rds/auth"
_ "github.com/jackc/pgx/v5"
"github.com/labstack/gommon/log"
"github.com/segmentio/ksuid"
"go.uber.org/fx"
"golang.org/x/net/context"
"gorm.io/driver/mysql"
Expand Down Expand Up @@ -324,6 +325,11 @@ func (e *GORMProjection) Persist(ctxt context.Context, logger Log, resources []R
var events []*Event
for _, resource := range resources {
if event, ok := resource.(*Event); ok {
if event.ID == "" {
event.ID = ksuid.New().String()
}
event.CreatedAt = time.Now()
event.UpdatedAt = time.Now()
events = append(events, event)
} else {
errs = append(errs, errors.New("resource is not an event"))
Expand Down

0 comments on commit 83deafe

Please sign in to comment.