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 2 levels deep #320

Closed
luke-robertson opened this issue May 9, 2019 · 12 comments
Closed

Populate 2 levels deep #320

luke-robertson opened this issue May 9, 2019 · 12 comments

Comments

@luke-robertson
Copy link

Hello, wandering if this is possible ?

&$populate=attachments
&$populate=attachments.creator

creator is a ID REF inside the attachment table to a user

Currently its only populating attachments which is good, but also need to populate creator inside the attachments if possible,

Thanks !

Luke

@luke-robertson
Copy link
Author

luke-robertson commented May 9, 2019

IV tired this :


    '&$populate=attachments' + '&$populate[]=attachments.creator'

But no luck

@luke-robertson
Copy link
Author

@daffl any idea how to do this please?

@luke-robertson
Copy link
Author

@ekryski you got any ideas ?

@daffl
Copy link
Member

daffl commented May 14, 2019

Set $populate to the nested array populating syntax in a before hook.

@luke-robertson
Copy link
Author

@daffl So this is not possible with the query string ? im going to have to create a custom hook to populate 2 levels ?

@daffl
Copy link
Member

daffl commented May 14, 2019

You could try the querystring representation of the query (require('qs').stringify(nestedSyntax)) but it may not work.

@luke-robertson
Copy link
Author

@daffl
Not sure how id do that, but thanks for the responce i appreciate it

@daffl
Copy link
Member

daffl commented May 15, 2019

The code sample I showed should show how it looks:

const qs = require('qs');
const query = qs.stringify({
  $populate: { 
     path: 'pages',
     populate: {
       path: 'components',
       model: 'Component'
     } 
  }
});

console.log(query);

This will print something like $populate[path]=pages&$populate[populate][path]=components&$populate[populate][model]=Component which would be the query string you'd pass. More information can be found in the NodeJS querystring and qs module documentation.

@luke-robertson
Copy link
Author

Oh right i see !

Sorry i misunderstood, thanks a lot ill give it a test

@luke-robertson
Copy link
Author

luke-robertson commented May 15, 2019

@daffl

const attachAssets = () => context => {
  const stringList = context.params.query['$populate'] || []

  context.params.query['$populate'] = {
    path: `assets ${stringList.join(' ')}`,
    populate: {
      path: 'creator',
    },
  }

  return context
}

😬

@johnnyx3m
Copy link

The code sample I showed should show how it looks:

const qs = require('qs');
const query = qs.stringify({
  $populate: { 
     path: 'pages',
     populate: {
       path: 'components',
       model: 'Component'
     } 
  }
});

console.log(query);

This will print something like $populate[path]=pages&$populate[populate][path]=components&$populate[populate][model]=Component which would be the query string you'd pass. More information can be found in the NodeJS querystring and qs module documentation.

Thanks @daffl This one works magically. Just a question, from this, can we also do a condition 2 levels down?

@DaddyWarbucks
Copy link

I am a bit confused here. The Feathers app should be using qs under the hood to parse queries already. This is typically handled with

app.use(express.urlencoded({ extended: true }));

in app.js. So if you are using the proper "nested query syntax", then the query should be parsed correctly.

Try these solutions out. Mongoose should support a couple of different syntax for populate.

// Mongoose supports space delimited string
&populate=attachments attachments.creator
// will become
q.populate('attachments attachments.creator')

// Mongoose supports array of strings
&populate[0]=attachments&populate[1]=attachments.creator
// will become
q.populate(['attachments', 'attachments.creator'])

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

4 participants