Knex: joining multiple tables

Created on 28 Aug 2014  路  3Comments  路  Source: knex/knex

I don't see any docs or examples on joining more than one table. Is this possible by just chaining .join calls?

question

Most helpful comment

Chaining join (multiple tables join) in knex is possible.

For example:

var knex = require('./db')
knex('follow')
.join('users as u1', 'u1.id', 'follow.follower')
.join('users as u2', 'u2.id', 'follow.followee')
.select('u1.username as follower_name', 'u2.username as followee_name')
.then(follows => {
  follows.map((follow) => {
    console.log(follow.follower_name + " -> " + follow.followee_name);
  })
})

All 3 comments

Yep

Chaining join (multiple tables join) in knex is possible.

For example:

var knex = require('./db')
knex('follow')
.join('users as u1', 'u1.id', 'follow.follower')
.join('users as u2', 'u2.id', 'follow.followee')
.select('u1.username as follower_name', 'u2.username as followee_name')
.then(follows => {
  follows.map((follow) => {
    console.log(follow.follower_name + " -> " + follow.followee_name);
  })
})

@guanzhou-zhao how can this be mapped to lool like this...

{
 "userId": 1,
 ...
 "followers": [
  {"userId": 2, ...},
  ...
]
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

saurabhghewari picture saurabhghewari  路  3Comments

PaulOlteanu picture PaulOlteanu  路  3Comments

ghost picture ghost  路  3Comments

nklhrstv picture nklhrstv  路  3Comments

mattgrande picture mattgrande  路  3Comments