Skip to content

Commit 4a9fe1a

Browse files
- add model hook
- execute callback in [creator, deleter, updater and finder]
1 parent 3ad36f2 commit 4a9fe1a

File tree

10 files changed

+561
-81
lines changed

10 files changed

+561
-81
lines changed

callback/callback.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package callback
1717
import (
1818
"context"
1919

20+
"github.com/chenmingyong0423/go-mongox/hook/model"
21+
2022
"github.com/chenmingyong0423/go-mongox/hook/field"
2123
"github.com/chenmingyong0423/go-mongox/operation"
2224
)
@@ -34,6 +36,12 @@ func initializeCallbacks() *Callback {
3436
return field.Execute(ctx, opCtx, operation.OpTypeBeforeInsert, opts...)
3537
},
3638
},
39+
{
40+
name: "mongox:model",
41+
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
42+
return model.Execute(ctx, opCtx, operation.OpTypeBeforeInsert, opts...)
43+
},
44+
},
3745
},
3846
afterInsert: make([]callbackHandler, 0),
3947
beforeUpdate: []callbackHandler{
@@ -54,10 +62,23 @@ func initializeCallbacks() *Callback {
5462
return field.Execute(ctx, opCtx, operation.OpTypeBeforeUpsert, opts...)
5563
},
5664
},
65+
{
66+
name: "mongox:model",
67+
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
68+
return model.Execute(ctx, opCtx, operation.OpTypeBeforeUpsert, opts...)
69+
},
70+
},
5771
},
5872
afterUpsert: make([]callbackHandler, 0),
5973
beforeFind: make([]callbackHandler, 0),
60-
afterFind: make([]callbackHandler, 0),
74+
afterFind: []callbackHandler{
75+
{
76+
name: "mongox:model",
77+
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
78+
return model.Execute(ctx, opCtx, operation.OpTypeAfterFind, opts...)
79+
},
80+
},
81+
},
6182
}
6283
}
6384

creator/creator.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options
6767
return nil, err
6868
}
6969

70-
if err != nil {
71-
return nil, err
72-
}
7370
result, err := c.collection.InsertMany(ctx, utils.ToAnySlice(docs...), opts...)
7471
if err != nil {
7572
return nil, err

creator/creator_e2e_test.go

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,44 @@ package creator
1818

1919
import (
2020
"context"
21+
"errors"
2122
"testing"
23+
"time"
24+
25+
"github.com/chenmingyong0423/go-mongox/callback"
26+
"github.com/chenmingyong0423/go-mongox/operation"
27+
28+
"go.mongodb.org/mongo-driver/bson/primitive"
2229

2330
"github.com/stretchr/testify/require"
2431

2532
"github.com/chenmingyong0423/go-mongox/builder/query"
2633

27-
"github.com/chenmingyong0423/go-mongox/types"
28-
2934
"github.com/stretchr/testify/assert"
3035
"go.mongodb.org/mongo-driver/mongo"
3136
"go.mongodb.org/mongo-driver/mongo/options"
3237
"go.mongodb.org/mongo-driver/mongo/readpref"
3338
)
3439

40+
type User struct {
41+
ID primitive.ObjectID `bson:"_id,omitempty"`
42+
Name string `bson:"name"`
43+
Age int64
44+
UnknownField string `bson:"-"`
45+
CreatedAt time.Time `bson:"created_at"`
46+
UpdatedAt time.Time `bson:"updated_at"`
47+
}
48+
49+
func (u *User) DefaultCreatedAt() {
50+
if u.CreatedAt.IsZero() {
51+
u.CreatedAt = time.Now().Local()
52+
}
53+
}
54+
55+
func (u *User) DefaultUpdatedAt() {
56+
u.UpdatedAt = time.Now().Local()
57+
}
58+
3559
func newCollection(t *testing.T) *mongo.Collection {
3660
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(options.Credential{
3761
Username: "test",
@@ -47,16 +71,15 @@ func newCollection(t *testing.T) *mongo.Collection {
4771

4872
func TestCreator_e2e_One(t *testing.T) {
4973
collection := newCollection(t)
50-
creator := NewCreator[types.TestUser](collection)
74+
creator := NewCreator[User](collection)
5175
testCases := []struct {
5276
name string
5377
before func(ctx context.Context, t *testing.T)
5478
after func(ctx context.Context, t *testing.T)
5579
opts []*options.InsertOneOptions
5680
ctx context.Context
57-
doc *types.TestUser
81+
doc *User
5882

59-
wantId string
6083
wantError assert.ErrorAssertionFunc
6184
}{
6285
{
@@ -82,7 +105,7 @@ func TestCreator_e2e_One(t *testing.T) {
82105
opts: []*options.InsertOneOptions{
83106
options.InsertOne().SetComment("test"),
84107
},
85-
doc: &types.TestUser{
108+
doc: &User{
86109
Name: "chenmingyong",
87110
Age: 24,
88111
},
@@ -109,18 +132,46 @@ func TestCreator_e2e_One(t *testing.T) {
109132
}
110133
})
111134
}
135+
t.Run("before hook error", func(t *testing.T) {
136+
ctx := context.Background()
137+
doc := &User{}
138+
callback.GetCallback().Register(operation.OpTypeBeforeInsert, "before hook error", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
139+
return errors.New("before hook error")
140+
})
141+
insertOneResult, err := creator.InsertOne(ctx, doc)
142+
require.Equal(t, err, errors.New("before hook error"))
143+
require.Nil(t, insertOneResult)
144+
callback.GetCallback().Remove(operation.OpTypeBeforeInsert, "before hook error")
145+
})
146+
t.Run("after hook error", func(t *testing.T) {
147+
ctx := context.Background()
148+
doc := &User{
149+
Name: "chenmingyong",
150+
Age: 24,
151+
}
152+
callback.GetCallback().Register(operation.OpTypeAfterInsert, "after hook error", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
153+
return errors.New("after hook error")
154+
})
155+
insertOneResult, err := creator.InsertOne(ctx, doc)
156+
require.Equal(t, err, errors.New("after hook error"))
157+
require.Nil(t, insertOneResult)
158+
callback.GetCallback().Remove(operation.OpTypeAfterInsert, "after hook error")
159+
deleteResult, err := collection.DeleteOne(ctx, query.Eq("name", "chenmingyong"))
160+
require.NoError(t, err)
161+
require.Equal(t, int64(1), deleteResult.DeletedCount)
162+
})
112163
}
113164

114165
func TestCreator_e2e_Many(t *testing.T) {
115166
collection := newCollection(t)
116-
creator := NewCreator[types.TestUser](collection)
167+
creator := NewCreator[User](collection)
117168
testCases := []struct {
118169
name string
119170
before func(ctx context.Context, t *testing.T)
120171
after func(ctx context.Context, t *testing.T)
121172

122173
ctx context.Context
123-
docs []*types.TestUser
174+
docs []*User
124175
opts []*options.InsertManyOptions
125176

126177
wantIdsLength int
@@ -149,7 +200,7 @@ func TestCreator_e2e_Many(t *testing.T) {
149200
options.InsertMany().SetComment("test"),
150201
},
151202
ctx: context.Background(),
152-
docs: []*types.TestUser{
203+
docs: []*User{
153204
{
154205
Name: "chenmingyong",
155206
Age: 24,
@@ -180,4 +231,32 @@ func TestCreator_e2e_Many(t *testing.T) {
180231
}
181232
})
182233
}
234+
t.Run("before hook error", func(t *testing.T) {
235+
ctx := context.Background()
236+
docs := []*User{{}}
237+
callback.GetCallback().Register(operation.OpTypeBeforeInsert, "before hook error", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
238+
return errors.New("before hook error")
239+
})
240+
insertOneResult, err := creator.InsertMany(ctx, docs)
241+
require.Equal(t, err, errors.New("before hook error"))
242+
require.Nil(t, insertOneResult)
243+
callback.GetCallback().Remove(operation.OpTypeBeforeInsert, "before hook error")
244+
})
245+
t.Run("after hook error", func(t *testing.T) {
246+
ctx := context.Background()
247+
docs := []*User{{
248+
Name: "chenmingyong",
249+
Age: 24,
250+
}}
251+
callback.GetCallback().Register(operation.OpTypeAfterInsert, "after hook error", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
252+
return errors.New("after hook error")
253+
})
254+
insertOneResult, err := creator.InsertMany(ctx, docs)
255+
require.Equal(t, err, errors.New("after hook error"))
256+
require.Nil(t, insertOneResult)
257+
callback.GetCallback().Remove(operation.OpTypeAfterInsert, "after hook error")
258+
deleteResult, err := collection.DeleteOne(ctx, query.Eq("name", "chenmingyong"))
259+
require.NoError(t, err)
260+
require.Equal(t, int64(1), deleteResult.DeletedCount)
261+
})
183262
}

deleter/deleter.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ package deleter
1717
import (
1818
"context"
1919

20+
"github.com/chenmingyong0423/go-mongox/callback"
21+
"github.com/chenmingyong0423/go-mongox/operation"
22+
2023
"go.mongodb.org/mongo-driver/mongo"
2124
"go.mongodb.org/mongo-driver/mongo/options"
2225
)
@@ -43,9 +46,41 @@ func (d *Deleter[T]) Filter(filter any) *Deleter[T] {
4346
}
4447

4548
func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
46-
return d.collection.DeleteOne(ctx, d.filter, opts...)
49+
opContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter))
50+
err := callback.GetCallback().Execute(ctx, opContext, operation.OpTypeBeforeDelete)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
result, err := d.collection.DeleteOne(ctx, d.filter, opts...)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
err = callback.GetCallback().Execute(ctx, opContext, operation.OpTypeAfterDelete)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
return result, nil
4766
}
4867

4968
func (d *Deleter[T]) DeleteMany(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
50-
return d.collection.DeleteMany(ctx, d.filter, opts...)
69+
opContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter))
70+
err := callback.GetCallback().Execute(ctx, opContext, operation.OpTypeBeforeDelete)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
result, err := d.collection.DeleteMany(ctx, d.filter, opts...)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
err = callback.GetCallback().Execute(ctx, opContext, operation.OpTypeAfterDelete)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
return result, nil
5186
}

0 commit comments

Comments
 (0)