Skip to content

Commit da90b0c

Browse files
Refactor:
- Remove global plugin instance - Add dbCallbacks field in Finder, Creator, Deleter and Updater - Support DB Callbacks
1 parent eadcc95 commit da90b0c

19 files changed

+440
-229
lines changed

callback/callback.go

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,6 @@ import (
2222

2323
type CbFn func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error
2424

25-
var Callbacks = initializeCallbacks()
26-
27-
func initializeCallbacks() *Callback {
28-
return &Callback{
29-
beforeInsert: make([]callbackHandler, 0),
30-
afterInsert: make([]callbackHandler, 0),
31-
beforeUpdate: make([]callbackHandler, 0),
32-
afterUpdate: make([]callbackHandler, 0),
33-
beforeDelete: make([]callbackHandler, 0),
34-
afterDelete: make([]callbackHandler, 0),
35-
beforeUpsert: make([]callbackHandler, 0),
36-
afterUpsert: make([]callbackHandler, 0),
37-
beforeFind: make([]callbackHandler, 0),
38-
afterFind: make([]callbackHandler, 0),
39-
}
40-
}
41-
4225
func InitializeCallbacks() *Callback {
4326
return &Callback{
4427
beforeInsert: make([]callbackHandler, 0),
@@ -54,10 +37,6 @@ func InitializeCallbacks() *Callback {
5437
}
5538
}
5639

57-
func GetCallback() *Callback {
58-
return Callbacks
59-
}
60-
6140
type Callback struct {
6241
beforeInsert []callbackHandler
6342
afterInsert []callbackHandler
@@ -71,6 +50,46 @@ type Callback struct {
7150
afterFind []callbackHandler
7251
}
7352

53+
func (c *Callback) BeforeInsert() []callbackHandler {
54+
return c.beforeInsert
55+
}
56+
57+
func (c *Callback) AfterInsert() []callbackHandler {
58+
return c.afterInsert
59+
}
60+
61+
func (c *Callback) BeforeUpdate() []callbackHandler {
62+
return c.beforeUpdate
63+
}
64+
65+
func (c *Callback) AfterUpdate() []callbackHandler {
66+
return c.afterUpdate
67+
}
68+
69+
func (c *Callback) BeforeDelete() []callbackHandler {
70+
return c.beforeDelete
71+
}
72+
73+
func (c *Callback) AfterDelete() []callbackHandler {
74+
return c.afterDelete
75+
}
76+
77+
func (c *Callback) BeforeUpsert() []callbackHandler {
78+
return c.beforeUpsert
79+
}
80+
81+
func (c *Callback) AfterUpsert() []callbackHandler {
82+
return c.afterUpsert
83+
}
84+
85+
func (c *Callback) BeforeFind() []callbackHandler {
86+
return c.beforeFind
87+
}
88+
89+
func (c *Callback) AfterFind() []callbackHandler {
90+
return c.afterFind
91+
}
92+
7493
func (c *Callback) Execute(ctx context.Context, opCtx *operation.OpContext, opType operation.OpType, opts ...any) error {
7594
switch opType {
7695
case operation.OpTypeBeforeInsert:
@@ -158,6 +177,48 @@ func (c *Callback) Register(opType operation.OpType, name string, fn CbFn) {
158177
name: name,
159178
fn: fn,
160179
})
180+
case operation.OpTypeBeforeAny:
181+
c.beforeInsert = append(c.beforeInsert, callbackHandler{
182+
name: name,
183+
fn: fn,
184+
})
185+
c.beforeUpdate = append(c.beforeUpdate, callbackHandler{
186+
name: name,
187+
fn: fn,
188+
})
189+
c.beforeDelete = append(c.beforeDelete, callbackHandler{
190+
name: name,
191+
fn: fn,
192+
})
193+
c.beforeUpsert = append(c.beforeUpsert, callbackHandler{
194+
name: name,
195+
fn: fn,
196+
})
197+
c.beforeFind = append(c.beforeFind, callbackHandler{
198+
name: name,
199+
fn: fn,
200+
})
201+
case operation.OpTypeAfterAny:
202+
c.afterInsert = append(c.afterInsert, callbackHandler{
203+
name: name,
204+
fn: fn,
205+
})
206+
c.afterUpdate = append(c.afterUpdate, callbackHandler{
207+
name: name,
208+
fn: fn,
209+
})
210+
c.afterDelete = append(c.afterDelete, callbackHandler{
211+
name: name,
212+
fn: fn,
213+
})
214+
c.afterUpsert = append(c.afterUpsert, callbackHandler{
215+
name: name,
216+
fn: fn,
217+
})
218+
c.afterFind = append(c.afterFind, callbackHandler{
219+
name: name,
220+
fn: fn,
221+
})
161222
}
162223
}
163224

@@ -183,6 +244,18 @@ func (c *Callback) Remove(opType operation.OpType, name string) {
183244
c.beforeFind = c.remove(c.beforeFind, name)
184245
case operation.OpTypeAfterFind:
185246
c.afterFind = c.remove(c.afterFind, name)
247+
case operation.OpTypeBeforeAny:
248+
c.beforeInsert = c.remove(c.beforeInsert, name)
249+
c.beforeUpdate = c.remove(c.beforeUpdate, name)
250+
c.beforeDelete = c.remove(c.beforeDelete, name)
251+
c.beforeUpsert = c.remove(c.beforeUpsert, name)
252+
c.beforeFind = c.remove(c.beforeFind, name)
253+
case operation.OpTypeAfterAny:
254+
c.afterInsert = c.remove(c.afterInsert, name)
255+
c.afterUpdate = c.remove(c.afterUpdate, name)
256+
c.afterDelete = c.remove(c.afterDelete, name)
257+
c.afterUpsert = c.remove(c.afterUpsert, name)
258+
c.afterFind = c.remove(c.afterFind, name)
186259
}
187260
}
188261

collection.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
func NewCollection[T any](db *Database, collection string) *Collection[T] {
2828
return &Collection[T]{
29+
db: db,
2930
collection: db.database().Collection(collection),
3031
callbacks: db.callbacks,
3132
}
@@ -39,19 +40,19 @@ type Collection[T any] struct {
3940
}
4041

4142
func (c *Collection[T]) Finder() *finder.Finder[T] {
42-
return finder.NewFinder[T](c.collection)
43+
return finder.NewFinder[T](c.collection, c.callbacks)
4344
}
4445

4546
func (c *Collection[T]) Creator() *creator.Creator[T] {
46-
return creator.NewCreator[T](c.collection)
47+
return creator.NewCreator[T](c.collection, c.callbacks)
4748
}
4849

4950
func (c *Collection[T]) Updater() *updater.Updater[T] {
50-
return updater.NewUpdater[T](c.collection)
51+
return updater.NewUpdater[T](c.collection, c.callbacks)
5152
}
5253

5354
func (c *Collection[T]) Deleter() *deleter.Deleter[T] {
54-
return deleter.NewDeleter[T](c.collection)
55+
return deleter.NewDeleter[T](c.collection, c.callbacks)
5556
}
5657
func (c *Collection[T]) Aggregator() *aggregator.Aggregator[T] {
5758
return aggregator.NewAggregator[T](c.collection)

collection_e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ func TestCollection_e2e_Updater(t *testing.T) {
4848
func TestCollection_e2e_Finder(t *testing.T) {
4949
collection := getCollection[any](t)
5050

51-
f := finder.NewFinder[any](collection.collection)
51+
f := finder.NewFinder[any](collection.collection, nil)
5252
assert.NotNil(t, f, "Expected non-nil Finder")
5353
}
5454

5555
func TestCollection_e2e_Creator(t *testing.T) {
5656
collection := getCollection[any](t)
5757

58-
c := creator.NewCreator[any](collection.collection)
58+
c := creator.NewCreator[any](collection.collection, nil)
5959
assert.NotNil(t, c, "Expected non-nil Creator")
6060
}
6161

collection_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ func TestCollection_New(t *testing.T) {
3333
}
3434

3535
func TestCollection_Finder(t *testing.T) {
36-
f := finder.NewFinder[any](&mongo.Collection{})
36+
f := finder.NewFinder[any](&mongo.Collection{}, nil)
3737
assert.NotNil(t, f, "Expected non-nil Finder")
3838
}
3939

4040
func TestCollection_Creator(t *testing.T) {
41-
c := creator.NewCreator[any](&mongo.Collection{})
41+
c := creator.NewCreator[any](&mongo.Collection{}, nil)
4242
assert.NotNil(t, c, "Expected non-nil Creator")
4343
}
4444

4545
func TestCollection_Updater(t *testing.T) {
46-
u := updater.NewUpdater[any](&mongo.Collection{})
46+
u := updater.NewUpdater[any](&mongo.Collection{}, nil)
4747
assert.NotNil(t, u, "Expected non-nil Updater")
4848
}
4949

creator/creator.go

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

20+
"github.com/chenmingyong0423/go-mongox/v2/callback"
21+
2022
"github.com/chenmingyong0423/go-mongox/v2/internal/pkg/utils"
2123

22-
"github.com/chenmingyong0423/go-mongox/v2/callback"
2324
"github.com/chenmingyong0423/go-mongox/v2/operation"
2425

2526
"go.mongodb.org/mongo-driver/v2/mongo"
@@ -35,15 +36,19 @@ type ICreator[T any] interface {
3536
var _ ICreator[any] = (*Creator[any])(nil)
3637

3738
type Creator[T any] struct {
38-
collection *mongo.Collection
39-
modelHook any
39+
collection *mongo.Collection
40+
41+
modelHook any
42+
43+
dbCallbacks *callback.Callback
4044
beforeHooks []hookFn[T]
4145
afterHooks []hookFn[T]
4246
}
4347

44-
func NewCreator[T any](collection *mongo.Collection) *Creator[T] {
48+
func NewCreator[T any](collection *mongo.Collection, dbCallbacks *callback.Callback) *Creator[T] {
4549
return &Creator[T]{
46-
collection: collection,
50+
collection: collection,
51+
dbCallbacks: dbCallbacks,
4752
}
4853
}
4954

@@ -66,7 +71,7 @@ func (c *Creator[T]) RegisterAfterHooks(hooks ...hookFn[T]) *Creator[T] {
6671
}
6772

6873
func (c *Creator[T]) preActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *OpContext[T], opType operation.OpType) error {
69-
err := callback.GetCallback().Execute(ctx, globalOpContext, opType)
74+
err := c.dbCallbacks.Execute(ctx, globalOpContext, opType)
7075
if err != nil {
7176
return err
7277
}
@@ -80,7 +85,7 @@ func (c *Creator[T]) preActionHandler(ctx context.Context, globalOpContext *oper
8085
}
8186

8287
func (c *Creator[T]) postActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *OpContext[T], opType operation.OpType) error {
83-
err := callback.GetCallback().Execute(ctx, globalOpContext, opType)
88+
err := c.dbCallbacks.Execute(ctx, globalOpContext, opType)
8489
if err != nil {
8590
return err
8691
}
@@ -105,6 +110,7 @@ func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...options.List
105110
return nil, err
106111
}
107112

113+
opContext.Result = result
108114
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeAfterInsert)
109115
if err != nil {
110116
return nil, err
@@ -114,8 +120,8 @@ func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...options.List
114120
}
115121

116122
func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...options.Lister[options.InsertManyOptions]) (*mongo.InsertManyResult, error) {
117-
opContext := operation.NewOpContext(c.collection, operation.WithDoc(docs), operation.WithMongoOptions(opts), operation.WithModelHook(c.modelHook))
118-
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeBeforeInsert)
123+
globalOpContext := operation.NewOpContext(c.collection, operation.WithDoc(docs), operation.WithMongoOptions(opts), operation.WithModelHook(c.modelHook))
124+
err := c.preActionHandler(ctx, globalOpContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeBeforeInsert)
119125
if err != nil {
120126
return nil, err
121127
}
@@ -125,7 +131,8 @@ func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...options.
125131
return nil, err
126132
}
127133

128-
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeAfterInsert)
134+
globalOpContext.Result = result
135+
err = c.postActionHandler(ctx, globalOpContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeAfterInsert)
129136
if err != nil {
130137
return nil, err
131138
}

creator/creator_e2e_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func newCollection(t *testing.T) *mongo.Collection {
7777

7878
func TestCreator_e2e_One(t *testing.T) {
7979
collection := newCollection(t)
80-
creator := NewCreator[User](collection)
80+
creator := NewCreator[User](collection, &callback.Callback{})
8181

8282
type globalHook struct {
8383
opType operation.OpType
@@ -306,7 +306,7 @@ func TestCreator_e2e_One(t *testing.T) {
306306
t.Run(tc.name, func(t *testing.T) {
307307
tc.before(tc.ctx, t)
308308
for _, hook := range tc.globalHook {
309-
callback.GetCallback().Register(hook.opType, hook.name, hook.fn)
309+
creator.dbCallbacks.Register(hook.opType, hook.name, hook.fn)
310310
}
311311
insertOneResult, err := creator.RegisterBeforeHooks(tc.beforeHook...).
312312
RegisterAfterHooks(tc.afterHook...).InsertOne(tc.ctx, tc.doc, tc.opts...)
@@ -318,7 +318,7 @@ func TestCreator_e2e_One(t *testing.T) {
318318
require.NotNil(t, insertOneResult.InsertedID)
319319
}
320320
for _, hook := range tc.globalHook {
321-
callback.GetCallback().Remove(hook.opType, hook.name)
321+
creator.dbCallbacks.Remove(hook.opType, hook.name)
322322
}
323323
creator.beforeHooks = nil
324324
creator.afterHooks = nil
@@ -328,7 +328,7 @@ func TestCreator_e2e_One(t *testing.T) {
328328

329329
func TestCreator_e2e_Many(t *testing.T) {
330330
collection := newCollection(t)
331-
creator := NewCreator[User](collection)
331+
creator := NewCreator[User](collection, &callback.Callback{})
332332

333333
type globalHook struct {
334334
opType operation.OpType
@@ -586,7 +586,7 @@ func TestCreator_e2e_Many(t *testing.T) {
586586
t.Run(tc.name, func(t *testing.T) {
587587
tc.before(tc.ctx, t)
588588
for _, hook := range tc.globalHook {
589-
callback.GetCallback().Register(hook.opType, hook.name, hook.fn)
589+
creator.dbCallbacks.Register(hook.opType, hook.name, hook.fn)
590590
}
591591
insertManyResult, err := creator.RegisterBeforeHooks(tc.beforeHook...).
592592
RegisterAfterHooks(tc.afterHook...).InsertMany(tc.ctx, tc.docs, tc.opts...)
@@ -599,7 +599,7 @@ func TestCreator_e2e_Many(t *testing.T) {
599599
require.Len(t, insertManyResult.InsertedIDs, tc.wantIdsLength)
600600
}
601601
for _, hook := range tc.globalHook {
602-
callback.GetCallback().Remove(hook.opType, hook.name)
602+
creator.dbCallbacks.Remove(hook.opType, hook.name)
603603
}
604604
creator.beforeHooks = nil
605605
creator.afterHooks = nil

creator/creator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (tu *TestUser) DefaultUpdatedAt() {
4949

5050
func TestNewCreator(t *testing.T) {
5151
mongoCollection := &mongo.Collection{}
52-
creator := NewCreator[any](mongoCollection)
52+
creator := NewCreator[any](mongoCollection, nil)
5353

5454
assert.NotNil(t, creator)
5555
assert.Equal(t, mongoCollection, creator.collection)

0 commit comments

Comments
 (0)