Skip to content

Commit 47b3abc

Browse files
author
Nino van Galen
committed
feat: added startsWith and endsWith filter in QueryBuilder
1 parent 9785e7d commit 47b3abc

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

lib/database/utilities/QueryBuilder.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,54 @@ module.exports = (sequelize) => {
114114
return this;
115115
}
116116

117+
/**
118+
* Sets a starts with filter using the provided value.
119+
*
120+
* @param {symbol} value The start value.
121+
* @returns {QueryBuilder} The current QueryBuilder instance.
122+
*/
123+
startsWith(value) {
124+
if (!this.queryBuilder.options.where) {
125+
this.queryBuilder.options.where = {};
126+
}
127+
128+
if (this.notFlag) {
129+
this.queryBuilder.options.where[this.column] = {
130+
[Op.notLike]: `${value}%`,
131+
};
132+
} else {
133+
this.queryBuilder.options.where[this.column] = {
134+
[Op.startsWith]: value,
135+
};
136+
}
137+
138+
return this.queryBuilder;
139+
}
140+
141+
/**
142+
* Sets a ends with filter using the provided value.
143+
*
144+
* @param {symbol} value The end value.
145+
* @returns {QueryBuilder} The current QueryBuilder instance.
146+
*/
147+
endsWith(value) {
148+
if (!this.queryBuilder.options.where) {
149+
this.queryBuilder.options.where = {};
150+
}
151+
152+
if (this.notFlag) {
153+
this.queryBuilder.options.where[this.column] = {
154+
[Op.notLike]: `%${value}`,
155+
};
156+
} else {
157+
this.queryBuilder.options.where[this.column] = {
158+
[Op.endsWith]: value,
159+
};
160+
}
161+
162+
return this.queryBuilder;
163+
}
164+
117165
/**
118166
* Sets the operation.
119167
*

test/database/utilities/QueryBuilder.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,45 @@ module.exports = () => {
117117
expect(result[1].id).to.equal(2);
118118
});
119119
});
120+
121+
describe('startsWith', () => {
122+
it('should return a single entity of which the title starts with ...', async () => {
123+
const queryBuilder = new QueryBuilder()
124+
.where('text').startsWith('Power interruption')
125+
.orderBy('id', 'asc');
126+
127+
const result = await LogRepository.findOne(queryBuilder);
128+
expect(result.text).to.equal('Power interruption due to unplugged wire.');
129+
});
130+
131+
it('should return a single entity of which the title not starts with ...', async () => {
132+
const queryBuilder = new QueryBuilder()
133+
.where('text').not().startsWith('Power interruption')
134+
.orderBy('id', 'asc');
135+
136+
const result = await LogRepository.findOne(queryBuilder);
137+
expect(result.text).to.equal('Detected particle ABC123');
138+
});
139+
});
140+
141+
describe('endsWith', () => {
142+
it('should return a single entity of which the title ends with ...', async () => {
143+
const queryBuilder = new QueryBuilder()
144+
.where('text').endsWith('accelerator!')
145+
.orderBy('id', 'asc');
146+
147+
const result = await LogRepository.findOne(queryBuilder);
148+
expect(result.text).to.equal('Cake at the particle accelerator!');
149+
});
150+
151+
it('should return a single entity of which the title not ends with ...', async () => {
152+
const queryBuilder = new QueryBuilder()
153+
.where('text').not().endsWith('accelerator!')
154+
.orderBy('id', 'asc');
155+
156+
const result = await LogRepository.findOne(queryBuilder);
157+
expect(result.text).to.equal('Power interruption due to unplugged wire.');
158+
});
159+
});
120160
});
121161
};

0 commit comments

Comments
 (0)