Skip to content

Commit 5835b9a

Browse files
author
Enes SAHIN
committed
Creates leftJoin and rightJoin methods
1 parent 1cb12ae commit 5835b9a

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
- [groupBy() / having()](#groupby--having)
4545
- [Joins](#joins)
4646
- [join()](#join)
47+
- [leftJoin()](#leftjoin)
48+
- [rightJoin()](#rightjoin)
4749
- [Aggregate Functions](#aggregate-functions)
4850
- [count()](#count)
4951
- [min()](#min)
@@ -406,7 +408,6 @@ authorBooks.then( results => {
406408

407409
## Joins
408410

409-
Currently **fluent-mysql** supports only inner join.
410411

411412
### join()
412413

@@ -424,6 +425,32 @@ users.then( results => {
424425
});
425426
```
426427

428+
### leftJoin()
429+
430+
```js
431+
let users = DB.table('users')
432+
.leftJoin('contacts', 'users.id', '=', 'contacts.user_id')
433+
.select('users.*', 'contacts.phone')
434+
.get();
435+
436+
users.then( results => {
437+
//...
438+
});
439+
```
440+
441+
### rightJoin()
442+
443+
```js
444+
let users = DB.table('users')
445+
.rightJoin('contacts', 'users.id', '=', 'contacts.user_id')
446+
.select('users.*', 'contacts.phone')
447+
.get();
448+
449+
users.then( results => {
450+
//...
451+
});
452+
```
453+
427454
## Aggregate Functions
428455

429456
You may call any of these methods after constructing your query. `min`, `max`, `avg` and `sum` methods take two parameters. First is the name of column in database. Second one is the column name after query. It may be anything you want. Second parameter is optional.

index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const mysql = require('mysql');
3030
* @property {boolean} fetchFirst
3131
* @property {boolean} existsQuery
3232
* @property {array} joins
33+
* @property {array} leftJoins
34+
* @property {array} rightJoins
3335
* @property {string} groupByColumn
3436
* @property {array} havingArray
3537
* @property {number} limitNumber
@@ -63,6 +65,8 @@ class Database {
6365
this.fetchFirst = false;
6466
this.existsQuery = false;
6567
this.joins = [];
68+
this.leftJoins = [];
69+
this.rightJoins = [];
6670
this.groupByColumn = null;
6771
this.havingArray = [];
6872
this.limitNumber = null;
@@ -126,11 +130,25 @@ class Database {
126130
*/
127131
getJoinStatement = () => {
128132
let joinStatement = '';
133+
129134
if(this.tableName != null && this.tableName != undefined && this.joins.length) {
130135
for (let index = 0; index < this.joins.length; index++) {
131136
joinStatement += ' INNER JOIN '+this.joins[index][0]+' ON '+this.joins[index][1] + ' '+this.joins[index][2] + ' ' + this.joins[index][3]+' ';
132137
}
133138
}
139+
140+
if(this.tableName != null && this.tableName != undefined && this.leftJoins.length) {
141+
for (let index = 0; index < this.leftJoins.length; index++) {
142+
joinStatement += ' LEFT JOIN '+this.leftJoins[index][0]+' ON '+this.leftJoins[index][1] + ' '+this.leftJoins[index][2] + ' ' + this.leftJoins[index][3]+' ';
143+
}
144+
}
145+
146+
if(this.tableName != null && this.tableName != undefined && this.rightJoins.length) {
147+
for (let index = 0; index < this.rightJoins.length; index++) {
148+
joinStatement += ' RIGHT JOIN '+this.rightJoins[index][0]+' ON '+this.rightJoins[index][1] + ' '+this.rightJoins[index][2] + ' ' + this.rightJoins[index][3]+' ';
149+
}
150+
}
151+
134152
return joinStatement;
135153
}
136154

@@ -778,6 +796,28 @@ class Database {
778796
return this;
779797
}
780798

799+
/**
800+
* Add left join arguments into leftJoins array
801+
*
802+
* @param {array} args
803+
* @return {Database}
804+
*/
805+
leftJoin = (...args) => {
806+
this.leftJoins.push(args);
807+
return this;
808+
}
809+
810+
/**
811+
* Add right join arguments into rightJoins array
812+
*
813+
* @param {array} args
814+
* @return {Database}
815+
*/
816+
rightJoin = (...args) => {
817+
this.rightJoins.push(args);
818+
return this;
819+
}
820+
781821
/**
782822
* Sets group by column
783823
*
@@ -917,6 +957,8 @@ class Database {
917957
this.fetchFirst = false;
918958
this.existsQuery = false;
919959
this.joins = [];
960+
this.leftJoins = [];
961+
this.rightJoins = [];
920962
this.groupByColumn = null;
921963
this.havingArray = [];
922964
this.limitNumber = null;

0 commit comments

Comments
 (0)