Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
✨ Added support for one negation case for relations
Browse files Browse the repository at this point in the history
refs #7

- `tags.slug:-animal`
  • Loading branch information
kirrg001 committed Nov 15, 2018
1 parent 811e4e6 commit 4a67b37
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ class MongoToKnex {

if (reference.config.type === 'manyToMany') {
if (isCompOp(reference.operator)) {
const comp = compOps[reference.operator] || '=';
const comp = reference.operator === '$ne' || reference.operator === '$nin' ? 'NOT IN' : 'IN';

// CASE: post.id IN (SELECT ...)
qb.where(`${this.tableName}.id`, 'IN', function () {
qb.where(`${this.tableName}.id`, comp, function () {
const innerQB = this
.select(`${reference.config.join_table}.${reference.config.join_from}`)
.from(`${reference.config.join_table}`)
Expand All @@ -173,7 +173,7 @@ class MongoToKnex {
_.each(statements, (value, key) => {
debug(`(buildRelationQuery) build relation where statements for ${key}`);

innerQB.where(`${value.join_table || value.table}.${value.column}`, comp, value.value);
innerQB.where(`${value.join_table || value.table}.${value.column}`, 'IN', !_.isArray(value.value) ? [value.value] : value.value);
});

return innerQB;
Expand Down
18 changes: 18 additions & 0 deletions test/integration/relations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ describe('Relations', function () {
});
});

describe('NEGATION', function () {
// should return posts without tags
// if a post has more than 1 tag, if one tag is animal, do not return
it('tags.slug is NOT "animal"', function () {
const mongoJSON = {
'tags.slug': {$ne: 'animal'}
};

const query = makeQuery(mongoJSON);

return query
.select()
.then((result) => {
result.should.be.an.Array().with.lengthOf(4);
});
});
});

describe('Multiple where clauses for relations', function () {
it('tags.slug equals "animal" and posts_tags.sort_order is 0 and featured is true', function () {
// where primary tag is "animal"
Expand Down

0 comments on commit 4a67b37

Please sign in to comment.