trunks
trunks

Reputation: 147

Objection JS get extra properties from joined table + filter

I have the following Model:

class Profile extends Model {

.......................

  static relationMappings = {
    finishers: {
      relation: Model.ManyToManyRelation,
      modelClass: Move,
      filter: (query) => query.select('move.id', 'move.type'),
      join: {
        from: 'profile.id',
        through: {
          from: 'profile_move.profile_id',
          to: 'profile_move.move_id',
          extra: {
            alternative_name: 'name',
          },
        },
        to: 'move.id',
      },
    },
  };
}

What I'm trying to do is to get only move.id & move.type from the move table and also the extra property name from the profile_move joined table. The problem is that if I use filter or a modifier it returns only move.id & move.type and not the extra property.

Route:

router.get('/', async (req, res, next) => {
  try {
    const data = await Profile.query()
      .orderBy('id')
      .withGraphFetched('finishers');

    res.json(data);
  } catch (error) {
    next(error);
  }
});

Upvotes: 1

Views: 1015

Answers (1)

trunks
trunks

Reputation: 147

The solution is to pass the extra property to filter:

filter: (query) => query.select('move.id', 'move.type', 'profile_move.name'),

Upvotes: 1

Related Questions