Skip to content

Commit

Permalink
feat: added not option for WhereQueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed Jun 3, 2020
1 parent d37ad45 commit c11c37a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
59 changes: 49 additions & 10 deletions lib/database/utilities/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.exports = (sequelize, Sequelize) => {
constructor(queryBuilder, column) {
this.queryBuilder = queryBuilder;
this.column = column;

this.notFlag = false;
}

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

this.queryBuilder.options.where[this.column] = value;
if (this.notFlag) {
this.queryBuilder.options.where[this.column] = {
[Op.ne]: value,
};
} else {
this.queryBuilder.options.where[this.column] = {
[Op.eq]: value,
};
}

return this.queryBuilder;
}

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

this.queryBuilder.options.where[this.column] = {
[Op.and]: values,
};
if (this.notFlag) {
this.queryBuilder.options.where[this.column] = {
[Op.notIn]: values,
};
} else {
this.queryBuilder.options.where[this.column] = {
[Op.and]: values,
};
}

return this.queryBuilder;
}
Expand All @@ -73,9 +90,15 @@ module.exports = (sequelize, Sequelize) => {
this.queryBuilder.options.where = {};
}

this.queryBuilder.options.where[this.column] = {
[Op.in]: values,
};
if (this.notFlag) {
this.queryBuilder.options.where[this.column] = {
[Op.notIn]: values,
};
} else {
this.queryBuilder.options.where[this.column] = {
[Op.in]: values,
};
}

return this.queryBuilder;
}
Expand All @@ -92,12 +115,28 @@ module.exports = (sequelize, Sequelize) => {
this.queryBuilder.options.where = {};
}

this.queryBuilder.options.where[this.column] = {
[Op.between]: [start, end],
};
if (this.notFlag) {
this.queryBuilder.options.where[this.column] = {
[Op.notBetween]: [start, end],
};
} else {
this.queryBuilder.options.where[this.column] = {
[Op.between]: [start, end],
};
}

return this.queryBuilder;
}

/**
* Sets the **NOT** flag to `true` for the next filter condition.
*
* @returns {WhereQueryBuilder} The current WhereQueryBuilder instance.
*/
not() {
this.notFlag = true;
return this;
}
}

/**
Expand Down
42 changes: 42 additions & 0 deletions test/database/utilities/QueryBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,48 @@ const { expect } = chai;

module.exports = () => {
describe('WhereQueryBuilder', () => {
describe('not', () => {
it('should return a single entity which does not have the provided id', async () => {
const queryBuilder = new QueryBuilder();
queryBuilder.where('id').not().is('1');

const result = await LogRepository.findOne(queryBuilder);
expect(result).to.not.be.null;
expect(result.id).to.equal(2);
});

it('should return entities not with the id in the provided range', async () => {
const queryBuilder = new QueryBuilder();
queryBuilder.where('id').not().between(1, 3);

const result = await LogRepository.findAll(queryBuilder);
expect(result).to.not.be.null;
expect(result).to.have.lengthOf(2);
expect(result[0].id).to.equal(4);
expect(result[1].id).to.equal(5);
});

it('should return a single entity with the provided id', async () => {
const queryBuilder = new QueryBuilder();
queryBuilder.where('id').not().oneOf('1', 2);
queryBuilder.orderBy('id', 'asc');

const result = await LogRepository.findOne(queryBuilder);
expect(result).to.not.be.null;
expect(result.id).to.equal(3);
});

it('should return a single entity with the provided id', async () => {
const queryBuilder = new QueryBuilder();
queryBuilder.where('id').not().allOf('1', 2);
queryBuilder.orderBy('id', 'asc');

const result = await LogRepository.findOne(queryBuilder);
expect(result).to.not.be.null;
expect(result.id).to.equal(3);
});
});

describe('between', () => {
it('should return entities with the id in the provided range', async () => {
const queryBuilder = new QueryBuilder();
Expand Down

0 comments on commit c11c37a

Please sign in to comment.