Move audit subscribe to client, Fix tests

This commit is contained in:
George Suntres
2026-04-24 15:51:08 -04:00
parent d673df0ca1
commit 66ba2b8874
8 changed files with 128 additions and 30 deletions

10
main.go
View File

@@ -94,6 +94,8 @@ type MongoClient struct {
WithAudit bool
// OnAudit callback called when audit is enabled.
OnAudit *OnAudit
// EventBus
EventBus *events.TypedEventBus
// IgnoreAudit a list of entities to ignore
IgnoreAudit []string
}
@@ -364,11 +366,11 @@ func Start(props *MongoStartProps) error {
client.WithAudit = props.WithAudit
client.OnAudit = props.OnAudit
client.EventBus = events.NewTypedEventBus()
if client.WithAudit {
var onAudit OnAudit = func(audit *AuditResult) error {
return events.Publish(audit)
return events.Publish(client.EventBus, audit)
}
client.OnAudit = &onAudit
}
@@ -402,6 +404,10 @@ func Start(props *MongoStartProps) error {
return nil
}
func (c *MongoClient) Subscribe(h events.Handler[AuditResult]) (cancel func()) {
return events.Subscribe(c.EventBus, h)
}
func GetClient() *mongo.Client {
return client.Client
}