Skip to content

Commit c11c37a

Browse files
committed
feat: added not option for WhereQueryBuilder
1 parent d37ad45 commit c11c37a

2 files changed

Lines changed: 91 additions & 10 deletions

File tree

lib/database/utilities/QueryBuilder.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module.exports = (sequelize, Sequelize) => {
2727
constructor(queryBuilder, column) {
2828
this.queryBuilder = queryBuilder;
2929
this.column = column;
30+
31+
this.notFlag = false;
3032
}
3133

3234
/**
@@ -40,7 +42,16 @@ module.exports = (sequelize, Sequelize) => {
4042
this.queryBuilder.options.where = {};
4143
}
4244

43-
this.queryBuilder.options.where[this.column] = value;
45+
if (this.notFlag) {
46+
this.queryBuilder.options.where[this.column] = {
47+
[Op.ne]: value,
48+
};
49+
} else {
50+
this.queryBuilder.options.where[this.column] = {
51+
[Op.eq]: value,
52+
};
53+
}
54+
4455
return this.queryBuilder;
4556
}
4657

@@ -55,9 +66,15 @@ module.exports = (sequelize, Sequelize) => {
5566
this.queryBuilder.options.where = {};
5667
}
5768

58-
this.queryBuilder.options.where[this.column] = {
59-
[Op.and]: values,
60-
};
69+
if (this.notFlag) {
70+
this.queryBuilder.options.where[this.column] = {
71+
[Op.notIn]: values,
72+
};
73+
} else {
74+
this.queryBuilder.options.where[this.column] = {
75+
[Op.and]: values,
76+
};
77+
}
6178

6279
return this.queryBuilder;
6380
}
@@ -73,9 +90,15 @@ module.exports = (sequelize, Sequelize) => {
7390
this.queryBuilder.options.where = {};
7491
}
7592

76-
this.queryBuilder.options.where[this.column] = {
77-
[Op.in]: values,
78-
};
93+
if (this.notFlag) {
94+
this.queryBuilder.options.where[this.column] = {
95+
[Op.notIn]: values,
96+
};
97+
} else {
98+
this.queryBuilder.options.where[this.column] = {
99+
[Op.in]: values,
100+
};
101+
}
79102

80103
return this.queryBuilder;
81104
}
@@ -92,12 +115,28 @@ module.exports = (sequelize, Sequelize) => {
92115
this.queryBuilder.options.where = {};
93116
}
94117

95-
this.queryBuilder.options.where[this.column] = {
96-
[Op.between]: [start, end],
97-
};
118+
if (this.notFlag) {
119+
this.queryBuilder.options.where[this.column] = {
120+
[Op.notBetween]: [start, end],
121+
};
122+
} else {
123+
this.queryBuilder.options.where[this.column] = {
124+
[Op.between]: [start, end],
125+
};
126+
}
98127

99128
return this.queryBuilder;
100129
}
130+
131+
/**
132+
* Sets the **NOT** flag to `true` for the next filter condition.
133+
*
134+
* @returns {WhereQueryBuilder} The current WhereQueryBuilder instance.
135+
*/
136+
not() {
137+
this.notFlag = true;
138+
return this;
139+
}
101140
}
102141

103142
/**

test/database/utilities/QueryBuilder.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,48 @@ const { expect } = chai;
2525

2626
module.exports = () => {
2727
describe('WhereQueryBuilder', () => {
28+
describe('not', () => {
29+
it('should return a single entity which does not have the provided id', async () => {
30+
const queryBuilder = new QueryBuilder();
31+
queryBuilder.where('id').not().is('1');
32+
33+
const result = await LogRepository.findOne(queryBuilder);
34+
expect(result).to.not.be.null;
35+
expect(result.id).to.equal(2);
36+
});
37+
38+
it('should return entities not with the id in the provided range', async () => {
39+
const queryBuilder = new QueryBuilder();
40+
queryBuilder.where('id').not().between(1, 3);
41+
42+
const result = await LogRepository.findAll(queryBuilder);
43+
expect(result).to.not.be.null;
44+
expect(result).to.have.lengthOf(2);
45+
expect(result[0].id).to.equal(4);
46+
expect(result[1].id).to.equal(5);
47+
});
48+
49+
it('should return a single entity with the provided id', async () => {
50+
const queryBuilder = new QueryBuilder();
51+
queryBuilder.where('id').not().oneOf('1', 2);
52+
queryBuilder.orderBy('id', 'asc');
53+
54+
const result = await LogRepository.findOne(queryBuilder);
55+
expect(result).to.not.be.null;
56+
expect(result.id).to.equal(3);
57+
});
58+
59+
it('should return a single entity with the provided id', async () => {
60+
const queryBuilder = new QueryBuilder();
61+
queryBuilder.where('id').not().allOf('1', 2);
62+
queryBuilder.orderBy('id', 'asc');
63+
64+
const result = await LogRepository.findOne(queryBuilder);
65+
expect(result).to.not.be.null;
66+
expect(result.id).to.equal(3);
67+
});
68+
});
69+
2870
describe('between', () => {
2971
it('should return entities with the id in the provided range', async () => {
3072
const queryBuilder = new QueryBuilder();

0 commit comments

Comments
 (0)