Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions helpers/conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function getValueInequalityOperator(value) {
return valuesThatUseIsOrIsNot.indexOf(value) > -1 ? 'is not' : '!='
}

function escapeRegex(value) {
return value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
}

/**
* Querying where column is equal to a value
* @param column {String} - Column name either table.column or column
Expand Down Expand Up @@ -161,8 +165,8 @@ conditionals.add('$nin', { cascade: false }, function(column, set, values, colle
return 'true'
}
return conditionals.get('$in').fn(column, set, values, collection, original)
.replace(new RegExp(column + ' in', 'g'), column + ' not in')
.replace(new RegExp(column + ' is', 'g'), column + ' is not');
.replace(new RegExp(escapeRegex(column) + ' in', 'g'), column + ' not in')
.replace(new RegExp(escapeRegex(column) + ' is', 'g'), column + ' is not');
});

/**
Expand Down
19 changes: 19 additions & 0 deletions test/conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,25 @@ describe('Conditions', function(){
);
});

it ('$nin with expression', function(){
var query = builder.sql({
type: 'select'
, table: 'users'
, where: {
id: {
'/* \\".* key */ (substring("id",1,1))': {
$nin: ['a']
}
}
}
});

assert.equal(
query.toString()
, `select "users".* from "users" where /* \\".* key */ (substring("id",1,1)) not in ($1)`
);
});

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