Search code examples
javascriptsqlnode.jsknex.js

construct inner join with AND using Knex.js in Node application


Is ti possible to write a join similar to this SQL join using knex.js in a Node application?

INNER JOIN member_staff ON members.id = member_staff.member_id AND staff.id = member_staff.staff_id

I assume it would look something like this(although this doesn't work and I can't find any docs which explain how to do what I want):

.innerJoin('member_staff', 'members.id', 'member_staff.member_id').onAnd('staff.id', 'member_staff.staff_id')

Solution

  • This is what I was looking for:

    .innerJoin('member_staff', function() {
        this.on('member.id', 'member_staff.member_id')
        .andOn('staff.id', 'member_staff.staff_id');
      })