-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement watcher that gets trigged by db changes
- Loading branch information
1 parent
f15e6bb
commit ef0232a
Showing
5 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package gomongo | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
type namespace struct { | ||
Database string `bson:"db"` | ||
Collection string `bson:"coll"` | ||
} | ||
|
||
type updateDescription struct { | ||
RemovedFields bson.A `bson:"removedFields"` | ||
UpdatedFields bson.M `bson:"updatedFields"` | ||
} | ||
|
||
type event struct { | ||
NS namespace `bson:"ns"` | ||
ClusterTime time.Time `bson:"clusterTime"` | ||
FullDocument bson.M `bson:"fullDocument"` | ||
DocumentKey bson.M `bson:"documentKey"` | ||
UpdateDescription updateDescription `bson:"updateDescription"` | ||
OperationType string `bson:"operationType"` | ||
} | ||
|
||
func watch(ctx context.Context, mongoDatabase *mongo.Database, handleEvent func(ctx context.Context, e event) error, collectionNamesToWatch []string) error { | ||
cs, err := mongoDatabase.Watch(ctx, mongo.Pipeline{}, options.ChangeStream().SetFullDocument(options.UpdateLookup)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer cs.Close(ctx) | ||
for cs.Next(ctx) { | ||
var e event | ||
if err := cs.Decode(&e); err != nil { | ||
return err | ||
} | ||
|
||
if collectionBellongToWatch(e.NS.Collection, collectionNamesToWatch) { | ||
err := handleEvent(ctx, e) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func collectionBellongToWatch(collectionName string, collections []string) bool { | ||
return slices.Contains(collections, collectionName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package gomongo | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type History interface { | ||
All(ctx context.Context) ([]Document, error) | ||
Count(ctx context.Context) (int, error) | ||
First(ctx context.Context) (Document, error) | ||
Last(ctx context.Context) (Document, error) | ||
Where(ctx context.Context, filter any) ([]Document, error) | ||
|
||
Drop(ctx context.Context) error | ||
|
||
Name() string | ||
} | ||
|
||
type Document struct { | ||
CreatedAt time.Time | ||
CollectionName string | ||
ObjectID ID | ||
Modified map[string]any | ||
UpdatedFields map[string]UpdatedField | ||
Action string | ||
} | ||
|
||
type UpdatedField struct { | ||
Old any | ||
New any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package gomongo | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/r3labs/diff" | ||
) | ||
|
||
type Watcher interface { | ||
Watch(ctx context.Context, collections ...string) error | ||
} | ||
|
||
type watcher struct { | ||
database *Database | ||
historyCollection *collection[Document] | ||
} | ||
|
||
func NewWatcher(database *Database, historyCollectionName string) (Watcher, History, error) { | ||
if err := validateDatabase(database); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
w := watcher{ | ||
database: database, | ||
historyCollection: &collection[Document]{database.mongoDatabase.Collection(historyCollectionName)}, | ||
} | ||
|
||
return w, w.historyCollection, nil | ||
} | ||
|
||
func (w watcher) Watch(ctx context.Context, collectionNamesToWatch ...string) error { | ||
err := watch(ctx, w.database.mongoDatabase, w.handleEvents, collectionNamesToWatch) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w watcher) handleEvents(ctx context.Context, e event) error { | ||
id, ok := e.DocumentKey["_id"].(ID) | ||
if !ok { | ||
return fmt.Errorf("could not get id from event document") | ||
} | ||
|
||
last, err := lastInsertedByObjectID(ctx, w.historyCollection, id) | ||
if err != nil && err != ErrDocumentNotFound { | ||
return fmt.Errorf("failed to get last entry") | ||
} | ||
|
||
updatedFields, err := updatedFields(last.Modified, e.FullDocument) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
doc := Document{ | ||
CreatedAt: e.ClusterTime, | ||
CollectionName: e.NS.Collection, | ||
ObjectID: id, | ||
Modified: e.FullDocument, | ||
Action: e.OperationType, | ||
UpdatedFields: updatedFields, | ||
} | ||
|
||
_, err = w.historyCollection.Create(ctx, doc) | ||
if err != nil && !errors.Is(err, ErrDuplicateKey) { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func lastInsertedByObjectID[T any](ctx context.Context, c *collection[T], objectID ID) (T, error) { | ||
filter := map[string]any{"objectid": objectID} | ||
return c.LastInserted(ctx, filter) | ||
} | ||
|
||
func updatedFields(last any, history any) (map[string]UpdatedField, error) { | ||
changes, err := diff.Diff(last, history) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get diff between entries") | ||
} | ||
|
||
updatedFields := make(map[string]UpdatedField, len(changes)) | ||
for _, change := range changes { | ||
field := strings.Join(change.Path, ".") | ||
if field != "_id" { | ||
updatedFields[field] = UpdatedField{ | ||
Old: change.From, | ||
New: change.To, | ||
} | ||
} | ||
} | ||
|
||
return updatedFields, nil | ||
} |