Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly support null values used within WHERE conditions. #127

Merged
merged 3 commits into from
Oct 25, 2017
Merged
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
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