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

Populate a field in an array (and place data in that field) #247

Closed
robertbart opened this issue Aug 21, 2017 · 3 comments
Closed

Populate a field in an array (and place data in that field) #247

robertbart opened this issue Aug 21, 2017 · 3 comments

Comments

@robertbart
Copy link

Steps to reproduce

This issue is very similar to #142.

I'm attempting to populate a field inside an array, with the data from the population replacing the field.

This is the data I'm working with:
Users

{ _id: '111', name: 'alice', age: '19' }
{ _id: '222', name: 'bob', age: '18' }

Team

{
  _id: '555',
  name: 'Alpha',
  members: [
    { user: '111', role: 'leader' },
    { user: '222', role: 'member' }
  ]
}

And the code I have in an attempt to do this.

{
    childField: "_id",
    nameAs: "members.user",
    parentField: "members",
    select: (_, parent) => parent.members ? ({ _id: { $in: parent.members.map(member => member.user) } }) : {},
    service: "users",
}

Expected behavior

{
  _id: '555',
  name: 'Alpha',
  members: [
    {
      user: { _id: '111', name: 'alice', age = '19' },
      role: 'leader' 
    },
    {
      user: { _id: '222', name: 'bob', age: '18' },
      role: 'member' 
    }
  ]
}

Actual behavior

Adds this to the response:

"_include": [
        "members.user"
]

Alternative 'half-working' solution.

The following code:

{
    childField: "_id",
    nameAs: "users",
    parentField: "members",
    select: (_, parent) => parent.members ? ({ _id: { $in: parent.members.map(member => member.user) } }) : {},
    service: "users",
}

produces:

{
  _id: '555',
  name: 'Alpha',
  members: [
    { user: '111', role: 'leader' },
    { user: '222', role: 'member' }
  ],
  users: [
    { user: '111', role: 'leader' },
    { user: '222', role: 'member' }
   ]
}

where I do have all the data I need but not formatted in a way that I want (the user models being placed into the user field in the members list).

@eddyystop
Copy link
Collaborator

The way to address your needs is to have a preliminary hook that extracts the foreign ids into a format acceptable to populate.

Iterating through an array of objects is difficult to implement. And where does it end? Those objects may themselves contain arrays of objects.

You can submit a PR that fully addresses this issue if you're interested in to doing so.

@robertbart
Copy link
Author

As a quick workaround, I changed the nameAsField to user, which created a list of users in the root of the object, and then created a second hook which moved them to their correct place.

When I'm done with prototyping the current project I'm working on, I'll come back and create a more permanent and better solution.

@eddyystop
Copy link
Collaborator

The new fastJoin hook can handle this scenario, as well as others populate can't.

It also makes roughly 10% of the service calls that populate needs, i.e. 2 calls instead of 20.

https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#fastjoin
https://feathers-plus.github.io/v1/feathers-hooks-common/guide.html#fastJoin

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

No branches or pull requests

2 participants