@@ -120,6 +120,18 @@ func newEventStoreWithClient(client *mongo.Client, clientOwnership clientOwnersh
120
120
return nil , fmt .Errorf ("could not ensure events index: %w" , err )
121
121
}
122
122
123
+ if _ , err := s .snapshots .Indexes ().CreateOne (ctx , mongo.IndexModel {
124
+ Keys : bson.M {"aggregate_id" : 1 },
125
+ }); err != nil {
126
+ return nil , fmt .Errorf ("could not ensure snapshot aggregate_id index: %w" , err )
127
+ }
128
+
129
+ if _ , err := s .snapshots .Indexes ().CreateOne (ctx , mongo.IndexModel {
130
+ Keys : bson.M {"version" : 1 },
131
+ }); err != nil {
132
+ return nil , fmt .Errorf ("could not ensure snapshot version index: %w" , err )
133
+ }
134
+
123
135
// Make sure the $all stream exists.
124
136
if err := s .streams .FindOne (ctx , bson.M {
125
137
"_id" : "$all" ,
@@ -498,7 +510,7 @@ func (s *EventStore) loadFromCursor(ctx context.Context, id uuid.UUID, cursor *m
498
510
}
499
511
500
512
func (s * EventStore ) LoadSnapshot (ctx context.Context , id uuid.UUID ) (* eh.Snapshot , error ) {
501
- result := s .snapshots .FindOne (ctx , bson.M {"_id " : id })
513
+ result := s .snapshots .FindOne (ctx , bson.M {"aggregate_id " : id }, options . FindOne (). SetSort (bson. M { "version" : - 1 }) )
502
514
if err := result .Err (); err != nil {
503
515
if errors .Is (err , mongo .ErrNoDocuments ) {
504
516
return nil , nil
@@ -521,7 +533,7 @@ func (s *EventStore) LoadSnapshot(ctx context.Context, id uuid.UUID) (*eh.Snapsh
521
533
}
522
534
}
523
535
524
- if snapshot .State , err = eh .CreateSnapshotData (record .Id , record .AggregateType ); err != nil {
536
+ if snapshot .State , err = eh .CreateSnapshotData (record .AggregateID , record .AggregateType ); err != nil {
525
537
return nil , & eh.EventStoreError {
526
538
Err : fmt .Errorf ("could not decode snapshot: %w" , err ),
527
539
Op : eh .EventStoreOpLoadSnapshot ,
@@ -568,7 +580,7 @@ func (s *EventStore) SaveSnapshot(ctx context.Context, id uuid.UUID, snapshot eh
568
580
}
569
581
570
582
record := SnapshotRecord {
571
- Id : id ,
583
+ AggregateID : id ,
572
584
AggregateType : snapshot .AggregateType ,
573
585
Timestamp : time .Now (),
574
586
Version : snapshot .Version ,
@@ -586,14 +598,9 @@ func (s *EventStore) SaveSnapshot(ctx context.Context, id uuid.UUID, snapshot eh
586
598
}
587
599
}
588
600
589
- if _ , err := s .snapshots .UpdateOne (ctx ,
590
- bson.M {
591
- "_id" : id .String (),
592
- },
593
- bson.M {
594
- "$set" : record ,
595
- },
596
- options .Update ().SetUpsert (true ),
601
+ if _ , err := s .snapshots .InsertOne (ctx ,
602
+ record ,
603
+ options .InsertOne (),
597
604
); err != nil {
598
605
return & eh.EventStoreError {
599
606
Err : fmt .Errorf ("could not save snapshot: %w" , err ),
@@ -616,7 +623,7 @@ func (s *EventStore) Close() error {
616
623
}
617
624
618
625
type SnapshotRecord struct {
619
- Id uuid.UUID `bson:"_id "`
626
+ AggregateID uuid.UUID `bson:"aggregate_id "`
620
627
RawData []byte `bson:"data"`
621
628
Timestamp time.Time `bson:"timestamp"`
622
629
Version int `bson:"version"`
0 commit comments