Closed sibelius closed 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.
in the ChildSchema you just need ChildSchema.fill('another', ...)
it is working, thanks for this awesome plugin
Maybe this plugin should provide this out of the box, as a feature
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')
?
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
So does it work for you? What extra steps exept defining PostSchema.fill('comments
, ..)` needed ?
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
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.)
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.)
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
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
actually I believe fill
already does what you need
see last example in readme
that's awesome, your plugin should be part of the mongoose core
I'm trying to combine this plugin with mongoose-deep-populate, is it possible?
Example: