aboutsummaryrefslogtreecommitdiff
path: root/models/persister.go
diff options
context:
space:
mode:
authorJulio Capote <jcapote@gmail.com>2023-01-06 19:16:35 +0000
committerJulio Capote <jcapote@gmail.com>2023-01-06 19:16:35 +0000
commitfa52883f424c95dcde16e9056631041af4c479c7 (patch)
treef7a4c71deac75348af65c836b1c88afb36df43dd /models/persister.go
parentca12c0d26c987e67deade02bdf645fda8af30016 (diff)
downloadcommunique-fa52883f424c95dcde16e9056631041af4c479c7.tar.gz
callback support for deduped save
Diffstat (limited to '')
-rw-r--r--models/persister.go13
1 files changed, 11 insertions, 2 deletions
diff --git a/models/persister.go b/models/persister.go
index 5f79e4e..f877488 100644
--- a/models/persister.go
+++ b/models/persister.go
@@ -21,7 +21,15 @@ func NewPersister(log *zap.SugaredLogger, db *badger.DB) *Persister {
return aso
}
-func (p *Persister) Store(model model) error {
+func (p *Persister) StoreWithCallback(m model, onSave func()) error {
+ return p.storeModel(m, onSave)
+}
+
+func (p *Persister) Store(m model) error {
+ return p.storeModel(m, func() {})
+}
+
+func (p *Persister) storeModel(model model, onSave func()) error {
log := p.log.With("model", model.Name()).With("DedupKey", model.DedupKey()).With("Key", model.Key())
log.Debug("Store()")
err := p.db.View(func(txn *badger.Txn) error {
@@ -35,8 +43,9 @@ func (p *Persister) Store(model model) error {
if updateErr != nil {
return updateErr
}
- log.Debug("saving")
+ log.Debug("saving and calling storeCallback")
updateErr = p.db.Update(model.SaveDedup) // stores the sha256
+ onSave()
}
return updateErr
})