Skip to content

Commit

Permalink
update custom and setOnInsert
Browse files Browse the repository at this point in the history
  • Loading branch information
phuong committed Oct 24, 2023
1 parent 0b257f9 commit af5fc42
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/mopertest/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (Hero) CollName() string {

// %s:%s/?w=majority&retryWrites=false
func init() {
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
err := mocom.Connect(ctx, "mongodb://localhost:27017/?w=majority&retryWrites=false", "defaultdb")
if err != nil {
log.Fatal("cannot connect mongo", err)
Expand Down
2 changes: 1 addition & 1 deletion mocom/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Find finds documents from from model's collection
// model should be implement `CollName() string`
func FindT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOptions) (res []T, err error) {
func Find[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOptions) (res []T, err error) {
var t T
cur, err := CollRead(t.CollName()).Find(ctx, filter, opts...)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions moper/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const (
_set = "$set"
_push = "$push"
_unset = "$unset"

_setOnInsert = "$setOnInsert"
)

// SET
Expand All @@ -22,6 +24,14 @@ func (d D) SetD(pairs D) D {
return append(d, bson.E{Key: _set, Value: pairs})
}

func (d D) SetOnInsert(key string, value interface{}) D {
return append(d, bson.E{Key: _setOnInsert, Value: bson.D{{Key: key, Value: value}}})
}

func (d D) SetManyOnInsert(pairs ...P) D {
return append(d, bson.E{Key: _setOnInsert, Value: toPair(pairs)})
}

// UNSET
func (d D) Unset(keys ...string) D {
res := D{}
Expand Down Expand Up @@ -73,3 +83,8 @@ func toPair(pairs []P) bson.D {

return updated
}

// CUSTOM
func (d D) CustomMany(cmd string, pairs ...P) D {
return append(d, bson.E{Key: cmd, Value: toPair(pairs)})
}

0 comments on commit af5fc42

Please sign in to comment.