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

Fix read model queries when selecting deeply-nested arrays #1552

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor preprocessProjections
  • Loading branch information
Castro, Mario committed Sep 25, 2024
commit 6813d460f36c408afb032a356c180e60290394fc
44 changes: 19 additions & 25 deletions packages/framework-provider-azure/src/helpers/query-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ function buildProjections(projections: ProjectionFor<unknown> | string = '*'): s
.join(', ')
}

/**
* Preprocesses the projections to handle nested arrays and objects.
*
* @param {ProjectionFor<unknown>} projections - The projections to preprocess.
* @returns {ProjectionFor<unknown>} - The preprocessed projections.
*/
function preprocessProjections(projections: ProjectionFor<unknown>): ProjectionFor<unknown> {
const processed = new Set<string>()

Expand All @@ -327,33 +333,21 @@ function preprocessProjections(projections: ProjectionFor<unknown>): ProjectionF
return acc
}, [] as number[])

if (arrayIndices.length === 0) {
// Case 1: No arrays in the projection
if (
arrayIndices.length === 0 ||
(arrayIndices[0] === 0 && arrayIndices.length === 1) ||
(arrayIndices[0] === 1 && arrayIndices.length === 1)
) {
// This block is accessed when one of the following occurs:
// - No arrays in the projection
// - Top-level array not followed by another array
// - Array nested within a top-level property, no arrays follow
processed.add(field)
} else if (arrayIndices[0] === 0) {
// Case 2.1 and 2.2: Array at the top level
if (arrayIndices.length === 1) {
// Case 2.1: Top-level array not followed by another array
processed.add(field)
} else {
// Case 2.2: Top-level array followed by a subarray
const processedField = parts.slice(0, arrayIndices[1] + 1).join('.')
processed.add(processedField.slice(0, -2)) // Remove '[]' from the last part
}
} else if (arrayIndices[0] === 1) {
// Case 2.3: Array nested within a top-level property
if (arrayIndices.length === 1) {
// Case 2.3.1: No arrays follow in the nesting
processed.add(field)
} else {
// Case 2.3.2: Nested arrays after it
const processedField = parts.slice(0, arrayIndices[1] + 1).join('.')
processed.add(processedField.slice(0, -2)) // Remove '[]' from the last part
}
} else {
// Case 2.4: Array nested deeper than a top-level property
const processedField = parts.slice(0, arrayIndices[0] + 1).join('.')
processed.add(processedField.slice(0, -2)) // Remove '[]' from the last part
// Cases with nested arrays or arrays deeper in the structure
const processToIndex = arrayIndices[0] === 0 || arrayIndices[0] === 1 ? arrayIndices[1] : arrayIndices[0]
const processedField = parts.slice(0, processToIndex + 1).join('.')
processed.add(processedField.slice(0, -2)) // Remove the '[]' from the last part
}
})

Expand Down
Loading