/
update.go
100 lines (88 loc) · 2.75 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package executor
import (
"context"
"github.com/qianwj/typed/mongo/bson"
"github.com/qianwj/typed/mongo/model"
"github.com/qianwj/typed/mongo/model/filters"
"github.com/qianwj/typed/mongo/model/update"
"github.com/qianwj/typed/mongo/options"
rawbson "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
rawopts "go.mongodb.org/mongo-driver/mongo/options"
)
type UpdateExecutor[D bson.Doc[I], I bson.ID] struct {
coll *mongo.Collection
filter *filters.Filter
update *update.Update
multi bool
docId *I
opts *rawopts.UpdateOptions
}
func NewUpdateOneExecutor[D bson.Doc[I], I bson.ID](primary *mongo.Collection, filter *filters.Filter, update *update.Update) *UpdateExecutor[D, I] {
return &UpdateExecutor[D, I]{
coll: primary,
filter: filter,
update: update,
opts: rawopts.Update(),
}
}
func NewUpdateManyExecutor[D bson.Doc[I], I bson.ID](primary *mongo.Collection, filter *filters.Filter, update *update.Update) *UpdateExecutor[D, I] {
return &UpdateExecutor[D, I]{
coll: primary,
filter: filter,
update: update,
multi: true,
opts: rawopts.Update(),
}
}
func NewUpdateByIdExecutor[D bson.Doc[I], I bson.ID](primary *mongo.Collection, id I, update *update.Update) *UpdateExecutor[D, I] {
return &UpdateExecutor[D, I]{
coll: primary,
docId: &id,
update: update,
opts: rawopts.Update(),
}
}
func (u *UpdateExecutor[D, I]) ArrayFilters(af options.ArrayFilters) *UpdateExecutor[D, I] {
u.opts.SetArrayFilters(af.Raw())
return u
}
// BypassDocumentValidation sets the value for the BypassDocumentValidation field.
func (u *UpdateExecutor[D, I]) BypassDocumentValidation() *UpdateExecutor[D, I] {
u.opts.SetBypassDocumentValidation(true)
return u
}
// Collation sets the value for the Collation field.
func (u *UpdateExecutor[D, I]) Collation(c *options.Collation) *UpdateExecutor[D, I] {
u.opts.SetCollation((*rawopts.Collation)(c))
return u
}
// Hint sets the value for the Hint field.
func (u *UpdateExecutor[D, I]) Hint(index string) *UpdateExecutor[D, I] {
u.opts.SetHint(index)
return u
}
// Upsert sets the value for the Upsert field.
func (u *UpdateExecutor[D, I]) Upsert() *UpdateExecutor[D, I] {
u.opts.SetUpsert(true)
return u
}
// Let sets the value for the Let field.
func (u *UpdateExecutor[D, I]) Let(l rawbson.M) *UpdateExecutor[D, I] {
u.opts.SetLet(l)
return u
}
func (u *UpdateExecutor[D, I]) Execute(ctx context.Context) (*model.UpdateResult[I], error) {
var (
err error
res *mongo.UpdateResult
)
if u.docId != nil {
res, err = u.coll.UpdateByID(ctx, u.docId, u.update, u.opts)
} else if u.multi {
res, err = u.coll.UpdateMany(ctx, u.filter, u.update, u.opts)
} else {
res, err = u.coll.UpdateOne(ctx, u.filter, u.update, u.opts)
}
return model.FromUpdateResult[I](res), err
}