feat: inprogress
This commit is contained in:
31
pkg/es/es.go
31
pkg/es/es.go
@@ -224,6 +224,10 @@ type PA[T any] interface {
|
||||
event.Aggregate
|
||||
*T
|
||||
}
|
||||
type PE[T any] interface {
|
||||
event.Event
|
||||
*T
|
||||
}
|
||||
|
||||
// Create uses fn to create a new aggregate and store in db.
|
||||
func Create[A any, T PA[A]](ctx context.Context, es *EventStore, streamID string, fn func(context.Context, T) error) (agg T, err error) {
|
||||
@@ -281,3 +285,30 @@ func Update[A any, T PA[A]](ctx context.Context, es *EventStore, streamID string
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Update uses fn to update an exsisting aggregate and store in db.
|
||||
func Upsert[A any, T PA[A]](ctx context.Context, es *EventStore, streamID string, fn func(context.Context, T) error) (agg T, err error) {
|
||||
ctx, span := logz.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
agg = new(A)
|
||||
agg.SetStreamID(streamID)
|
||||
|
||||
if err = es.Load(ctx, agg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = fn(ctx, agg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = event.ShouldExist(agg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = es.Save(ctx, agg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -4,4 +4,20 @@ type Meta @goModel(model: "github.com/sour-is/ev/pkg/es/event.Meta") {
|
||||
streamID: String!
|
||||
created: Time!
|
||||
position: Int!
|
||||
}
|
||||
|
||||
extend type Query {
|
||||
events(streamID: String! paging: PageInput): Connection!
|
||||
}
|
||||
extend type Subscription {
|
||||
"""after == 0 start from begining, after == -1 start from end"""
|
||||
eventAdded(streamID: String! after: Int! = -1): Event
|
||||
}
|
||||
|
||||
type Event implements Edge {
|
||||
id: ID!
|
||||
|
||||
eventID: String!
|
||||
values: Map!
|
||||
eventMeta: Meta!
|
||||
}
|
||||
@@ -29,16 +29,16 @@ func Append(a Aggregate, lis ...Event) {
|
||||
|
||||
// NotExists returns error if there are no events present.
|
||||
func NotExists(a Aggregate) error {
|
||||
if a.StreamVersion() != 0 {
|
||||
return fmt.Errorf("%w, got version == %d", ErrShouldNotExist, a.StreamVersion())
|
||||
if a.Version() != 0 {
|
||||
return fmt.Errorf("%w, got version == %d", ErrShouldNotExist, a.Version())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ShouldExists returns error if there are no events present.
|
||||
func ShouldExist(a Aggregate) error {
|
||||
if a.StreamVersion() == 0 {
|
||||
return fmt.Errorf("%w, got version == %d", ErrShouldExist, a.StreamVersion())
|
||||
if a.Version() == 0 {
|
||||
return fmt.Errorf("%w, got version == %d", ErrShouldExist, a.Version())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -246,3 +246,12 @@ func embedJSON(s string) json.RawMessage {
|
||||
}
|
||||
return []byte(fmt.Sprintf(`"%s"`, strings.Replace(s, `"`, `\"`, -1)))
|
||||
}
|
||||
|
||||
func values(e Event) map[string]any {
|
||||
m := make(map[string]any)
|
||||
v := reflect.ValueOf(e)
|
||||
for _, idx := range reflect.VisibleFields(v) {
|
||||
field := v.FieldByIndex(idx.Index)
|
||||
m[field.]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user