From 889926819089953a8fcb3a85f4f80d64d0e1982e Mon Sep 17 00:00:00 2001 From: Travis Hill Date: Mon, 23 Oct 2017 16:22:11 -0400 Subject: [PATCH] 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 --- types/knex/index.d.ts | 24 +++++++++ types/knex/knex-tests.ts | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/types/knex/index.d.ts b/types/knex/index.d.ts index 6b09871f76..974c3eb0a4 100644 --- a/types/knex/index.d.ts +++ b/types/knex/index.d.ts @@ -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; } diff --git a/types/knex/knex-tests.ts b/types/knex/knex-tests.ts index de5414eac0..95d28b1b2b 100644 --- a/types/knex/knex-tests.ts +++ b/types/knex/knex-tests.ts @@ -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')