Skip to content

Commit

Permalink
fix: Fix using is/in in $nin subquery (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
johansteffner authored and jrf0110 committed Jun 26, 2018
1 parent b4427c1 commit 7c0172c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions helpers/conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ conditionals.add('$in', { cascade: false }, function(column, set, values, collec
*/
conditionals.add('$nin', { cascade: false }, function(column, set, values, collection, original){
return conditionals.get('$in').fn(column, set, values, collection, original)
.replace(/in/g, 'not in')
.replace(/is/g, 'is not');
.replace(new RegExp(column + ' in', 'g'), column + ' not in')
.replace(new RegExp(column + ' is', 'g'), column + ' is not');
});

/**
Expand Down
26 changes: 26 additions & 0 deletions test/conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,32 @@ describe('Conditions', function(){
assert.deepEqual(query.values , []);
});

it ('$nin with subquery', function(){
var query = builder.sql({
type: 'select'
, table: 'users'
, where: {
id: {
$nin: {
type: 'select'
, table: 'sub'
, columns: ['user_id']
, where: {
value: {
$null: false
}
}
}
}
}
});

assert.equal(
query.toString()
, 'select "users".* from "users" where "users"."id" not in (select "sub"."user_id" from "sub" where "sub"."value" is not null)'
);
});

it ('should allow an arbitrary amount of conditions', function(){
var query = builder.sql({
type: 'select'
Expand Down

0 comments on commit 7c0172c

Please sign in to comment.