Created
May 22, 2017 20:10
-
-
Save miporto/217142519444d3b7235dcc1547cc359d to your computer and use it in GitHub Desktop.
Knex InnerJoin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dbRaw | |
.from(tables.users) | |
.select('users.*', dbRaw.raw('array_agg(users_users."friend_id") as friends')) | |
.where('id', 1) | |
.leftJoin(tables.users_users, 'users.id', 'users_users.user_id') | |
.groupBy('id') | |
.then((user) => { | |
res.status(200).json(user); | |
}); | |
const testUser = (req, res) => { | |
dbRaw | |
.with('user_friends', (qb) => { | |
qb | |
.with('friends_ids', (qb2) => { | |
qb2 | |
.select('*') | |
.from('users') | |
.leftJoin(tables.users_users, 'users.id', 'users_users.user_id') | |
}) | |
.select('*', dbRaw.raw('row_to_json(users.*) as contact')) | |
.from('friends_ids') | |
.leftJoin(tables.users, 'friends_ids.friend_id', 'users.id') | |
}) | |
.from('user_friends') | |
// .where('id', 1) | |
.select('*') | |
// .select('users.*', dbRaw.raw('array_agg(users_users."friend_id") as friends')) | |
// .where('id', 1) | |
// .leftJoin(tables.users_users, 'users.id', 'users_users.user_id') | |
// .groupBy('id') | |
.then((user) => { | |
res.status(200).json(user); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment