@@ -33,7 +33,9 @@ type iCreator[T any] interface {
3333}
3434
3535type Creator [T any ] struct {
36- collection * mongo.Collection
36+ collection * mongo.Collection
37+ beforeHooks []beforeHookFn [T ]
38+ afterHooks []afterHookFn
3739}
3840
3941func NewCreator [T any ](collection * mongo.Collection ) * Creator [T ] {
@@ -42,27 +44,70 @@ func NewCreator[T any](collection *mongo.Collection) *Creator[T] {
4244 }
4345}
4446
47+ // RegisterBeforeHooks is used to set the after hooks of the insert operation
48+ // If you register the hook for InsertOne, the opContext.Docs will be nil
49+ // If you register the hook for InsertMany, the opContext.Doc will be nil
50+ func (c * Creator [T ]) RegisterBeforeHooks (hooks ... beforeHookFn [T ]) * Creator [T ] {
51+ c .beforeHooks = append (c .beforeHooks , hooks ... )
52+ return c
53+ }
54+
55+ func (c * Creator [T ]) RegisterAfterHooks (hooks ... afterHookFn ) * Creator [T ] {
56+ c .afterHooks = append (c .afterHooks , hooks ... )
57+ return c
58+ }
59+
60+ func (c * Creator [T ]) preActionHandler (ctx context.Context , globalOpContext * operation.OpContext , opContext * BeforeOpContext [T ], opType operation.OpType ) error {
61+ err := callback .GetCallback ().Execute (ctx , globalOpContext , opType )
62+ if err != nil {
63+ return err
64+ }
65+ for _ , beforeHook := range c .beforeHooks {
66+ err = beforeHook (ctx , opContext )
67+ if err != nil {
68+ return err
69+ }
70+ }
71+ return nil
72+ }
73+
74+ func (c * Creator [T ]) postActionHandler (ctx context.Context , globalOpContext * operation.OpContext , opContext * AfterOpContext , opType operation.OpType ) error {
75+ err := callback .GetCallback ().Execute (ctx , globalOpContext , opType )
76+ if err != nil {
77+ return err
78+ }
79+ for _ , afterHook := range c .afterHooks {
80+ err = afterHook (ctx , opContext )
81+ if err != nil {
82+ return err
83+ }
84+ }
85+ return nil
86+ }
87+
4588func (c * Creator [T ]) InsertOne (ctx context.Context , doc * T , opts ... * options.InsertOneOptions ) (* mongo.InsertOneResult , error ) {
4689 opContext := operation .NewOpContext (c .collection , operation .WithDoc (doc ))
47- err := callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeBeforeInsert )
90+ err := c . preActionHandler ( ctx , opContext , NewBeforeOpContext ( c . collection , WithDoc ( doc )) , operation .OpTypeBeforeInsert )
4891 if err != nil {
4992 return nil , err
5093 }
94+
5195 result , err := c .collection .InsertOne (ctx , doc , opts ... )
5296 if err != nil {
5397 return nil , err
5498 }
5599
56- err = callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeAfterInsert )
100+ err = c . postActionHandler ( ctx , opContext , NewAfterOpContext ( c . collection ) , operation .OpTypeAfterInsert )
57101 if err != nil {
58102 return nil , err
59103 }
104+
60105 return result , nil
61106}
62107
63108func (c * Creator [T ]) InsertMany (ctx context.Context , docs []* T , opts ... * options.InsertManyOptions ) (* mongo.InsertManyResult , error ) {
64109 opContext := operation .NewOpContext (c .collection , operation .WithDoc (docs ))
65- err := callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeBeforeInsert )
110+ err := c . preActionHandler ( ctx , opContext , NewBeforeOpContext ( c . collection , WithDocs ( docs )) , operation .OpTypeBeforeInsert )
66111 if err != nil {
67112 return nil , err
68113 }
@@ -72,7 +117,7 @@ func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options
72117 return nil , err
73118 }
74119
75- err = callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeAfterInsert )
120+ err = c . postActionHandler ( ctx , opContext , NewAfterOpContext ( c . collection ) , operation .OpTypeAfterInsert )
76121 if err != nil {
77122 return nil , err
78123 }
0 commit comments