Skip to content

Commit

Permalink
Properly support null values used within WHERE conditions. (#127)
Browse files Browse the repository at this point in the history
* Properly support null values used within WHERE conditions.

* Made new NULL where condition test not rely on specific result ordering.

* Make another  NULL where condition test not rely on specific result ordering.
  • Loading branch information
jayalfredprufrock authored and daffl committed Oct 25, 2017
1 parent b4f0e96 commit 5144155
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 133 deletions.
172 changes: 40 additions & 132 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Service {
return query[method].call(query, column, value);
}

return query.where(column, operator, value);
return operator === '=' ? query.where(column, value) : query.where(column, operator, value);
});
}

Expand Down
108 changes: 108 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,114 @@ describe('Feathers Knex Service', () => {
app.service('/people').remove(null);
});
});

it('where conditions support NULL values properly', () => {
app.service('/people').create([{
name: 'Dave without age',
age: null
}, {
name: 'Dave',
age: 32
}, {
name: 'Dada',
age: 1
}]);

return app.service('/people').find({
query: {
age: null
}
}).then(data => {
expect(data.length).to.equal(1);
expect(data[0].name).to.be.equal('Dave without age');
expect(data[0].age).to.be.equal(null);
app.service('/people').remove(null);
});
});

it('where conditions support NOT NULL case properly', () => {
app.service('/people').create([{
name: 'Dave without age',
age: null
}, {
name: 'Dave',
age: 32
}, {
name: 'Dada',
age: 1
}]);

return app.service('/people').find({
query: {
age: {$ne: null}
}
}).then(data => {
expect(data.length).to.equal(2);
expect(data[0].name).to.not.be.equal('Dave without age');
expect(data[0].age).to.not.be.equal(null);
expect(data[1].name).to.not.be.equal('Dave without age');
expect(data[1].age).to.not.be.equal(null);
app.service('/people').remove(null);
});
});

it('where conditions support NULL values within AND conditions', () => {
app.service('/people').create([{
name: 'Dave',
age: null
}, {
name: 'Dave',
age: 32
}, {
name: 'Dada',
age: 1
}]);

return app.service('/people').find({
query: {
age: null,
name: 'Dave'
}
}).then(data => {
expect(data.length).to.equal(1);
expect(data[0].name).to.be.equal('Dave');
expect(data[0].age).to.be.equal(null);
app.service('/people').remove(null);
});
});

it('where conditions support NULL values within OR conditions', () => {
app.service('/people').create([{
name: 'Dave without age',
age: null
}, {
name: 'Dave',
age: 32
}, {
name: 'Dada',
age: 1
}]);

return app.service('/people').find({
query: {
$or: [
{
age: null
},
{
name: 'Dada'
}
]
}
}).then(data => {
expect(data.length).to.equal(2);
expect(data[0].name).not.be.equal('Dave');
expect(data[0].age).not.be.equal(32);
expect(data[1].name).not.be.equal('Dave');
expect(data[1].age).not.be.equal(32);
app.service('/people').remove(null);
});
});
});
});

Expand Down

0 comments on commit 5144155

Please sign in to comment.