js-data-sql icon indicating copy to clipboard operation
js-data-sql copied to clipboard

Querying through relations

Open scott-taj opened this issue 9 years ago • 1 comments

I've been working on upgrading all of my js-data stuff to v3 and I've run into an issue with querying through relations. I'm using js-data/js-data-http (both v3) on the front end and js-data/js-data-sql (both v3) on my API. I have two mappers:


const User = db.dataHealth.store.defineMapper({
  name: 'user',
  tableName: 'user',
  relations: {
    belongsTo: {
      reseller: {
        localField: 'reseller',
        foreignKey: 'reseller_id'
      },
      customer: {
        localField: 'customer',
        foreignKey: 'customer_id'
      }
    },
    hasMany: {
      login: {
        localField: 'logins',
        foreignKey: 'user_id'
      }
    }
  }
});

And


const Customer = db.dataHealth.store.defineMapper({
  name: 'customer',
  tableName: 'customer',
  relations: {
    hasMany: {
      user: {
        localField: 'user',
        foreignKey: 'customer_id'
      },
      account: {
        localField: 'accounts',
        foreignKey: 'customer_id'
      }
    },
    belongsTo: {
      reseller: {
        localField: 'reseller',
        foreignKey: 'reseller_id'
      },
      customer_type: {
        localField: 'customer_type',
        foreignKey: 'customer_type_id',
      }
    }
  }
});

In js-data v2, I was able to do queries like the following:

User.findAll({where: {user.customer.name: 'foo'}});

In js-data v3, queries like this produce the error

missing FROM-clause entry for table "customer"

I really need this functionality. Any suggestions? Thanks!

scott-taj avatar Nov 02 '16 19:11 scott-taj

The querying through relations didn’t make it into js-data-sql 1.x because it didn't quite fit into way the new Base Adapter works. Porting that feature forward is on the roadmap, but if you’d like to try porting it that would be nice.

I'm event sure how the original feature worked as I didn't implement it. I'll try to take a look at it though.

jmdobry avatar Nov 10 '16 00:11 jmdobry