Filter by properties of related models (use SQL JOIN in queries)
Ex: I have models Book and Category and i want to query Book with condition like below: Books with title contain "Art" and belongs to Category "Programming", please tell me how to do it ? I run this filter in Books model. GET /Books
{"include":{"categories"}, "where": {"title":{"like":"^Art"}, "categories.name":"Programming"}}
@pookdeveloper You mean that you use
typeormas a total replacement ofloopback? Or combined?
I use combined
@pookdeveloper Thank you, is somewhere a tutorial on how we can setup this solution? It seems that this can solve many problems that loopback has not addressed yet.
I will try today to do a tutorial
@pookdeveloper could you please give us an example? thanks very much!
@melote @afilp i'm writting a tutorial, i will send you the link
https://medium.com/@davidgarciasantes/typeorm-with-loopback-3-68b195cbe6de?source=friends_link&sk=56276d0cb3a319ef96d2996247b7d3f6
It's not hard to just override the _buildWhere method at boot time. For example (i use Oracle DB)
const set = require('lodash/set');
module.exports = function (app) {
const connector = app.dataSources.db.connector;
const _buildWhere = connector._buildWhere.bind(connector);
connector._buildWhere = function (model, where) {
for (var key in where) {
if (key.includes('.')) {
Object.assign(where, set({}, key, where[key]));
delete where[key];
}
}
let whereClause = _buildWhere(model, where);
const relations = app.models[model].relations;
for (const key in where) {
if (relations && relations[key]) {
const relation = relations[key];
const inSelect = this.buildSelect(relation.modelTo.modelName, {
fields: [relation.keyTo],
where: {
and: [
app.models[model]._coerce(where[key], {}, app.models[relation.modelTo.modelName].definition)
]
}
}, {parameterize: false, order: false});
whereClause = new ParameterizedSQL({
sql: whereClause.sql + (whereClause.sql !== '' ? ' AND ' : '') + this.columnEscaped(relation.modelFrom.modelName, relation.keyFrom) + ' IN (' + inSelect.sql + ')',
params: whereClause.params.concat(inSelect.params),
});
}
}
return whereClause;
}
}
And then GET /Books
{"include":{"categories"}, "where": {"title":{"like":"^Art"}, "categories.name":"Programming"}}
or just
{"where": {"categories.name":"Programming"}}
or even
{"where": {"models1.models2.models3.field":"someValue"}}
{"where":{"models1":{"and":[{"someField1":"someValue1"},{"someField2":"someValue2"}]}}}
It's not hard to just override the _buildWhere method at boot time. For example (i use Oracle DB)
const set = require('lodash/set'); module.exports = function (app) { const connector = app.dataSources.db.connector; const _buildWhere = connector._buildWhere.bind(connector); connector._buildWhere = function (model, where) { for (var key in where) { if (key.includes('.')) { Object.assign(where, set({}, key, where[key])); delete where[key]; } } let whereClause = _buildWhere(model, where); const relations = app.models[model].relations; for (const key in where) { if (relations && relations[key]) { const relation = relations[key]; const inSelect = this.buildSelect(relation.modelTo.modelName, { fields: [relation.keyTo], where: { and: [ app.models[model]._coerce(where[key], {}, app.models[relation.modelTo.modelName].definition) ] } }, {parameterize: false, order: false}); whereClause = new ParameterizedSQL({ sql: whereClause.sql + (whereClause.sql !== '' ? ' AND ' : '') + this.columnEscaped(relation.modelFrom.modelName, relation.keyFrom) + ' IN (' + inSelect.sql + ')', params: whereClause.params.concat(inSelect.params), }); } } return whereClause; } }And then GET /Books
{"include":{"categories"}, "where": {"title":{"like":"^Art"}, "categories.name":"Programming"}}or just
{"where": {"categories.name":"Programming"}}or even
{"where": {"models1.models2.models3.field":"someValue"}} {"where":{"models1":{"and":[{"someField1":"someValue1"},{"someField2":"someValue2"}]}}}
How to use it in loopback4?
It's not hard to just override the _buildWhere method at boot time. For example (i use Oracle DB)
const set = require('lodash/set'); module.exports = function (app) { const connector = app.dataSources.db.connector; const _buildWhere = connector._buildWhere.bind(connector); connector._buildWhere = function (model, where) { for (var key in where) { if (key.includes('.')) { Object.assign(where, set({}, key, where[key])); delete where[key]; } } let whereClause = _buildWhere(model, where); const relations = app.models[model].relations; for (const key in where) { if (relations && relations[key]) { const relation = relations[key]; const inSelect = this.buildSelect(relation.modelTo.modelName, { fields: [relation.keyTo], where: { and: [ app.models[model]._coerce(where[key], {}, app.models[relation.modelTo.modelName].definition) ] } }, {parameterize: false, order: false}); whereClause = new ParameterizedSQL({ sql: whereClause.sql + (whereClause.sql !== '' ? ' AND ' : '') + this.columnEscaped(relation.modelFrom.modelName, relation.keyFrom) + ' IN (' + inSelect.sql + ')', params: whereClause.params.concat(inSelect.params), }); } } return whereClause; } }And then GET /Books
{"include":{"categories"}, "where": {"title":{"like":"^Art"}, "categories.name":"Programming"}}or just
{"where": {"categories.name":"Programming"}}or even
{"where": {"models1.models2.models3.field":"someValue"}} {"where":{"models1":{"and":[{"someField1":"someValue1"},{"someField2":"someValue2"}]}}}How to use it in loopback4?
where does ParameterizedSQL come from?
@enisinanaj
const ParameterizedSQL = require('loopback-connector/lib/parameterized-sql');
Or perhaps the way to go is switch to typeORM ....
But at this time support for it in lb is at useless... nestjs ?
8 years and counting. Still, this basic feature is not there. If I would have known earlier about this, I would have not chosen LB4
I switched to nestjs + typeorm and no regrets