loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Filter by properties of related models (use SQL JOIN in queries)

Open bluestaralone opened this issue 12 years ago • 53 comments

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"}}

bluestaralone avatar Oct 23 '14 02:10 bluestaralone

@pookdeveloper You mean that you use typeorm as a total replacement of loopback? Or combined?

I use combined

pookdeveloper avatar May 10 '19 11:05 pookdeveloper

@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.

afilp avatar May 11 '19 07:05 afilp

I will try today to do a tutorial

pookdeveloper avatar May 11 '19 08:05 pookdeveloper

@pookdeveloper could you please give us an example? thanks very much!

melochale avatar May 11 '19 11:05 melochale

@melote @afilp i'm writting a tutorial, i will send you the link

pookdeveloper avatar May 12 '19 10:05 pookdeveloper

https://medium.com/@davidgarciasantes/typeorm-with-loopback-3-68b195cbe6de?source=friends_link&sk=56276d0cb3a319ef96d2996247b7d3f6

pookdeveloper avatar May 12 '19 10:05 pookdeveloper

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"}]}}}

alexdee2007 avatar Mar 21 '20 19:03 alexdee2007

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?

mituso89 avatar Oct 01 '20 09:10 mituso89

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 avatar Nov 16 '21 12:11 enisinanaj

@enisinanaj

const ParameterizedSQL = require('loopback-connector/lib/parameterized-sql');

alexdee2007 avatar Nov 16 '21 13:11 alexdee2007

Or perhaps the way to go is switch to typeORM ....

But at this time support for it in lb is at useless... nestjs ?

cantoute avatar Nov 16 '21 16:11 cantoute

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

pandayamitray avatar Jan 22 '22 15:01 pandayamitray

I switched to nestjs + typeorm and no regrets

cantoute avatar Jan 22 '22 16:01 cantoute