Improve example with a `projection` which flattens the selected fields

This commit is contained in:
jos 2020-01-04 12:21:01 +01:00
parent 540566f0cd
commit ad430a3173
1 changed files with 8 additions and 1 deletions

View File

@ -72,7 +72,14 @@
} }
if (projection) { if (projection) {
query = `_.map(${query}, item => _.pick(item, ${JSON.stringify(projection.fields)}))` // It is possible to make a util function "pickFlat"
// and use that when building the query to make it more readable.
const fields = projection.fields.map(field => {
const name = _.last(field.split('.'))
return ` '${name}': _.get(item, '${field}')`
})
query = `_.map(${query}, item => ({\n${fields.join(',\n')}})\n)`
} }
return query return query