From ad430a31735e42684fbce79c52959e1398389b98 Mon Sep 17 00:00:00 2001 From: jos Date: Sat, 4 Jan 2020 12:21:01 +0100 Subject: [PATCH] Improve example with a `projection` which flattens the selected fields --- examples/23_custom_query_language.html | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/23_custom_query_language.html b/examples/23_custom_query_language.html index 9cbe2fd..c6e8d43 100644 --- a/examples/23_custom_query_language.html +++ b/examples/23_custom_query_language.html @@ -72,7 +72,14 @@ } 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