mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-08 00:02:49 +01:00
42
bus/databaseupdate.go
Normal file
42
bus/databaseupdate.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package bus
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/arangodb/go-driver"
|
||||
emitter "github.com/emitter-io/go/v2"
|
||||
)
|
||||
|
||||
const channelDatabaseUpdate = "databaseupdate"
|
||||
|
||||
type DatabaseUpdateType string
|
||||
|
||||
const (
|
||||
DatabaseEntryRead DatabaseUpdateType = "read"
|
||||
DatabaseEntryCreated DatabaseUpdateType = "created"
|
||||
DatabaseEntryUpdated DatabaseUpdateType = "updated"
|
||||
)
|
||||
|
||||
type DatabaseUpdateMsg struct {
|
||||
IDs []driver.DocumentID `json:"ids"`
|
||||
Type DatabaseUpdateType `json:"type"`
|
||||
}
|
||||
|
||||
func (b *Bus) PublishDatabaseUpdate(ids []driver.DocumentID, databaseUpdateType DatabaseUpdateType) error {
|
||||
return b.jsonPublish(&DatabaseUpdateMsg{
|
||||
IDs: ids,
|
||||
Type: databaseUpdateType,
|
||||
}, channelDatabaseUpdate, b.config.databaseUpdateBusKey)
|
||||
}
|
||||
|
||||
func (b *Bus) SubscribeDatabaseUpdate(f func(msg *DatabaseUpdateMsg)) error {
|
||||
return b.safeSubscribe(b.config.databaseUpdateBusKey, channelDatabaseUpdate, func(c *emitter.Client, m emitter.Message) {
|
||||
var msg DatabaseUpdateMsg
|
||||
if err := json.Unmarshal(m.Payload(), &msg); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
go f(&msg)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user