Skip to content

fix: transform inq to jsonContains for referencesMany relation queries - #2615

Open
aaqilniz wants to merge 5 commits into
loopbackio:masterfrom
aaqilniz:fix/inq-in-refmany
Open

aaqilniz wants to merge 5 commits into
loopbackio:masterfrom
aaqilniz:fix/inq-in-refmany

Conversation

@aaqilniz

@aaqilniz aaqilniz commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes query execution for referencesMany relations where standard inq queries fail against array fields stored as JSON.

Changes

  • Updates DataAccessObject.find to inspect model definitions and settings for referencesMany relations.
  • Automatically detects when a where clause queries a referencesMany relation's keyFrom field using an inq operator.
  • Translates { field: { inq: [...] } } into { field: { jsonContains: [...] } }, allowing downstream connectors to construct proper JSON array containment queries (e.g., JSON_CONTAINS).
  • Tests Added: Added unit tests covering query translation, non-inq operator preservation, and edge cases where options/models are omitted.

Closes Issue loopbackio/loopback-next#11169

Checklist

  • Sign off your commits with DCO (Developer Certificate of Origin)
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Commit messages are following our guidelines

@aaqilniz aaqilniz changed the title fix: handle inq for referencesMany fix: transform inq to jsonContains for referencesMany relation queries Oct 31, 2026
@aaqilniz
aaqilniz marked this pull request as ready for review November 1, 2026 06:05
@aaqilniz
aaqilniz requested a review from dhmlau as a code owner November 1, 2026 06:05
@aaqilniz
aaqilniz force-pushed the fix/inq-in-refmany branch 2 times, most recently from 4a56ff3 to cd07b47 Compare November 1, 2026 06:29
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>

@samarpanB samarpanB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this. The reported JSON-backed referencesMany issue is valid, but the shared DAO-level rewrite needs to preserve existing query semantics and connector compatibility before we can merge. I've left inline comments with the specific concerns and requested tests.

Comment thread lib/dao.js Outdated
}
if ((!relations || !Object.keys(relations).length) && this) {
if (this.settings && this.settings.relations) {
relations = this.settings.relations;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we prioritize the resolved this.relations metadata here?

The relation configuration in settings.relations may define foreignKey rather than keyFrom. The latter is populated when the relation definition is resolved.

Since the current logic prefers settings.relations, it can miss valid referencesMany relations and silently skip the conversion.

Can we use the normalized relation definitions instead of checking multiple metadata representations?

@aaqilniz aaqilniz Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that settings.relations may contain the original relation configuration rather than the resolved metadata.

I've revised the implementation to remove the dependency on options.model entirely. This also keeps the change independent of the controller-to-repository options propagation approach I was trying to add in loopbackio/loopback-next#11146.

The DAO now retrieves relations from this.relations first and falls back to this.definition.settings.relations when this.relations is empty. I've verified locally that this fallback provides the resolved relation metadata, including keyFrom, in this case.

Comment thread lib/dao.js Outdated
);
if (matchingRelation && query.where[key] && query.where[key].inq) {
// Mutate the operator from 'inq' to 'jsonContains'
query.where[key] = {jsonContains: query.where[key].inq};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned this changes the semantics of existing inq queries.

For example, with inq: [1, 2], the expected array-membership behavior would typically match a record containing either ID. However, MySQL's JSON_CONTAINS with [1, 2] requires both values to be present.

This could silently exclude valid records.

Can we clarify the expected semantics for multiple IDs and ensure the translation preserves them? We should also cover this with an actual query test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. I agree that passing the entire array to a single JSON_CONTAINS call would require all values to be present.

However, the current implementation generates a separate JSON_CONTAINS condition for each value and joins them with OR. So inq: [1, 2] translates to:

JSON_CONTAINS(column, '1') OR JSON_CONTAINS(column, '2').

This preserves the expected inq semantics: a record matches if its array contains either ID.

Comment thread lib/dao.js Outdated
const matchingRelation = Object.values(relations).find(
r => r.type === 'referencesMany' && r.keyFrom === key,
);
if (matchingRelation && query.where[key] && query.where[key].inq) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this transformation happens in the shared DAO layer, it will affect all connectors, not just the SQL connector where the issue was reported.

The memory connector already has array-specific matching logic. Replacing inq with jsonContains could break queries that currently work there.

We also need to establish whether MySQL, PostgreSQL, MongoDB and other relevant connectors support this operator consistently.

Would it be safer to handle this through connector-specific logic or an explicit connector capability rather than rewriting the operator globally?

@aaqilniz aaqilniz Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about this. Looking at your comment, I think the most reasonable approach is not to rewrite the inq operator to jsonContains globally, but to preserve the inq operator and handle the referencesMany based inq at the respective connector.

I think this can be achieved by passing a flag like isReferencesMany to buildExpression, allowing the connectors to know if the relation is a referencesMany.

Comment thread lib/dao.js Outdated
}
}
if (query && query.where && relations) {
for (const key in query.where) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only handles top-level properties in the where clause.

What happens when the same inq condition is nested inside an and or or expression?

Those conditions would bypass the transformation, producing inconsistent behavior depending on the query structure.

If we retain this approach, shouldn't we handle nested logical conditions as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. Now the transformation considers nested properties too.

let interceptedQuery;

// Stub connector.all to inspect the processed query
connector.all = function(model, query, options, cb) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests verify that the query object is transformed, but they don't verify that the transformed query actually works.

Could we add integration tests using real stored records to validate:

  • Single and multiple IDs
  • Expected match-any versus match-all behavior
  • Existing inq behavior with the memory connector
  • Nested logical filters
  • Actual query execution with the affected SQL connector

Also, we're overwriting connector.all without restoring it. We should restore the original method after each test to avoid affecting other tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added tests for multiple referencesMany properties, nested and/or conditions, and ensuring unrelated properties/operators remain unchanged.

For integration tests, I think we'll have to wait until these changes to mysql-connector become available.

For now, the DAO tests verify the query transformation; they cannot validate SQL generation or actual database matching behavior.

Comment thread tsconfig.json Resolved
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants