Skip to content

Querying through relations #76

Open

Description

@scott-taj

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!

Activity

  1. jmdobry commented on Nov 10, 2016

    @jmdobry
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions