Skip to content

Commit

Permalink
fix: make before find hook receive pointer to query
Browse files Browse the repository at this point in the history
  • Loading branch information
lxnre-codes committed Sep 4, 2023
1 parent dd280a5 commit abc7aea
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/database/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions examples/database/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion examples/database/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
}
14 changes: 7 additions & 7 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit abc7aea

Please sign in to comment.