From ef21e83cbe3e2ee0a8867a8effded233253f9144 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sat, 31 Oct 2026 20:15:35 +0500 Subject: [PATCH 1/5] fix: handle inq for referencesMany Signed-off-by: Muhammad Aaqil --- lib/dao.js | 31 ++++++++++++ test/references-many-find.test.js | 82 +++++++++++++++++++++++++++++++ tsconfig.json | 2 +- 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 test/references-many-find.test.js diff --git a/lib/dao.js b/lib/dao.js index b80c30d3b..ffe67ed43 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1762,6 +1762,37 @@ DataAccessObject.all = function() { */ DataAccessObject.find = function find(query, options, cb) { + let relations = null; + if (options && options.model) { + if (options.model.relations) { + relations = options.model.relations; + } else if (options.model.settings && options.model.settings.relations) { + relations = options.model.settings.relations; + } else if (options.model.definition && options.model.definition.relations) { + relations = options.model.definition.relations; + } + } + if ((!relations || !Object.keys(relations).length) && this) { + if (this.settings && this.settings.relations) { + relations = this.settings.relations; + } else if (this.definition && this.definition.relations) { + relations = this.definition.relations; + } else if (this.relations) { + relations = this.relations; + } + } + if (query && query.where && relations) { + for (const key in query.where) { + // Find if the current 'where' key matches a referencesMany relation's keyFrom + const matchingRelation = Object.values(relations).find( + r => r.type === 'referencesMany' && r.keyFrom === key, + ); + if (matchingRelation && query.where[key] && query.where[key].inq) { + // Mutate the operator from 'inq' to 'jsonContains' + query.where[key] = {jsonContains: query.where[key].inq}; + } + } + } const connectionPromise = stillConnecting(this.getDataSource(), this, arguments); if (connectionPromise) { return connectionPromise; diff --git a/test/references-many-find.test.js b/test/references-many-find.test.js new file mode 100644 index 000000000..15b908a2b --- /dev/null +++ b/test/references-many-find.test.js @@ -0,0 +1,82 @@ +'use strict'; + +describe('DataAccessObject.find with referencesMany relations', function() { + let db, Post, Tag; + + before(function() { + /* global getSchema:false, connectorCapabilities:false */ + db = getSchema(); + + Tag = db.define('Tag', { + id: {type: Number, id: true}, + name: String, + }); + + Post = db.define('Post', { + id: {type: Number, id: true}, + title: String, + tagIds: {type: Array, sqliteType: 'JSON'}, + }, { + relations: { + tags: { + type: 'referencesMany', + model: 'Tags', + keyFrom: 'tagIds', + }, + }, + }); + }); + + beforeEach(async function() { + await db.automigrate(); + }); + + it('mutates {inq: [...] } to {jsonContains: [...] } for referencesMany keyFrom fields', async function() { + const connector = db.connector; + let interceptedQuery; + + // Stub connector.all to inspect the processed query + connector.all = function(model, query, options, cb) { + interceptedQuery = query; + cb(null, []); + }; + + const query = {where: {tagIds: {inq: [1, 2]}}}; + + await Post.find(query, {model: Post}); + interceptedQuery.should.deepEqual({where: {tagIds: {jsonContains: [1, 2]}}}); + }); + + it('does not mutate non-inq operators on referencesMany keyFrom fields', async function() { + const connector = db.connector; + let interceptedQuery; + + connector.all = function(model, query, options, cb) { + interceptedQuery = query; + cb(null, []); + }; + + const query = {where: {tagIds: {eq: [1, 2]}}}; + + await Post.find(query, {model: Post}); + + interceptedQuery.should.deepEqual({where: {tagIds: {eq: [1, 2]}}}); + }); + + it('handles queries without options.model gracefully without throwing', async function() { + const connector = db.connector; + let interceptedQuery; + + connector.all = function(model, query, options, cb) { + interceptedQuery = query; + cb(null, []); + }; + + const query = {where: {tagIds: {inq: [1, 2]}}}; + + // Calling without options.model + await Post.find(query); + + interceptedQuery.should.deepEqual({where: {tagIds: {jsonContains: [1, 2]}}}); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index e3cfa08b7..62d8519c1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "experimentalDecorators": true, "noImplicitAny": true, "strictNullChecks": true, - + "skipLibCheck": true, "lib": ["es2018", "dom"], "module": "commonjs", "moduleResolution": "node", From b864cf7ae4f121f4805bc8112918534fde73fdb2 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Fri, 4 Dec 2026 21:57:02 +0500 Subject: [PATCH 2/5] fix: hanlde nested inq Signed-off-by: Muhammad Aaqil --- lib/dao.js | 58 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index ffe67ed43..1a1e9b7c7 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1762,15 +1762,16 @@ DataAccessObject.all = function() { */ DataAccessObject.find = function find(query, options, cb) { - let relations = null; - if (options && options.model) { - if (options.model.relations) { - relations = options.model.relations; - } else if (options.model.settings && options.model.settings.relations) { - relations = options.model.settings.relations; - } else if (options.model.definition && options.model.definition.relations) { - relations = options.model.definition.relations; - } + let relations; + if (this.relations && Object.keys(this.relations).length) { + relations = this.relations; + } else if ( + options && + options.model && + options.model.definition && + options.model.definition.relations + ) { + relations = options.model.definition.relations; } if ((!relations || !Object.keys(relations).length) && this) { if (this.settings && this.settings.relations) { @@ -1782,16 +1783,7 @@ DataAccessObject.find = function find(query, options, cb) { } } if (query && query.where && relations) { - for (const key in query.where) { - // Find if the current 'where' key matches a referencesMany relation's keyFrom - const matchingRelation = Object.values(relations).find( - r => r.type === 'referencesMany' && r.keyFrom === key, - ); - if (matchingRelation && query.where[key] && query.where[key].inq) { - // Mutate the operator from 'inq' to 'jsonContains' - query.where[key] = {jsonContains: query.where[key].inq}; - } - } + transformReferencesManyInq(query.where, relations); } const connectionPromise = stillConnecting(this.getDataSource(), this, arguments); if (connectionPromise) { @@ -2018,6 +2010,34 @@ DataAccessObject.find = function find(query, options, cb) { return cb.promise; }; +function transformReferencesManyInq(where, relations) { + if (!where || typeof where !== 'object' || !relations) { + return; + } + + if (Array.isArray(where)) { + where.forEach(item => transformReferencesManyInq(item, relations)); + return; + } + + for (const key in where) { + const value = where[key]; + + if (key === 'and' || key === 'or' || key === 'not') { + transformReferencesManyInq(value, relations); + continue; + } + + const matchingRelation = Object.values(relations).find( + r => r.type === 'referencesMany' && r.keyFrom === key, + ); + + if (matchingRelation && value && Array.isArray(value.inq)) { + where[key] = {jsonContains: value.inq}; + } + } +} + function isDefined(value) { return value !== undefined; } From 0d8c7e2c6e5c59343385ffc55b4032655dae98e4 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Fri, 4 Dec 2026 22:47:16 +0500 Subject: [PATCH 3/5] test: test nested inq to jsoncontains Signed-off-by: Muhammad Aaqil --- test/references-many-find.test.js | 157 ++++++++++++++++++++++++------ 1 file changed, 129 insertions(+), 28 deletions(-) diff --git a/test/references-many-find.test.js b/test/references-many-find.test.js index 15b908a2b..a7c725e70 100644 --- a/test/references-many-find.test.js +++ b/test/references-many-find.test.js @@ -1,13 +1,20 @@ 'use strict'; describe('DataAccessObject.find with referencesMany relations', function() { - let db, Post, Tag; + let db, Post, connector; + let interceptedQuery; + let originalAllDescriptor; before(function() { - /* global getSchema:false, connectorCapabilities:false */ + /* global getSchema:false */ db = getSchema(); - Tag = db.define('Tag', { + db.define('Tag', { + id: {type: Number, id: true}, + name: String, + }); + + db.define('Category', { id: {type: Number, id: true}, name: String, }); @@ -16,67 +23,161 @@ describe('DataAccessObject.find with referencesMany relations', function() { id: {type: Number, id: true}, title: String, tagIds: {type: Array, sqliteType: 'JSON'}, + categoryIds: {type: Array, sqliteType: 'JSON'}, }, { relations: { tags: { type: 'referencesMany', - model: 'Tags', + model: 'Tag', keyFrom: 'tagIds', }, + categories: { + type: 'referencesMany', + model: 'Category', + keyFrom: 'categoryIds', + }, }, }); }); beforeEach(async function() { await db.automigrate(); + + connector = db.connector; + interceptedQuery = undefined; + }); + + afterEach(function() { + if (originalAllDescriptor) { + Object.defineProperty(connector, 'all', originalAllDescriptor); + } else { + delete connector.all; + } + + originalAllDescriptor = undefined; }); - it('mutates {inq: [...] } to {jsonContains: [...] } for referencesMany keyFrom fields', async function() { - const connector = db.connector; - let interceptedQuery; + function interceptConnectorAll() { + originalAllDescriptor = Object.getOwnPropertyDescriptor( + connector, + 'all', + ); - // Stub connector.all to inspect the processed query connector.all = function(model, query, options, cb) { interceptedQuery = query; cb(null, []); }; + } + it('transforms inq conditions on referencesMany properties', async function() { + interceptConnectorAll(); - const query = {where: {tagIds: {inq: [1, 2]}}}; + const query = { + where: { + tagIds: {inq: [1, 2]}, + }, + }; await Post.find(query, {model: Post}); - interceptedQuery.should.deepEqual({where: {tagIds: {jsonContains: [1, 2]}}}); - }); - it('does not mutate non-inq operators on referencesMany keyFrom fields', async function() { - const connector = db.connector; - let interceptedQuery; + interceptedQuery.should.deepEqual({ + where: { + tagIds: {jsonContains: [1, 2]}, + }, + }); + }); + it('transforms multiple referencesMany properties', async function() { + interceptConnectorAll(); - connector.all = function(model, query, options, cb) { - interceptedQuery = query; - cb(null, []); + const query = { + where: { + tagIds: {inq: [1, 2]}, + categoryIds: {inq: [3, 4]}, + }, }; - const query = {where: {tagIds: {eq: [1, 2]}}}; + await Post.find(query, {model: Post}); + + interceptedQuery.should.deepEqual({ + where: { + tagIds: {jsonContains: [1, 2]}, + categoryIds: {jsonContains: [3, 4]}, + }, + }); + }); + it('transforms nested and/or conditions', async function() { + interceptConnectorAll(); + + const query = { + where: { + and: [ + {tagIds: {inq: [1, 2]}}, + { + or: [ + {categoryIds: {inq: [3, 4]}}, + {title: 'Example'}, + ], + }, + ], + }, + }; await Post.find(query, {model: Post}); - interceptedQuery.should.deepEqual({where: {tagIds: {eq: [1, 2]}}}); + interceptedQuery.should.deepEqual({ + where: { + and: [ + {tagIds: {jsonContains: [1, 2]}}, + { + or: [ + {categoryIds: {jsonContains: [3, 4]}}, + {title: 'Example'}, + ], + }, + ], + }, + }); }); + it('does not transform inq conditions on non-referencesMany properties', async function() { + interceptConnectorAll(); + + const query = { + where: { + title: {inq: ['First', 'Second']}, + }, + }; - it('handles queries without options.model gracefully without throwing', async function() { - const connector = db.connector; - let interceptedQuery; + await Post.find(query, {model: Post}); - connector.all = function(model, query, options, cb) { - interceptedQuery = query; - cb(null, []); + interceptedQuery.should.deepEqual(query); + }); + it('does not transform non-inq operators on referencesMany properties', async function() { + interceptConnectorAll(); + + const query = { + where: { + tagIds: {eq: [1, 2]}, + }, }; - const query = {where: {tagIds: {inq: [1, 2]}}}; + await Post.find(query, {model: Post}); + + interceptedQuery.should.deepEqual(query); + }); + it('handles queries without options.model gracefully', async function() { + interceptConnectorAll(); + + const query = { + where: { + tagIds: {inq: [1, 2]}, + }, + }; - // Calling without options.model await Post.find(query); - interceptedQuery.should.deepEqual({where: {tagIds: {jsonContains: [1, 2]}}}); + interceptedQuery.should.deepEqual({ + where: { + tagIds: {jsonContains: [1, 2]}, + }, + }); }); }); From ee11df04dc9ea46c2fe55fca3559343d0f3bac7f Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Fri, 4 Dec 2026 23:06:34 +0500 Subject: [PATCH 4/5] fix: remove dependency on rest-controller options Signed-off-by: Muhammad Aaqil --- lib/dao.js | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 1a1e9b7c7..3bfaf33e5 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1766,21 +1766,11 @@ DataAccessObject.find = function find(query, options, cb) { if (this.relations && Object.keys(this.relations).length) { relations = this.relations; } else if ( - options && - options.model && - options.model.definition && - options.model.definition.relations + this.definition && + this.definition.settings && + this.definition.settings.relations ) { - relations = options.model.definition.relations; - } - if ((!relations || !Object.keys(relations).length) && this) { - if (this.settings && this.settings.relations) { - relations = this.settings.relations; - } else if (this.definition && this.definition.relations) { - relations = this.definition.relations; - } else if (this.relations) { - relations = this.relations; - } + relations = this.definition.settings.relations; } if (query && query.where && relations) { transformReferencesManyInq(query.where, relations); From 8cb53b4f4903b1a45246a870cbcf47bfa20eafe5 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sat, 5 Dec 2026 08:42:15 +0500 Subject: [PATCH 5/5] fix: remove unnecessary change Signed-off-by: Muhammad Aaqil --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 62d8519c1..e3cfa08b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "experimentalDecorators": true, "noImplicitAny": true, "strictNullChecks": true, - "skipLibCheck": true, + "lib": ["es2018", "dom"], "module": "commonjs", "moduleResolution": "node",