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
31 changes: 31 additions & 0 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
82 changes: 82 additions & 0 deletions test/references-many-find.test.js
Original file line number Diff line number Diff line change
@@ -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]}}});
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"experimentalDecorators": true,
"noImplicitAny": true,
"strictNullChecks": true,

"skipLibCheck": true,
"lib": ["es2018", "dom"],
"module": "commonjs",
"moduleResolution": "node",
Expand Down
Loading