Add filters support for a saving

v2
Nikita Tokarchuk 4 years ago
parent 848703d56d
commit 28be1f46b9
  1. 6
      mongox/database/saveone.go
  2. 2
      mongox/mongox.go

@ -12,13 +12,15 @@ import (
)
// SaveOne saves a single source document to the database
func (d *Database) SaveOne(source interface{}) (err error) {
func (d *Database) SaveOne(source interface{}, filters ...interface{}) (err error) {
collection := d.GetCollectionOf(source)
opts := options.FindOneAndReplace()
id := base.GetID(source)
protected := base.GetProtection(source)
composed := query.Compose(bson.M{"_id": id})
composed := query.Compose(filters...)
composed.And(bson.M{"_id": id})
opts.SetUpsert(true)
opts.SetReturnDocument(options.After)

@ -27,7 +27,7 @@ type Database interface {
LoadArray(target interface{}, filters ...interface{}) (err error)
LoadOne(target interface{}, filters ...interface{}) (err error)
LoadStream(target interface{}, filters ...interface{}) (loader StreamLoader, err error)
SaveOne(source interface{}) (err error)
SaveOne(source interface{}, filters ...interface{}) (err error)
IndexEnsure(cfg interface{}, document interface{}) (err error)
}

Loading…
Cancel
Save