From ef21e83cbe3e2ee0a8867a8effded233253f9144 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sat, 31 Oct 2026 20:15:35 +0500 Subject: [PATCH] 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",