Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lean and populate #169

Closed
dottodot opened this issue Feb 28, 2017 · 12 comments
Closed

lean and populate #169

dottodot opened this issue Feb 28, 2017 · 12 comments

Comments

@dottodot
Copy link

Should I be able to use lean and populate together, I'm pretty sure you normally can with mongoose but I'm finding that population only works when lean is set to false on the model that's being populated.

@marshallswain
Copy link
Member

@dottodot, it's possible that using Mongoose's populate depends on returning Mongoose documents, which would be turned off when using lean: false. You can take a look at the populate hook in feathers-hooks-common as a replacement for it. It is functionally equivalent to Mongoose's populate, and works event with lean: true turned on for the service.

@dottodot
Copy link
Author

Sorry I'm actually referring to using the populate hook, I'm not using the Mongoose populate at all.

@daffl
Copy link
Member

daffl commented Feb 28, 2017

Mongoose model instances do not allow adding or removing properties. Therefore all hooks that do modify the result data only work with plain objects (returned by lean: true - which is the default in the latest versions of this adapter).

@dottodot
Copy link
Author

OK so i'm using this after hook

hooks.populate({
    schema
  })

with schema

module.exports = {
    include: [{
      service: 'api/term',
      nameAs: 'product_type',
      parentField: 'product_type',
      childField: '_id',
      query: {
        $select: ['title'],
        $sort: {
          weight: 1
        }
      }
    }]
}

with lean on the term service, the population doesn't happen. The population only works if I set lean to false on the term service.

@daffl
Copy link
Member

daffl commented Feb 28, 2017

lean has to be true or you have to use the toObject hook (this behaviour is already documented here).

@marshallswain
Copy link
Member

marshallswain commented Feb 28, 2017

@dottodot the ultimate answer is yes. You should be able to use lean and populate together. Ideally all services are set to lean:true. If you have an example of how it doesn't work, please post a repo that we can pull and test. Otherwise, it's really hard (at least for me) to make quick time to help you out.

@dottodot
Copy link
Author

The example I posted above doesn't work or do you need the full code.

@marshallswain
Copy link
Member

@dottodot it would be much more helpful if you could post a repo. That way I don't have to setup these services on my break from work. ;)

@CImrie
Copy link

CImrie commented Mar 24, 2017

I can confirm this (but I can't guarantee I'm not just being stupid!).
Old-style populate() works fine without having to set lean:true on the service.
However with the new populate (i.e. with a schema), it doesn't matter if lean is true or if I use service.hooks.toObject()

Tracked it down to feathers-hooks-common/lib/populate.js line:114:
return Object.assign.apply(Object, [item].concat(_toConsumableArray(children)));

Logging this line out to the console gives back the object I want but without property populated. I logged out children in that line and it correctly finds the object I want to join with, but doesn't seem to be able to set it on the parent object.

Unfortunately my codebase is closed-source but both are services generated by the feathers generate command and use Mongoose. One is a "User" service, the other an "Organization".
The User has a reference field to the Organization with the field named organization.
It is the organization field I want populated with the real organization object.

@daffl
Copy link
Member

daffl commented Mar 24, 2017

If you do Object.assign(mongooseModel) strange things will happen. I ran into this before and it wasn't easy to debug (because if you do a console.log it still looks like the right object which it isn't). For me all those issues have been fixed setting lean: true (or upgrading to v4.0 of this adapter and later).

@CImrie
Copy link

CImrie commented Mar 28, 2017

Thanks for your reply @daffl .
I've no idea what caused it or what has changed in the latest adapter version, but I updated to v5.0.3 of feathers-mongoose and it's now working.

Thanks!

@daffl daffl closed this as completed Mar 28, 2017
@eddyystop
Copy link
Contributor

eddyystop commented Mar 29, 2017

The legacy-populate converted any ORM objects it found. to regular JS objects. The current populate requires normal JS objects.

We had a running discussion about whether all hooks should handle ORM objects, but decided that defaulting feathers-mongoose to lean:true made more sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants