diff --git a/lib/dao.js b/lib/dao.js index b80c30d3b..3bfaf33e5 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1762,6 +1762,19 @@ DataAccessObject.all = function() { */ DataAccessObject.find = function find(query, options, cb) { + let relations; + if (this.relations && Object.keys(this.relations).length) { + relations = this.relations; + } else if ( + this.definition && + this.definition.settings && + this.definition.settings.relations + ) { + relations = this.definition.settings.relations; + } + if (query && query.where && relations) { + transformReferencesManyInq(query.where, relations); + } const connectionPromise = stillConnecting(this.getDataSource(), this, arguments); if (connectionPromise) { return connectionPromise; @@ -1987,6 +2000,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; } diff --git a/test/references-many-find.test.js b/test/references-many-find.test.js new file mode 100644 index 000000000..a7c725e70 --- /dev/null +++ b/test/references-many-find.test.js @@ -0,0 +1,183 @@ +'use strict'; + +describe('DataAccessObject.find with referencesMany relations', function() { + let db, Post, connector; + let interceptedQuery; + let originalAllDescriptor; + + before(function() { + /* global getSchema:false */ + db = getSchema(); + + db.define('Tag', { + id: {type: Number, id: true}, + name: String, + }); + + db.define('Category', { + id: {type: Number, id: true}, + name: String, + }); + + Post = db.define('Post', { + id: {type: Number, id: true}, + title: String, + tagIds: {type: Array, sqliteType: 'JSON'}, + categoryIds: {type: Array, sqliteType: 'JSON'}, + }, { + relations: { + tags: { + type: 'referencesMany', + 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; + }); + + function interceptConnectorAll() { + originalAllDescriptor = Object.getOwnPropertyDescriptor( + connector, + 'all', + ); + + 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]}, + }, + }; + + await Post.find(query, {model: Post}); + + interceptedQuery.should.deepEqual({ + where: { + tagIds: {jsonContains: [1, 2]}, + }, + }); + }); + it('transforms multiple referencesMany properties', async function() { + interceptConnectorAll(); + + const query = { + where: { + tagIds: {inq: [1, 2]}, + categoryIds: {inq: [3, 4]}, + }, + }; + + 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: { + 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']}, + }, + }; + + await Post.find(query, {model: Post}); + + interceptedQuery.should.deepEqual(query); + }); + it('does not transform non-inq operators on referencesMany properties', async function() { + interceptConnectorAll(); + + const query = { + where: { + tagIds: {eq: [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]}, + }, + }; + + await Post.find(query); + + interceptedQuery.should.deepEqual({ + where: { + tagIds: {jsonContains: [1, 2]}, + }, + }); + }); +});