Repository navigation
feat: add support for jsonContains operator on array fields #923
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -584,6 +584,43 @@ PostgreSQL.prototype.buildExpression = function(columnName, operator, | |
| operatorValue); | ||
| case 'match': | ||
| 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': | ||
| let values = operatorValue; | ||
| if (typeof values === 'string') { | ||
| try { | ||
| values = JSON.parse(values); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parsing every string with |
||
| } catch (error) { | ||
| values = [values]; | ||
| } | ||
| } | ||
| if (!Array.isArray(values)) { values = [values]; } | ||
|
|
||
| const itemType = propertyDefinition.type[0]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This path assumes |
||
| const postgresTypeMap = new Map([ | ||
| [Number, 'INTEGER'], | ||
| [String, 'TEXT'], | ||
| ]); | ||
| const arrayItemType = postgresTypeMap.get(itemType); | ||
|
|
||
| if (!arrayItemType) { | ||
| throw new Error(`Unsupported array item type: ${itemType ? itemType.name : 'unknown'}`); | ||
| } | ||
| const isArray = propertyDefinition.postgresql.dataType === 'ARRAY'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| const conditions = []; | ||
| const params = []; | ||
|
|
||
| values.forEach(value => { | ||
| if (isArray) { | ||
| conditions.push(`${columnName} @> ARRAY[?]::${arrayItemType}[]`); | ||
| params.push(value); | ||
| } else { | ||
| conditions.push(`${columnName} @> CAST(? AS JSONB)`); | ||
| params.push(JSON.stringify([value])); | ||
| } | ||
| }); | ||
| return new ParameterizedSQL(`(${conditions.join(' OR ')})`, params); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
||
| default: | ||
| // invoke the base implementation of `buildExpression` | ||
| return this.invokeSuper('buildExpression', columnName, operator, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -983,6 +983,91 @@ describe('postgresql connector', function() { | |
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| context('jsonContains operator', function() { | ||
| let ItemWithJson, ItemWithIntegerArray, ItemWithStringArray; | ||
|
|
||
| before(function(done) { | ||
| db = global.getDataSource(); | ||
| // Define a test model utilizing a jsonb column type to test JSON containment filters. | ||
| ItemWithJson = db.define('ItemWithJson', { | ||
| name: {type: String}, | ||
| tags: { | ||
| type: [String], | ||
| postgresql: { | ||
| dataType: 'jsonb', | ||
| }, | ||
| }, | ||
| }); | ||
| ItemWithIntegerArray = db.define('ItemWithIntegerArray', { | ||
| name: {type: String}, | ||
| tags: { | ||
| type: [Number], | ||
| postgresql: { | ||
| dataType: 'jsonb', | ||
| }, | ||
| }, | ||
| }); | ||
| ItemWithStringArray = db.define('ItemWithStringArray', { | ||
| name: {type: String}, | ||
| tags: { | ||
| type: [String], | ||
| postgresql: { | ||
| dataType: 'jsonb', | ||
| }, | ||
| }, | ||
| }); | ||
| db.automigrate(['ItemWithJson', 'ItemWithIntegerArray', 'ItemWithStringArray'], done); | ||
| }); | ||
|
|
||
| beforeEach(function(done) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The added tests inspect |
||
| ItemWithJson.destroyAll(); | ||
| ItemWithIntegerArray.destroyAll(); | ||
| ItemWithStringArray.destroyAll(done); | ||
| }); | ||
|
|
||
| after(function(done) { | ||
| ItemWithJson.destroyAll(); | ||
| ItemWithIntegerArray.destroyAll(); | ||
| ItemWithStringArray.destroyAll(done); | ||
| }); | ||
|
|
||
| it('should support unit-level SQL building verification for jsonContains', function() { | ||
| const where = db.connector.buildWhere( | ||
| 'ItemWithJson', | ||
| {tags: {jsonContains: ['bar', 'foo']}}, | ||
| ); | ||
|
|
||
| where.toJSON().should.eql({ | ||
| sql: 'WHERE ("tags" @> CAST(? AS JSONB) OR "tags" @> CAST(? AS JSONB))', | ||
| params: ['["bar"]', '["foo"]'], | ||
| }); | ||
| }); | ||
|
|
||
| it('should support unit-level SQL building verification for jsonContains with integer array', function() { | ||
| const where = db.connector.buildWhere( | ||
| 'ItemWithIntegerArray', | ||
| {tags: {jsonContains: [1, 2]}}, | ||
| ); | ||
|
|
||
| where.toJSON().should.eql({ | ||
| sql: 'WHERE ("tags" @> CAST(? AS JSONB) OR "tags" @> CAST(? AS JSONB))', | ||
| params: ['[1]', '[2]'], | ||
| }); | ||
| }); | ||
|
|
||
| it('should support unit-level SQL building verification for jsonContains with string array', function() { | ||
| const where = db.connector.buildWhere( | ||
| 'ItemWithStringArray', | ||
| {tags: {jsonContains: ['bar', 'foo']}}, | ||
| ); | ||
|
|
||
| where.toJSON().should.eql({ | ||
| sql: 'WHERE ("tags" @> CAST(? AS JSONB) OR "tags" @> CAST(? AS JSONB))', | ||
| params: ['["bar"]', '["foo"]'], | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Serial properties', function() { | ||
|
|
||
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.
We need to define the public semantics of
jsonContainsbefore 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 rewritinginqintojsonContains, this risks different results across connectors for the same filter. Can we preserve theinqcontract explicitly and establish consistent operator semantics across the relevant connectors?