Knex.join: table can be a subquery (#23177)

* Allow tableName in joins to be QueryCallback

* Add tests for join with callback as table
This commit is contained in:
Omar Diab
2018-01-29 15:36:21 -05:00
committed by Sheetal Nandi
parent fb4fc8d1c2
commit 23faf25636
2 changed files with 44 additions and 6 deletions

12
types/knex/index.d.ts vendored
View File

@@ -183,12 +183,12 @@ declare namespace Knex {
interface Join {
(raw: Raw): QueryBuilder;
(tableName: TableName, clause: (this: JoinClause, join: JoinClause) => void): QueryBuilder;
(tableName: TableName, columns: { [key: string]: string | number | Raw }): QueryBuilder;
(tableName: TableName, raw: Raw): QueryBuilder;
(tableName: TableName, column1: string, column2: string): QueryBuilder;
(tableName: TableName, column1: string, raw: Raw): QueryBuilder;
(tableName: TableName, column1: string, operator: string, column2: string): QueryBuilder;
(tableName: TableName | QueryCallback, clause: (this: JoinClause, join: JoinClause) => void): QueryBuilder;
(tableName: TableName | QueryCallback, columns: { [key: string]: string | number | Raw }): QueryBuilder;
(tableName: TableName | QueryCallback, raw: Raw): QueryBuilder;
(tableName: TableName | QueryCallback, column1: string, column2: string): QueryBuilder;
(tableName: TableName | QueryCallback, column1: string, raw: Raw): QueryBuilder;
(tableName: TableName | QueryCallback, column1: string, operator: string, column2: string): QueryBuilder;
}
interface JoinClause {

View File

@@ -459,6 +459,44 @@ knex.select('*').from('accounts').joinRaw('natural full join table1').where('id'
knex.select('*').from('accounts').join(knex.raw('natural full join table1')).where('id', 1);
knex.select('*').from('accounts')
.join(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.leftJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.leftOuterJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.rightJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.rightOuterJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.innerJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.crossJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.fullOuterJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex.select('*').from('accounts')
.outerJoin(function() {
this.select('*').from('accounts').as('special_accounts');
}, 'special_accounts.a', '=', 'accounts.b');
knex('customers')
.distinct('first_name', 'last_name')
.select();