-
Notifications
You must be signed in to change notification settings - Fork 59
feat(query): support path select #203
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds support for selecting nested fields via dot paths in query.select
, transforming flat key selections into a nested object structure.
- Introduces
lodash.merge
and a newbuildDocFromPath
helper to recursively build nested selections. - Updates
buildDocFromHash.js
to applyselectFields.reduce
with the new helper. - Adds new test cases in
query.test.js
to cover nested selections, missing values, and multiple nested fields.
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/mocks/helpers/buildDocFromHash.js | Import merge , implement nested select logic, add buildDocFromPath helper |
tests/query.test.js | Add tests for nested select behavior and streamline query chaining |
Files not reviewed (1)
- package.json: Language not supported
Comments suppressed due to low confidence (1)
src/mocks/helpers/buildDocFromHash.js:67
- When the root property doesn’t exist on
data
,buildDocFromPath
currently builds nested empty objects (e.g.,{size: {height: {}}}
) instead of returning{}
. Add a guard: ifdata[root]
isundefined
ornull
, return{}
immediately.
const [root, ...subPath] = path;
@@ -56,3 +58,15 @@ module.exports = function buildDocFromHash(hash = {}, id = 'abc123', selectField | |||
}, | |||
}; | |||
}; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding a JSDoc comment above buildDocFromPath
to explain its parameters, return value, and overall behavior for better maintainability.
/** | |
* Recursively builds a nested object from a given data object and a path array. | |
* | |
* @param {Object} data - The source object containing the data to extract. | |
* @param {string[]} path - An array of strings representing the path to the desired data. | |
* Each string corresponds to a key in the object hierarchy. | |
* @returns {Object} A new object containing the data at the specified path. If the path | |
* does not exist in the data, an empty object is returned. | |
*/ |
Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <[email protected]>
Description
Instead of simply copying each selected key to result object, it breaks each select arg string by dots and recursively copies each key.
Before
.select('a.b')
=>{ 'a.b': ... }
After
.select('a.b')
=>{a: {b: ... }}
Related issues
Fixes #199
Limitations
If two paths inside same property are selected (e.g.
"a.b"
and"a.c"
) result will carry only the latest selected path.See skipped test case