Improve example with a `projection` which flattens the selected fields
This commit is contained in:
parent
540566f0cd
commit
ad430a3173
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue