buunguyen / mongoose-deep-populate

Mongoose plugin to enable deep population of nested models ⛺
MIT License
469 stars 44 forks source link

How to get it working with Keystone.js? #1

Closed art1c0 closed 9 years ago

art1c0 commented 9 years ago

First of all thanks for a great plugin! I'm trying to use your plugin within Keystone.js application and cannot get it working. Keystone uses own mechanism of Lists although it's based on mongoose. I tried to do something like that:

var list = keystone.list('Thing'); list.schema.plugin(deepPopulate); list.deepPopulate ---- method is undefined list.model.deepPopulate ---- method is undefined

If you can give any help on how to get this working it would be really great and will expand usage of your plugin as well :)

buunguyen commented 9 years ago

May be relating to this. Are you sure you didn't call list.register() before invoking list.schema.plugin()?

art1c0 commented 9 years ago

Yes, that helped. Actually sequence should be the following:

var Thing = new keystone.List('Thing');
Thing.schema.plugin(deepPopulate);
Thing.register();

It works, but it has to be enabled for every model separately, it would be much better if that would be possible on all-models level...

Another question I met is how to select specific fields I want to get in the output? Now I get all models fully populated, which is not what I really want - I need only some properties for each model. How to define this?

buunguyen commented 9 years ago

it would be much better if that would be possible on all-models level

The Mongoose way is mongoose.plugin(deepPopulate), should work with anything that uses Mongoose. If not, you should check with the Keystone.js guys.

select specific fields I want to get in the output

That's not supported now. This is something that can be implemented, but I need to think of a way to not make the API overly complicated.

buunguyen commented 9 years ago

Populate options (including custom select) is supported in v0.0.4.

askdesigners commented 9 years ago

And is it correct that querying should look like: Day.model.find().deepPopulate('userdays userdays.user').exec(function.... ?

Doing this throws an error " TypeError: Object # has no method 'deepPopulate' " Am I missing something?

buunguyen commented 9 years ago

Is this for Keystone.js? The gist is that this plugin extends the following Mongoose objects: query, model and model instance. I'm not sure how Keystone.js integrates with Mongoose, just make sure you invoke deepPopulate on the right object.

buunguyen commented 9 years ago

@articobandurini just so you know, the latest version of the plugin supports selecting specific fields for the output.

egaba commented 9 years ago

+1 I am also curious to know if anyones got this to work with Keystone.js?

VykintasZaboras commented 9 years ago

I'm doing:

Post.paginate({
    page: req.query.page || 1,
    perPage: 50,
    maxPages: 10
}).populate('comments','author content date')
.deepPopulate('author.name')

and i included the plugin like so:

var deepPopulate = require('mongoose-deep-populate');
Post.schema.plugin(deepPopulate);
Post.register();

But i still get the error:

TypeError: undefined is not a function

Anyone got it working with keystone?

buunguyen commented 9 years ago

See https://github.com/buunguyen/mongoose-deep-populate/issues/18#issuecomment-110934841 (kudos to @AndrewFinlay). Basically Keystone and this plugin uses different instances of Mongoose. The fix is explicitly adding Mongoose as a dependency in package.json.