wclr / mongoose-fill

Virtual async fileds for mongoose.js
61 stars 8 forks source link

Can I use this together with Mongoose-Deep-Populate plugin? #1

Closed sibelius closed 8 years ago

sibelius commented 8 years ago

I'm trying to combine this plugin with mongoose-deep-populate, is it possible?

Example:

Model.find().deepPopulate(['child']).fill('child.another')
wclr commented 8 years ago

mongoose-fill works on the model it self with predefined .fill(..) config. So it will not fill props of populated objects (either you populate them with deep-populate or standard populate out of the box.

But actually something like this I think that this may work for you:

// you define fill on child
ChildSchema.fill('another', function(callback){
  ...
})

Then you define fill on parent:

ModelSchema.fill('child.another', function(callback){
  // this.child should be populated
  // probably it will in your case  
  this.child.fill('another', callback)
})

Not shure if child.another (with dot) for fill name will work out, if not replace it with something else.

Actually I think it can be accompblished in some way.

sibelius commented 8 years ago

in the ChildSchema you just need ChildSchema.fill('another', ...)

it is working, thanks for this awesome plugin

sibelius commented 8 years ago

Maybe this plugin should provide this out of the box, as a feature

wclr commented 8 years ago

Actually I didn't understand you problem completly what works, what should work and what doesn't =)

you have fill defined on you child, and you want it to be auto filled from querying parent model using .fill('child.another')?

sibelius commented 8 years ago

My use case is this:

NewsFeed model has one Post that has many Comments

NewsFeed.find().deepPopulate(['post', 'post.user']).fill('post.comments').exec()

Comments are not embedded on Post, neither Post references all its comments, that's why I need to fill comments inside posts

My newsfeed do not have only Posts, it has other models

wclr commented 8 years ago

So does it work for you? What extra steps exept defining PostSchema.fill('comments, ..)` needed ?

sibelius commented 8 years ago

I have to define:

NewsFeedSchema.fill('post.comments', (cb) => this.post.fill('comments') )
PostSchema.fill('comments', (cb) => Comment.find({postId: this._id}).exec(cb))

instead of just having the fill in the child schema

wclr commented 8 years ago

Ah ok, well this probably could be supported out of the box, but not sure because, there is seem to be too much magic already.)

wclr commented 8 years ago

Besides mongoose-fill sitll kinda hack to get rid of callback hell with custom methods and simplify the code (though not more than embedded populate). With async/await maybe it will be more convinient to use custom methods, but I prefere rxjs streams currently for this purpose.)

sibelius commented 8 years ago

Callback is really a hell. I'm using koa@2 that uses async/await, that's awesome. I wish mongoose interoperability with async function is better

sibelius commented 8 years ago

One think that will make this plugin even better is to accept parameters on fill

For instance

Post.fill('comments', (args, cb) => Comment.find({postId: this._id}).limit(args.limit).exec(cb));
Post.fill('comments', 10).exec(); // get only 10 comments of Post

I'm going to see whether I can implement it and provide a PR for this case

wclr commented 8 years ago

actually I believe fill already does what you need see last example in readme

sibelius commented 8 years ago

that's awesome, your plugin should be part of the mongoose core