// AppendStream will encode provided event slice and try to append them to
// an indicated stream. If the stream does not exist it will be created.
// If the stream already exists an optimistic concurrency check will be performed
// using a compound key (stream-expectedVer).
// expectedVer should be InitialStreamVersion for new streams and the latest
// stream version for existing streams, otherwise a concurrency error
// will be raised
AppendStream will encode provided event slice and try to append them to
an indicated stream. If the stream does not exist it will be created.
If the stream already exists an optimistic concurrency check will be performed
using a compound key (stream-expectedVer).
expectedVer should be InitialStreamVersion for new streams and the latest
stream version for existing streams, otherwise a concurrency error
will be raised
// AppendStream will encode provided event slice and try to append them to // an indicated stream . If the stream does not exist it will be created . // If the stream already exists an optimistic concurrency check will be performed // using a compound key ( stream - expectedVer ) . // expectedVer should be InitialStreamVersion for new streams and the latest // stream version for existing streams otherwise a concurrency error // will be raised
func (es *EventStore) AppendStream(
	ctx context.Context,
	stream string,
	expectedVer int,
	evts []interface{},
	opts ...AppendStreamOpt) error {
	if len(stream) == 0 {
		return fmt.Errorf("stream name must be provided")
	}
	if expectedVer < 0 {
		return fmt.Errorf("expected version cannot be less than 0")
	}
	if len(evts) < 1 {
		return fmt.Errorf("please provide at least one event to append")
	}
	cfg := AppendStreamConfig{}
	for _, opt := range opts {
		cfg = opt(cfg)
	}
	events := make([]gormEvent, len(evts))
	m, err := json.Marshal(cfg.meta)
	if err != nil {
		return err
	}
	for i, evt := range evts {
		encoded, err := es.enc.Encode(evt)
		if err != nil {
			return err
		}
		expectedVer++
		events[i] = gormEvent{
			Stream:        stream,
			StreamVersion: expectedVer,
			Data:          encoded.Data,
			Meta:          string(m),
			Type:          encoded.Type,
		}
	}
	tx := es.db.Create(&events)
	if e, ok := tx.Error.(sqlite3.Error); ok && e.Code == 19 {
		return ErrConcurrencyCheckFailed
	}
	return tx.Error
}
func ( es * EventStore ) AppendStream ( ctx context . Context , stream string , expectedVer int , evts [ ] interface { } , opts ... AppendStreamOpt ) error { if len ( stream ) == 0 { return fmt . Errorf ( " " ) } if expectedVer < 0 { return fmt . Errorf ( " " ) } if len ( evts ) < 1 { return fmt . Errorf ( " " ) } cfg := AppendStreamConfig { } for _ , opt := range opts { cfg = opt ( cfg ) } events := make ( [ ] gormEvent , len ( evts ) ) m , err := json . Marshal ( cfg . meta ) if err != nil { return err } for i , evt := range evts { encoded , err := es . enc . Encode ( evt ) if err != nil { return err } expectedVer ++ events [ i ] = gormEvent { Stream : stream , StreamVersion : expectedVer , Data : encoded . Data , Meta : string ( m ) , Type : encoded . Type , } } tx := es . db . Create ( & events ) if e , ok := tx . Error . ( sqlite3 . Error ) ; ok && e . Code == 19 { return ErrConcurrencyCheckFailed } return tx . Error }
AppendStream will encode provided event slice and try to append them to
an indicated stream.
AppendStream will encode provided event slice and try to append them to an indicated stream .