Skip to content

Commit 22bb159

Browse files
author
lifeng.lif
committed
feat: finder support skip, limit, sort
1 parent f2dc64b commit 22bb159

File tree

5 files changed

+281
-33
lines changed

5 files changed

+281
-33
lines changed

bsonx/bsonx.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package bsonx
1717
import (
1818
"bytes"
1919

20+
"github.com/chenmingyong0423/go-mongox/v2/internal/pkg/utils"
21+
2022
"go.mongodb.org/mongo-driver/v2/bson"
2123
)
2224

@@ -89,3 +91,19 @@ func dToM(d bson.D) bson.M {
8991
}
9092
return m
9193
}
94+
95+
// StringSortToBsonD transform string sort to bson D
96+
// "-created_at" => bson.D{{"created_at", -1}}
97+
// []string{"age", "-created_at"} => bson.D{{"age", 1}, {"created_at", -1}}
98+
func StringSortToBsonD(sorts ...string) bson.D {
99+
var res bson.D
100+
for _, sort := range sorts {
101+
key, n := utils.SplitSortField(sort)
102+
if key == "" {
103+
continue
104+
}
105+
res = append(res, bson.E{Key: key, Value: n})
106+
}
107+
108+
return res
109+
}

bsonx/bsonx_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,30 @@ func TestA(t *testing.T) {
6262
}
6363

6464
}
65+
66+
func TestStringSortToBsonD(t *testing.T) {
67+
testCases := []struct {
68+
name string
69+
value any
70+
want bson.D
71+
}{
72+
{
73+
name: "one sort",
74+
value: []string{"-created_at"},
75+
want: bson.D{{"created_at", -1}},
76+
},
77+
{
78+
name: "two sort",
79+
value: []string{"age", "-created_at"},
80+
want: bson.D{{"age", 1}, {"created_at", -1}},
81+
},
82+
}
83+
84+
for _, tc := range testCases {
85+
t.Run(tc.name, func(t *testing.T) {
86+
w, _ := bson.Marshal(tc.want)
87+
v, _ := bson.Marshal(StringSortToBsonD(tc.value.([]string)...))
88+
assert.Equal(t, string(w), string(v))
89+
})
90+
}
91+
}

finder/finder.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package finder
1616

1717
import (
1818
"context"
19-
2019
"github.com/chenmingyong0423/go-mongox/v2/callback"
2120
"github.com/chenmingyong0423/go-mongox/v2/operation"
2221

@@ -45,6 +44,9 @@ type Finder[T any] struct {
4544
modelHook any
4645
beforeHooks []beforeHookFn
4746
afterHooks []afterHookFn[T]
47+
48+
skip, limit int64
49+
sort any
4850
}
4951

5052
func (f *Finder[T]) RegisterBeforeHooks(hooks ...beforeHookFn) *Finder[T] {
@@ -66,6 +68,21 @@ func (f *Finder[T]) Filter(filter any) *Finder[T] {
6668
return f
6769
}
6870

71+
func (f *Finder[T]) Limit(limit int64) *Finder[T] {
72+
f.limit = limit
73+
return f
74+
}
75+
76+
func (f *Finder[T]) Skip(skip int64) *Finder[T] {
77+
f.skip = skip
78+
return f
79+
}
80+
81+
func (f *Finder[T]) Sort(sort any) *Finder[T] {
82+
f.sort = sort
83+
return f
84+
}
85+
6986
func (f *Finder[T]) Updates(update any) *Finder[T] {
7087
f.updates = update
7188
return f
@@ -109,6 +126,13 @@ func (f *Finder[T]) postActionHandler(ctx context.Context, globalOpContext *oper
109126
}
110127

111128
func (f *Finder[T]) FindOne(ctx context.Context, opts ...options.Lister[options.FindOneOptions]) (*T, error) {
129+
if f.skip > 0 {
130+
opts = append(opts, options.FindOne().SetSkip(f.skip))
131+
}
132+
if f.sort != nil {
133+
opts = append(opts, options.FindOne().SetSort(f.sort))
134+
}
135+
112136
t := new(T)
113137

114138
globalOpContext := operation.NewOpContext(f.collection, operation.WithDoc(t), operation.WithFilter(f.filter), operation.WithMongoOptions(opts), operation.WithModelHook(f.modelHook))
@@ -131,6 +155,16 @@ func (f *Finder[T]) FindOne(ctx context.Context, opts ...options.Lister[options.
131155
}
132156

133157
func (f *Finder[T]) Find(ctx context.Context, opts ...options.Lister[options.FindOptions]) ([]*T, error) {
158+
if f.skip > 0 {
159+
opts = append(opts, options.Find().SetSkip(f.skip))
160+
}
161+
if f.limit > 0 {
162+
opts = append(opts, options.Find().SetLimit(f.limit))
163+
}
164+
if f.sort != nil {
165+
opts = append(opts, options.Find().SetSort(f.sort))
166+
}
167+
134168
t := make([]*T, 0)
135169

136170
opContext := operation.NewOpContext(f.collection, operation.WithFilter(f.filter), operation.WithMongoOptions(opts), operation.WithModelHook(f.modelHook))

0 commit comments

Comments
 (0)