Skip to content

feat: add support for jsonContains operator on array fields - #923

Open
aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/support-jsoncontains
Open

aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/support-jsoncontains

Conversation

@aaqilniz

@aaqilniz aaqilniz commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor
  • Implements the jsonContains operator mapping inside PostgreSQL's buildExpression to support array/containment queries (such as those required for referencesMany relations).
  • Automatically parses and normalizes stringified JSON or single values into an array, generating PostgreSQL-native containment checks (col @> CAST(? AS JSONB)) connected with OR.
  • Adds comprehensive unit and integration tests covering record filtering and raw SQL string/parameter generation for JSONB fields.

This PR depends on PR #11883 (loopbackio/loopback-next#11883) to successfully access property metadata after relation creation.

This PR also depends on loopbackio/loopback-next#11146. The find method never resolves the options parameter. This PR adds that.

This PR also depends on loopbackio/loopback-datasource-juggler#2615. The datasource-juggler identifies the inq filter with referencesMany and converts the filter to jsonContains.

Closes Issue loopbackio/loopback-next#11169

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • 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

Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
@aaqilniz
aaqilniz force-pushed the feat/support-jsoncontains branch from 77041f3 to c453e6c Compare November 6, 2026 17:26

@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 tackling JSONB membership support. The proposed SQL translation is promising for the reported scenario, but there are correctness and compatibility issues around native arrays, input typing, empty filters, cross-connector semantics, and missing database-backed regression coverage. I've left specific inline comments; I'd like these addressed before merging.

Comment thread lib/postgresql.js
if (!arrayItemType) {
throw new Error(`Unsupported array item type: ${itemType ? itemType.name : 'unknown'}`);
}
const isArray = propertyDefinition.postgresql.dataType === 'ARRAY';

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 native-array detection looks incorrect. The connector already supports native PostgreSQL mappings such as varchar[] (see toColumnValue()), but dataType === 'ARRAY' would not recognize them. They would instead be sent through the JSONB branch, generating an incompatible @> operand type. Can we distinguish actual native array types from JSONB columns and reuse the existing array operators (including containsAnyOf for overlap) where appropriate? Please add native-array execution tests.

Comment thread lib/postgresql.js
let values = operatorValue;
if (typeof values === 'string') {
try {
values = JSON.parse(values);

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.

Parsing every string with JSON.parse() can silently change identifier types. For example, a legitimate string ID '123' becomes the number 123, and JSONB distinguishes ['123'] from [123], so an existing reference may no longer match. Can we normalize values using the declared element type instead of guessing from string contents? Please add a numeric-looking string ID regression test.

Comment thread lib/postgresql.js
params.push(JSON.stringify([value]));
}
});
return new ParameterizedSQL(`(${conditions.join(' OR ')})`, params);

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.

For jsonContains: [], conditions stays empty and this returns (), which is invalid SQL. Empty candidate lists can occur in dynamically generated inq filters. Please define explicit empty-input behavior (typically a false predicate for match-any) and test it.

Comment thread lib/postgresql.js
}
if (!Array.isArray(values)) { values = [values]; }

const itemType = propertyDefinition.type[0];

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 path assumes propertyDefinition.type is an array and propertyDefinition.postgresql exists. A model array property does not necessarily declare PostgreSQL-specific metadata, so the later .dataType access can throw a TypeError rather than handling the filter deliberately. Can we validate the supported property types/storage mappings and return a clear error or supported fallback?

Comment thread lib/postgresql.js
return new ParameterizedSQL(`to_tsvector(${columnName}) @@ to_tsquery(?)`, [operatorValue]);
// Map the abstract 'jsonContains' operator to PostgreSQL's JSONB containment operator (@>),
// parsing or normalizing string/array inputs to build parameterized clauses connected with OR.
case 'jsonContains':

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.

We need to define the public semantics of jsonContains before adding this as a connector operator. Here an array of candidates becomes OR/match-any, while JSON containment of an array in other connectors can mean match-all. Since datasource-juggler #2615 proposes rewriting inq into jsonContains, this risks different results across connectors for the same filter. Can we preserve the inq contract explicitly and establish consistent operator semantics across the relevant connectors?

Comment thread test/postgresql.test.js
db.automigrate(['ItemWithJson', 'ItemWithIntegerArray', 'ItemWithStringArray'], done);
});

beforeEach(function(done) {

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.

The added tests inspect buildWhere() output but do not persist records and execute a find() against PostgreSQL, so they cannot catch type/cast failures or incorrect results. Please add data-backed cases for JSONB, native arrays, multiple candidates, numeric-looking string IDs, empty inputs, and the actual referencesMany flow. Also, this hook only waits for the final destroyAll(done); the preceding two destroyAll() calls are unawaited and may overlap with subsequent tests. Could we await all three operations?

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