// NewJSONFileProjection makes use of flush after projection in order to
// periodically write accounts to a json file on disk
NewJSONFileProjection makes use of flush after projection in order to
periodically write accounts to a json file on disk
// NewJSONFileProjection makes use of flush after projection in order to // periodically write accounts to a json file on disk
func NewJSONFileProjection(fname string) eventstore.Projection {
	var accounts []string
	return eventstore.FlushAfter(
		func(data eventstore.EventData) error {
			switch data.Event.(type) {
			case account.NewAccountOpenned:
				evt := data.Event.(account.NewAccountOpenned)
				accounts = append(accounts, fmt.Sprintf("Account: #%s | Holder: <%s>", evt.ID, evt.Holder))
			default:
				fmt.Println("not interested in this event")
			}
			return nil
		},
		func() error {
			if len(accounts) == 0 {
				return nil
			}
			data, err := json.Marshal(accounts)
			if err != nil {
				return err
			}
			err = os.WriteFile(fname, data, os.ModeAppend|os.ModePerm)
			if err != nil {
				return err
			}
			return nil
		},
		3*time.Second,
	)
}
func NewJSONFileProjection ( fname string ) eventstore . Projection { var accounts [ ] string return eventstore . FlushAfter ( func ( data eventstore . EventData ) error { switch data . Event . ( type ) { case account . NewAccountOpenned : evt := data . Event . ( account . NewAccountOpenned ) accounts = append ( accounts , fmt . Sprintf ( " " , evt . ID , evt . Holder ) ) default : fmt . Println ( " " ) } return nil } , func ( ) error { if len ( accounts ) == 0 { return nil } data , err := json . Marshal ( accounts ) if err != nil { return err } err = os . WriteFile ( fname , data , os . ModeAppend | os . ModePerm ) if err != nil { return err } return nil } , 3 * time . Second , ) }
NewJSONFileProjection makes use of flush after projection in order to
periodically write accounts to a json file on disk
NewJSONFileProjection makes use of flush after projection in order to periodically write accounts to a json file on disk