From abc7aead934d87f3f630beceb82489d25d1d1ef8 Mon Sep 17 00:00:00 2001 From: 0xbuidl <74317911+0x-buidl@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:08:58 -0400 Subject: [PATCH] fix: make before find hook receive pointer to query --- examples/database/author.go | 2 +- examples/database/book.go | 14 +++++++------- examples/database/review.go | 2 +- examples/main.go | 30 ++++++++++++++++++++++++++++++ model.go | 14 +++++++------- query.go | 4 ++-- 6 files changed, 48 insertions(+), 18 deletions(-) diff --git a/examples/database/author.go b/examples/database/author.go index 2e48fd3..bd4c4f8 100644 --- a/examples/database/author.go +++ b/examples/database/author.go @@ -23,6 +23,6 @@ func NewAuthorModel(coll *mongo.Collection) *AuthorModel { return mgs.NewModel[Author, *mgs.DefaultSchema](coll) } -func (a Author) Validate(ctx context.Context, arg *mgs.HookArg[Author]) error { +func (Author) Validate(ctx context.Context, arg *mgs.HookArg[Author]) error { return nil } diff --git a/examples/database/book.go b/examples/database/book.go index cb8351c..6ac3345 100644 --- a/examples/database/book.go +++ b/examples/database/book.go @@ -28,26 +28,26 @@ func NewBookModel(coll *mongo.Collection) *BookModel { return mgs.NewModel[Book, *mgs.DefaultSchema](coll) } -func (b Book) Validate(ctx context.Context, arg *mgs.HookArg[Book]) error { +func (Book) Validate(ctx context.Context, arg *mgs.HookArg[Book]) error { var err error - doc := arg.Data().(*BookDoc) - if _, ok := doc.Doc.Author.(primitive.ObjectID); !ok { + book := arg.Data().(*BookDoc) + if _, ok := book.Doc.Author.(primitive.ObjectID); !ok { err = fmt.Errorf("author must be ObjectID") } return err } -func (book Book) BeforeValidate(ctx context.Context, arg *mgs.HookArg[Book]) error { +func (Book) BeforeValidate(ctx context.Context, arg *mgs.HookArg[Book]) error { return nil } -func (book Book) AfterValidate(ctx context.Context, arg *mgs.HookArg[Book]) error { +func (Book) AfterValidate(ctx context.Context, arg *mgs.HookArg[Book]) error { return nil } -func (book Book) BeforeCreate(ctx context.Context, arg *mgs.HookArg[Book]) error { +func (Book) BeforeCreate(ctx context.Context, arg *mgs.HookArg[Book]) error { return nil } @@ -72,7 +72,7 @@ func (book Book) AfterDelete(ctx context.Context, arg *mgs.HookArg[Book]) error } func (book Book) BeforeFind(ctx context.Context, arg *mgs.HookArg[Book]) error { - q := arg.Data().(*mgs.Query[Book]).Filter + q := *arg.Data().(*mgs.Query[Book]).Filter q["deleted"] = false return nil } diff --git a/examples/database/review.go b/examples/database/review.go index 4668a90..447a714 100644 --- a/examples/database/review.go +++ b/examples/database/review.go @@ -23,6 +23,6 @@ func NewReviewModel(coll *mongo.Collection) *ReviewModel { return mgs.NewModel[Review, *mgs.DefaultSchema](coll) } -func (r Review) Validate(ctx context.Context, arg *mgs.HookArg[Review]) error { +func (Review) Validate(ctx context.Context, arg *mgs.HookArg[Review]) error { return nil } diff --git a/examples/main.go b/examples/main.go index 0f9174d..ca7b0e0 100644 --- a/examples/main.go +++ b/examples/main.go @@ -7,6 +7,8 @@ import ( "time" "github.com/0x-buidl/mgs/examples/database" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -49,4 +51,32 @@ func setup() func(context.Context) { func main() { ctx := context.Background() defer setup()(ctx) + + books, err := bookModel.CreateMany( + ctx, + []database.Book{ + {Author: primitive.NewObjectID()}, + {Author: primitive.NewObjectID(), Deleted: true}, + }, + ) + if err != nil { + log.Fatal(err) + } + + book, err := bookModel.FindOne(ctx, bson.M{"_id": books[0].GetID()}) + if err != nil { + log.Fatal(err) + } + if book == nil { + log.Fatal("book should not be nil") + } + + book, err = bookModel.FindOne(ctx, bson.M{"_id": books[1].GetID()}) + if err != mongo.ErrNoDocuments { + log.Fatal("should be mongo.ErrNoDocuments") + } + + if book != nil { + log.Fatal("book should be nil") + } } diff --git a/model.go b/model.go index a3b3ffd..38e8d50 100644 --- a/model.go +++ b/model.go @@ -152,7 +152,7 @@ func (model *Model[T, P]) DeleteOne( callback := func(sessCtx mongo.SessionContext) (interface{}, error) { ds := model.docSample() - qarg := NewQuery[T]().SetFilter(query).SetOperation(DeleteOne).SetOptions(opts) + qarg := NewQuery[T]().SetFilter(&query).SetOperation(DeleteOne).SetOptions(opts) err := runBeforeDeleteHooks(ctx, ds, newHookArg[T](qarg, DeleteOne)) if err != nil { return nil, err @@ -186,7 +186,7 @@ func (model *Model[T, P]) DeleteMany( callback := func(sessCtx mongo.SessionContext) (interface{}, error) { ds := model.docSample() - qarg := NewQuery[T]().SetFilter(query).SetOperation(DeleteMany).SetOptions(opts) + qarg := NewQuery[T]().SetFilter(&query).SetOperation(DeleteMany).SetOptions(opts) err := runBeforeDeleteHooks(ctx, ds, newHookArg[T](qarg, DeleteMany)) if err != nil { return nil, err @@ -224,7 +224,7 @@ func (model *Model[T, P]) FindById( doc := model.docSample() query := bson.M{} - qarg := NewQuery[T]().SetFilter(query).SetOperation(FindOne).SetOptions(opts) + qarg := NewQuery[T]().SetFilter(&query).SetOperation(FindOne).SetOptions(opts) err = runBeforeFindHooks(ctx, doc, newHookArg[T](qarg, FindOne)) if err != nil { return nil, err @@ -275,7 +275,7 @@ func (model *Model[T, P]) FindOne( ) (*Document[T, P], error) { doc := model.docSample() - qarg := NewQuery[T]().SetFilter(query).SetOperation(FindOne).SetOptions(opts) + qarg := NewQuery[T]().SetFilter(&query).SetOperation(FindOne).SetOptions(opts) err := runBeforeFindHooks(ctx, doc, newHookArg[T](qarg, FindOne)) if err != nil { return nil, err @@ -322,7 +322,7 @@ func (model *Model[T, P]) Find( ) ([]*Document[T, P], error) { d := model.docSample() - qarg := NewQuery[T]().SetFilter(query).SetOperation(FindMany).SetOptions(opts) + qarg := NewQuery[T]().SetFilter(&query).SetOperation(FindMany).SetOptions(opts) err := runBeforeFindHooks(ctx, d, newHookArg[T](qarg, FindMany)) if err != nil { return nil, err @@ -391,7 +391,7 @@ func (model *Model[T, P]) UpdateOne(ctx context.Context, ds := model.docSample() callback := func(sessCtx mongo.SessionContext) (interface{}, error) { - qa := NewQuery[T]().SetFilter(query). + qa := NewQuery[T]().SetFilter(&query). SetUpdate(&update). SetOperation(UpdateOne). SetOptions(opts) @@ -433,7 +433,7 @@ func (model *Model[T, P]) UpdateMany(ctx context.Context, ds := model.docSample() callback := func(sessCtx mongo.SessionContext) (interface{}, error) { - qa := NewQuery[T]().SetFilter(query). + qa := NewQuery[T]().SetFilter(&query). SetUpdate(&update). SetOperation(UpdateMany). SetOptions(opts) diff --git a/query.go b/query.go index a6c68b2..001f52d 100644 --- a/query.go +++ b/query.go @@ -7,7 +7,7 @@ import ( // Query is a struct that holds information about the current operation beign executed on a model. type Query[T Schema] struct { // The document filter for this operation - Filter bson.M + Filter *bson.M // Update payload if Operation is an update operation Update *bson.M // Options specific to the current operation @@ -38,7 +38,7 @@ func NewQuery[T Schema]() *Query[T] { } // SetFilter sets the Query filter field. -func (q *Query[T]) SetFilter(f bson.M) *Query[T] { +func (q *Query[T]) SetFilter(f *bson.M) *Query[T] { q.Filter = f return q }