knex: Add JoinClause 'on' variations (#20668)

* knex: Add JoinClause 'on' variations

* knex: onBetween's range parameter accepts an array with exactly two elements

* knex: Adds additional 'and' and 'or' methods for 'on' variations
This commit is contained in:
Travis Hill 2017-10-23 16:22:11 -04:00 committed by Sheetal Nandi
parent 912fde2dde
commit 8899268190
2 changed files with 137 additions and 0 deletions

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

@ -206,6 +206,30 @@ declare namespace Knex {
orOn(column1: string, column2: string): JoinClause;
orOn(column1: string, raw: Raw): JoinClause;
orOn(column1: string, operator: string, column2: string): JoinClause;
onIn(column1: string, values: any[]): JoinClause;
andOnIn(column1: string, values: any[]): JoinClause;
orOnIn(column1: string, values: any[]): JoinClause;
onNotIn(column1: string, values: any[]): JoinClause;
andOnNotIn(column1: string, values: any[]): JoinClause;
orOnNotIn(column1: string, values: any[]): JoinClause;
onNull(column1: string): JoinClause;
andOnNull(column1: string): JoinClause;
orOnNull(column1: string): JoinClause;
onNotNull(column1: string): JoinClause;
andOnNotNull(column1: string): JoinClause;
orOnNotNull(column1: string): JoinClause;
onExists(callback: () => void): JoinClause;
andOnExists(callback: () => void): JoinClause;
orOnExists(callback: () => void): JoinClause;
onNotExists(callback: () => void): JoinClause;
andOnNotExists(callback: () => void): JoinClause;
orOnNotExists(callback: () => void): JoinClause;
onBetween(column1: string, range: [any, any]): JoinClause;
andOnBetween(column1: string, range: [any, any]): JoinClause;
orOnBetween(column1: string, range: [any, any]): JoinClause;
onNotBetween(column1: string, range: [any, any]): JoinClause;
andOnNotBetween(column1: string, range: [any, any]): JoinClause;
orOnNotBetween(column1: string, range: [any, any]): JoinClause;
using(column: string | string[] | Raw | { [key: string]: string | Raw }): JoinClause;
type(type: string): JoinClause;
}

View File

@ -250,6 +250,119 @@ knex.select('*').from('users').join('accounts', function(join: Knex.JoinClause)
join.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id');
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onIn('contacts.id', [7, 15, 23, 41])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnIn('contacts.id', [7, 15, 23, 41])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnIn('contacts.id', [7, 15, 23, 41])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onNotIn('contacts.id', [7, 15, 23, 41])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnNotIn('contacts.id', [7, 15, 23, 41])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnNotIn('contacts.id', [7, 15, 23, 41])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onNull('contacts.email')
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnNull('contacts.email')
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnNull('contacts.email')
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onNotNull('contacts.email')
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnNotNull('contacts.email')
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnNotNull('contacts.email')
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onNotExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnNotExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnNotExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onBetween('contacts.id', [5, 30])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnBetween('contacts.id', [5, 30])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnBetween('contacts.id', [5, 30])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onNotBetween('contacts.id', [5, 30])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').andOnNotBetween('contacts.id', [5, 30])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').orOnNotBetween('contacts.id', [5, 30])
});
knex.select('*').from('users').join('contacts', function() {
this.on('users.id', '=', 'contacts.id').onNotExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
});
knex.select('*').from('users').join('accounts', (join: Knex.JoinClause) => {
join.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')