Virtuals that works with reference to another model are not populated properly due to how transformDocuments works.
Mongoose adapter is unable to populate virtuals with internal action call because not populated virtuals are undefined in JSON object. Thus it calls the populate action with an undefined value instead of an id or a list of ids.
Solution:
Mongoose adapter should pre-populate virtuals if they are passed in the ation populate field.
For that matter, we can compare requested populate field list action.params.populate to schema virtuals service.schema.model.schema.virtuals.
Fields that occurs in both should be pre-populated with _id field only.
Then the populate method will be able to operate properly.
I've written acceptance tests for this feature here.
Potential drawbacks:
Population request will be called twice:
one time by mongoose when it populates the virtual to get list of ids for the field.
one time by moleculer when it actually populate the field using service configuration.
It may not be that costly as population request may be internally cached by mongo engine.
I'll try to propose an implementation of it.
Let me know if you have a better idea to solve this issue.
Using:
mongoose:6.10.5
moleculer:0.14.29
moleculer-db:0.8.23
moculer-db-adapter-mongoose:0.9.2
Problem:
Virtuals that works with reference to another model are not populated properly due to how
transformDocuments
works. Mongoose adapter is unable to populate virtuals with internal action call because not populated virtuals areundefined
in JSON object. Thus it calls the populate action with an undefined value instead of an id or a list of ids.Solution:
Mongoose adapter should pre-populate virtuals if they are passed in the ation
populate
field.For that matter, we can compare requested populate field list
action.params.populate
to schema virtualsservice.schema.model.schema.virtuals
.Fields that occurs in both should be pre-populated with
_id
field only.Then the populate method will be able to operate properly.
I've written acceptance tests for this feature here.
Potential drawbacks:
Population request will be called twice:
mongoose
when it populates the virtual to get list of ids for the field.moleculer
when it actually populate the field using service configuration.It may not be that costly as population request may be internally cached by mongo engine.
I'll try to propose an implementation of it.
Let me know if you have a better idea to solve this issue.