Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
183 changes: 183 additions & 0 deletions test/references-many-find.test.js
Original file line number Diff line number Diff line change
@@ -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) {

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.

These tests verify that the query object is transformed, but they don't verify that the transformed query actually works.

Could we add integration tests using real stored records to validate:

  • Single and multiple IDs
  • Expected match-any versus match-all behavior
  • Existing inq behavior with the memory connector
  • Nested logical filters
  • Actual query execution with the affected SQL connector

Also, we're overwriting connector.all without restoring it. We should restore the original method after each test to avoid affecting other tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added tests for multiple referencesMany properties, nested and/or conditions, and ensuring unrelated properties/operators remain unchanged.

For integration tests, I think we'll have to wait until these changes to mysql-connector become available.

For now, the DAO tests verify the query transformation; they cannot validate SQL generation or actual database matching behavior.

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]},
},
});
});
});
Loading