/
findone_and_update.go
121 lines (105 loc) · 3.69 KB
/
findone_and_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package executor
import (
"context"
"github.com/qianwj/typed/mongo/bson"
"github.com/qianwj/typed/mongo/model/filters"
"github.com/qianwj/typed/mongo/model/projections"
"github.com/qianwj/typed/mongo/model/sorts"
"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"
"time"
)
type FindOneAndUpdateExecutor[D bson.Doc[I], I bson.ID] struct {
coll *mongo.Collection
filter *filters.Filter
update *update.Update
opts *rawopts.FindOneAndUpdateOptions
}
func NewFindOneAndUpdateExecutor[D bson.Doc[I], I bson.ID](
primary *mongo.Collection,
filter *filters.Filter,
update *update.Update,
) *FindOneAndUpdateExecutor[D, I] {
return &FindOneAndUpdateExecutor[D, I]{
coll: primary,
filter: filter,
update: update,
opts: rawopts.FindOneAndUpdate(),
}
}
// BypassDocumentValidation sets the value for the BypassDocumentValidation field.
func (f *FindOneAndUpdateExecutor[D, I]) BypassDocumentValidation() *FindOneAndUpdateExecutor[D, I] {
f.opts.SetBypassDocumentValidation(true)
return f
}
// ArrayFilters sets the value for the ArrayFilters field.
func (f *FindOneAndUpdateExecutor[D, I]) ArrayFilters(af options.ArrayFilters) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetArrayFilters(af.Raw())
return f
}
// Collation sets the value for the Collation field.
func (f *FindOneAndUpdateExecutor[D, I]) Collation(collation *options.Collation) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetCollation((*rawopts.Collation)(collation))
return f
}
// Comment sets the value for the Comment field.
func (f *FindOneAndUpdateExecutor[D, I]) Comment(comment interface{}) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetComment(comment)
return f
}
// MaxTime sets the value for the MaxTime field.
//
// NOTE(benjirewis): MaxTime will be deprecated in a future release. The more general Timeout
// option may be used in its place to control the amount of time that a single operation can
// run before returning an error. MaxTime is ignored if Timeout is set on the client.
func (f *FindOneAndUpdateExecutor[D, I]) MaxTime(d time.Duration) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetMaxTime(d)
return f
}
// Projection sets the value for the Projection field.
func (f *FindOneAndUpdateExecutor[D, I]) Projection(projection *projections.Options) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetProjection(projection)
return f
}
func (f *FindOneAndUpdateExecutor[D, I]) ReturnBefore() *FindOneAndUpdateExecutor[D, I] {
f.opts.SetReturnDocument(rawopts.Before)
return f
}
func (f *FindOneAndUpdateExecutor[D, I]) ReturnAfter() *FindOneAndUpdateExecutor[D, I] {
f.opts.SetReturnDocument(rawopts.After)
return f
}
// Sort sets the value for the Sort field.
func (f *FindOneAndUpdateExecutor[D, I]) Sort(sort *sorts.Options) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetSort(sort)
return f
}
// Upsert sets the value for the Upsert field.
func (f *FindOneAndUpdateExecutor[D, I]) Upsert() *FindOneAndUpdateExecutor[D, I] {
f.opts.SetUpsert(true)
return f
}
// Hint sets the value for the Hint field.
func (f *FindOneAndUpdateExecutor[D, I]) Hint(index string) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetHint(index)
return f
}
// Let sets the value for the Let field.
func (f *FindOneAndUpdateExecutor[D, I]) Let(l rawbson.M) *FindOneAndUpdateExecutor[D, I] {
f.opts.SetLet(l)
return f
}
func (f *FindOneAndUpdateExecutor[D, I]) Execute(ctx context.Context) (D, error) {
res := f.coll.FindOneAndUpdate(ctx, f.filter, f.update, f.opts)
var data D
if res.Err() != nil {
return data, res.Err()
}
if err := res.Decode(&data); err != nil {
return data, err
}
return data, nil
}