Skip to content

Commit

Permalink
Merge pull request #17 from 0x-buidl/fix/empty_doc_with_single_find
Browse files Browse the repository at this point in the history
fix: empty document when populating with findOne and FindById
  • Loading branch information
lxnre-codes committed Sep 21, 2023
2 parents 81a6100 + a0678c5 commit 8acad27
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 2 additions & 0 deletions internal/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ func mergeFindOptsWithAggregatOpts[T UnionFindOpts](opt T) (mongo.Pipeline, *opt
pipelineOpts = append(pipelineOpts, bson.D{{Key: "$project", Value: opt.Projection}})
}
queryOpts = opt.QueryOptions
pipelineOpts = append(pipelineOpts, bson.D{{Key: "$limit", Value: 1}})

}
return pipelineOpts, aggOpts, queryOpts
}
Expand Down
22 changes: 7 additions & 15 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ func (model *Model[T, P]) FindById(ctx context.Context, id any, opts ...*mopt.Fi
opt.FindOneOptions = fopt
opt.QueryOptions = qopt

docs, err := findWithPopulate[*mopt.FindOneOptions, T, P](
ctx, model.collection, query, doc.doc, opt)
docs, err := findWithPopulate[*mopt.FindOneOptions, T, P](ctx, model.collection, query, doc.doc, opt)
if err != nil {
return nil, err
}

if len(docs) == 0 {
return nil, mongo.ErrNoDocuments
}
doc = docs[0]
} else {
err = model.collection.FindOne(ctx, query, fopt).Decode(doc)
if err != nil {
Expand Down Expand Up @@ -262,15 +262,15 @@ func (model *Model[T, P]) FindOne(ctx context.Context, query bson.M, opts ...*mo
opt.FindOneOptions = fopt
opt.QueryOptions = qopt

docs, err := findWithPopulate[*mopt.FindOneOptions, T, P](
ctx, model.collection, query, doc.doc, opt)
docs, err := findWithPopulate[*mopt.FindOneOptions, T, P](ctx, model.collection, query, doc.doc, opt)
if err != nil {
return nil, err
}

if len(docs) == 0 {
return nil, mongo.ErrNoDocuments
}
doc = docs[0]
} else {
err = model.collection.FindOne(ctx, query, fopt).Decode(doc)
if err != nil {
Expand Down Expand Up @@ -306,9 +306,7 @@ func (model *Model[T, P]) Find(ctx context.Context, query bson.M, opts ...*mopt.
opt := mopt.Find()
opt.FindOptions = fopt
opt.QueryOptions = qopt
docs, err = findWithPopulate[*mopt.FindOptions, T, P](
ctx, model.collection, query,
d.doc, opt)
docs, err = findWithPopulate[*mopt.FindOptions, T, P](ctx, model.collection, query, d.doc, opt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -354,10 +352,7 @@ func (model *Model[T, P]) UpdateOne(ctx context.Context, query bson.M, update bs
ds := model.docSample()

callback := func(sessCtx mongo.SessionContext) (interface{}, error) {
qa := NewQuery[T]().SetFilter(&query).
SetUpdate(&update).
SetOperation(UpdateOne).
SetOptions(opts)
qa := NewQuery[T]().SetFilter(&query).SetUpdate(&update).SetOperation(UpdateOne).SetOptions(opts)

err := runBeforeUpdateHooks(ctx, ds, newHookArg[T](qa, UpdateOne))
if err != nil {
Expand Down Expand Up @@ -394,10 +389,7 @@ func (model *Model[T, P]) UpdateMany(ctx context.Context, query bson.M, update b
ds := model.docSample()

callback := func(sessCtx mongo.SessionContext) (interface{}, error) {
qa := NewQuery[T]().SetFilter(&query).
SetUpdate(&update).
SetOperation(UpdateMany).
SetOptions(opts)
qa := NewQuery[T]().SetFilter(&query).SetUpdate(&update).SetOperation(UpdateMany).SetOptions(opts)

err := runBeforeUpdateHooks(ctx, ds, newHookArg[T](qa, UpdateMany))
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,15 @@ func TestModel_Populate(t *testing.T) {

assert.NoError(t, err, "Find should not return error")
assert.NotNil(t, book, "book should not be nil")
assert.NotZero(t, book.GetID(), "book.ID should not be zero")
assert.NotEmpty(t, book.Doc.Authors, "book should have authors")

for _, author := range book.Doc.Authors {
assert.NotEmpty(t, author.(bson.M)["name"], "author should not be empty")
}

chapters := book.Doc.Chapters
assert.NotEmpty(t, chapters, "book should have chapters")
for _, chapter := range chapters {
author := chapter.Author.(bson.M)
assert.NotEmpty(t, author["name"], "author should not be empty")
Expand All @@ -234,12 +237,15 @@ func TestModel_Populate(t *testing.T) {

assert.NoError(t, err, "Find should not return error")
assert.NotNil(t, book, "book should not be nil")
assert.NotZero(t, book.GetID(), "book.ID should not be zero")
assert.NotEmpty(t, book.Doc.Authors, "book should have authors")

for _, author := range book.Doc.Authors {
assert.NotEmpty(t, author.(bson.M)["name"], "author should not be empty")
}

chapters := book.Doc.Chapters
assert.NotEmpty(t, chapters, "book should have chapters")
for _, chapter := range chapters {
author := chapter.Author.(bson.M)
assert.NotEmpty(t, author["name"], "author should not be empty")
Expand Down

0 comments on commit 8acad27

Please sign in to comment.